68dabdb05728bd1b62007aeeee438505a0f06816
[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         case "${argstype}" in
235                 kernel|kernelruntask)
236                         export_kernel_args
237                         MAKE=${KERNEL_MAKE}
238                         ;;
239                 utils)
240                         export_utils_args
241                         MAKE=${UTILS_MAKE}
242                         ;;
243         esac
244
245         case "${argstype}" in
246                 kernel|kernelruntask) ARGS="`compile_kernel_args`" ;;
247                 utils) ARGS="`compile_utils_args`" ;;
248                 *) ARGS="" ;;
249         esac
250         shift 2
251
252         # the eval usage is needed in the next set of code
253         # as ARGS can contain spaces and quotes, eg:
254         # ARGS='CC="ccache gcc"'
255         if [ "${argstype}" == 'kernelruntask' ]
256         then
257                 # Silent operation, forced -j1
258                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} -j1 ${ARGS} ${target} $*" 1 0 1
259                 eval ${MAKE} -s ${MAKEOPTS} -j1 "${ARGS}" ${target} $*
260                 RET=$?
261         elif [ "${LOGLEVEL}" -gt "1" ]
262         then
263                 # Output to stdout and logfile
264                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $*" 1 0 1
265                 eval ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $* 2>&1 | tee -a ${LOGFILE}
266                 RET=${PIPESTATUS[0]}
267         else
268                 # Output to logfile only
269                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${ARGS} ${1} $*" 1 0 1
270                 eval ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $* >> ${LOGFILE} 2>&1
271                 RET=$?
272         fi
273         [ ${RET} -ne 0 ] &&
274                 gen_die "Failed to compile the \"${target}\" target..."
275
276         unset MAKE
277         unset ARGS
278
279         case "${argstype}" in
280                 kernel) unset_kernel_args ;;
281                 utils) unset_utils_args ;;
282         esac
283 }
284
285 compile_modules() {
286         print_info 1 "        >> Compiling ${KV} modules..."
287         cd ${KERNEL_DIR}
288         compile_generic modules kernel
289         export UNAME_MACHINE="${ARCH}"
290         [ "${INSTALL_MOD_PATH}" != '' ] && export INSTALL_MOD_PATH
291         MAKEOPTS="${MAKEOPTS} -j1" compile_generic "modules_install" kernel
292         unset UNAME_MACHINE
293 }
294
295 compile_kernel() {
296         [ "${KERNEL_MAKE}" = '' ] &&
297                 gen_die "KERNEL_MAKE undefined - I don't know how to compile a kernel for this arch!"
298         cd ${KERNEL_DIR}
299         local kernel_make_directive="${KERNEL_MAKE_DIRECTIVE}"
300         if [ "${KERNEL_MAKE_DIRECTIVE_OVERRIDE}" != "${DEFAULT_KERNEL_MAKE_DIRECTIVE_OVERRIDE}" ]; then
301                 kernel_make_directive="${KERNEL_MAKE_DIRECTIVE_OVERRIDE}"
302         fi
303         print_info 1 "        >> Compiling ${KV} ${kernel_make_directive/_install/ [ install ]/}..."
304         compile_generic "${kernel_make_directive}" kernel
305         if [ "${KERNEL_MAKE_DIRECTIVE_2}" != '' ]
306         then
307                 print_info 1 "        >> Starting supplimental compile of ${KV}: ${KERNEL_MAKE_DIRECTIVE_2}..."
308                 compile_generic "${KERNEL_MAKE_DIRECTIVE_2}" kernel
309         fi
310
311         local firmware_in_kernel_line=`fgrep CONFIG_FIRMWARE_IN_KERNEL "${KERNEL_DIR}"/.config`
312         if [ -n "${firmware_in_kernel_line}" -a "${firmware_in_kernel_line}" != CONFIG_FIRMWARE_IN_KERNEL=y ]
313         then
314                 print_info 1 "        >> Installing firmware ('make firmware_install') due to CONFIG_FIRMWARE_IN_KERNEL != y..."
315                 MAKEOPTS="${MAKEOPTS} -j1" compile_generic "firmware_install" kernel
316         else
317                 print_info 1 "        >> Not installing firmware as it's included in the kernel already (CONFIG_FIRMWARE_IN_KERNEL=y)..."
318         fi
319
320         local tmp_kernel_binary=$(find_kernel_binary ${KERNEL_BINARY_OVERRIDE:-${KERNEL_BINARY}})
321         local tmp_kernel_binary2=$(find_kernel_binary ${KERNEL_BINARY_2})
322         if [ -z "${tmp_kernel_binary}" ]
323         then
324                 gen_die "Cannot locate kernel binary"
325         fi
326
327         if isTrue "${CMD_INSTALL}"
328         then
329                 copy_image_with_preserve "kernel" \
330                         "${tmp_kernel_binary}" \
331                         "kernel-${KNAME}-${ARCH}-${KV}"
332
333                 copy_image_with_preserve "System.map" \
334                         "System.map" \
335                         "System.map-${KNAME}-${ARCH}-${KV}"
336
337                 if isTrue "${GENZIMAGE}"
338                 then
339                         copy_image_with_preserve "kernelz" \
340                                 "${tmp_kernel_binary2}" \
341                                 "kernelz-${KV}"
342                 fi
343         else
344                 cp "${tmp_kernel_binary}" "${TMPDIR}/kernel-${KNAME}-${ARCH}-${KV}" ||
345                         gen_die "Could not copy the kernel binary to ${TMPDIR}!"
346                 cp "System.map" "${TMPDIR}/System.map-${KNAME}-${ARCH}-${KV}" ||
347                         gen_die "Could not copy System.map to ${TMPDIR}!"
348                 if isTrue "${GENZIMAGE}"
349                 then
350                         cp "${tmp_kernel_binary2}" "${TMPDIR}/kernelz-${KV}" ||
351                                 gen_die "Could not copy the kernelz binary to ${TMPDIR}!"
352                 fi
353         fi
354 }
355
356 compile_busybox() {
357         [ -f "${BUSYBOX_SRCTAR}" ] ||
358                 gen_die "Could not find busybox source tarball: ${BUSYBOX_SRCTAR}!"
359
360         if [ -n "${BUSYBOX_CONFIG}" ]
361         then
362                 [ -f "${BUSYBOX_CONFIG}" ] ||
363                         gen_die "Could not find busybox config file: ${BUSYBOX_CONFIG}"
364         elif isTrue "${NETBOOT}" && [ -f "$(arch_replace "${GK_SHARE}/arch/%%ARCH%%/netboot-busy-config")" ]
365         then
366                 BUSYBOX_CONFIG="$(arch_replace "${GK_SHARE}/arch/%%ARCH%%/netboot-busy-config")"
367         elif isTrue "${NETBOOT}" && [ -f "${GK_SHARE}/netboot/busy-config" ]
368         then
369                 BUSYBOX_CONFIG="${GK_SHARE}/netboot/busy-config"
370         elif [ -f "$(arch_replace "${GK_SHARE}/arch/%%ARCH%%/busy-config")" ]
371         then
372                 BUSYBOX_CONFIG="$(arch_replace "${GK_SHARE}/arch/%%ARCH%%/busy-config")"
373         elif [ -f "${GK_SHARE}/defaults/busy-config" ]
374         then
375                 BUSYBOX_CONFIG="${GK_SHARE}/defaults/busy-config"
376         else
377                 gen_die "Could not find a busybox config file"
378         fi
379
380         # Delete cache if stored config's MD5 does not match one to be used
381         if [ -f "${BUSYBOX_BINCACHE}" ]
382         then
383                 oldconfig_md5=$(tar -xjf "${BUSYBOX_BINCACHE}" -O .config.gk_orig 2>/dev/null | md5sum)
384                 newconfig_md5=$(md5sum < "${BUSYBOX_CONFIG}")
385                 if [ "${oldconfig_md5}" != "${newconfig_md5}" ]
386                 then
387                         print_info 1 "busybox: >> Removing stale cache..."
388                         rm -rf "${BUSYBOX_BINCACHE}"
389                 else
390                         print_info 1 "busybox: >> Using cache"
391                 fi
392         fi
393
394         if [ ! -f "${BUSYBOX_BINCACHE}" ]
395         then
396                 cd "${TEMP}"
397                 rm -rf "${BUSYBOX_DIR}" > /dev/null
398                 /bin/tar -jxpf ${BUSYBOX_SRCTAR} ||
399                         gen_die 'Could not extract busybox source tarball!'
400                 [ -d "${BUSYBOX_DIR}" ] ||
401                         gen_die 'Busybox directory ${BUSYBOX_DIR} is invalid!'
402                 cp "${BUSYBOX_CONFIG}" "${BUSYBOX_DIR}/.config"
403                 cp "${BUSYBOX_CONFIG}" "${BUSYBOX_DIR}/.config.gk_orig"
404                 cd "${BUSYBOX_DIR}"
405                 apply_patches busybox ${BUSYBOX_VER}
406                 print_info 1 'busybox: >> Configuring...'
407                 yes '' 2>/dev/null | compile_generic oldconfig utils
408
409                 print_info 1 'busybox: >> Compiling...'
410                 compile_generic all utils
411                 print_info 1 'busybox: >> Copying to cache...'
412                 [ -f "${TEMP}/${BUSYBOX_DIR}/busybox" ] ||
413                         gen_die 'Busybox executable does not exist!'
414                 strip "${TEMP}/${BUSYBOX_DIR}/busybox" ||
415                         gen_die 'Could not strip busybox binary!'
416                 tar -cj -C "${TEMP}/${BUSYBOX_DIR}" -f "${BUSYBOX_BINCACHE}" busybox .config .config.gk_orig ||
417                         gen_die 'Could not create the busybox bincache!'
418
419                 cd "${TEMP}"
420                 rm -rf "${BUSYBOX_DIR}" > /dev/null
421         fi
422 }
423
424 compile_lvm() {
425         if [ -f "${LVM_BINCACHE}" ]
426         then
427                 print_info 1 "lvm: >> Using cache"
428         else
429                 [ -f "${LVM_SRCTAR}" ] ||
430                         gen_die "Could not find LVM source tarball: ${LVM_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
431                 cd "${TEMP}"
432                 rm -rf ${LVM_DIR} > /dev/null
433                 /bin/tar -zxpf ${LVM_SRCTAR} ||
434                         gen_die 'Could not extract LVM source tarball!'
435                 [ -d "${LVM_DIR}" ] ||
436                         gen_die 'LVM directory ${LVM_DIR} is invalid!'
437                 cd "${LVM_DIR}"
438                 apply_patches lvm ${LVM_VER}
439                 print_info 1 'lvm: >> Configuring...'
440                         CFLAGS="-fPIC" \
441                         ./configure --enable-static_link --prefix=${TEMP}/lvm \
442                                 --with-lvm1=internal --with-clvmd=none --with-cluster=none \
443                                 --disable-readline --disable-selinux --with-mirrors=internal \
444                                 --with-snapshots=internal --with-pool=internal >> ${LOGFILE} 2>&1 || \
445                                 gen_die 'Configure of lvm failed!'
446                 print_info 1 'lvm: >> Compiling...'
447                         compile_generic '' utils
448                         compile_generic 'install' utils
449
450                 cd "${TEMP}/lvm"
451                 print_info 1 '      >> Copying to bincache...'
452                 strip "sbin/lvm.static" ||
453                         gen_die 'Could not strip lvm.static!'
454                 # See bug 382555
455                 strip "sbin/dmsetup.static" ||
456                         gen_die 'Could not strip dmsetup.static'
457                 /bin/tar -cjf "${LVM_BINCACHE}" sbin/lvm.static sbin/dmsetup.static ||
458                         gen_die 'Could not create binary cache'
459
460                 cd "${TEMP}"
461                 rm -rf "${TEMP}/device-mapper" > /dev/null
462                 rm -rf "${LVM_DIR}" lvm
463         fi
464 }
465
466 compile_mdadm() {
467         if [ -f "${MDADM_BINCACHE}" ]
468         then
469                 print_info 1 '          MDADM: Using cache'
470         else
471                 [ -f "${MDADM_SRCTAR}" ] ||
472                         gen_die "Could not find MDADM source tarball: ${MDADM_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
473                 cd "${TEMP}"
474                 rm -rf "${MDADM_DIR}" > /dev/null
475                 /bin/tar -jxpf "${MDADM_SRCTAR}" ||
476                         gen_die 'Could not extract MDADM source tarball!'
477                 [ -d "${MDADM_DIR}" ] ||
478                         gen_die 'MDADM directory ${MDADM_DIR} is invalid!'
479
480                 cd "${MDADM_DIR}"
481                 apply_patches mdadm ${MDADM_VER}
482                 sed -i "/^CFLAGS = /s:^CFLAGS = \(.*\)$:CFLAGS = -Os:" Makefile
483                 sed -i "/^CXFLAGS = /s:^CXFLAGS = \(.*\)$:CXFLAGS = -Os:" Makefile
484                 sed -i "/^CWFLAGS = /s:^CWFLAGS = \(.*\)$:CWFLAGS = -Wall:" Makefile
485                 sed -i "s/^# LDFLAGS = -static/LDFLAGS = -static/" Makefile
486
487                 print_info 1 'mdadm: >> Compiling...'
488                         compile_generic 'mdadm mdmon' utils
489
490                 mkdir -p "${TEMP}/mdadm/sbin"
491                 install -m 0755 -s mdadm "${TEMP}/mdadm/sbin/mdadm"
492                 install -m 0755 -s mdmon "${TEMP}/mdadm/sbin/mdmon"
493                 print_info 1 '      >> Copying to bincache...'
494                 cd "${TEMP}/mdadm"
495                 strip "sbin/mdadm" "sbin/mdmon" ||
496                         gen_die 'Could not strip mdadm binaries!'
497                 /bin/tar -cjf "${MDADM_BINCACHE}" sbin/mdadm sbin/mdmon ||
498                         gen_die 'Could not create binary cache'
499
500                 cd "${TEMP}"
501                 rm -rf "${MDADM_DIR}" mdadm
502         fi
503 }
504
505 compile_dmraid() {
506         compile_device_mapper
507         if [ ! -f "${DMRAID_BINCACHE}" ]
508         then
509                 [ -f "${DMRAID_SRCTAR}" ] ||
510                         gen_die "Could not find DMRAID source tarball: ${DMRAID_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
511                 cd "${TEMP}"
512                 rm -rf ${DMRAID_DIR} > /dev/null
513                 /bin/tar -jxpf ${DMRAID_SRCTAR} ||
514                         gen_die 'Could not extract DMRAID source tarball!'
515                 [ -d "${DMRAID_DIR}" ] ||
516                         gen_die 'DMRAID directory ${DMRAID_DIR} is invalid!'
517                 rm -rf "${TEMP}/device-mapper" > /dev/null
518                 /bin/tar -jxpf "${DEVICE_MAPPER_BINCACHE}" -C "${TEMP}" ||
519                         gen_die "Could not extract device-mapper binary cache!";
520
521                 cd "${DMRAID_DIR}"
522                 apply_patches dmraid ${DMRAID_VER}
523                 print_info 1 'dmraid: >> Configuring...'
524
525                 LDFLAGS="-L${TEMP}/device-mapper/lib" \
526                 CFLAGS="-I${TEMP}/device-mapper/include" \
527                 CPPFLAGS="-I${TEMP}/device-mapper/include" \
528                 ./configure --enable-static_link --prefix=${TEMP}/dmraid >> ${LOGFILE} 2>&1 ||
529                         gen_die 'Configure of dmraid failed!'
530
531                 # We dont necessarily have selinux installed yet... look into
532                 # selinux global support in the future.
533                 sed -i tools/Makefile -e "s|DMRAIDLIBS += -lselinux||g"
534                 ###echo "DMRAIDLIBS += -lselinux -lsepol" >> tools/Makefile
535                 mkdir -p "${TEMP}/dmraid"
536                 print_info 1 'dmraid: >> Compiling...'
537                 # Force dmraid to be built with -j1 for bug #188273
538                 MAKEOPTS="${MAKEOPTS} -j1" compile_generic '' utils
539                 #compile_generic 'install' utils
540                 mkdir ${TEMP}/dmraid/sbin
541                 install -m 0755 -s tools/dmraid "${TEMP}/dmraid/sbin/dmraid"
542                 print_info 1 '      >> Copying to bincache...'
543                 cd "${TEMP}/dmraid"
544                 /bin/tar -cjf "${DMRAID_BINCACHE}" sbin/dmraid ||
545                         gen_die 'Could not create binary cache'
546
547                 cd "${TEMP}"
548                 rm -rf "${TEMP}/device-mapper" > /dev/null
549                 rm -rf "${DMRAID_DIR}" dmraid
550         fi
551 }
552
553 compile_device_mapper() {
554         if [ ! -f "${DEVICE_MAPPER_BINCACHE}" ]
555         then
556                 [ ! -f "${DEVICE_MAPPER_SRCTAR}" ] &&
557                         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!"
558                 cd "${TEMP}"
559                 rm -rf "${DEVICE_MAPPER_DIR}"
560                 /bin/tar -zxpf "${DEVICE_MAPPER_SRCTAR}"
561                 [ ! -d "${DEVICE_MAPPER_DIR}" ] &&
562                         gen_die "device-mapper directory ${DEVICE_MAPPER_DIR} invalid"
563                 cd "${DEVICE_MAPPER_DIR}"
564                 apply_patches device-mapper ${DEVICE_MAPPER_VER}
565                 CFLAGS="-fPIC" \
566                 ./configure --prefix=${TEMP}/device-mapper --enable-static_link \
567                         --disable-selinux >> ${LOGFILE} 2>&1 ||
568                         gen_die 'Configuring device-mapper failed!'
569                 print_info 1 'device-mapper: >> Compiling...'
570                 compile_generic '' utils
571                 compile_generic 'install' utils
572                 print_info 1 '        >> Copying to cache...'
573                 cd "${TEMP}"
574                 rm -rf "${TEMP}/device-mapper/man" ||
575                         gen_die 'Could not remove manual pages!'
576                 strip "${TEMP}/device-mapper/sbin/dmsetup" ||
577                         gen_die 'Could not strip dmsetup binary!'
578                 /bin/tar -jcpf "${DEVICE_MAPPER_BINCACHE}" device-mapper ||
579                         gen_die 'Could not tar up the device-mapper binary!'
580                 [ -f "${DEVICE_MAPPER_BINCACHE}" ] ||
581                         gen_die 'device-mapper cache not created!'
582                 cd "${TEMP}"
583                 rm -rf "${DEVICE_MAPPER_DIR}" > /dev/null
584                 rm -rf "${TEMP}/device-mapper" > /dev/null
585         fi
586 }
587
588 compile_e2fsprogs() {
589         if [ -f "${BLKID_BINCACHE}" ]
590         then
591                 print_info 1 "blkid: >> Using cache"
592         else
593                 [ ! -f "${E2FSPROGS_SRCTAR}" ] &&
594                         gen_die "Could not find e2fsprogs source tarball: ${E2FSPROGS_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
595                 cd "${TEMP}"
596                 rm -rf "${E2FSPROGS_DIR}"
597                 tar -zxpf "${E2FSPROGS_SRCTAR}"
598                 [ ! -d "${E2FSPROGS_DIR}" ] &&
599                         gen_die "e2fsprogs directory ${E2FSPROGS_DIR} invalid"
600                 cd "${E2FSPROGS_DIR}"
601                 apply_patches e2fsprogs ${E2FSPROGS_VER}
602                 print_info 1 'e2fsprogs: >> Configuring...'
603                 LDFLAGS=-static ./configure >> ${LOGFILE} 2>&1 ||
604                         gen_die 'Configuring e2fsprogs failed!'
605                 print_info 1 'e2fsprogs: >> Compiling...'
606                 MAKE=${UTILS_MAKE} MAKEOPTS="${MAKEOPTS} -j1" compile_generic "" ""
607                 print_info 1 'blkid: >> Copying to cache...'
608                 [ -f "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ] ||
609                         gen_die 'Blkid executable does not exist!'
610                 strip "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ||
611                         gen_die 'Could not strip blkid binary!'
612                 bzip2 "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ||
613                         gen_die 'bzip2 compression of blkid failed!'
614                 mv "${TEMP}/${E2FSPROGS_DIR}/misc/blkid.bz2" "${BLKID_BINCACHE}" ||
615                         gen_die 'Could not copy the blkid binary to the package directory, does the directory exist?'
616
617                 cd "${TEMP}"
618                 rm -rf "${E2FSPROGS_DIR}" > /dev/null
619         fi
620 }
621
622 compile_fuse() {
623         if [ ! -f "${FUSE_BINCACHE}" ]
624         then
625                 [ ! -f "${FUSE_SRCTAR}" ] &&
626                         gen_die "Could not find fuse source tarball: ${FUSE_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
627                 cd "${TEMP}"
628                 rm -rf "${FUSE_DIR}"
629                 tar -zxpf "${FUSE_SRCTAR}"
630                 [ ! -d "${FUSE_DIR}" ] &&
631                         gen_die "fuse directory ${FUSE_DIR} invalid"
632                 cd "${FUSE_DIR}"
633                 apply_patches fuse ${FUSE_VER}
634                 print_info 1 'fuse: >> Configuring...'
635                 ./configure  --disable-kernel-module --disable-example >> ${LOGFILE} 2>&1 ||
636                         gen_die 'Configuring fuse failed!'
637                 print_info 1 'fuse: >> Compiling...'
638                 MAKE=${UTILS_MAKE} compile_generic "" ""
639
640                 # Since we're linking statically against libfuse, we don't need to cache the .so
641 #               print_info 1 'libfuse: >> Copying to cache...'
642 #               [ -f "${TEMP}/${FUSE_DIR}/lib/.libs/libfuse.so" ] ||
643 #                       gen_die 'libfuse.so does not exist!'
644 #               strip "${TEMP}/${FUSE_DIR}/lib/.libs/libfuse.so" ||
645 #                       gen_die 'Could not strip libfuse.so!'
646 #               cd "${TEMP}/${FUSE_DIR}/lib/.libs"
647 #               tar -cjf "${FUSE_BINCACHE}" libfuse*so* ||
648 #                       gen_die 'Could not create fuse bincache!'
649
650                 cd "${TEMP}"
651 #               rm -rf "${FUSE_DIR}" > /dev/null
652         fi
653 }
654
655 compile_unionfs_fuse() {
656         if [ ! -f "${UNIONFS_FUSE_BINCACHE}" ]
657         then
658
659                 # We'll call compile_fuse() from here, since it's not needed directly by anything else
660                 compile_fuse
661
662                 [ ! -f "${UNIONFS_FUSE_SRCTAR}" ] &&
663                         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!"
664                 cd "${TEMP}"
665                 rm -rf "${UNIONFS_FUSE_DIR}"
666                 tar -jxpf "${UNIONFS_FUSE_SRCTAR}"
667                 [ ! -d "${UNIONFS_FUSE_DIR}" ] &&
668                         gen_die "unionfs-fuse directory ${UNIONFS_FUSE_DIR} invalid"
669                 cd "${UNIONFS_FUSE_DIR}"
670                 apply_patches unionfs-fuse ${UNIONFS_FUSE_VER}
671                 print_info 1 'unionfs-fuse: >> Compiling...'
672                 sed -i "/^\(CFLAGS\|CPPFLAGS\)/s:^\\(.*\\)$:\\1 -static -I${TEMP}/${FUSE_DIR}/include -L${TEMP}/${FUSE_DIR}/lib/.libs:" Makefile src/Makefile
673                 sed -i "/^LIB = /s:^LIB = \(.*\)$:LIB = -static -L${TEMP}/${FUSE_DIR}/lib/.libs \1 -ldl -lrt:" Makefile src/Makefile
674                 MAKE=${UTILS_MAKE} compile_generic "" ""
675                 print_info 1 'unionfs-fuse: >> Copying to cache...'
676                 [ -f "${TEMP}/${UNIONFS_FUSE_DIR}/src/unionfs" ] ||
677                         gen_die 'unionfs binary does not exist!'
678                 strip "${TEMP}/${UNIONFS_FUSE_DIR}/src/unionfs" ||
679                         gen_die 'Could not strip unionfs binary!'
680                 bzip2 "${TEMP}/${UNIONFS_FUSE_DIR}/src/unionfs" ||
681                         gen_die 'bzip2 compression of unionfs binary failed!'
682                 mv "${TEMP}/${UNIONFS_FUSE_DIR}/src/unionfs.bz2" "${UNIONFS_FUSE_BINCACHE}" ||
683                         gen_die 'Could not copy the unionfs binary to the package directory, does the directory exist?'
684
685                 cd "${TEMP}"
686                 rm -rf "${UNIONFS_FUSE_DIR}" > /dev/null
687         fi
688 }
689
690 compile_iscsi() {
691         if [ ! -f "${ISCSI_BINCACHE}" ]
692         then
693                 [ ! -f "${ISCSI_SRCTAR}" ] &&
694                         gen_die "Could not find iSCSI source tarball: ${ISCSI_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
695                 cd "${TEMP}"
696                 rm -rf "${ISCSI_DIR}"
697                 tar -zxpf "${ISCSI_SRCTAR}"
698                 [ ! -d "${ISCSI_DIR}" ] &&
699                         gen_die "ISCSI directory ${ISCSI_DIR} invalid"
700                                 print_info 1 'iSCSI: >> Compiling...'
701                 cd "${TEMP}/${ISCSI_DIR}"
702                 apply_patches iscsi ${ISCSI_VER}
703
704                 # Only build userspace
705                 print_info 1 'iSCSI: >> Configuring userspace...'
706                 cd utils/open-isns || gen_die 'Could not enter open-isns dir'
707                 # we currently have a patch that changes configure.ac
708                 # once given patch is dropped, drop autoconf too
709                 autoconf || gen_die 'Could not tweak open-iscsi configuration'
710                 ./configure --without-slp >> ${LOGFILE} 2>&1 || gen_die 'Could not configure userspace'
711                 cd ../.. || gen_die 'wtf?'
712                 MAKE=${UTILS_MAKE} compile_generic "user" ""
713
714                 # if kernel modules exist, copy them to initramfs, otherwise it will be compiled into the kernel
715                 mkdir -p "${TEMP}/initramfs-iscsi-temp/lib/modules/${RELEASE}/kernel/drivers/scsi/"
716                 for modname in iscsi_tcp libiscsi scsi_transport_iscsi
717                 do
718                         if [ -e "${CMD_KERNEL_DIR}/drivers/scsi/${modname}.ko" ]
719                         then
720                                 cp ${CMD_KERNEL_DIR}/drivers/scsi/${modname}.ko "${TEMP}/initramfs-iscsi-temp/lib/modules/${RELEASE}/kernel/drivers/scsi/"
721                         fi
722                 done
723
724                 cd "${TEMP}/initramfs-iscsi-temp/"
725                 print_info 1 'iscsistart: >> Copying to cache...'
726                 [ -f "${TEMP}/${ISCSI_DIR}/usr/iscsistart" ] ||
727                         gen_die 'iscsistart executable does not exist!'
728                 strip "${TEMP}/${ISCSI_DIR}/usr/iscsistart" ||
729                         gen_die 'Could not strip iscsistart binary!'
730                 bzip2 "${TEMP}/${ISCSI_DIR}/usr/iscsistart" ||
731                         gen_die 'bzip2 compression of iscsistart failed!'
732                 mv "${TEMP}/${ISCSI_DIR}/usr/iscsistart.bz2" "${ISCSI_BINCACHE}" ||
733                         gen_die 'Could not copy the iscsistart binary to the package directory, does the directory exist?'
734
735                 cd "${TEMP}"
736                 rm -rf "${ISCSI_DIR}" > /dev/null
737         fi
738 }
739
740 compile_gpg() {
741         if [ -f "${GPG_BINCACHE}" ]
742         then
743                 print_info 1 "gnupg: >> Using cache"
744         else
745                 [ ! -f "${GPG_SRCTAR}" ] &&
746                         gen_die "Could not find gnupg source tarball: ${GPG_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
747                 cd "${TEMP}"
748                 rm -rf "${GPG_DIR}"
749                 tar -jxf "${GPG_SRCTAR}"
750                 [ ! -d "${GPG_DIR}" ] &&
751                         gen_die "gnupg directory ${GPG_DIR} invalid"
752                 cd "${GPG_DIR}"
753                 apply_patches gnupg ${GPG_VER}
754                 print_info 1 'gnupg: >> Configuring...'
755                 # --enable-minimal works, but it doesn't reduce the command length much.
756                 # Given its history and the precision this needs, explicit is cleaner.
757                 LDFLAGS='-static' CFLAGS='-Os' ./configure --prefix=/ \
758                         --enable-static-rnd=linux --disable-dev-random --disable-asm \
759                         --disable-selinux-support --disable-gnupg-iconv --disable-card-support \
760                         --disable-agent-support --disable-bzip2 --disable-exec \
761                         --disable-photo-viewers --disable-keyserver-helpers --disable-ldap \
762                         --disable-hkp --disable-finger --disable-generic --disable-mailto \
763                         --disable-keyserver-path --disable-dns-srv --disable-dns-pka \
764                         --disable-dns-cert --disable-nls --disable-threads --disable-regex \
765                         --disable-optimization --with-included-zlib --without-capabilities \
766                         --without-tar --without-ldap --without-libcurl --without-mailprog \
767                         --without-libpth-prefix --without-libiconv-prefix --without-libintl-prefix\
768                         --without-zlib --without-bzip2 --without-libusb --without-readline \
769                                 >> ${LOGFILE} 2>&1 || gen_die 'Configuring gnupg failed!'
770                 print_info 1 'gnupg: >> Compiling...'
771                 compile_generic "" "utils"
772                 print_info 1 'gnupg: >> Copying to cache...'
773                 [ -f "${TEMP}/${GPG_DIR}/g10/gpg" ] ||
774                         gen_die 'gnupg executable does not exist!'
775                 strip "${TEMP}/${GPG_DIR}/g10/gpg" ||
776                         gen_die 'Could not strip gpg binary!'
777                 bzip2 -z -c "${TEMP}/${GPG_DIR}/g10/gpg" > "${GPG_BINCACHE}" ||
778                         gen_die 'Could not copy the gpg binary to the package directory, does the directory exist?'
779
780                 cd "${TEMP}"
781                 rm -rf "${GPG_DIR}" > /dev/null
782         fi
783 }