style: Fix trailing whitespace error
[genkernel.git] / gen_compile.sh
1 #!/bin/bash
2 # $Id$
3
4 compile_kernel_args() {
5         local ARGS
6
7         ARGS=''
8         if [ "${KERNEL_CROSS_COMPILE}" != '' ]
9         then
10                 ARGS="${ARGS} CROSS_COMPILE=\"${KERNEL_CROSS_COMPILE}\""
11         else
12                 if [ "${KERNEL_CC}" != '' ]
13                 then
14                         ARGS="CC=\"${KERNEL_CC}\""
15                 fi
16                 if [ "${KERNEL_LD}" != '' ]
17                 then
18                         ARGS="${ARGS} LD=\"${KERNEL_LD}\""
19                 fi
20                 if [ "${KERNEL_AS}" != '' ]
21                 then
22                         ARGS="${ARGS} AS=\"${KERNEL_AS}\""
23                 fi
24                 if [ -n "${KERNEL_ARCH}" ]
25                 then
26                         ARGS="${ARGS} ARCH=\"${KERNEL_ARCH}\""
27                 fi
28         fi
29         echo -n "${ARGS}"
30 }
31
32 compile_utils_args()
33 {
34         local ARGS
35         ARGS=''
36
37         if [ -n "${UTILS_CROSS_COMPILE}" ]
38         then
39                 UTILS_CC="${UTILS_CROSS_COMPILE}gcc"
40                 UTILS_LD="${UTILS_CROSS_COMPILE}ld"
41                 UTILS_AS="${UTILS_CROSS_COMPILE}as"
42         fi
43
44         if [ "${UTILS_ARCH}" != '' ]
45         then
46                 ARGS="ARCH=\"${UTILS_ARCH}\""
47         fi
48         if [ "${UTILS_CC}" != '' ]
49         then
50                 ARGS="CC=\"${UTILS_CC}\""
51         fi
52         if [ "${UTILS_LD}" != '' ]
53         then
54                 ARGS="${ARGS} LD=\"${UTILS_LD}\""
55         fi
56         if [ "${UTILS_AS}" != '' ]
57         then
58                 ARGS="${ARGS} AS=\"${UTILS_AS}\""
59         fi
60
61         echo -n "${ARGS}"
62 }
63
64 export_utils_args()
65 {
66         save_args
67         if [ "${UTILS_ARCH}" != '' ]
68         then
69                 export ARCH="${UTILS_ARCH}"
70         fi
71         if [ "${UTILS_CC}" != '' ]
72         then
73                 export CC="${UTILS_CC}"
74         fi
75         if [ "${UTILS_LD}" != '' ]
76         then
77                 export LD="${UTILS_LD}"
78         fi
79         if [ "${UTILS_AS}" != '' ]
80         then
81                 export AS="${UTILS_AS}"
82         fi
83 }
84
85 unset_utils_args()
86 {
87         if [ "${UTILS_ARCH}" != '' ]
88         then
89                 unset ARCH
90         fi
91         if [ "${UTILS_CC}" != '' ]
92         then
93                 unset CC
94         fi
95         if [ "${UTILS_LD}" != '' ]
96         then
97                 unset LD
98         fi
99         if [ "${UTILS_AS}" != '' ]
100         then
101                 unset AS
102         fi
103         reset_args
104 }
105
106 export_kernel_args()
107 {
108         if [ "${KERNEL_CC}" != '' ]
109         then
110                 export CC="${KERNEL_CC}"
111         fi
112         if [ "${KERNEL_LD}" != '' ]
113         then
114                 export LD="${KERNEL_LD}"
115         fi
116         if [ "${KERNEL_AS}" != '' ]
117         then
118                 export AS="${KERNEL_AS}"
119         fi
120         if [ "${KERNEL_CROSS_COMPILE}" != '' ]
121         then
122                 export CROSS_COMPILE="${KERNEL_CROSS_COMPILE}"
123         fi
124 }
125
126 unset_kernel_args()
127 {
128         if [ "${KERNEL_CC}" != '' ]
129         then
130                 unset CC
131         fi
132         if [ "${KERNEL_LD}" != '' ]
133         then
134                 unset LD
135         fi
136         if [ "${KERNEL_AS}" != '' ]
137         then
138                 unset AS
139         fi
140         if [ "${KERNEL_CROSS_COMPILE}" != '' ]
141         then
142                 unset CROSS_COMPILE
143         fi
144 }
145 save_args()
146 {
147         if [ "${ARCH}" != '' ]
148         then
149                 export ORIG_ARCH="${ARCH}"
150         fi
151         if [ "${CC}" != '' ]
152         then
153                 export ORIG_CC="${CC}"
154         fi
155         if [ "${LD}" != '' ]
156         then
157                 export ORIG_LD="${LD}"
158         fi
159         if [ "${AS}" != '' ]
160         then
161                 export ORIG_AS="${AS}"
162         fi
163         if [ "${CROSS_COMPILE}" != '' ]
164         then
165                 export ORIG_CROSS_COMPILE="${CROSS_COMPILE}"
166         fi
167 }
168 reset_args()
169 {
170         if [ "${ORIG_ARCH}" != '' ]
171         then
172                 export ARCH="${ORIG_ARCH}"
173                 unset ORIG_ARCH
174         fi
175         if [ "${ORIG_CC}" != '' ]
176         then
177                 export CC="${ORIG_CC}"
178                 unset ORIG_CC
179         fi
180         if [ "${ORIG_LD}" != '' ]
181         then
182                 export LD="${ORIG_LD}"
183                 unset ORIG_LD
184         fi
185         if [ "${ORIG_AS}" != '' ]
186         then
187                 export AS="${ORIG_AS}"
188                 unset ORIG_AS
189         fi
190         if [ "${ORIG_CROSS_COMPILE}" != '' ]
191         then
192                 export CROSS_COMPILE="${ORIG_CROSS_COMPILE}"
193                 unset ORIG_CROSS_COMPILE
194         fi
195 }
196
197 apply_patches() {
198         util=$1
199         version=$2
200
201         if [ -d "${GK_SHARE}/patches/${util}/${version}" ]
202         then
203                 print_info 1 "${util}: >> Applying patches..."
204                 for i in ${GK_SHARE}/patches/${util}/${version}/*{diff,patch}
205                 do
206                         [ -f "${i}" ] || continue
207                         patch_success=0
208                         for j in `seq 0 5`
209                         do
210                                 patch -p${j} --backup-if-mismatch -f < "${i}" >/dev/null
211                                 if [ $? = 0 ]
212                                 then
213                                         patch_success=1
214                                         break
215                                 fi
216                         done
217                         if [ ${patch_success} -eq 1 ]
218                         then
219                                 print_info 1 "          - `basename ${i}`"
220                         else
221                                 gen_die "could not apply patch ${i} for ${util}-${version}"
222                         fi
223                 done
224         fi
225 }
226
227 compile_generic() {
228         local RET
229         [ "$#" -lt '2' ] &&
230                 gen_die 'compile_generic(): improper usage!'
231         local target=${1}
232         local argstype=${2}
233
234         if [ "${argstype}" = 'kernel' ] || [ "${argstype}" = 'runtask' ]
235         then
236                 export_kernel_args
237                 MAKE=${KERNEL_MAKE}
238         elif [ "${2}" = 'utils' ]
239         then
240                 export_utils_args
241                 MAKE=${UTILS_MAKE}
242         fi
243         case "${argstype}" in
244                 kernel) ARGS="`compile_kernel_args`" ;;
245                 utils) ARGS="`compile_utils_args`" ;;
246                 *) ARGS="" ;; # includes runtask
247         esac
248         shift 2
249
250         # the eval usage is needed in the next set of code
251         # as ARGS can contain spaces and quotes, eg:
252         # ARGS='CC="ccache gcc"'
253         if [ "${argstype}" == 'runtask' ]
254         then
255                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS/-j?/j1} ${ARGS} ${target} $*" 1 0 1
256                 eval ${MAKE} -s ${MAKEOPTS/-j?/-j1} "${ARGS}" ${target} $*
257                 RET=$?
258         elif [ "${LOGLEVEL}" -gt "1" ]
259         then
260                 # Output to stdout and logfile
261                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $*" 1 0 1
262                 eval ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $* 2>&1 | tee -a ${LOGFILE}
263                 RET=${PIPESTATUS[0]}
264         else
265                 # Output to logfile only
266                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${ARGS} ${1} $*" 1 0 1
267                 eval ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $* >> ${LOGFILE} 2>&1
268                 RET=$?
269         fi
270         [ ${RET} -ne 0 ] &&
271                 gen_die "Failed to compile the \"${target}\" target..."
272
273         unset MAKE
274         unset ARGS
275         if [ "${argstype}" = 'kernel' ]
276         then
277                 unset_kernel_args
278         elif [ "${argstype}" = 'utils' ]
279         then
280                 unset_utils_args
281         fi
282 }
283
284 compile_modules() {
285         print_info 1 "        >> Compiling ${KV} modules..."
286         cd ${KERNEL_DIR}
287         compile_generic modules kernel
288         export UNAME_MACHINE="${ARCH}"
289         [ "${INSTALL_MOD_PATH}" != '' ] && export INSTALL_MOD_PATH
290         compile_generic "modules_install" kernel
291         unset UNAME_MACHINE
292 }
293
294 compile_kernel() {
295         [ "${KERNEL_MAKE}" = '' ] &&
296                 gen_die "KERNEL_MAKE undefined - I don't know how to compile a kernel for this arch!"
297         cd ${KERNEL_DIR}
298         print_info 1 "        >> Compiling ${KV} ${KERNEL_MAKE_DIRECTIVE/_install/ [ install ]/}..."
299         compile_generic "${KERNEL_MAKE_DIRECTIVE}" kernel
300         if [ "${KERNEL_MAKE_DIRECTIVE_2}" != '' ]
301         then
302                 print_info 1 "        >> Starting supplimental compile of ${KV}: ${KERNEL_MAKE_DIRECTIVE_2}..."
303                 compile_generic "${KERNEL_MAKE_DIRECTIVE_2}" kernel
304         fi
305
306         local firmware_in_kernel_line=`fgrep CONFIG_FIRMWARE_IN_KERNEL "${KERNEL_DIR}"/.config`
307         if [ -n "${firmware_in_kernel_line}" -a "${firmware_in_kernel_line}" != CONFIG_FIRMWARE_IN_KERNEL=y ]
308         then
309                 print_info 1 "        >> Installing firmware ('make firmware_install') due to CONFIG_FIRMWARE_IN_KERNEL != y..."
310                 compile_generic "firmware_install" kernel
311         else
312                 print_info 1 "        >> Not installing firmware as it's included in the kernel already (CONFIG_FIRMWARE_IN_KERNEL=y)..."
313         fi
314
315         local tmp_kernel_binary=$(find_kernel_binary ${KERNEL_BINARY})
316         local tmp_kernel_binary2=$(find_kernel_binary ${KERNEL_BINARY_2})
317         if [ -z "${tmp_kernel_binary}" ]
318         then
319                 gen_die "Cannot locate kernel binary"
320         fi
321
322         if ! isTrue "${CMD_NOINSTALL}"
323         then
324                 copy_image_with_preserve "kernel" \
325                         "${tmp_kernel_binary}" \
326                         "kernel-${KNAME}-${ARCH}-${KV}"
327
328                 copy_image_with_preserve "System.map" \
329                         "System.map" \
330                         "System.map-${KNAME}-${ARCH}-${KV}"
331
332                 if isTrue "${GENZIMAGE}"
333                 then
334                         copy_image_with_preserve "kernelz" \
335                                 "${tmp_kernel_binary2}" \
336                                 "kernelz-${KV}"
337                 fi
338         else
339                 cp "${tmp_kernel_binary}" "${TMPDIR}/kernel-${KNAME}-${ARCH}-${KV}" ||
340                         gen_die "Could not copy the kernel binary to ${TMPDIR}!"
341                 cp "System.map" "${TMPDIR}/System.map-${KNAME}-${ARCH}-${KV}" ||
342                         gen_die "Could not copy System.map to ${TMPDIR}!"
343                 if isTrue "${GENZIMAGE}"
344                 then
345                         cp "${tmp_kernel_binary2}" "${TMPDIR}/kernelz-${KV}" ||
346                                 gen_die "Could not copy the kernelz binary to ${TMPDIR}!"
347                 fi
348         fi
349 }
350
351 compile_busybox() {
352         [ -f "${BUSYBOX_SRCTAR}" ] ||
353                 gen_die "Could not find busybox source tarball: ${BUSYBOX_SRCTAR}!"
354
355         if [ -n "${BUSYBOX_CONFIG}" ]
356         then
357                 [ -f "${BUSYBOX_CONFIG}" ] ||
358                         gen_die "Could not find busybox config file: ${BUSYBOX_CONFIG}"
359         elif isTrue "${NETBOOT}" && [ -f "$(arch_replace "${GK_SHARE}/arch/%%ARCH%%/netboot-busy-config")" ]
360         then
361                 BUSYBOX_CONFIG="$(arch_replace "${GK_SHARE}/arch/%%ARCH%%/netboot-busy-config")"
362         elif isTrue "${NETBOOT}" && [ -f "${GK_SHARE}/netboot/busy-config" ]
363         then
364                 BUSYBOX_CONFIG="${GK_SHARE}/netboot/busy-config"
365         elif [ -f "$(arch_replace "${GK_SHARE}/arch/%%ARCH%%/busy-config")" ]
366         then
367                 BUSYBOX_CONFIG="$(arch_replace "${GK_SHARE}/arch/%%ARCH%%/busy-config")"
368         elif [ -f "${GK_SHARE}/defaults/busy-config" ]
369         then
370                 BUSYBOX_CONFIG="${GK_SHARE}/defaults/busy-config"
371         else
372                 gen_die "Could not find a busybox config file"
373         fi
374
375         # Delete cache if stored config's MD5 does not match one to be used
376         if [ -f "${BUSYBOX_BINCACHE}" ]
377         then
378                 oldconfig_md5=$(tar -xjf "${BUSYBOX_BINCACHE}" -O .config.gk_orig 2>/dev/null | md5sum)
379                 newconfig_md5=$(md5sum < "${BUSYBOX_CONFIG}")
380                 if [ "${oldconfig_md5}" != "${newconfig_md5}" ]
381                 then
382                         print_info 1 "busybox: >> Removing stale cache..."
383                         rm -rf "${BUSYBOX_BINCACHE}"
384                 else
385                         print_info 1 "busybox: >> Using cache"
386                 fi
387         fi
388
389         if [ ! -f "${BUSYBOX_BINCACHE}" ]
390         then
391                 cd "${TEMP}"
392                 rm -rf "${BUSYBOX_DIR}" > /dev/null
393                 /bin/tar -jxpf ${BUSYBOX_SRCTAR} ||
394                         gen_die 'Could not extract busybox source tarball!'
395                 [ -d "${BUSYBOX_DIR}" ] ||
396                         gen_die 'Busybox directory ${BUSYBOX_DIR} is invalid!'
397                 cp "${BUSYBOX_CONFIG}" "${BUSYBOX_DIR}/.config"
398                 cp "${BUSYBOX_CONFIG}" "${BUSYBOX_DIR}/.config.gk_orig"
399                 cd "${BUSYBOX_DIR}"
400                 apply_patches busybox ${BUSYBOX_VER}
401                 print_info 1 'busybox: >> Configuring...'
402                 yes '' 2>/dev/null | compile_generic oldconfig utils
403
404                 print_info 1 'busybox: >> Compiling...'
405                 compile_generic all utils
406                 print_info 1 'busybox: >> Copying to cache...'
407                 [ -f "${TEMP}/${BUSYBOX_DIR}/busybox" ] ||
408                         gen_die 'Busybox executable does not exist!'
409                 strip "${TEMP}/${BUSYBOX_DIR}/busybox" ||
410                         gen_die 'Could not strip busybox binary!'
411                 tar -cj -C "${TEMP}/${BUSYBOX_DIR}" -f "${BUSYBOX_BINCACHE}" busybox .config .config.gk_orig ||
412                         gen_die 'Could not create the busybox bincache!'
413
414                 cd "${TEMP}"
415                 rm -rf "${BUSYBOX_DIR}" > /dev/null
416         fi
417 }
418
419 compile_lvm() {
420         if [ ! -f "${LVM_BINCACHE}" ]
421         then
422                 [ -f "${LVM_SRCTAR}" ] ||
423                         gen_die "Could not find LVM source tarball: ${LVM_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
424                 cd "${TEMP}"
425                 rm -rf ${LVM_DIR} > /dev/null
426                 /bin/tar -zxpf ${LVM_SRCTAR} ||
427                         gen_die 'Could not extract LVM source tarball!'
428                 [ -d "${LVM_DIR}" ] ||
429                         gen_die 'LVM directory ${LVM_DIR} is invalid!'
430                 rm -rf "${TEMP}/device-mapper" > /dev/null
431                 /bin/tar -jxpf "${DEVICE_MAPPER_BINCACHE}" -C "${TEMP}" ||
432                         gen_die "Could not extract device-mapper binary cache!";
433                 
434                 cd "${LVM_DIR}"
435                 apply_patches lvm ${LVM_VER}
436                 print_info 1 'lvm: >> Configuring...'
437                         CFLAGS="-fPIC" \
438                         ./configure --enable-static_link --prefix=${TEMP}/lvm \
439                                 --with-lvm1=internal --with-clvmd=none --with-cluster=none \
440                                 --disable-readline --disable-selinux --with-mirrors=internal \
441                                 --with-snapshots=internal --with-pool=internal >> ${LOGFILE} 2>&1 || \
442                                 gen_die 'Configure of lvm failed!'
443                 print_info 1 'lvm: >> Compiling...'
444                         compile_generic '' utils
445                         compile_generic 'install' utils
446
447                 cd "${TEMP}/lvm"
448                 print_info 1 '      >> Copying to bincache...'
449                 strip "sbin/lvm.static" ||
450                         gen_die 'Could not strip lvm.static!'
451                 /bin/tar -cjf "${LVM_BINCACHE}" sbin/lvm.static ||
452                         gen_die 'Could not create binary cache'
453
454                 cd "${TEMP}"
455                 rm -rf "${TEMP}/device-mapper" > /dev/null
456                 rm -rf "${LVM_DIR}" lvm
457         fi
458 }
459
460 compile_mdadm() {
461         if [ ! -f "${MDADM_BINCACHE}" ]
462         then
463                 [ -f "${MDADM_SRCTAR}" ] ||
464                         gen_die "Could not find MDADM source tarball: ${MDADM_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
465                 cd "${TEMP}"
466                 rm -rf "${MDADM_DIR}" > /dev/null
467                 /bin/tar -jxpf "${MDADM_SRCTAR}" ||
468                         gen_die 'Could not extract MDADM source tarball!'
469                 [ -d "${MDADM_DIR}" ] ||
470                         gen_die 'MDADM directory ${MDADM_DIR} is invalid!'
471
472                 cd "${MDADM_DIR}"
473                 sed -i "/^CFLAGS = /s:^CFLAGS = \(.*\)$:CFLAGS = -Os:" Makefile
474                 sed -i "/^CXFLAGS = /s:^CXFLAGS = \(.*\)$:CXFLAGS = -Os:" Makefile
475                 sed -i "/^CWFLAGS = /s:^CWFLAGS = \(.*\)$:CWFLAGS = -Wall:" Makefile
476                 sed -i "s/^# LDFLAGS = -static/LDFLAGS = -static/" Makefile
477
478                 print_info 1 'mdadm: >> Compiling...'
479                         compile_generic 'mdadm mdmon' utils
480
481                 mkdir -p "${TEMP}/mdadm/sbin"
482                 install -m 0755 -s mdadm "${TEMP}/mdadm/sbin/mdadm"
483                 install -m 0755 -s mdmon "${TEMP}/mdadm/sbin/mdmon"
484                 print_info 1 '      >> Copying to bincache...'
485                 cd "${TEMP}/mdadm"
486                 strip "sbin/mdadm" "sbin/mdmon" ||
487                         gen_die 'Could not strip mdadm binaries!'
488                 /bin/tar -cjf "${MDADM_BINCACHE}" sbin/mdadm sbin/mdmon ||
489                         gen_die 'Could not create binary cache'
490
491                 cd "${TEMP}"
492                 rm -rf "${MDADM_DIR}" mdadm
493         fi
494 }
495
496 compile_dmraid() {
497         compile_device_mapper
498         if [ ! -f "${DMRAID_BINCACHE}" ]
499         then
500                 [ -f "${DMRAID_SRCTAR}" ] ||
501                         gen_die "Could not find DMRAID source tarball: ${DMRAID_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
502                 cd "${TEMP}"
503                 rm -rf ${DMRAID_DIR} > /dev/null
504                 /bin/tar -jxpf ${DMRAID_SRCTAR} ||
505                         gen_die 'Could not extract DMRAID source tarball!'
506                 [ -d "${DMRAID_DIR}" ] ||
507                         gen_die 'DMRAID directory ${DMRAID_DIR} is invalid!'
508                 rm -rf "${TEMP}/device-mapper" > /dev/null
509                 /bin/tar -jxpf "${DEVICE_MAPPER_BINCACHE}" -C "${TEMP}" ||
510                         gen_die "Could not extract device-mapper binary cache!";
511                 
512                 cd "${DMRAID_DIR}"
513                 print_info 1 'dmraid: >> Configuring...'
514                 
515                 LDFLAGS="-L${TEMP}/device-mapper/lib" \
516                 CFLAGS="-I${TEMP}/device-mapper/include" \
517                 CPPFLAGS="-I${TEMP}/device-mapper/include" \
518                 ./configure --enable-static_link --prefix=${TEMP}/dmraid >> ${LOGFILE} 2>&1 ||
519                         gen_die 'Configure of dmraid failed!'
520
521                 # We dont necessarily have selinux installed yet... look into
522                 # selinux global support in the future.
523                 sed -i tools/Makefile -e "s|DMRAIDLIBS += -lselinux||g"
524                 ###echo "DMRAIDLIBS += -lselinux -lsepol" >> tools/Makefile
525                 mkdir -p "${TEMP}/dmraid"
526                 print_info 1 'dmraid: >> Compiling...'
527                 # Force dmraid to be built with -j1 for bug #188273
528                 MAKEOPTS=-j1 compile_generic '' utils
529                 #compile_generic 'install' utils
530                 mkdir ${TEMP}/dmraid/sbin
531                 install -m 0755 -s tools/dmraid "${TEMP}/dmraid/sbin/dmraid"
532                 print_info 1 '      >> Copying to bincache...'
533                 cd "${TEMP}/dmraid"
534                 /bin/tar -cjf "${DMRAID_BINCACHE}" sbin/dmraid ||
535                         gen_die 'Could not create binary cache'
536
537                 cd "${TEMP}"
538                 rm -rf "${TEMP}/device-mapper" > /dev/null
539                 rm -rf "${DMRAID_DIR}" dmraid
540         fi
541 }
542
543 compile_device_mapper() {
544         if [ ! -f "${DEVICE_MAPPER_BINCACHE}" ]
545         then
546                 [ ! -f "${DEVICE_MAPPER_SRCTAR}" ] &&
547                         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!"
548                 cd "${TEMP}"
549                 rm -rf "${DEVICE_MAPPER_DIR}"
550                 /bin/tar -zxpf "${DEVICE_MAPPER_SRCTAR}"
551                 [ ! -d "${DEVICE_MAPPER_DIR}" ] &&
552                         gen_die "device-mapper directory ${DEVICE_MAPPER_DIR} invalid"
553                 cd "${DEVICE_MAPPER_DIR}"
554                 CFLAGS="-fPIC" \
555                 ./configure --prefix=${TEMP}/device-mapper --enable-static_link \
556                         --disable-selinux >> ${LOGFILE} 2>&1 ||
557                         gen_die 'Configuring device-mapper failed!'
558                 print_info 1 'device-mapper: >> Compiling...'
559                 compile_generic '' utils
560                 compile_generic 'install' utils
561                 print_info 1 '        >> Copying to cache...'
562                 cd "${TEMP}"
563                 rm -rf "${TEMP}/device-mapper/man" ||
564                         gen_die 'Could not remove manual pages!'
565                 strip "${TEMP}/device-mapper/sbin/dmsetup" ||
566                         gen_die 'Could not strip dmsetup binary!'
567                 /bin/tar -jcpf "${DEVICE_MAPPER_BINCACHE}" device-mapper ||
568                         gen_die 'Could not tar up the device-mapper binary!'
569                 [ -f "${DEVICE_MAPPER_BINCACHE}" ] ||
570                         gen_die 'device-mapper cache not created!'
571                 cd "${TEMP}"
572                 rm -rf "${DEVICE_MAPPER_DIR}" > /dev/null
573                 rm -rf "${TEMP}/device-mapper" > /dev/null
574         fi
575 }
576
577 compile_e2fsprogs() {
578         if [ -f "${BLKID_BINCACHE}" ]
579         then
580                 print_info 1 "blkid: >> Using cache"
581         else
582                 [ ! -f "${E2FSPROGS_SRCTAR}" ] &&
583                         gen_die "Could not find e2fsprogs source tarball: ${E2FSPROGS_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
584                 cd "${TEMP}"
585                 rm -rf "${E2FSPROGS_DIR}"
586                 tar -zxpf "${E2FSPROGS_SRCTAR}"
587                 [ ! -d "${E2FSPROGS_DIR}" ] &&
588                         gen_die "e2fsprogs directory ${E2FSPROGS_DIR} invalid"
589                 cd "${E2FSPROGS_DIR}"
590                 print_info 1 'e2fsprogs: >> Configuring...'
591                 LDFLAGS=-static ./configure >> ${LOGFILE} 2>&1 ||
592                         gen_die 'Configuring e2fsprogs failed!'
593                 print_info 1 'e2fsprogs: >> Compiling...'
594                 MAKE=${UTILS_MAKE} compile_generic "" ""
595                 print_info 1 'blkid: >> Copying to cache...'
596                 [ -f "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ] ||
597                         gen_die 'Blkid executable does not exist!'
598                 strip "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ||
599                         gen_die 'Could not strip blkid binary!'
600                 bzip2 "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ||
601                         gen_die 'bzip2 compression of blkid failed!'
602                 mv "${TEMP}/${E2FSPROGS_DIR}/misc/blkid.bz2" "${BLKID_BINCACHE}" ||
603                         gen_die 'Could not copy the blkid binary to the package directory, does the directory exist?'
604
605                 cd "${TEMP}"
606                 rm -rf "${E2FSPROGS_DIR}" > /dev/null
607         fi
608 }
609
610 compile_fuse() {
611         if [ ! -f "${FUSE_BINCACHE}" ]
612         then
613                 [ ! -f "${FUSE_SRCTAR}" ] &&
614                         gen_die "Could not find fuse source tarball: ${FUSE_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
615                 cd "${TEMP}"
616                 rm -rf "${FUSE_DIR}"
617                 tar -zxpf "${FUSE_SRCTAR}"
618                 [ ! -d "${FUSE_DIR}" ] &&
619                         gen_die "fuse directory ${FUSE_DIR} invalid"
620                 cd "${FUSE_DIR}"
621                 print_info 1 'fuse: >> Configuring...'
622                 ./configure  --disable-kernel-module --disable-example >> ${LOGFILE} 2>&1 ||
623                         gen_die 'Configuring fuse failed!'
624                 print_info 1 'fuse: >> Compiling...'
625                 MAKE=${UTILS_MAKE} compile_generic "" ""
626
627                 # Since we're linking statically against libfuse, we don't need to cache the .so
628 #               print_info 1 'libfuse: >> Copying to cache...'
629 #               [ -f "${TEMP}/${FUSE_DIR}/lib/.libs/libfuse.so" ] ||
630 #                       gen_die 'libfuse.so does not exist!'
631 #               strip "${TEMP}/${FUSE_DIR}/lib/.libs/libfuse.so" ||
632 #                       gen_die 'Could not strip libfuse.so!'
633 #               cd "${TEMP}/${FUSE_DIR}/lib/.libs"
634 #               tar -cjf "${FUSE_BINCACHE}" libfuse*so* ||
635 #                       gen_die 'Could not create fuse bincache!'
636
637                 cd "${TEMP}"
638 #               rm -rf "${FUSE_DIR}" > /dev/null
639         fi
640 }
641
642 compile_unionfs_fuse() {
643         if [ ! -f "${UNIONFS_FUSE_BINCACHE}" ]
644         then
645
646                 # We'll call compile_fuse() from here, since it's not needed directly by anything else
647                 compile_fuse
648
649                 [ ! -f "${UNIONFS_FUSE_SRCTAR}" ] &&
650                         gen_die "Could not find unionfs-fuse source tarball: ${UNIONFS_FUSE_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
651                 cd "${TEMP}"
652                 rm -rf "${UNIONFS_FUSE_DIR}"
653                 tar -jxpf "${UNIONFS_FUSE_SRCTAR}"
654                 [ ! -d "${UNIONFS_FUSE_DIR}" ] &&
655                         gen_die "unionfs-fuse directory ${UNIONFS_FUSE_DIR} invalid"
656                 cd "${UNIONFS_FUSE_DIR}"
657                 print_info 1 'unionfs-fuse: >> Compiling...'
658                 sed -i "/^\(CFLAGS\|CPPFLAGS\)/s:^\\(.*\\)$:\\1 -static -I${TEMP}/${FUSE_DIR}/include -L${TEMP}/${FUSE_DIR}/lib/.libs:" Makefile src/Makefile
659                 sed -i "/^LIB = /s:^LIB = \(.*\)$:LIB = -static -L${TEMP}/${FUSE_DIR}/lib/.libs \1 -ldl -lrt:" Makefile src/Makefile
660                 MAKE=${UTILS_MAKE} compile_generic "" ""
661                 print_info 1 'unionfs-fuse: >> Copying to cache...'
662                 [ -f "${TEMP}/${UNIONFS_FUSE_DIR}/src/unionfs" ] ||
663                         gen_die 'unionfs binary does not exist!'
664                 strip "${TEMP}/${UNIONFS_FUSE_DIR}/src/unionfs" ||
665                         gen_die 'Could not strip unionfs binary!'
666                 bzip2 "${TEMP}/${UNIONFS_FUSE_DIR}/src/unionfs" ||
667                         gen_die 'bzip2 compression of unionfs binary failed!'
668                 mv "${TEMP}/${UNIONFS_FUSE_DIR}/src/unionfs.bz2" "${UNIONFS_FUSE_BINCACHE}" ||
669                         gen_die 'Could not copy the unionfs binary to the package directory, does the directory exist?'
670
671                 cd "${TEMP}"
672                 rm -rf "${UNIONFS_FUSE_DIR}" > /dev/null
673         fi
674 }
675
676 compile_iscsi() {
677         if [ ! -f "${ISCSI_BINCACHE}" ]
678         then
679                 [ ! -f "${ISCSI_SRCTAR}" ] &&
680                         gen_die "Could not find iSCSI source tarball: ${ISCSI_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
681                 cd "${TEMP}"
682                 rm -rf "${ISCSI_DIR}"
683                 tar -zxpf "${ISCSI_SRCTAR}"
684                 [ ! -d "${ISCSI_DIR}" ] &&
685                         gen_die "ISCSI directory ${ISCSI_DIR} invalid"
686                                 print_info 1 'iSCSI: >> Compiling...'
687                 cd "${TEMP}/${ISCSI_DIR}"
688
689                 # Only build userspace
690                 MAKE=${UTILS_MAKE} compile_generic "user" ""
691         
692                 # if kernel modules exist, copy them to initramfs, otherwise it will be compiled into the kernel
693                 mkdir -p "${TEMP}/initramfs-iscsi-temp/lib/modules/${RELEASE}/kernel/drivers/scsi/"
694                 for modname in iscsi_tcp libiscsi scsi_transport_iscsi
695                 do
696                         if [ -e "${CMD_KERNEL_DIR}/drivers/scsi/${modname}.ko" ]
697                         then
698                                 cp ${CMD_KERNEL_DIR}/drivers/scsi/${modname}.ko "${TEMP}/initramfs-iscsi-temp/lib/modules/${RELEASE}/kernel/drivers/scsi/"
699                         fi
700                 done
701
702                 cd "${TEMP}/initramfs-iscsi-temp/"
703                 print_info 1 'iscsistart: >> Copying to cache...'
704                 [ -f "${TEMP}/${ISCSI_DIR}/usr/iscsistart" ] ||
705                         gen_die 'iscsistart executable does not exist!'
706                 strip "${TEMP}/${ISCSI_DIR}/usr/iscsistart" ||
707                         gen_die 'Could not strip iscsistart binary!'
708                 bzip2 "${TEMP}/${ISCSI_DIR}/usr/iscsistart" ||
709                         gen_die 'bzip2 compression of iscsistart failed!'
710                 mv "${TEMP}/${ISCSI_DIR}/usr/iscsistart.bz2" "${ISCSI_BINCACHE}" ||
711                         gen_die 'Could not copy the iscsistart binary to the package directory, does the directory exist?'
712
713                 cd "${TEMP}"
714                 rm -rf "${ISCSI_DIR}" > /dev/null
715         fi
716 }
717
718 compile_gpg() {
719         if [ -f "${GPG_BINCACHE}" ]
720         then
721                 print_info 1 "gnupg: >> Using cache"
722         else
723                 [ ! -f "${GPG_SRCTAR}" ] &&
724                         gen_die "Could not find gnupg source tarball: ${GPG_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
725                 cd "${TEMP}"
726                 rm -rf "${GPG_DIR}"
727                 tar -jxf "${GPG_SRCTAR}"
728                 [ ! -d "${GPG_DIR}" ] &&
729                         gen_die "gnupg directory ${GPG_DIR} invalid"
730                 cd "${GPG_DIR}"
731                 print_info 1 'gnupg: >> Configuring...'
732                 # --enable-minimal works, but it doesn't reduce the command length much.
733                 # Given its history and the precision this needs, explicit is cleaner.
734                 LDFLAGS='-static' CFLAGS='-Os' ./configure --prefix=/ \
735                         --enable-static-rnd=linux --disable-dev-random --disable-asm \
736                         --disable-selinux-support --disable-gnupg-iconv --disable-card-support \
737                         --disable-agent-support --disable-bzip2 --disable-exec \
738                         --disable-photo-viewers --disable-keyserver-helpers --disable-ldap \
739                         --disable-hkp --disable-finger --disable-generic --disable-mailto \
740                         --disable-keyserver-path --disable-dns-srv --disable-dns-pka \
741                         --disable-dns-cert --disable-nls --disable-threads --disable-regex \
742                         --disable-optimization --with-included-zlib --without-capabilities \
743                         --without-tar --without-ldap --without-libcurl --without-mailprog \
744                         --without-libpth-prefix --without-libiconv-prefix --without-libintl-prefix\
745                         --without-zlib --without-bzip2 --without-libusb --without-readline \
746                                 >> ${LOGFILE} 2>&1 || gen_die 'Configuring gnupg failed!'
747                 print_info 1 'gnupg: >> Compiling...'
748                 compile_generic "" "utils"
749                 print_info 1 'gnupg: >> Copying to cache...'
750                 [ -f "${TEMP}/${GPG_DIR}/g10/gpg" ] ||
751                         gen_die 'gnupg executable does not exist!'
752                 strip "${TEMP}/${GPG_DIR}/g10/gpg" ||
753                         gen_die 'Could not strip gpg binary!'
754                 bzip2 -z -c "${TEMP}/${GPG_DIR}/g10/gpg" > "${GPG_BINCACHE}" ||
755                         gen_die 'Could not copy the gpg binary to the package directory, does the directory exist?'
756
757                 cd "${TEMP}"
758                 rm -rf "${GPG_DIR}" > /dev/null
759         fi
760 }