Move export of FEATURES to setup_myfeatures.
[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 /usr/lib/distcc/bin/${i}
179                 ln -s /usr/bin/distcc /usr/lib/distcc/bin/${i}
180         done
181         rm /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 /usr/${LIBDIR}/icecc/bin/${i}
188                 ln -s /usr/bin/icecc /usr/${LIBDIR}/icecc/bin/${i}
189         done
190         rm /usr/${LIBDIR}/icecc/bin/*-wrapper
191 }
192
193 update_env_settings(){
194         /usr/sbin/env-update
195         source /etc/profile
196         [ -f /tmp/envscript ] && source /tmp/envscript
197 }
198
199 die() {
200         echo "$1"
201         exit 1
202 }
203
204 make_destpath() {
205         if  [ "${1}" = "" ]
206         then
207                 export ROOT=/
208         else
209                 export ROOT=${1}
210                 if [ ! -d ${ROOT} ]
211                 then
212                         install -d ${ROOT}
213                 fi
214         fi
215 }
216
217 run_emerge() {
218         # Sets up the ROOT= parameter
219         # with no options ROOT=/
220         make_destpath ${clst_root_path}
221         
222         export EMERGE_WARNING_DELAY=0   
223         export CLEAN_DELAY=0
224         export EBEEP_IGNORE=0
225         export EPAUSE_IGNORE=0
226         export CONFIG_PROTECT="-*"
227
228         if [ -n "${clst_VERBOSE}" ]
229         then
230                 echo "ROOT=${ROOT} emerge ${clst_myemergeopts} -pt $@" || exit 1
231                 emerge ${clst_myemergeopts} -pt $@ || exit 3
232                 echo "Press any key within 15 seconds to pause the build..."
233                 read -s -t 15 -n 1
234                 if [ $? -eq 0 ]
235                 then
236                         echo "Press any key to continue..."
237                         read -s -n 1
238                 fi
239         fi
240
241         echo "emerge ${clst_myemergeopts} $@" || exit 1
242
243         emerge ${clst_myemergeopts} $@ || exit 1
244 }
245
246 # Functions
247 # Copy libs of a executable in the chroot
248 function copy_libs() {
249         # Check if it's a dynamix exec
250         ldd ${1} > /dev/null 2>&1 || return
251
252         for lib in `ldd ${1} | awk '{ print $3 }'`
253         do
254                 echo ${lib}
255                 if [ -e ${lib} ]
256                 then
257                         if [ ! -e ${clst_root_path}/${lib} ]
258                         then
259                                 copy_file ${lib}
260                                 [ -e "${clst_root_path}/${lib}" ] && \
261                                 strip -R .comment -R .note ${clst_root_path}/${lib} \
262                                 || echo "WARNING : Cannot strip lib ${clst_root_path}/${lib} !"
263                         fi
264                 else
265                         echo "WARNING : Some library was not found for ${lib} !"
266                 fi
267         done
268 }
269
270 function copy_symlink() {
271         STACK=${2}
272         [ "${STACK}" = "" ] && STACK=16 || STACK=$((${STACK} - 1 ))
273
274         if [ ${STACK} -le 0 ] 
275         then
276                 echo "WARNING : ${TARGET} : too many levels of symbolic links !"
277                 return
278         fi
279
280         [ ! -e ${clst_root_path}/`dirname ${1}` ] && \
281                 mkdir -p ${clst_root_path}/`dirname ${1}`
282         [ ! -e ${clst_root_path}/${1} ] && \
283                 cp -vfdp ${1} ${clst_root_path}/${1}
284         
285         if [[ -n $(type -p realpath) ]]; then
286             TARGET=`realpath ${1}`
287         else
288             TARGET=`readlink -f ${1}`
289         fi
290         if [ -h ${TARGET} ]
291         then
292                 copy_symlink ${TARGET} ${STACK}
293         else
294                 copy_file ${TARGET}
295         fi
296 }
297
298 function copy_file() {
299         f="${1}"
300
301         if [ ! -e "${f}" ]
302         then
303                 echo "WARNING : File not found : ${f}"
304                 continue
305         fi
306
307         [ ! -e ${clst_root_path}/`dirname ${f}` ] && \
308                 mkdir -p ${clst_root_path}/`dirname ${f}`
309         [ ! -e ${clst_root_path}/${f} ] && \
310                 cp -vfdp ${f} ${clst_root_path}/${f}
311         if [ -x ${f} -a ! -h ${f} ]
312         then
313                 copy_libs ${f}
314                 strip -R .comment -R .note ${clst_root_path}/${f} > /dev/null 2>&1
315         elif [ -h ${f} ]
316         then
317                 copy_symlink ${f}
318         fi
319 }
320
321 create_handbook_icon() {
322         # This function creates a local icon to the Gentoo Handbook
323         echo "[Desktop Entry]
324 Encoding=UTF-8
325 Version=1.0
326 Type=Link
327 URL=file:///mnt/cdrom/docs/handbook/html/index.html
328 Terminal=false
329 Name=Gentoo Linux Handbook
330 GenericName=Gentoo Linux Handbook
331 Comment=This is a link to the local copy of the Gentoo Linux Handbook.
332 Icon=text-editor" > /usr/share/applications/gentoo-handbook.desktop
333 }