Fix for bug #57865. Thanks to Martin Parm for the patch!
[genkernel.git] / gen_compile.sh
1 #!/bin/bash
2
3 compile_kernel_args()
4 {
5         local ARGS
6
7         ARGS=''
8         if [ "${KERNEL_CC}" != '' ]
9         then
10                 ARGS="CC=\"${KERNEL_CC}\""
11         fi
12         if [ "${KERNEL_LD}" != '' ]
13         then
14                 ARGS="${ARGS} LD=\"${KERNEL_LD}\""
15         fi
16         if [ "${KERNEL_AS}" != '' ]
17         then
18                 ARGS="${ARGS} AS=\"${KERNEL_AS}\""
19         fi
20
21         echo -n "${ARGS}"
22 }
23
24 compile_utils_args()
25 {
26         local ARGS
27
28         ARGS=''
29         if [ "${UTILS_CC}" != '' ]
30         then
31                 ARGS="CC=\"${UTILS_CC}\""
32         fi
33         if [ "${UTILS_LD}" != '' ]
34         then
35                 ARGS="${ARGS} LD=\"${UTILS_LD}\""
36         fi
37         if [ "${UTILS_AS}" != '' ]
38         then
39                 ARGS="${ARGS} AS=\"${UTILS_AS}\""
40         fi
41
42         echo -n "${ARGS}"
43 }
44
45 export_utils_args()
46 {
47         if [ "${UTILS_CC}" != '' ]
48         then
49                 export CC="${UTILS_CC}"
50         fi
51         if [ "${UTILS_LD}" != '' ]
52         then
53                 export LD="${UTILS_LD}"
54         fi
55         if [ "${UTILS_AS}" != '' ]
56         then
57                 export AS="${UTILS_AS}"
58         fi
59 }
60
61 unset_utils_args()
62 {
63         if [ "${UTILS_CC}" != '' ]
64         then
65                 unset CC
66         fi
67         if [ "${UTILS_LD}" != '' ]
68         then
69                 unset LD
70         fi
71         if [ "${UTILS_AS}" != '' ]
72         then
73                 unset AS
74         fi
75 }
76
77 export_kernel_args()
78 {
79         if [ "${KERNEL_CC}" != '' ]
80         then
81                 export CC="${KERNEL_CC}"
82         fi
83         if [ "${KERNEL_LD}" != '' ]
84         then
85                 export LD="${KERNEL_LD}"
86         fi
87         if [ "${KERNEL_AS}" != '' ]
88         then
89                 export AS="${KERNEL_AS}"
90         fi
91 }
92
93 unset_kernel_args()
94 {
95         if [ "${KERNEL_CC}" != '' ]
96         then
97                 unset CC
98         fi
99         if [ "${KERNEL_LD}" != '' ]
100         then
101                 unset LD
102         fi
103         if [ "${KERNEL_AS}" != '' ]
104         then
105                 unset AS
106         fi
107 }
108
109 compile_generic() {
110         local RET
111         [ "$#" -lt '2' ] && gen_die "compile_generic(): improper usage"
112
113         if [ "${2}" = 'kernel' ] || [ "${2}" = 'runtask' ]
114         then
115                 export_kernel_args
116                 MAKE=${KERNEL_MAKE}
117         elif [ "${2}" = 'utils' ]
118         then
119                 export_utils_args
120                 MAKE=${UTILS_MAKE}
121         fi
122
123         if [ "${2}" == 'runtask' ]
124         then
125                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${1}" 1 0 1
126                 ${MAKE} -s ${MAKEOPTS/-j?/-j1} ${1}
127                 RET=$?
128         elif [ "${DEBUGLEVEL}" -gt "1" ]
129         then
130                 # Output to stdout and debugfile
131                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${1}" 1 0 1
132                 ${MAKE} ${MAKEOPTS} ${1} 2>&1 | tee -a ${DEBUGFILE}
133                 RET=$?
134         else
135                 # Output to debugfile only
136                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${1}" 1 0 1
137                 ${MAKE} ${MAKEOPTS} ${1} >> ${DEBUGFILE} 2>&1
138                 RET=$?
139         fi
140         [ "${RET}" -ne '0' ] && gen_die "Failed to compile the \"${1}\" target..."
141
142         unset MAKE
143         if [ "${2}" = 'kernel' ]
144         then
145                 unset_kernel_args
146         elif [ "${2}" = 'utils' ]
147         then
148                 unset_utils_args
149         fi
150 }
151
152 extract_dietlibc_bincache() {
153         cd "${TEMP}"
154         rm -rf "${TEMP}/diet" > /dev/null
155         tar -jxpf "${DIETLIBC_BINCACHE}" || gen_die "Could not extract dietlibc bincache!"
156         [ ! -d "${TEMP}/diet" ] && gen_die "${TEMP}/diet directory not found!"
157         cd - > /dev/null
158 }
159
160 clean_dietlibc_bincache() {
161         cd "${TEMP}"
162         rm -rf "${TEMP}/diet" > /dev/null
163         cd - > /dev/null
164 }
165
166 compile_dep() {
167         # Only run ``make dep'' for 2.4 kernels
168         if [ "${VER}" -eq '2' -a "${PAT}" -le '4' ]
169         then
170                 print_info 1 "kernel: >> Making dependencies..."
171                 cd ${KERNEL_DIR}
172                 compile_generic dep kernel
173         fi
174 }
175
176 compile_modules() {
177         print_info 1 "        >> Compiling ${KV} modules..."
178         cd ${KERNEL_DIR}
179         compile_generic modules kernel
180         export UNAME_MACHINE="${ARCH}"
181         # On 2.4 kernels, if MAKEOPTS > -j1 it can cause failures
182         if [ "${VER}" -eq '2' -a "${PAT}" -le '4' ]
183         then
184                 MAKEOPTS_SAVE="${MAKEOPTS}"
185                 MAKEOPTS='-j1'
186         fi
187         [ "${INSTALL_MOD_PATH}" != '' ] && export INSTALL_MOD_PATH
188         compile_generic "modules_install" kernel
189         [ "${VER}" -eq '2' -a "${PAT}" -le '4' ] && MAKEOPTS="${MAKEOPTS_SAVE}"
190         export MAKEOPTS
191         unset UNAME_MACHINE
192 }
193
194 compile_kernel() {
195         [ "${KERNEL_MAKE}" = '' ] && gen_die "KERNEL_MAKE undefined - I don't know how to compile a kernel for this arch!"
196         cd ${KERNEL_DIR}
197         print_info 1 "        >> Compiling ${KV} ${KERNEL_MAKE_DIRECTIVE/_install/ [ install ]/}..."
198         compile_generic "${KERNEL_MAKE_DIRECTIVE}" kernel
199         if [ "${KERNEL_MAKE_DIRECTIVE_2}" != '' ]
200         then
201                 print_info 1 "        >> Starting supplimental compile of ${KV}: ${KERNEL_MAKE_DIRECTIVE_2}..."
202                 compile_generic "${KERNEL_MAKE_DIRECTIVE_2}" kernel
203         fi
204         if ! isTrue "${CMD_NOINSTALL}"
205         then
206                 cp "${KERNEL_BINARY}" "/boot/kernel-${KV}" || gen_die 'Could not copy the kernel binary to /boot!'
207                 cp "System.map" "/boot/System.map-${KV}" || gen_die 'Could not copy System.map to /boot!'
208         else
209                 cp "${KERNEL_BINARY}" "${TEMP}/kernel-${KV}" || gen_die "Could not copy the kernel binary to ${TEMP}!"
210                 cp "System.map" "${TEMP}/System.map-${KV}" || gen_die "Could not copy System.map to ${TEMP}!"
211         fi
212 }
213
214 compile_busybox() {
215         if [ ! -f "${BUSYBOX_BINCACHE}" ]
216         then
217                 [ -f "${BUSYBOX_SRCTAR}" ] || gen_die "Could not find busybox source tarball: ${BUSYBOX_SRCTAR}!"
218                 [ -f "${BUSYBOX_CONFIG}" ] || gen_die "Cound not find busybox config file: ${BUSYBOX_CONFIG}!"
219                 cd "${TEMP}"
220                 rm -rf ${BUSYBOX_DIR} > /dev/null
221                 tar -jxpf ${BUSYBOX_SRCTAR} || gen_die 'Could not extract busybox source tarball!'
222                 [ -d "${BUSYBOX_DIR}" ] || gen_die 'Busybox directory ${BUSYBOX_DIR} is invalid!'
223                 cp "${BUSYBOX_CONFIG}" "${BUSYBOX_DIR}/.config"
224                 cd "${BUSYBOX_DIR}"
225 # Busybox and dietlibc don't play nice right now
226 #               if [ "${USE_DIETLIBC}" -eq "1" ]
227 #               then
228 #                       extract_dietlibc_bincache
229 #                       OLD_CC="${UTILS_CC}"
230 #                       UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
231 #               fi
232                 print_info 1 'busybox: >> Configuring...'
233                 yes '' | compile_generic oldconfig utils
234                 print_info 1 'busybox: >> Compiling...'
235                 compile_generic all utils
236 # Busybox and dietlibc don't play nice right now
237 #               if [ "${USE_DIETLIBC}" -eq "1" ]
238 #               then
239 #                       clean_dietlibc_bincache
240 #                       UTILS_CC="${OLD_CC}"
241 #               fi
242                 print_info 1 'busybox: >> Copying to cache...'
243                 [ -f "${TEMP}/${BUSYBOX_DIR}/busybox" ] || gen_die 'Busybox executable does not exist!'
244                 strip "${TEMP}/${BUSYBOX_DIR}/busybox" || gen_die 'Could not strip busybox binary!'
245                 bzip2 "${TEMP}/${BUSYBOX_DIR}/busybox" || gen_die 'bzip2 compression of busybox failed!'
246                 mv "${TEMP}/${BUSYBOX_DIR}/busybox.bz2" "${BUSYBOX_BINCACHE}" || gen_die 'Could not copy the busybox binary to package directory, does the directory exist?'
247
248                 cd "${TEMP}"
249                 rm -rf "${BUSYBOX_DIR}" > /dev/null
250         fi
251 }
252
253 compile_modutils() {
254         local ARGS
255         if [ ! -f "${MODUTILS_BINCACHE}" ]
256         then
257                 [ ! -f "${MODUTILS_SRCTAR}" ] && gen_die "Could not find modutils source tarball: ${MODUTILS_SRCTAR}!"
258                 cd "${TEMP}"
259                 rm -rf "${MODUTILS_DIR}"
260                 tar -jxpf "${MODUTILS_SRCTAR}"
261                 [ ! -d "${MODUTILS_DIR}" ] && gen_die "Modutils directory ${MODUTILS_DIR} invalid!"
262                 cd "${MODUTILS_DIR}"
263                 print_info 1 "modutils: >> Configuring..."
264
265                 if [ "${USE_DIETLIBC}" -eq '1' ]
266                 then
267                         extract_dietlibc_bincache
268                         OLD_CC="${UTILS_CC}"
269                         UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
270                 fi
271
272                 export_utils_args
273                 export ARCH=${ARCH}
274                 ./configure --disable-combined --enable-insmod-static >> ${DEBUGFILE} 2>&1 || gen_die 'Configuring modutils failed!'
275                 unset_utils_args
276
277                 print_info 1 'modutils: >> Compiling...'
278                 compile_generic all utils
279
280                 if [ "${USE_DIETLIBC}" -eq '1' ]
281                 then
282                         clean_dietlibc_bincache
283                         UTILS_CC="${OLD_CC}"
284                 fi
285
286                 print_info 1 'modutils: >> Copying to cache...'
287                 [ -f "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" ] || gen_die "insmod.static does not exist after the compilation of modutils!"
288                 strip "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" || gen_die 'Could not strip insmod.static!'
289                 bzip2 "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" || gen_die 'Compression of insmod.static failed!'
290                 mv "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static.bz2" "${MODUTILS_BINCACHE}" || gen_die 'Could not move the compressed insmod binary to the package cache!'
291
292                 cd "${TEMP}"
293                 rm -rf "${MODULE_INIT_TOOLS_DIR}" > /dev/null
294         fi
295 }
296
297 compile_module_init_tools() {
298         local ARGS
299         if [ ! -f "${MODULE_INIT_TOOLS_BINCACHE}" ]
300         then
301                 [ ! -f "${MODULE_INIT_TOOLS_SRCTAR}" ] && gen_die "Could not find module-init-tools source tarball: ${MODULE_INIT_TOOLS_SRCTAR}"
302                 cd "${TEMP}"
303                 rm -rf "${MODULE_INIT_TOOLS_DIR}"
304                 tar -jxpf "${MODULE_INIT_TOOLS_SRCTAR}"
305                 [ ! -d "${MODULE_INIT_TOOLS_DIR}" ] && gen_die "Module-init-tools directory ${MODULE_INIT_TOOLS_DIR} invalid"
306                 cd "${MODULE_INIT_TOOLS_DIR}"
307                 print_info 1 'module-init-tools: >> Configuring'
308
309                 if [ "${USE_DIETLIBC}" -eq '1' ]
310                 then
311                         extract_dietlibc_bincache
312                         OLD_CC="${UTILS_CC}"
313                         UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
314                 fi
315
316                 export_utils_args
317                 ./configure >> ${DEBUGFILE} 2>&1 || gen_die 'Configure of module-init-tools failed!'
318                 unset_utils_args
319                 print_info 1 '                   >> Compiling...'
320                 compile_generic "all" utils
321
322                 if [ "${USE_DIETLIBC}" -eq '1' ]
323                 then
324                         clean_dietlibc_bincache
325                         UTILS_CC="${OLD_CC}"
326                 fi
327
328                 print_info 1 '                   >> Copying to cache...'
329                 [ -f "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" ] || gen_die 'insmod.static does not exist after the compilation of module-init-tools!'
330                 strip "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" || gen_die 'Could not strip insmod.static!'
331                 bzip2 "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" || gen_die 'Compression of insmod.static failed!'
332                 [ -f "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static.bz2" ] || gen_die 'Could not find compressed insmod.static.bz2 binary!'
333                 mv "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static.bz2" "${MODULE_INIT_TOOLS_BINCACHE}" || gen_die 'Could not move the compressed insmod binary to the package cache!'
334
335                 cd "${TEMP}"
336                 rm -rf "${MODULE_INIT_TOOLS_DIR}" > /dev/null
337         fi
338 }
339
340 compile_devfsd() {
341         local ARGS
342         if [ ! -f "${DEVFSD_BINCACHE}" -o ! -f "${DEVFSD_CONF_BINCACHE}" ]
343         then
344                 [ ! -f "${DEVFSD_SRCTAR}" ] && gen_die "Could not find devfsd source tarball: ${DEVFSD_SRCTAR}"
345                 cd "${TEMP}"
346                 rm -rf "${DEVFSD_DIR}"
347                 tar -jxpf "${DEVFSD_SRCTAR}"
348                 [ ! -d "${DEVFSD_DIR}" ] && gen_die "Devfsd directory ${DEVFSD_DIR} invalid"
349                 cd "${DEVFSD_DIR}"
350
351                 if [ "${USE_DIETLIBC}" -eq '1' ]
352                 then
353                         extract_dietlibc_bincache
354                         OLD_CC="${UTILS_CC}"
355                         UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
356                 fi
357
358                 print_info 1 'devfsd: >> Compiling...'
359                 if [ "${USE_DIETLIBC}" -eq '1' ]
360                 then
361                         compile_generic 'has_dlopen=0 has_rpcsvc=0' utils
362                 else
363                         compile_generic 'LDFLAGS=-static' utils
364                 fi
365
366                 if [ "${USE_DIETLIBC}" -eq '1' ]
367                 then
368                         clean_dietlibc_bincache
369                         UTILS_CC="${OLD_CC}"
370                 fi
371
372                 print_info 1 '        >> Copying to cache...'
373                 [ -f "${TEMP}/${DEVFSD_DIR}/devfsd" ] || gen_die 'The devfsd executable does not exist after the compilation of devfsd!'
374                 strip "${TEMP}/${DEVFSD_DIR}/devfsd" || gen_die 'Could not strip devfsd!'
375                 bzip2 "${TEMP}/${DEVFSD_DIR}/devfsd" || gen_die 'Compression of devfsd failed!'
376                 [ -f "${TEMP}/${DEVFSD_DIR}/devfsd.bz2" ] || gen_die 'Could not find compressed devfsd.bz2 binary!'
377                 mv "${TEMP}/${DEVFSD_DIR}/devfsd.bz2" "${DEVFSD_BINCACHE}" || gen_die 'Could not move compressed binary to the package cache!'
378
379                 [ -f "${TEMP}/${DEVFSD_DIR}/devfsd.conf" ] || gen_die 'devfsd.conf does not exist after the compilation of devfsd!'
380                 bzip2 "${TEMP}/${DEVFSD_DIR}/devfsd.conf" || gen_die 'Compression of devfsd.conf failed!'
381                 mv "${TEMP}/${DEVFSD_DIR}/devfsd.conf.bz2" "${DEVFSD_CONF_BINCACHE}" || gen_die 'Could not move the compressed configuration to the package cache!'
382
383                 cd "${TEMP}"
384                 rm -rf "${DEVFSD_DIR}" > /dev/null
385         fi
386 }
387
388 compile_dietlibc() {
389         local BUILD_DIETLIBC
390         local ORIGTEMP
391
392         BUILD_DIETLIBC=0
393         [ ! -f "${DIETLIBC_BINCACHE}" ] && BUILD_DIETLIBC=1
394         [ ! -f "${DIETLIBC_BINCACHE_TEMP}" ] && BUILD_DIETLIBC=1
395         if ! isTrue "${BUILD_DIETLIBC}"
396         then
397                 ORIGTEMP=`cat "${DIETLIBC_BINCACHE_TEMP}"`
398                 if [ "${TEMP}" != "${ORIGTEMP}" ]
399                 then
400                         print_warning 1 'dietlibc: Bincache exists, but the current temporary directory'
401                         print_warning 1 '          is different to the original. Rebuilding.'
402                         BUILD_DIETLIBC=1
403                 fi
404         fi
405
406         if [ "${BUILD_DIETLIBC}" -eq '1' ]
407         then
408                 [ -f "${DIETLIBC_SRCTAR}" ] || gen_die "Could not find dietlibc source tarball: ${DIETLIBC_SRCTAR}"
409                 cd "${TEMP}"
410                 rm -rf "${DIETLIBC_DIR}" > /dev/null
411                 tar -jxpf ${DIETLIBC_SRCTAR} || gen_die "Could not extract dietlibc source tarball"
412                 [ -d "${DIETLIBC_DIR}" ] || gen_die "Dietlibc directory ${DIETLIBC_DIR} is invalid!"
413                 cd "${DIETLIBC_DIR}"
414                 print_info 1 "dietlibc: >> Compiling..."
415                 compile_generic "prefix=${TEMP}/diet" utils
416                 print_info 1 "          >> Installing..."
417                 compile_generic "prefix=${TEMP}/diet install" utils
418                 print_info 1 "          >> Copying to bincache..."
419                 cd ${TEMP}
420                 tar -jcpf "${DIETLIBC_BINCACHE}" diet || gen_die "Could not tar up the dietlibc binary!"
421                 [ -f "${DIETLIBC_BINCACHE}" ] || gen_die 'Dietlibc cache not created!'
422                 echo "${TEMP}" > "${DIETLIBC_BINCACHE_TEMP}"
423
424                 cd "${TEMP}"
425                 rm -rf "${DIETLIBC_DIR}" > /dev/null
426                 rm -rf "${TEMP}/diet" > /dev/null
427         fi
428 }