89f0c96455869f583344bbc58345cd164455f5b7
[catalyst.git] / targets / support / chroot-functions.sh
1 #!/bin/bash
2
3 # Trap these signals and kill ourselves if recieved
4 # Force ourselves to die if any of these signals are recieved
5 # most likely our controlling terminal is gone
6 trap "echo SIGTERM signal recieved killing $0 with pid $$;kill -9 $$" SIGTERM
7 trap "echo SIGHUP signal recieved killing $0 with pid $$;kill -9 $$" SIGHUP
8 trap "echo SIGKILL signal recieved killing $0 with pid $$;kill -9 $$" SIGKILL
9
10 #SIGINT interrupt character (usually Ctrl-C)
11 #       * example: high-level sequence of events
12 #       * my process (call it "P") is running
13 #       * user types ctrl-c
14 #       * kernel recognizes this and generates SIGINT signal
15 trap "echo SIGINT signal recieved killing $0 with pid $$;kill -9 $$" SIGINT
16
17 check_genkernel_version(){
18         if [ -x /usr/bin/genkernel ]
19         then
20                 genkernel_version=$(genkernel --version)
21                 genkernel_version_major=${genkernel_version%%.*}
22                 genkernel_version_minor_sub=${genkernel_version#${genkernel_version_major}.}
23                 genkernel_version_minor=${genkernel_version_minor_sub%%.*}
24                 genkernel_version_sub=${genkernel_version##*.}
25                 if [ -n "${genkernel_version}" -a "${genkernel_version_major}" -eq '3' -a "${genkernel_version_minor}" -ge '3' ]
26                 then
27                         echo "Genkernel version ${genkernel_version} found ... continuing"
28                 else
29                         echo "ERROR: Your genkernel version is too low in your seed stage.  genkernel version 3.3.0"
30                         echo "or greater is required."
31                         exit 1
32                 fi
33         else
34                 exit 1
35         fi
36 }
37
38 get_libdir() {
39         ABI=$(portageq envvar ABI)
40         DEFAULT_ABI=$(portageq envvar DEFAULT_ABI)
41         LIBDIR_default=$(portageq envvar LIBDIR_default)
42
43         local abi
44         if [ $# -gt 0 ]
45         then
46                 abi=${1}
47         elif [ -n "${ABI}" ]
48         then
49                 abi=${ABI}
50         elif [ -n "${DEFAULT_ABI}" ]
51         then
52                 abi=${DEFAULT_ABI}
53         else
54                 abi="default"
55         fi
56
57         local var="LIBDIR_${abi}"
58         var=$(portageq envvar ${var})
59         echo ${var}
60 }
61
62 setup_myfeatures(){
63         setup_myemergeopts
64         if [ -n "${clst_CCACHE}" ]
65         then
66                 export clst_myfeatures="${clst_myfeatures} ccache"
67                 #if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_ccache ]
68                 #then
69                 #       echo "CCACHE Autoresume point found not emerging ccache"
70                 #else
71                         clst_root_path=/ run_emerge --oneshot --nodeps ccache || exit 1
72                 #       touch /tmp/.clst_ccache
73                 #fi
74         fi
75
76         if [ -n "${clst_DISTCC}" ]
77         then
78                 export clst_myfeatures="${clst_myfeatures} distcc"
79                 export DISTCC_HOSTS="${clst_distcc_hosts}"
80                 #if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_distcc ]
81                 #then
82                 #       echo "DISTCC Autoresume point found not emerging distcc"
83                 #else
84                         USE="-gtk -gnome" clst_root_path=/ run_emerge --oneshot --nodeps distcc || exit 1
85                         #touch /tmp/.clst_distcc
86                 #fi
87                 mkdir -p /etc/distcc
88                 echo "${clst_distcc_hosts}" > /etc/distcc/hosts
89
90                 # This sets up automatic cross-distcc-fu according to
91                 # http://www.gentoo.org/doc/en/cross-compiling-distcc.xml
92                 CHOST=$(portageq envvar CHOST)
93                 # TODO: change to use get_libdir
94                 cd /usr/lib/distcc/bin
95                 rm cc gcc g++ c++ 2>/dev/null
96                 echo -e '#!/bin/bash\nexec /usr/lib/distcc/bin/'${CHOST}'-g${0:$[-2]} "$@"' > ${CHOST}-wrapper
97                 chmod a+x /usr/lib/distcc/bin/${CHOST}-wrapper
98                 for i in cc gcc g++ c++; do ln -s ${CHOST}-wrapper ${i}; done
99         fi
100
101         if [ -n "${clst_ICECREAM}" ]
102         then
103                 clst_root_path=/ run_emerge --oneshot --nodeps sys-devel/icecream || exit 1
104
105                 # This sets up automatic cross-icecc-fu according to
106                 # http://gentoo-wiki.com/HOWTO_Setup_An_ICECREAM_Compile_Cluster#Icecream_and_cross-compiling
107                 CHOST=$(portageq envvar CHOST)
108                 LIBDIR=$(get_libdir)
109                 cd /usr/${LIBDIR}/icecc/bin
110                 rm cc gcc g++ c++ 2>/dev/null
111                 echo -e '#!/bin/bash\nexec /usr/'${LIBDIR}'/icecc/bin/'${CHOST}'-g${0:$[-2]} "$@"' > ${CHOST}-wrapper
112                 chmod a+x ${CHOST}-wrapper
113                 for i in cc gcc g++ c++; do ln -s ${CHOST}-wrapper ${i}; done
114                 export PATH="/usr/lib/icecc/bin:${PATH}"
115                 export PREROOTPATH="/usr/lib/icecc/bin"
116         fi
117         export FEATURES="${clst_myfeatures}"
118 }
119
120 setup_myemergeopts(){
121         if [ -n "${clst_VERBOSE}" ]
122         then
123                 clst_myemergeopts="--verbose"
124         else
125                 clst_myemergeopts="--quiet"
126         fi
127         if [ -n "${clst_FETCH}" ]
128         then
129                 export bootstrap_opts="-f"
130                 export clst_myemergeopts="${clst_myemergeopts} -f"
131         elif [ -n "${clst_PKGCACHE}" ]
132         then
133                 export clst_myemergeopts="${clst_myemergeopts} --usepkg --buildpkg --newuse"
134                 export bootstrap_opts="-r"
135         fi
136 }
137
138 setup_portage(){
139         # portage needs to be merged manually with USE="build" set to avoid frying
140         # our make.conf. emerge system could merge it otherwise.
141 #       if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_portage ]
142 #       then
143 #               echo "Portage Autoresume point found not emerging portage"
144 #       else
145                 USE="build" run_emerge --oneshot --nodeps portage
146 #               touch /tmp/.clst_portage || exit 1
147 #       fi
148 }
149
150 setup_gcc(){
151         if [ -x /usr/bin/gcc-config ]
152         then
153                 mythang=$( cd /etc/env.d/gcc; ls ${clst_CHOST}-* | head -n 1 )
154                 if [ -z "${mythang}" ]
155                 then
156                         mythang=1
157                 fi
158                 gcc-config ${mythang}; update_env_settings
159         fi
160 }
161
162 setup_binutils(){
163         if [ -x /usr/bin/binutils-config ]
164         then
165                 mythang=$( cd /etc/env.d/binutils; ls ${clst_CHOST}-* | head -n 1 )
166                 if [ -z "${mythang}" ]
167                 then
168                         mythang=1
169                 fi
170                 binutils-config ${mythang}; update_env_settings
171         fi
172 }
173
174 cleanup_distcc() {
175         rm -rf /etc/distcc/hosts
176         for i in cc gcc c++ g++; do
177                 # TODO: change to use get_libdir
178                 rm -f /usr/lib/distcc/bin/${i}
179                 ln -s /usr/bin/distcc /usr/lib/distcc/bin/${i}
180         done
181         rm -f /usr/lib/distcc/bin/*-wrapper
182 }
183
184 cleanup_icecream() {
185         LIBDIR=$(get_libdir)
186         for i in cc gcc c++ g++; do
187                 rm -f /usr/${LIBDIR}/icecc/bin/${i}
188                 ln -s /usr/bin/icecc /usr/${LIBDIR}/icecc/bin/${i}
189         done
190         rm -f /usr/${LIBDIR}/icecc/bin/*-wrapper
191 }
192
193 cleanup_stages() {
194         if [ -n "${clst_DISTCC}" ]
195         then
196                 cleanup_distcc
197         fi
198         if [ -n "${clst_ICECREAM}" ]
199         then
200                 cleanup_icecream
201         fi
202         case ${clst_target} in
203                 stage1|stage2|stage3)
204                         rm -f /var/lib/portage/world
205                         touch /var/lib/portage/world
206                         ;;
207                 *)
208                         echo "Skipping removal of world file for ${clst_target}"
209                         ;;
210         esac
211
212         rm -f /var/log/emerge.log /var/log/portage/elog/*
213         rm -rf /var/tmp/*
214 }
215
216 update_env_settings(){
217         which env-update > /dev/null 2>&1
218         ret=$?
219         if [ $ret -eq 0 ]
220         then
221                 ENV_UPDATE=`which env-update`
222                 ${ENV_UPDATE}
223         else
224                 echo "WARNING: env-update not found, skipping!"
225         fi
226         source /etc/profile
227         [ -f /tmp/envscript ] && source /tmp/envscript
228 }
229
230 die() {
231         echo "$1"
232         exit 1
233 }
234
235 make_destpath() {
236         if  [ "${1}" = "" ]
237         then
238                 export ROOT=/
239         else
240                 export ROOT=${1}
241                 if [ ! -d ${ROOT} ]
242                 then
243                         install -d ${ROOT}
244                 fi
245         fi
246 }
247
248 run_emerge() {
249         # Sets up the ROOT= parameter
250         # with no options ROOT=/
251         make_destpath ${clst_root_path}
252         
253         export EMERGE_WARNING_DELAY=0   
254         export CLEAN_DELAY=0
255         export EBEEP_IGNORE=0
256         export EPAUSE_IGNORE=0
257         export CONFIG_PROTECT="-*"
258
259         if [ -n "${clst_VERBOSE}" ]
260         then
261                 echo "ROOT=${ROOT} emerge ${clst_myemergeopts} -pt $@" || exit 1
262                 emerge ${clst_myemergeopts} -pt $@ || exit 3
263                 echo "Press any key within 15 seconds to pause the build..."
264                 read -s -t 15 -n 1
265                 if [ $? -eq 0 ]
266                 then
267                         echo "Press any key to continue..."
268                         read -s -n 1
269                 fi
270         fi
271
272         echo "emerge ${clst_myemergeopts} $@" || exit 1
273
274         emerge ${clst_myemergeopts} $@ || exit 1
275 }
276
277 show_debug() {
278         if [ "${clst_DEBUG}" = "1" ]
279         then
280                 unset PACKAGES
281                 echo "DEBUG:"
282                 echo "Profile/target info:"
283                 echo "Profile inheritance:"
284                 python -c 'import portage; print portage.settings.profiles'
285                 # TODO: grab our entire env
286                 # <zmedico> to get see the ebuild env you can do something like:
287                 # `set > /tmp/env_dump.${EBUILD_PHASE}` inside /etc/portage/bashrc
288                 echo
289                 echo "STAGE1_USE:            $(portageq envvar STAGE1_USE)"
290                 echo
291                 echo "USE (profile):         $(portageq envvar USE)"
292                 echo "USE (stage1):          ${USE}"
293                 echo "FEATURES (profile):    $(portageq envvar FEATURES)"
294                 echo "FEATURES (stage1):     ${FEATURES}"
295                 echo
296                 echo "ARCH:                  $(portageq envvar ARCH)"
297                 echo "CHOST:                 $(portageq envvar CHOST)"
298                 echo "CFLAGS:                $(portageq envvar CFLAGS)"
299                 echo
300                 echo "PROFILE_ARCH:          $(portageq envvar PROFILE_ARCH)"
301                 echo
302                 echo "ABI:                   $(portageq envvar ABI)"
303                 echo "DEFAULT_ABI:           $(portageq envvar DEFAULT_ABI)"
304                 echo "KERNEL_ABI:            $(portageq envvar KERNEL_ABI)"
305                 echo "MULTILIB_ABIS:         $(portageq envvar MULTILIB_ABIS)"
306                 echo
307         fi
308 }
309
310 run_default_funcs() {
311         if [ "${RUN_DEFAULT_FUNCS}" != "no" ]
312         then
313                 update_env_settings
314                 setup_myfeatures
315                 show_debug
316         fi
317 }
318
319 # Functions
320 # Copy libs of a executable in the chroot
321 function copy_libs() {
322         # Check if it's a dynamix exec
323         ldd ${1} > /dev/null 2>&1 || return
324
325         for lib in `ldd ${1} | awk '{ print $3 }'`
326         do
327                 echo ${lib}
328                 if [ -e ${lib} ]
329                 then
330                         if [ ! -e ${clst_root_path}/${lib} ]
331                         then
332                                 copy_file ${lib}
333                                 [ -e "${clst_root_path}/${lib}" ] && \
334                                 strip -R .comment -R .note ${clst_root_path}/${lib} \
335                                 || echo "WARNING : Cannot strip lib ${clst_root_path}/${lib} !"
336                         fi
337                 else
338                         echo "WARNING : Some library was not found for ${lib} !"
339                 fi
340         done
341 }
342
343 function copy_symlink() {
344         STACK=${2}
345         [ "${STACK}" = "" ] && STACK=16 || STACK=$((${STACK} - 1 ))
346
347         if [ ${STACK} -le 0 ] 
348         then
349                 echo "WARNING : ${TARGET} : too many levels of symbolic links !"
350                 return
351         fi
352
353         [ ! -e ${clst_root_path}/`dirname ${1}` ] && \
354                 mkdir -p ${clst_root_path}/`dirname ${1}`
355         [ ! -e ${clst_root_path}/${1} ] && \
356                 cp -vfdp ${1} ${clst_root_path}/${1}
357         
358         if [[ -n $(type -p realpath) ]]; then
359                 TARGET=`realpath ${1}`
360         else
361                 TARGET=`readlink -f ${1}`
362         fi
363         if [ -h ${TARGET} ]
364         then
365                 copy_symlink ${TARGET} ${STACK}
366         else
367                 copy_file ${TARGET}
368         fi
369 }
370
371 function copy_file() {
372         f="${1}"
373
374         if [ ! -e "${f}" ]
375         then
376                 echo "WARNING : File not found : ${f}"
377                 continue
378         fi
379
380         [ ! -e ${clst_root_path}/`dirname ${f}` ] && \
381                 mkdir -p ${clst_root_path}/`dirname ${f}`
382         [ ! -e ${clst_root_path}/${f} ] && \
383                 cp -vfdp ${f} ${clst_root_path}/${f}
384         if [ -x ${f} -a ! -h ${f} ]
385         then
386                 copy_libs ${f}
387                 strip -R .comment -R .note ${clst_root_path}/${f} > /dev/null 2>&1
388         elif [ -h ${f} ]
389         then
390                 copy_symlink ${f}
391         fi
392 }
393
394 create_handbook_icon() {
395         # This function creates a local icon to the Gentoo Handbook
396         echo "[Desktop Entry]
397 Encoding=UTF-8
398 Version=1.0
399 Type=Link
400 URL=file:///mnt/cdrom/docs/handbook/html/index.html
401 Terminal=false
402 Name=Gentoo Linux Handbook
403 GenericName=Gentoo Linux Handbook
404 Comment=This is a link to the local copy of the Gentoo Linux Handbook.
405 Icon=text-editor" > /usr/share/applications/gentoo-handbook.desktop
406 }
407
408 # We do this everywhere, so why not put it in this script
409 run_default_funcs
410