Changed multiple spaces to tabs to satisfy my OCD.
[catalyst.git] / targets / support / chroot-functions.sh
1
2 # Trap these signals and kill ourselves if recieved
3 # Force ourselves to die if any of these signals are recieved
4 # most likely our controlling terminal is gone
5 trap "echo SIGTERM signal recieved killing $0 with pid $$;kill -9 $$" SIGTERM
6 trap "echo SIGHUP signal recieved killing $0 with pid $$;kill -9 $$" SIGHUP
7 trap "echo SIGKILL signal recieved killing $0 with pid $$;kill -9 $$" SIGKILL
8
9 #SIGINT interrupt character (usually Ctrl-C)
10 #       * example: high-level sequence of events
11 #       * my process (call it "P") is running
12 #       * user types ctrl-c
13 #       * kernel recognizes this and generates SIGINT signal
14 trap "echo SIGINT signal recieved killing $0 with pid $$;kill -9 $$" SIGINT
15  
16 check_portage_version(){
17         portage_version=`/usr/lib/portage/bin/portageq best_version / sys-apps/portage \
18                 | cut -d/ -f2 | cut -d- -f2,3`
19         if [ -n ${portage_version} -a `echo ${portage_version} | cut -d- -f1 | cut -d. -f3` -lt '51' ]
20         then
21                 echo "ERROR: Your portage version is too low in your seed stage.  Portage version"
22                 echo "2.0.51 or greater is required."
23                 exit 1
24         fi
25 }
26
27 check_genkernel_version(){
28         if [ -x /usr/bin/genkernel ]
29         then
30                 genkernel_version=$(genkernel --version)
31                 genkernel_version_major=${genkernel_version%%.*}
32                 genkernel_version_minor_sub=${genkernel_version#${genkernel_version_major}.}
33                 genkernel_version_minor=${genkernel_version_minor_sub%%.*}
34                 genkernel_version_sub=${genkernel_version##*.}
35                 if [ -n "${genkernel_version}" -a "${genkernel_version_major}" -eq '3' -a "${genkernel_version_minor}" -ge '3' ]
36                 then
37                         echo "Genkernel version ${genkernel_version} found ... continuing"
38                 else
39                         echo "ERROR: Your genkernel version is too low in your seed stage.  genkernel version 3.3.0"
40                         echo "or greater is required."
41                         exit 1
42                 fi
43         else
44                 exit 1
45         fi
46 }
47
48 setup_myfeatures(){
49
50         if [ -n "${clst_CCACHE}" ]
51         then
52                 export clst_myfeatures="${clst_myfeatures} ccache"
53                 #if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_ccache ]
54                 #then
55                 #       echo "CCACHE Autoresume point found not emerging ccache"
56                 #else
57                         emerge --oneshot --nodeps -b -k ccache || exit 1
58                 #       touch /tmp/.clst_ccache
59                 #fi
60         fi
61
62         if [ -n "${clst_DISTCC}" ]
63         then
64                 export clst_myfeatures="${clst_myfeatures} distcc"
65                 export DISTCC_HOSTS="${clst_distcc_hosts}"
66                 #if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_distcc ]
67                 #then
68                 #       echo "DISTCC Autoresume point found not emerging distcc"
69                 #else
70                         USE="-gtk -gnome" emerge --oneshot --nodeps -b -k distcc || exit 1
71                         #touch /tmp/.clst_distcc
72                 #fi
73                 mkdir -p /etc/distcc
74                 echo "${clst_distcc_hosts}" > /etc/distcc/hosts
75         fi
76 }
77
78 setup_myemergeopts(){
79         if [ -n "${clst_PKGCACHE}" ]
80         then
81                 export clst_myemergeopts="--usepkg --buildpkg --newuse"
82                 export bootstrap_opts="-r"
83         fi
84 }
85
86
87 setup_portage(){
88         # portage needs to be merged manually with USE="build" set to avoid frying our
89         # make.conf. emerge system could merge it otherwise.
90  
91         if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_portage ]
92         then
93                 echo "Portage Autoresume point found not emerging portage"
94         else
95                 USE="build" emerge --oneshot --nodeps portage
96                 touch /tmp/.clst_portage || exit 1
97         fi
98 }
99
100 setup_gcc(){
101         if [ -x /usr/bin/gcc-config ]
102         then
103                 mythang=$( cd /etc/env.d/gcc; ls ${clst_CHOST}-* | head -n 1 )
104                 if [ -z "${mythang}" ]
105                 then
106                         mythang=1
107                 fi
108                 gcc-config ${mythang}; update_env_settings
109         fi
110 }
111
112 setup_binutils(){
113         if [ -x /usr/bin/binutils-config ]
114         then
115                 mythang=$( cd /etc/env.d/binutils; ls ${clst_CHOST}-* | head -n 1 )
116                 if [ -z "${mythang}" ]
117                 then
118                         mythang=1
119                 fi
120                 binutils-config ${mythang}; update_env_settings
121         fi
122 }
123
124 update_env_settings(){
125         /usr/sbin/env-update
126         source /etc/profile
127         [ -f /tmp/envscript ] && source /tmp/envscript
128 }
129
130 die() {
131         echo "$1"
132         exit 1
133 }
134
135 make_destpath() {
136         if  [ "${1}" = "" ]
137         then
138                 export ROOT=/
139         else
140                 export ROOT=${1}
141                 if [ ! -d ${ROOT} ]
142                 then
143                         install -d ${ROOT}
144                 fi
145         fi
146 }
147
148 run_emerge() {
149
150         # Sets up the ROOT= parameter
151         # with no options ROOT=/
152         make_destpath ${clst_root_path}
153         
154         if [ -n "${clst_VERBOSE}" ]
155         then
156                 echo "ROOT=${ROOT} emerge ${clst_myemergeopts} -vpt $@" || exit 1
157                 emerge ${clst_myemergeopts} -vpt $@ || exit 3
158                 echo "Press any key within 15 seconds to pause the build..."
159                 read -s -t 15 -n 1
160                 if [ $? -eq 0 ]
161                 then
162                         echo "Press any key to continue..."
163                         read -s -n 1
164                 fi
165         fi
166
167         echo "emerge ${clst_myemergeopts} $@" || exit 1
168
169         if [ -n "${clst_FETCH}" ]
170         then
171                 export bootstrap_opts="-f"
172                 emerge ${clst_myemergeopts} -f $@ || exit 1
173         fi
174
175         emerge ${clst_myemergeopts} $@ || exit 1
176 }
177
178 # Functions
179 # Copy libs of a executable in the chroot
180 function copy_libs() {
181
182         # Check if it's a dynamix exec
183
184         ldd ${1} > /dev/null 2>&1 || return
185         
186         for lib in `ldd ${1} | awk '{ print $3 }'`
187         do
188                 echo ${lib}
189                 if [ -e ${lib} ]
190                 then
191                         if [ ! -e ${clst_root_path}/${lib} ]
192                         then
193                                 copy_file ${lib}
194                                 [ -e "${clst_root_path}/${lib}" ] && strip -R .comment -R .note ${clst_root_path}/${lib} || echo "WARNING : Cannot strip lib ${clst_root_path}/${lib} !"
195                         fi
196                 else
197                         echo "WARNING : Some library was not found for ${lib} !"
198                 fi
199         done
200 }
201
202 function copy_symlink() {
203
204         STACK=${2}
205         [ "${STACK}" = "" ] && STACK=16 || STACK=$((${STACK} - 1 ))
206
207         if [ ${STACK} -le 0 ] 
208         then
209                 echo "WARNING : ${TARGET} : too many levels of symbolic links !"
210                 return
211         fi
212
213         [ ! -e ${clst_root_path}/`dirname ${1}` ] && mkdir -p ${clst_root_path}/`dirname ${1}`
214         [ ! -e ${clst_root_path}/${1} ] && cp -vfdp ${1} ${clst_root_path}/${1}
215         
216         TARGET=`readlink -f ${1}`
217         if [ -h ${TARGET} ]
218         then
219                 copy_symlink ${TARGET} ${STACK}
220         else
221                 copy_file ${TARGET}
222         fi
223         }               
224
225 function copy_file() {
226
227         f="${1}"
228
229         if [ ! -e "${f}" ]
230         then
231                 echo "WARNING : File not found : ${f}"
232                 continue
233         fi
234
235         [ ! -e ${clst_root_path}/`dirname ${f}` ] && mkdir -p ${clst_root_path}/`dirname ${f}`
236         [ ! -e ${clst_root_path}/${f} ] && cp -vfdp ${f} ${clst_root_path}/${f}
237         if [ -x ${f} -a ! -h ${f} ]
238         then
239                 copy_libs ${f}
240                 strip -R .comment -R .note ${clst_root_path}/${f} > /dev/null 2>&1
241         elif [ -h ${f} ]
242         then
243                 copy_symlink ${f}
244         fi
245 }