statically define genkernel location to eliminate which command failure if genkernel...
[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         fi
76 }
77
78 setup_myemergeopts(){
79         if [ -n "${clst_PKGCACHE}" ]
80         then
81                 export clst_myemergeopts="--usepkg --buildpkg --newuse"
82                 export bootstrap_opts="-r"
83         fi
84
85         if [ -n "${clst_FETCH}" ]
86         then
87                 export clst_myemergeopts="${clst_myemergeopts} -f"
88                 export bootstrap_opts="-f"
89         fi
90 }
91
92
93 setup_portage(){
94     # portage needs to be merged manually with USE="build" set to avoid frying our
95     # make.conf. emerge system could merge it otherwise.
96  
97     if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_portage ]
98     then
99         echo "Portage Autoresume point found not emerging portage"
100     else
101         USE="build" emerge portage && touch /tmp/.clst_portage || exit 1
102     fi
103 }
104
105 setup_gcc(){
106         if [ -x /usr/bin/gcc-config ]
107         then
108                 mythang=$( cd /etc/env.d/gcc; ls ${clst_CHOST}-* | head -n 1 )
109                 gcc-config ${mythang}; update_env_settings
110         fi
111 }
112
113 update_env_settings(){
114     /usr/sbin/env-update
115     source /etc/profile
116     [ -f /tmp/envscript ] && source /tmp/envscript
117 }
118
119 die() {
120     echo "$1"
121     exit 1
122 }
123
124 make_destpath() {
125         if  [ "${1}" = "" ]
126         then
127                 export ROOT=/
128         else
129                 export ROOT=${1}
130                 if [ ! -d ${ROOT} ]
131                 then
132                         install -d ${ROOT}
133                 fi
134         fi
135 }
136
137 run_emerge() {
138
139     # Sets up the ROOT= parameter
140     # with no options ROOT=/
141     make_destpath ${clst_root_path}
142         
143     if [ -n "${clst_VERBOSE}" ]
144         then
145                 echo "ROOT=${ROOT} emerge ${clst_myemergeopts} -vpt $@" || exit 1
146                 emerge ${clst_myemergeopts} -vpt $@ || exit 3
147                 echo "Press any key within 15 seconds to pause the build..."
148                 read -s -t 15 -n 1
149                 if [ $? -eq 0 ]
150                 then
151                         echo "Press any key to continue..."
152                         read -s -n 1
153                 fi
154         fi
155         echo "emerge ${clst_myemergeopts} $@" || exit 1
156         emerge ${clst_myemergeopts} $@ || exit 1
157 }
158
159 # Functions
160 # Copy libs of a executable in the chroot
161 function copy_libs() {
162
163         # Check if it's a dynamix exec
164
165         ldd ${1} > /dev/null 2>&1 || return
166     
167         for lib in `ldd ${1} | awk '{ print $3 }'`
168         do
169                 echo ${lib}
170                 if [ -e ${lib} ]
171                 then
172                         if [ ! -e ${clst_root_path}/${lib} ]
173                         then
174                                 copy_file ${lib}
175                                 [ -e "${clst_root_path}/${lib}" ] && strip -R .comment -R .note ${clst_root_path}/${lib} || echo "WARNING : Cannot strip lib ${clst_root_path}/${lib} !"
176                         fi
177                 else
178                         echo "WARNING : Some library was not found for ${lib} !"
179                 fi
180         done
181
182 }
183
184 function copy_symlink() {
185
186         STACK=${2}
187         [ "${STACK}" = "" ] && STACK=16 || STACK=$((${STACK} - 1 ))
188
189         if [ ${STACK} -le 0 ] 
190         then
191                 echo "WARNING : ${TARGET} : too many levels of symbolic links !"
192                 return
193         fi
194
195         [ ! -e ${clst_root_path}/`dirname ${1}` ] && mkdir -p ${clst_root_path}/`dirname ${1}`
196         [ ! -e ${clst_root_path}/${1} ] && cp -vfdp ${1} ${clst_root_path}/${1}
197         
198         TARGET=`readlink -f ${1}`
199         if [ -h ${TARGET} ]
200         then
201                 copy_symlink ${TARGET} ${STACK}
202         else
203                 copy_file ${TARGET}
204         fi
205     }           
206
207 function copy_file() {
208
209         f="${1}"
210
211         if [ ! -e "${f}" ]
212         then
213                 echo "WARNING : File not found : ${f}"
214                 continue
215         fi
216
217         [ ! -e ${clst_root_path}/`dirname ${f}` ] && mkdir -p ${clst_root_path}/`dirname ${f}`
218         [ ! -e ${clst_root_path}/${f} ] && cp -vfdp ${f} ${clst_root_path}/${f}
219         if [ -x ${f} -a ! -h ${f} ]
220         then
221                 copy_libs ${f}
222                 strip -R .comment -R .note ${clst_root_path}/${f} > /dev/null 2>&1
223         elif [ -h ${f} ]
224         then
225                 copy_symlink ${f}
226         fi
227 }
228
229