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