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