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