Add apply_patches() function to automagically apply patches for a given util
[genkernel.git] / gen_compile.sh
1 #!/bin/bash
2
3 compile_kernel_args() {
4         local ARGS
5
6         ARGS=''
7         if [ "${KERNEL_CROSS_COMPILE}" != '' ]
8         then
9                 ARGS="${ARGS} CROSS_COMPILE=\"${KERNEL_CROSS_COMPILE}\""
10         else
11                 if [ "${KERNEL_CC}" != '' ]
12                 then
13                         ARGS="CC=\"${KERNEL_CC}\""
14                 fi
15                 if [ "${KERNEL_LD}" != '' ]
16                 then
17                         ARGS="${ARGS} LD=\"${KERNEL_LD}\""
18                 fi
19                 if [ "${KERNEL_AS}" != '' ]
20                 then
21                         ARGS="${ARGS} AS=\"${KERNEL_AS}\""
22                 fi
23         fi
24         echo -n "${ARGS}"
25 }
26
27 compile_utils_args()
28 {
29         local ARGS
30
31         ARGS=''
32         if [ "${UTILS_ARCH}" != '' ]
33         then
34                 ARGS="ARCH=\"${UTILS_ARCH}\""
35         fi
36         if [ "${UTILS_CC}" != '' ]
37         then
38                 ARGS="CC=\"${UTILS_CC}\""
39         fi
40         if [ "${UTILS_LD}" != '' ]
41         then
42                 ARGS="${ARGS} LD=\"${UTILS_LD}\""
43         fi
44         if [ "${UTILS_AS}" != '' ]
45         then
46                 ARGS="${ARGS} AS=\"${UTILS_AS}\""
47         fi
48
49         echo -n "${ARGS}"
50 }
51
52 export_utils_args()
53 {
54         save_args
55         if [ "${UTILS_ARCH}" != '' ]
56         then
57                 export ARCH="${UTILS_ARCH}"
58         fi
59         if [ "${UTILS_CC}" != '' ]
60         then
61                 export CC="${UTILS_CC}"
62         fi
63         if [ "${UTILS_LD}" != '' ]
64         then
65                 export LD="${UTILS_LD}"
66         fi
67         if [ "${UTILS_AS}" != '' ]
68         then
69                 export AS="${UTILS_AS}"
70         fi
71 }
72
73 unset_utils_args()
74 {
75         if [ "${UTILS_ARCH}" != '' ]
76         then
77                 unset ARCH
78         fi
79         if [ "${UTILS_CC}" != '' ]
80         then
81                 unset CC
82         fi
83         if [ "${UTILS_LD}" != '' ]
84         then
85                 unset LD
86         fi
87         if [ "${UTILS_AS}" != '' ]
88         then
89                 unset AS
90         fi
91         reset_args
92 }
93
94 export_kernel_args()
95 {
96         if [ "${KERNEL_CC}" != '' ]
97         then
98                 export CC="${KERNEL_CC}"
99         fi
100         if [ "${KERNEL_LD}" != '' ]
101         then
102                 export LD="${KERNEL_LD}"
103         fi
104         if [ "${KERNEL_AS}" != '' ]
105         then
106                 export AS="${KERNEL_AS}"
107         fi
108         if [ "${KERNEL_CROSS_COMPILE}" != '' ]
109         then
110                 export CROSS_COMPILE="${KERNEL_CROSS_COMPILE}"
111         fi
112 }
113
114 unset_kernel_args()
115 {
116         if [ "${KERNEL_CC}" != '' ]
117         then
118                 unset CC
119         fi
120         if [ "${KERNEL_LD}" != '' ]
121         then
122                 unset LD
123         fi
124         if [ "${KERNEL_AS}" != '' ]
125         then
126                 unset AS
127         fi
128         if [ "${KERNEL_CROSS_COMPILE}" != '' ]
129         then
130                 unset CROSS_COMPILE
131         fi
132 }
133 save_args()
134 {
135         if [ "${ARCH}" != '' ]
136         then
137                 export ORIG_ARCH="${ARCH}"
138         fi
139         if [ "${CC}" != '' ]
140         then
141                 export ORIG_CC="${CC}"
142         fi
143         if [ "${LD}" != '' ]
144         then
145                 export ORIG_LD="${LD}"
146         fi
147         if [ "${AS}" != '' ]
148         then
149                 export ORIG_AS="${AS}"
150         fi
151         if [ "${CROSS_COMPILE}" != '' ]
152         then
153                 export ORIG_CROSS_COMPILE="${CROSS_COMPILE}"
154         fi
155 }
156 reset_args()
157 {
158         if [ "${ORIG_ARCH}" != '' ]
159         then
160                 export ARCH="${ORIG_ARCH}"
161                 unset ORIG_ARCH
162         fi
163         if [ "${ORIG_CC}" != '' ]
164         then
165                 export CC="${ORIG_CC}"
166                 unset ORIG_CC
167         fi
168         if [ "${ORIG_LD}" != '' ]
169         then
170                 export LD="${ORIG_LD}"
171                 unset ORIG_LD
172         fi
173         if [ "${ORIG_AS}" != '' ]
174         then
175                 export AS="${ORIG_AS}"
176                 unset ORIG_AS
177         fi
178         if [ "${ORIG_CROSS_COMPILE}" != '' ]
179         then
180                 export CROSS_COMPILE="${ORIG_CROSS_COMPILE}"
181                 unset ORIG_CROSS_COMPILE
182         fi
183 }
184
185 apply_patches() {
186         util=$1
187         version=$1
188
189         if [ -d "${GK_SHARE}/patches/${util}/${version}" ]
190         then
191                 for i in "${GK_SHARE}/patches/${util}/${version}/*"
192                 do
193                         patch_success=0
194                         for j in `seq 0 5`
195                         do
196                                 patch -p${j} -f < "${i}"
197                                 if [ $? = 0 ]
198                                 then
199                                         patch_success=1
200                                         break
201                                 fi
202                         done
203                         if [ ${patch_success} != 1 ]
204                         then
205 #                               return 1
206                                 gen_die "could not apply patch ${i} for ${util}-${version}"
207                         fi
208                 done
209         fi
210 }
211
212 compile_generic() {
213         local RET
214         [ "$#" -lt '2' ] &&
215                 gen_die 'compile_generic(): improper usage!'
216         local target=${1}
217         local argstype=${2}
218
219         if [ "${argstype}" = 'kernel' ] || [ "${argstype}" = 'runtask' ]
220         then
221                 export_kernel_args
222                 MAKE=${KERNEL_MAKE}
223         elif [ "${2}" = 'utils' ]
224         then
225                 export_utils_args
226                 MAKE=${UTILS_MAKE}
227         fi
228         case "${argstype}" in
229                 kernel) ARGS="`compile_kernel_args`" ;;
230                 utils) ARGS="`compile_utils_args`" ;;
231                 *) ARGS="" ;; # includes runtask
232         esac
233         shift 2
234
235         # the eval usage is needed in the next set of code
236         # as ARGS can contain spaces and quotes, eg:
237         # ARGS='CC="ccache gcc"'
238         if [ "${argstype}" == 'runtask' ]
239         then
240                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS/-j?/j1} ${ARGS} ${target} $*" 1 0 1
241                 eval ${MAKE} -s ${MAKEOPTS/-j?/-j1} "${ARGS}" ${target} $*
242                 RET=$?
243         elif [ "${LOGLEVEL}" -gt "1" ]
244         then
245                 # Output to stdout and logfile
246                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $*" 1 0 1
247                 eval ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $* 2>&1 | tee -a ${LOGFILE}
248                 RET=${PIPESTATUS[0]}
249         else
250                 # Output to logfile only
251                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${ARGS} ${1} $*" 1 0 1
252                 eval ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $* >> ${LOGFILE} 2>&1
253                 RET=$?
254         fi
255         [ "${RET}" -ne '0' ] &&
256                 gen_die "Failed to compile the \"${target}\" target..."
257
258         unset MAKE
259         unset ARGS
260         if [ "${argstype}" = 'kernel' ]
261         then
262                 unset_kernel_args
263         elif [ "${argstype}" = 'utils' ]
264         then
265                 unset_utils_args
266         fi
267 }
268
269 extract_dietlibc_bincache() {
270         cd "${TEMP}"
271         rm -rf "${TEMP}/diet" > /dev/null
272         /bin/tar -jxpf "${DIETLIBC_BINCACHE}" ||
273                 gen_die 'Could not extract dietlibc bincache!'
274         [ ! -d "${TEMP}/diet" ] &&
275                 gen_die "${TEMP}/diet directory not found!"
276         cd - > /dev/null
277 }
278
279 clean_dietlibc_bincache() {
280         cd "${TEMP}"
281         rm -rf "${TEMP}/diet" > /dev/null
282         cd - > /dev/null
283 }
284
285 compile_dep() {
286         # Only run ``make dep'' for 2.4 kernels
287         if [ "${VER}" -eq '2' -a "${KERN_24}" -eq '1' ]
288         then
289                 print_info 1 "kernel: >> Making dependencies..."
290                 cd ${KERNEL_DIR}
291                 compile_generic dep kernel
292         fi
293 }
294
295 compile_modules() {
296         print_info 1 "        >> Compiling ${KV} modules..."
297         cd ${KERNEL_DIR}
298         compile_generic modules kernel
299         export UNAME_MACHINE="${ARCH}"
300         # On 2.4 kernels, if MAKEOPTS > -j1 it can cause failures
301         if [ "${VER}" -eq '2' -a "${KERN_24}" -eq '1' ]
302         then
303                 MAKEOPTS_SAVE="${MAKEOPTS}"
304                 MAKEOPTS="${MAKEOPTS_SAVE/-j?/-j1}"
305         fi
306         [ "${INSTALL_MOD_PATH}" != '' ] && export INSTALL_MOD_PATH
307         compile_generic "modules_install" kernel
308         [ "${VER}" -eq '2' -a "${KERN_24}" -eq '1' ] && MAKEOPTS="${MAKEOPTS_SAVE}"
309         export MAKEOPTS
310         unset UNAME_MACHINE
311 }
312
313 compile_kernel() {
314         [ "${KERNEL_MAKE}" = '' ] &&
315                 gen_die "KERNEL_MAKE undefined - I don't know how to compile a kernel for this arch!"
316         cd ${KERNEL_DIR}
317         print_info 1 "        >> Compiling ${KV} ${KERNEL_MAKE_DIRECTIVE/_install/ [ install ]/}..."
318         compile_generic "${KERNEL_MAKE_DIRECTIVE}" kernel
319         if [ "${KERNEL_MAKE_DIRECTIVE_2}" != '' ]
320         then
321                 print_info 1 "        >> Starting supplimental compile of ${KV}: ${KERNEL_MAKE_DIRECTIVE_2}..."
322                 compile_generic "${KERNEL_MAKE_DIRECTIVE_2}" kernel
323         fi
324         if ! isTrue "${CMD_NOINSTALL}"
325         then
326                 copy_image_with_preserve "kernel" \
327                         "${KERNEL_BINARY}" \
328                         "kernel-${KNAME}-${ARCH}-${KV}"
329
330                 copy_image_with_preserve "System.map" \
331                         "System.map" \
332                         "System.map-${KNAME}-${ARCH}-${KV}"
333
334                 if [ "${ENABLE_PEGASOS_HACKS}" = 'yes' ]
335                 then
336                         copy_image_with_preserve "kernelz" \
337                                 "${KERNEL_BINARY_2}" \
338                                 "kernelz-${KV}"
339                 fi
340         else
341                 cp "${KERNEL_BINARY}" "${TMPDIR}/kernel-${KNAME}-${ARCH}-${KV}" ||
342                         gen_die "Could not copy the kernel binary to ${TMPDIR}!"
343                 cp "System.map" "${TMPDIR}/System.map-${KNAME}-${ARCH}-${KV}" ||
344                         gen_die "Could not copy System.map to ${TMPDIR}!"
345                 if [ "${ENABLE_PEGASOS_HACKS}" = 'yes' ]
346                 then
347                         cp "${KERNEL_BINARY_2}" "${TMPDIR}/kernelz-${KV}" ||
348                                 gen_die "Could not copy the kernelz binary to ${TMPDIR}!"
349                 fi
350         fi
351 }
352
353 compile_busybox() {
354         [ -f "${BUSYBOX_SRCTAR}" ] ||
355                 gen_die "Could not find busybox source tarball: ${BUSYBOX_SRCTAR}!"
356         [ -f "${BUSYBOX_CONFIG}" ] ||
357                 gen_die "Cound not find busybox config file: ${BUSYBOX_CONFIG}!"
358         cd "${TEMP}"
359         rm -rf "${BUSYBOX_DIR}" > /dev/null
360         /bin/tar -jxpf ${BUSYBOX_SRCTAR} ||
361                 gen_die 'Could not extract busybox source tarball!'
362         [ -d "${BUSYBOX_DIR}" ] ||
363                 gen_die 'Busybox directory ${BUSYBOX_DIR} is invalid!'
364         cp "${BUSYBOX_CONFIG}" "${BUSYBOX_DIR}/.config"
365         sed -i ${BUSYBOX_DIR}/.config -e 's/#\? \?CONFIG_FEATURE_INSTALLER[ =].*/CONFIG_FEATURE_INSTALLER=y/g'
366         cd "${BUSYBOX_DIR}"
367 #       patch -p1 < "${GK_SHARE}/pkg/busybox-1.1.3+gentoo-mdadm.patch"
368 #       patch -p1 < "${GK_SHARE}/pkg/busybox-1.1.3+gentoo-mdadm2.patch"
369 #       patch -p1 < "${GK_SHARE}/pkg/busybox-1.1.3+gentoo-mdadm3.patch"
370         apply_patches busybox ${BUSYBOX_VER}
371         print_info 1 'busybox: >> Configuring...'
372         yes '' 2>/dev/null | compile_generic oldconfig utils
373
374         # Delete cache if stored config's MD5 does not match one to be used
375         if [ -f "${BUSYBOX_BINCACHE}" -a -f "${BUSYBOX_CONFIG}" ]
376         then
377                 oldconfig_md5=$(tar -xjf "${BUSYBOX_BINCACHE}" -O .config | md5sum)
378                 newconfig_md5=$(md5sum < .config)
379                 if [ "${oldconfig_md5}" != "${newconfig_md5}" ]
380                 then
381                         print_info 1 "busybox: >> Removing stale cache..."
382                         rm -rf "${BUSYBOX_BINCACHE}"
383                 else
384                         print_info 1 "busybox: >> Using cache"
385                 fi
386         fi
387
388         if [ ! -f "${BUSYBOX_BINCACHE}" ]
389         then
390                 print_info 1 'busybox: >> Compiling...'
391                 compile_generic all utils
392                 print_info 1 'busybox: >> Copying to cache...'
393                 [ -f "${TEMP}/${BUSYBOX_DIR}/busybox" ] ||
394                         gen_die 'Busybox executable does not exist!'
395                 strip "${TEMP}/${BUSYBOX_DIR}/busybox" ||
396                         gen_die 'Could not strip busybox binary!'
397                 tar -cj -C "${TEMP}/${BUSYBOX_DIR}" -f "${BUSYBOX_BINCACHE}" busybox .config ||
398                         gen_die 'Could not create the busybox bincache!'
399         fi
400
401         cd "${TEMP}"
402         rm -rf "${BUSYBOX_DIR}" > /dev/null
403 }
404
405 compile_lvm() {
406         compile_device_mapper
407         if [ ! -f "${LVM_BINCACHE}" ]
408         then
409                 [ -f "${LVM_SRCTAR}" ] ||
410                         gen_die "Could not find LVM source tarball: ${LVM_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
411                 cd "${TEMP}"
412                 rm -rf ${LVM_DIR} > /dev/null
413                 /bin/tar -zxpf ${LVM_SRCTAR} ||
414                         gen_die 'Could not extract LVM source tarball!'
415                 [ -d "${LVM_DIR}" ] ||
416                         gen_die 'LVM directory ${LVM_DIR} is invalid!'
417                 rm -rf "${TEMP}/device-mapper" > /dev/null
418                 /bin/tar -jxpf "${DEVICE_MAPPER_BINCACHE}" -C "${TEMP}" ||
419                         gen_die "Could not extract device-mapper binary cache!";
420                 
421                 cd "${LVM_DIR}"
422                 print_info 1 'lvm: >> Configuring...'
423                         LDFLAGS="-L${TEMP}/device-mapper/lib" \
424                         CFLAGS="-I${TEMP}/device-mapper/include" \
425                         CPPFLAGS="-I${TEMP}/device-mapper/include" \
426                         ./configure --enable-static_link --prefix=${TEMP}/lvm >> ${LOGFILE} 2>&1 ||
427                                 gen_die 'Configure of lvm failed!'
428                 print_info 1 'lvm: >> Compiling...'
429                         compile_generic '' utils
430                         compile_generic 'install' utils
431
432                 cd "${TEMP}/lvm"
433                 print_info 1 '      >> Copying to bincache...'
434                 strip "sbin/lvm.static" ||
435                         gen_die 'Could not strip lvm.static!'
436                 /bin/tar -cjf "${LVM_BINCACHE}" sbin/lvm.static ||
437                         gen_die 'Could not create binary cache'
438
439                 cd "${TEMP}"
440                 rm -rf "${TEMP}/device-mapper" > /dev/null
441                 rm -rf "${LVM_DIR}" lvm
442         fi
443 }
444
445 compile_dmraid() {
446         compile_device_mapper
447         if [ ! -f "${DMRAID_BINCACHE}" ]
448         then
449                 [ -f "${DMRAID_SRCTAR}" ] ||
450                         gen_die "Could not find DMRAID source tarball: ${DMRAID_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
451                 cd "${TEMP}"
452                 rm -rf ${DMRAID_DIR} > /dev/null
453                 /bin/tar -jxpf ${DMRAID_SRCTAR} ||
454                         gen_die 'Could not extract DMRAID source tarball!'
455                 [ -d "${DMRAID_DIR}" ] ||
456                         gen_die 'DMRAID directory ${DMRAID_DIR} is invalid!'
457                 rm -rf "${TEMP}/device-mapper" > /dev/null
458                 /bin/tar -jxpf "${DEVICE_MAPPER_BINCACHE}" -C "${TEMP}" ||
459                         gen_die "Could not extract device-mapper binary cache!";
460                 
461                 cd "${DMRAID_DIR}"
462                 print_info 1 'dmraid: >> Configuring...'
463                 
464                 LDFLAGS="-L${TEMP}/device-mapper/lib" \
465                 CFLAGS="-I${TEMP}/device-mapper/include" \
466                 CPPFLAGS="-I${TEMP}/device-mapper/include" \
467                 ./configure --enable-static_link --prefix=${TEMP}/dmraid >> ${LOGFILE} 2>&1 ||
468                         gen_die 'Configure of dmraid failed!'
469
470                 # We dont necessarily have selinux installed yet... look into
471                 # selinux global support in the future.
472                 sed -i tools/Makefile -e "s|DMRAIDLIBS += -lselinux||g"
473                 ###echo "DMRAIDLIBS += -lselinux -lsepol" >> tools/Makefile
474                 mkdir -p "${TEMP}/dmraid"
475                 print_info 1 'dmraid: >> Compiling...'
476                 # Force dmraid to be built with -j1 for bug #188273
477                 MAKEOPTS=-j1 compile_generic '' utils
478                 #compile_generic 'install' utils
479                 mkdir ${TEMP}/dmraid/sbin
480                 install -m 0755 -s tools/dmraid "${TEMP}/dmraid/sbin/dmraid"
481                 print_info 1 '      >> Copying to bincache...'
482                 cd "${TEMP}/dmraid"
483                 /bin/tar -cjf "${DMRAID_BINCACHE}" sbin/dmraid ||
484                         gen_die 'Could not create binary cache'
485
486                 cd "${TEMP}"
487                 rm -rf "${TEMP}/device-mapper" > /dev/null
488                 rm -rf "${DMRAID_DIR}" dmraid
489         fi
490 }
491
492 compile_device_mapper() {
493         if [ ! -f "${DEVICE_MAPPER_BINCACHE}" ]
494         then
495                 [ ! -f "${DEVICE_MAPPER_SRCTAR}" ] &&
496                         gen_die "Could not find device-mapper source tarball: ${DEVICE_MAPPER_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
497                 cd "${TEMP}"
498                 rm -rf "${DEVICE_MAPPER_DIR}"
499                 /bin/tar -zxpf "${DEVICE_MAPPER_SRCTAR}"
500                 [ ! -d "${DEVICE_MAPPER_DIR}" ] &&
501                         gen_die "device-mapper directory ${DEVICE_MAPPER_DIR} invalid"
502                 cd "${DEVICE_MAPPER_DIR}"
503                 ./configure --prefix=${TEMP}/device-mapper --enable-static_link \
504                         --disable-selinux >> ${LOGFILE} 2>&1 ||
505                         gen_die 'Configuring device-mapper failed!'
506                 print_info 1 'device-mapper: >> Compiling...'
507                 compile_generic '' utils
508                 compile_generic 'install' utils
509                 print_info 1 '        >> Copying to cache...'
510                 cd "${TEMP}"
511                 rm -rf "${TEMP}/device-mapper/man" ||
512                         gen_die 'Could not remove manual pages!'
513                 strip "${TEMP}/device-mapper/sbin/dmsetup" ||
514                         gen_die 'Could not strip dmsetup binary!'
515                 /bin/tar -jcpf "${DEVICE_MAPPER_BINCACHE}" device-mapper ||
516                         gen_die 'Could not tar up the device-mapper binary!'
517                 [ -f "${DEVICE_MAPPER_BINCACHE}" ] ||
518                         gen_die 'device-mapper cache not created!'
519                 cd "${TEMP}"
520                 rm -rf "${DEVICE_MAPPER_DIR}" > /dev/null
521                 rm -rf "${TEMP}/device-mapper" > /dev/null
522         fi
523 }
524
525 compile_e2fsprogs() {
526         if [ ! -f "${BLKID_BINCACHE}" ]
527         then
528                 [ ! -f "${E2FSPROGS_SRCTAR}" ] &&
529                         gen_die "Could not find e2fsprogs source tarball: ${E2FSPROGS_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
530                 cd "${TEMP}"
531                 rm -rf "${E2FSPROGS_DIR}"
532                 tar -zxpf "${E2FSPROGS_SRCTAR}"
533                 [ ! -d "${E2FSPROGS_DIR}" ] &&
534                         gen_die "e2fsprogs directory ${E2FSPROGS_DIR} invalid"
535                 cd "${E2FSPROGS_DIR}"
536                 print_info 1 'e2fsprogs: >> Configuring...'
537                 ./configure  --with-ldopts=-static >> ${LOGFILE} 2>&1 ||
538                         gen_die 'Configuring e2fsprogs failed!'
539                 print_info 1 'e2fsprogs: >> Compiling...'
540                 MAKE=${UTILS_MAKE} compile_generic "" ""
541                 print_info 1 'blkid: >> Copying to cache...'
542                 [ -f "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ] ||
543                         gen_die 'Blkid executable does not exist!'
544                 strip "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ||
545                         gen_die 'Could not strip blkid binary!'
546                 bzip2 "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ||
547                         gen_die 'bzip2 compression of blkid failed!'
548                 mv "${TEMP}/${E2FSPROGS_DIR}/misc/blkid.bz2" "${BLKID_BINCACHE}" ||
549                         gen_die 'Could not copy the blkid binary to the package directory, does the directory exist?'
550
551                 cd "${TEMP}"
552                 rm -rf "${E2FSPROGS_DIR}" > /dev/null
553         fi
554 }