Revert "chroot-functions.sh: emerge sys-apps/shadow before distcc"
[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                 clst_root_path=/ run_merge --oneshot --nodeps --noreplace dev-util/ccache || exit 1
68         fi
69
70         if [ -n "${clst_DISTCC}" ]
71         then
72                 export clst_myfeatures="${clst_myfeatures} distcc"
73                 export DISTCC_HOSTS="${clst_distcc_hosts}"
74                 [ -e /etc/make.conf ] && \
75                         echo 'USE="${USE} -avahi -gtk -gnome"' >> /etc/make.conf
76                 # We install distcc to / on stage1, then use --noreplace, so we need to
77                 # have some way to check if we need to reinstall distcc without being
78                 # able to rely on USE, so we check for the distcc user and force a
79                 # reinstall if it isn't found.
80                 if [ "$(getent passwd distcc | cut -d: -f1)" != "distcc" ]
81                 then
82                         clst_root_path=/ run_merge --oneshot --nodeps sys-devel/distcc || exit 1
83                 else
84                         clst_root_path=/ run_merge --oneshot --nodeps --noreplace sys-devel/distcc || exit 1
85                 fi
86                 sed -i '/USE="${USE} -avahi -gtk -gnome"/d' /etc/make.conf
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                 LIBDIR=$(get_libdir)
94                 cd /usr/${LIBDIR}/distcc/bin
95                 rm cc gcc g++ c++ 2>/dev/null
96                 echo -e '#!/bin/bash\nexec /usr/'${LIBDIR}'/distcc/bin/'${CHOST}'-g${0:$[-2]} "$@"' > ${CHOST}-wrapper
97                 chmod a+x /usr/${LIBDIR}/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_merge --oneshot --nodeps --noreplace 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} -news"
118 }
119
120 setup_myemergeopts(){
121         if [ -n "${clst_VERBOSE}" ]
122         then
123                 clst_myemergeopts="--verbose"
124         else
125                 clst_myemergeopts="--quiet"
126                 bootstrap_opts="${bootstrap_opts} -q"
127         fi
128         if [ -n "${clst_FETCH}" ]
129         then
130                 export bootstrap_opts="${bootstrap_opts} -f"
131                 export clst_myemergeopts="${clst_myemergeopts} -f"
132         elif [ -n "${clst_PKGCACHE}" ]
133         then
134                 export clst_myemergeopts="${clst_myemergeopts} --usepkg --buildpkg --newuse"
135                 export bootstrap_opts="${bootstrap_opts} -r"
136         fi
137 }
138
139 setup_binutils(){
140         if [ -x /usr/bin/binutils-config ]
141         then
142                 mythang=$( cd /etc/env.d/binutils; ls ${clst_CHOST}-* | head -n 1 )
143                 if [ -z "${mythang}" ]
144                 then
145                         mythang=1
146                 fi
147                 binutils-config ${mythang}; update_env_settings
148         fi
149 }
150
151 setup_gcc(){
152         if [ -x /usr/bin/gcc-config ]
153         then
154                 mythang=$( cd /etc/env.d/gcc; ls ${clst_CHOST}-* | head -n 1 )
155                 if [ -z "${mythang}" ]
156                 then
157                         mythang=1
158                 fi
159                 gcc-config ${mythang}; update_env_settings
160         fi
161 }
162
163 setup_pkgmgr(){
164         # We need to merge our package manager with USE="build" set in case it is
165         # portage to avoid frying our /etc/make.conf file.  Otherwise, we could
166         # just let emerge system could merge it.
167         [ -e /etc/make.conf ] && echo 'USE="${USE} build"' >> /etc/make.conf
168         run_merge --oneshot --nodeps sys-apps/portage
169         sed -i '/USE="${USE} build"/d' /etc/make.conf
170 }
171
172 cleanup_distcc() {
173         LIBDIR=$(get_libdir)
174         rm -rf /etc/distcc/hosts
175         for i in cc gcc c++ g++; do
176                 rm -f /usr/${LIBDIR}/distcc/bin/${i}
177                 ln -s /usr/bin/distcc /usr/${LIBDIR}/distcc/bin/${i}
178         done
179         rm -f /usr/${LIBDIR}/distcc/bin/*-wrapper
180 }
181
182 cleanup_icecream() {
183         LIBDIR=$(get_libdir)
184         for i in cc gcc c++ g++; do
185                 rm -f /usr/${LIBDIR}/icecc/bin/${i}
186                 ln -s /usr/bin/icecc /usr/${LIBDIR}/icecc/bin/${i}
187         done
188         rm -f /usr/${LIBDIR}/icecc/bin/*-wrapper
189 }
190
191 cleanup_stages() {
192         make_destpath
193         if [ -n "${clst_DISTCC}" ]
194         then
195                 cleanup_distcc
196         fi
197         if [ -n "${clst_ICECREAM}" ]
198         then
199                 cleanup_icecream
200         fi
201         case ${clst_target} in
202                 stage3|system)
203                         run_merge --depclean --with-bdeps=n
204                         ;;
205                 *)
206                         echo "Skipping depclean operation for ${clst_target}"
207                         ;;
208         esac
209         case ${clst_target} in
210                 stage1|stage2|stage3)
211                         rm -f /var/lib/portage/world
212                         touch /var/lib/portage/world
213                         ;;
214                 *)
215                         echo "Skipping removal of world file for ${clst_target}"
216                         ;;
217         esac
218
219         rm -f /var/log/emerge.log /var/log/portage/elog/*
220 }
221
222 update_env_settings(){
223         [ -x /usr/sbin/env-update ] && /usr/sbin/env-update
224         source /etc/profile
225         [ -f /tmp/envscript ] && source /tmp/envscript
226 }
227
228 die() {
229         echo "$1"
230         exit 1
231 }
232
233 make_destpath() {
234         # ROOT is / by default, so remove any ROOT= settings from make.conf
235         sed -i '/ROOT=/d' /etc/make.conf
236         export ROOT=/
237         if [ "${1}" != "/" -a -n "${1}" ]
238         then
239                 echo "ROOT=\"${1}\"" >> /etc/make.conf
240                 export ROOT=${1}
241         fi
242         if [ ! -d ${ROOT} ]
243         then
244                 install -d ${ROOT}
245         fi
246 }
247
248 run_merge() {
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                 echo
286                 # TODO: make this work on non-portage
287                 emerge --info
288                 # TODO: grab our entire env
289                 # <zmedico> to get see the ebuild env you can do something like:
290                 # `set > /tmp/env_dump.${EBUILD_PHASE}` inside /etc/portage/bashrc
291                 # XXX: Also, portageq does *not* source profile.bashrc at any time.
292                 echo
293                 echo "STAGE1_USE:            $(portageq envvar STAGE1_USE)"
294                 echo
295                 echo "USE (profile):         $(portageq envvar USE)"
296                 echo "FEATURES (profile):    $(portageq envvar FEATURES)"
297                 echo
298                 echo "ARCH:                  $(portageq envvar ARCH)"
299                 echo "CHOST:                 $(portageq envvar CHOST)"
300                 echo "CFLAGS:                $(portageq envvar CFLAGS)"
301                 echo
302                 echo "These should be blank on non-multilib profiles."
303                 echo "ABI:                   $(portageq envvar ABI)"
304                 echo "DEFAULT_ABI:           $(portageq envvar DEFAULT_ABI)"
305                 echo "KERNEL_ABI:            $(portageq envvar KERNEL_ABI)"
306                 echo "LIBDIR:                $(get_libdir)"
307                 echo "MULTILIB_ABIS:         $(portageq envvar MULTILIB_ABIS)"
308                 echo "PROFILE_ARCH:          $(portageq envvar PROFILE_ARCH)"
309                 echo
310         fi
311 }
312
313 run_default_funcs() {
314         if [ "${RUN_DEFAULT_FUNCS}" != "no" ]
315         then
316                 update_env_settings
317                 setup_myfeatures
318                 show_debug
319         fi
320 }
321
322 # Functions
323 # Copy libs of a executable in the chroot
324 function copy_libs() {
325         # Check if it's a dynamix exec
326         ldd ${1} > /dev/null 2>&1 || return
327
328         for lib in `ldd ${1} | awk '{ print $3 }'`
329         do
330                 echo ${lib}
331                 if [ -e ${lib} ]
332                 then
333                         if [ ! -e ${clst_root_path}/${lib} ]
334                         then
335                                 copy_file ${lib}
336                                 [ -e "${clst_root_path}/${lib}" ] && \
337                                 strip -R .comment -R .note ${clst_root_path}/${lib} \
338                                 || echo "WARNING : Cannot strip lib ${clst_root_path}/${lib} !"
339                         fi
340                 else
341                         echo "WARNING : Some library was not found for ${lib} !"
342                 fi
343         done
344 }
345
346 function copy_symlink() {
347         STACK=${2}
348         [ "${STACK}" = "" ] && STACK=16 || STACK=$((${STACK} - 1 ))
349
350         if [ ${STACK} -le 0 ]
351         then
352                 echo "WARNING : ${TARGET} : too many levels of symbolic links !"
353                 return
354         fi
355
356         [ ! -e ${clst_root_path}/`dirname ${1}` ] && \
357                 mkdir -p ${clst_root_path}/`dirname ${1}`
358         [ ! -e ${clst_root_path}/${1} ] && \
359                 cp -vfdp ${1} ${clst_root_path}/${1}
360
361         if [[ -n $(type -p realpath) ]]; then
362                 TARGET=`realpath ${1}`
363         else
364                 TARGET=`readlink -f ${1}`
365         fi
366         if [ -h ${TARGET} ]
367         then
368                 copy_symlink ${TARGET} ${STACK}
369         else
370                 copy_file ${TARGET}
371         fi
372 }
373
374 function copy_file() {
375         f="${1}"
376
377         if [ ! -e "${f}" ]
378         then
379                 echo "WARNING : File not found : ${f}"
380                 continue
381         fi
382
383         [ ! -e ${clst_root_path}/`dirname ${f}` ] && \
384                 mkdir -p ${clst_root_path}/`dirname ${f}`
385         [ ! -e ${clst_root_path}/${f} ] && \
386                 cp -vfdp ${f} ${clst_root_path}/${f}
387         if [ -x ${f} -a ! -h ${f} ]
388         then
389                 copy_libs ${f}
390                 strip -R .comment -R .note ${clst_root_path}/${f} > /dev/null 2>&1
391         elif [ -h ${f} ]
392         then
393                 copy_symlink ${f}
394         fi
395 }
396
397 create_handbook_icon() {
398         # This function creates a local icon to the Gentoo Handbook
399         echo "[Desktop Entry]
400 Encoding=UTF-8
401 Version=1.0
402 Type=Link
403 URL=file:///mnt/cdrom/docs/handbook/html/index.html
404 Terminal=false
405 Name=Gentoo Linux Handbook
406 GenericName=Gentoo Linux Handbook
407 Comment=This is a link to the local copy of the Gentoo Linux Handbook.
408 Icon=text-editor" > /usr/share/applications/gentoo-handbook.desktop
409 }
410
411 # We do this everywhere, so why not put it in this script
412 run_default_funcs
413