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