Since device-mapper tries to build with selinux by default, we've removed it, then...
[genkernel.git] / gen_compile.sh
1 #!/bin/bash
2
3 compile_kernel_args()
4 {
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         fi
25         echo -n "${ARGS}"
26 }
27
28 compile_utils_args()
29 {
30         local ARGS
31
32         ARGS=''
33         if [ "${UTILS_ARCH}" != '' ]
34         then
35                 ARGS="ARCH=\"${UTILS_ARCH}\""
36         fi
37         if [ "${UTILS_CC}" != '' ]
38         then
39                 ARGS="CC=\"${UTILS_CC}\""
40         fi
41         if [ "${UTILS_LD}" != '' ]
42         then
43                 ARGS="${ARGS} LD=\"${UTILS_LD}\""
44         fi
45         if [ "${UTILS_AS}" != '' ]
46         then
47                 ARGS="${ARGS} AS=\"${UTILS_AS}\""
48         fi
49
50         echo -n "${ARGS}"
51 }
52
53 export_utils_args()
54 {
55         save_args
56         if [ "${UTILS_ARCH}" != '' ]
57         then
58                 export ARCH="${UTILS_ARCH}"
59         fi
60         if [ "${UTILS_CC}" != '' ]
61         then
62                 export CC="${UTILS_CC}"
63         fi
64         if [ "${UTILS_LD}" != '' ]
65         then
66                 export LD="${UTILS_LD}"
67         fi
68         if [ "${UTILS_AS}" != '' ]
69         then
70                 export AS="${UTILS_AS}"
71         fi
72 }
73
74 unset_utils_args()
75 {
76         if [ "${UTILS_ARCH}" != '' ]
77         then
78                 unset ARCH
79         fi
80         if [ "${UTILS_CC}" != '' ]
81         then
82                 unset CC
83         fi
84         if [ "${UTILS_LD}" != '' ]
85         then
86                 unset LD
87         fi
88         if [ "${UTILS_AS}" != '' ]
89         then
90                 unset AS
91         fi
92         reset_args
93 }
94
95 export_kernel_args()
96 {
97         if [ "${KERNEL_CC}" != '' ]
98         then
99                 export CC="${KERNEL_CC}"
100         fi
101         if [ "${KERNEL_LD}" != '' ]
102         then
103                 export LD="${KERNEL_LD}"
104         fi
105         if [ "${KERNEL_AS}" != '' ]
106         then
107                 export AS="${KERNEL_AS}"
108         fi
109         if [ "${KERNEL_CROSS_COMPILE}" != '' ]
110         then
111                 export CROSS_COMPILE="${KERNEL_CROSS_COMPILE}"
112         fi
113 }
114
115 unset_kernel_args()
116 {
117         if [ "${KERNEL_CC}" != '' ]
118         then
119                 unset CC
120         fi
121         if [ "${KERNEL_LD}" != '' ]
122         then
123                 unset LD
124         fi
125         if [ "${KERNEL_AS}" != '' ]
126         then
127                 unset AS
128         fi
129         if [ "${KERNEL_CROSS_COMPILE}" != '' ]
130         then
131                 unset CROSS_COMPILE
132         fi
133 }
134 save_args()
135 {
136         if [ "${ARCH}" != '' ]
137         then
138                 export ORIG_ARCH="${ARCH}"
139         fi
140         if [ "${CC}" != '' ]
141         then
142                 export ORIG_CC="${CC}"
143         fi
144         if [ "${LD}" != '' ]
145         then
146                 export ORIG_LD="${LD}"
147         fi
148         if [ "${AS}" != '' ]
149         then
150                 export ORIG_AS="${AS}"
151         fi
152         if [ "${CROSS_COMPILE}" != '' ]
153         then
154                 export ORIG_CROSS_COMPILE="${CROSS_COMPILE}"
155         fi
156 }
157 reset_args()
158 {
159         if [ "${ORIG_ARCH}" != '' ]
160         then
161                 export ARCH="${ORIG_ARCH}"
162                 unset ORIG_ARCH
163         fi
164         if [ "${ORIG_CC}" != '' ]
165         then
166                 export CC="${ORIG_CC}"
167                 unset ORIG_CC
168         fi
169         if [ "${ORIG_LD}" != '' ]
170         then
171                 export LD="${ORIG_LD}"
172                 unset ORIG_LD
173         fi
174         if [ "${ORIG_AS}" != '' ]
175         then
176                 export AS="${ORIG_AS}"
177                 unset ORIG_AS
178         fi
179         if [ "${ORIG_CROSS_COMPILE}" != '' ]
180         then
181                 export CROSS_COMPILE="${ORIG_CROSS_COMPILE}"
182                 unset ORIG_CROSS_COMPILE
183         fi
184 }
185
186 compile_generic() {
187         local RET
188         [ "$#" -lt '2' ] &&
189                 gen_die 'compile_generic(): improper usage!'
190         local target=${1}
191         local argstype=${2}
192
193         if [ "${argstype}" = 'kernel' ] || [ "${argstype}" = 'runtask' ]
194         then
195                 export_kernel_args
196                 MAKE=${KERNEL_MAKE}
197         elif [ "${2}" = 'utils' ]
198         then
199                 export_utils_args
200                 MAKE=${UTILS_MAKE}
201         fi
202         case "${argstype}" in
203                 kernel) ARGS="`compile_kernel_args`" ;;
204                 utils) ARGS="`compile_utils_args`" ;;
205                 *) ARGS="" ;; # includes runtask
206         esac
207         shift 2
208
209
210         # the eval usage is needed in the next set of code
211         # as ARGS can contain spaces and quotes, eg:
212         # ARGS='CC="ccache gcc"'
213         if [ "${argstype}" == 'runtask' ]
214         then
215                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS/-j?/j1} ${ARGS} ${target} $*" 1 0 1
216                 eval ${MAKE} -s ${MAKEOPTS/-j?/-j1} "${ARGS}" ${target} $*
217                 RET=$?
218         elif [ "${DEBUGLEVEL}" -gt "1" ]
219         then
220                 # Output to stdout and debugfile
221                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $*" 1 0 1
222                 eval ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $* 2>&1 | tee -a ${DEBUGFILE}
223                 RET=${PIPESTATUS[0]}
224         else
225                 # Output to debugfile only
226                 print_info 2 "COMMAND: ${MAKE} ${MAKEOPTS} ${ARGS} ${1} $*" 1 0 1
227                 eval ${MAKE} ${MAKEOPTS} ${ARGS} ${target} $* >> ${DEBUGFILE} 2>&1
228                 RET=$?
229         fi
230         [ "${RET}" -ne '0' ] &&
231                 gen_die "Failed to compile the \"${target}\" target..."
232
233         unset MAKE
234         unset ARGS
235         if [ "${argstype}" = 'kernel' ]
236         then
237                 unset_kernel_args
238         elif [ "${argstype}" = 'utils' ]
239         then
240                 unset_utils_args
241         fi
242 }
243
244 extract_dietlibc_bincache() {
245         cd "${TEMP}"
246         rm -rf "${TEMP}/diet" > /dev/null
247         /bin/tar -jxpf "${DIETLIBC_BINCACHE}" ||
248                 gen_die 'Could not extract dietlibc bincache!'
249         [ ! -d "${TEMP}/diet" ] &&
250                 gen_die "${TEMP}/diet directory not found!"
251         cd - > /dev/null
252 }
253
254 clean_dietlibc_bincache() {
255         cd "${TEMP}"
256         rm -rf "${TEMP}/diet" > /dev/null
257         cd - > /dev/null
258 }
259
260 compile_dep() {
261         # Only run ``make dep'' for 2.4 kernels
262         if [ "${VER}" -eq '2' -a "${KERN_24}" -eq '1' ]
263         then
264                 print_info 1 "kernel: >> Making dependencies..."
265                 cd ${KERNEL_DIR}
266                 compile_generic dep kernel
267         fi
268 }
269
270 compile_modules() {
271         print_info 1 "        >> Compiling ${KV} modules..."
272         cd ${KERNEL_DIR}
273         compile_generic modules kernel
274         export UNAME_MACHINE="${ARCH}"
275         # On 2.4 kernels, if MAKEOPTS > -j1 it can cause failures
276         if [ "${VER}" -eq '2' -a "${KERN_24}" -eq '1' ]
277         then
278                 MAKEOPTS_SAVE="${MAKEOPTS}"
279                 MAKEOPTS="${MAKEOPTS_SAVE/-j?/-j1}"
280         fi
281         [ "${INSTALL_MOD_PATH}" != '' ] && export INSTALL_MOD_PATH
282         compile_generic "modules_install" kernel
283         [ "${VER}" -eq '2' -a "${KERN_24}" -eq '1' ] && MAKEOPTS="${MAKEOPTS_SAVE}"
284         export MAKEOPTS
285         unset UNAME_MACHINE
286 }
287
288 compile_kernel() {
289         [ "${KERNEL_MAKE}" = '' ] &&
290                 gen_die "KERNEL_MAKE undefined - I don't know how to compile a kernel for this arch!"
291         cd ${KERNEL_DIR}
292         print_info 1 "        >> Compiling ${KV} ${KERNEL_MAKE_DIRECTIVE/_install/ [ install ]/}..."
293         compile_generic "${KERNEL_MAKE_DIRECTIVE}" kernel
294         if [ "${KERNEL_MAKE_DIRECTIVE_2}" != '' ]
295         then
296                 print_info 1 "        >> Starting supplimental compile of ${KV}: ${KERNEL_MAKE_DIRECTIVE_2}..."
297                 compile_generic "${KERNEL_MAKE_DIRECTIVE_2}" kernel
298         fi
299         if ! isTrue "${CMD_NOINSTALL}"
300         then
301                 cp "${KERNEL_BINARY}" "${BOOTDIR}/kernel-${KNAME}-${ARCH}-${KV}" ||
302                         gen_die 'Could not copy the kernel binary to ${BOOTDIR}!'
303                 cp "System.map" "${BOOTDIR}/System.map-${KNAME}-${ARCH}-${KV}" ||
304                         gen_die 'Could not copy System.map to ${BOOTDIR}!'
305                 if [ "${KERNEL_BINARY_2}" != '' -a "${GENERATE_Z_IMAGE}" = '1' ]
306                 then
307                         cp "${KERNEL_BINARY_2}" "${BOOTDIR}/kernelz-${KV}" ||
308                                 gen_die 'Could not copy the kernelz binary to ${BOOTDIR}!'
309                 fi
310         else
311                 cp "${KERNEL_BINARY}" "${TMPDIR}/kernel-${KNAME}-${ARCH}-${KV}" ||
312                         gen_die "Could not copy the kernel binary to ${TMPDIR}!"
313                 cp "System.map" "${TMPDIR}/System.map-${KNAME}-${ARCH}-${KV}" ||
314                         gen_die "Could not copy System.map to ${TMPDIR}!"
315                 if [ "${KERNEL_BINARY_2}" != '' -a "${GENERATE_Z_IMAGE}" = '1' ]
316                 then
317                         cp "${KERNEL_BINARY_2}" "${TMPDIR}/kernelz-${KV}" ||
318                                 gen_die "Could not copy the kernelz binary to ${TMPDIR}!"
319                 fi
320         fi
321 }
322
323 compile_unionfs_modules() {
324         if [ ! -f "${UNIONFS_MODULES_BINCACHE}" ]
325         then
326                 [ -f "${UNIONFS_SRCTAR}" ] ||
327                         gen_die "Could not find unionfs source tarball: ${UNIONFS_SRCTAR}!"
328                 cd "${TEMP}"
329                 rm -rf ${UNIONFS_DIR} > /dev/null
330                 rm -rf unionfs > /dev/null
331                 mkdir -p unionfs
332                 /bin/tar -zxpf ${UNIONFS_SRCTAR} ||
333                         gen_die 'Could not extract unionfs source tarball!'
334                 [ -d "${UNIONFS_DIR}" ] ||
335                         gen_die 'Unionfs directory ${UNIONFS_DIR} is invalid!'
336                 cd "${UNIONFS_DIR}"
337                 print_info 1 'unionfs modules: >> Compiling...'
338                 echo "LINUXSRC=${KERNEL_DIR}" >> fistdev.mk
339                 echo 'TOPINC=-I$(LINUXSRC)/include' >> fistdev.mk
340                 echo "MODDIR= /lib/modules/${KV}" >> fistdev.mk
341                 echo "KERNELVERSION=${KV}" >> fistdev.mk
342                 # Fix for hardened/selinux systems to have extened attributes
343                 # per r2d2's request.  Also add -DUNIONFS_UNSUPPORTED for 2.6.16
344                 echo "EXTRACFLAGS=-DUNIONFS_XATTR -DFIST_SETXATTR_CONSTVOID -DUNIONFS_UNSUPPORTED" \
345                         >> fistdev.mk
346                 # Here we do something really nasty and disable debugging, along with
347                 # change our default CFLAGS
348                 echo "UNIONFS_DEBUG_CFLAG=-DUNIONFS_NDEBUG" >> fistdev.mk
349                 echo "UNIONFS_OPT_CFLAG= -O2 -pipe" >> fistdev.mk
350
351                 if [ "${PAT}" -ge '6' ]
352                 then
353                         cd "${TEMP}"
354                         cd "${UNIONFS_DIR}"
355                         # Compile unionfs module within the unionfs
356                         # environment not within the kernelsrc dir
357                         make unionfs.ko || gen_die 'failed to compile unionfs'
358                 else
359                         gen_die 'unionfs is only supported on 2.6 targets'
360                 fi
361                 print_info 1 'unionfs: >> Copying to cache...'
362         
363                 mkdir -p ${TEMP}/unionfs/lib/modules/${KV}/kernel/fs/unionfs
364                 
365                 if [ -f unionfs.ko ]
366                 then 
367                         cp -f unionfs.ko ${TEMP}/unionfs/lib/modules/${KV}/kernel/fs/unionfs
368                 else
369                         cp -f unionfs.o ${TEMP}/unionfs/lib/modules/${KV}/kernel/fs/unionfs
370                 fi
371         
372                 cd ${TEMP}/unionfs
373                 /bin/tar -cjf "${UNIONFS_MODULES_BINCACHE}" . ||
374                         gen_die 'Could not create unionfs modules binary cache'
375         
376                 cd "${TEMP}"
377                 rm -rf "${UNIONFS_DIR}" > /dev/null
378                 rm -rf unionfs > /dev/null
379         fi
380 }
381
382 compile_unionfs_utils() {
383         if [ ! -f "${UNIONFS_BINCACHE}" ]
384         then
385                 [ -f "${UNIONFS_SRCTAR}" ] ||
386                         gen_die "Could not find unionfs source tarball: ${UNIONFS_SRCTAR}!"
387                 cd "${TEMP}"
388                 rm -rf ${UNIONFS_DIR} > /dev/null
389                 rm -rf unionfs > /dev/null
390                 mkdir -p unionfs/sbin
391                 /bin/tar -zxpf ${UNIONFS_SRCTAR} ||
392                         gen_die 'Could not extract unionfs source tarball!'
393                 [ -d "${UNIONFS_DIR}" ] ||
394                         gen_die 'Unionfs directory ${UNIONFS_DIR} is invalid!'
395                 cd "${UNIONFS_DIR}"
396                 print_info 1 'unionfs tools: >> Compiling...'
397                 sed -i Makefile -e 's|${CC} -o|${CC} -static -o|g'
398                 compile_generic utils utils
399                 
400                 print_info 1 'unionfs: >> Copying to cache...'
401                 strip uniondbg unionctl
402                 cp uniondbg ${TEMP}/unionfs/sbin/ || 
403                         gen_die 'Could not copy the uniondbg binary to the tmp directory'
404                 cp unionctl ${TEMP}/unionfs/sbin/ ||
405                         gen_die 'Could not copy the unionctl binary to the tmp directory'
406                 cd ${TEMP}/unionfs      
407                 /bin/tar -cjf "${UNIONFS_BINCACHE}" . ||
408                         gen_die 'Could not create unionfs tools binary cache'
409                 
410                 cd "${TEMP}"
411                 rm -rf "${UNIONFS_DIR}" > /dev/null
412                 rm -rf unionfs > /dev/null
413         fi
414 }
415
416 compile_busybox() {
417         if [ ! -f "${BUSYBOX_BINCACHE}" ]
418         then
419                 [ -f "${BUSYBOX_SRCTAR}" ] ||
420                         gen_die "Could not find busybox source tarball: ${BUSYBOX_SRCTAR}!"
421                 [ -f "${BUSYBOX_CONFIG}" ] ||
422                         gen_die "Cound not find busybox config file: ${BUSYBOX_CONFIG}!"
423                 cd "${TEMP}"
424                 rm -rf ${BUSYBOX_DIR} > /dev/null
425                 /bin/tar -jxpf ${BUSYBOX_SRCTAR} ||
426                         gen_die 'Could not extract busybox source tarball!'
427                 [ -d "${BUSYBOX_DIR}" ] ||
428                         gen_die 'Busybox directory ${BUSYBOX_DIR} is invalid!'
429                 cp "${BUSYBOX_CONFIG}" "${BUSYBOX_DIR}/.config"
430                 sed -i ${BUSYBOX_DIR}/.config -e 's/#\? \?CONFIG_FEATURE_INSTALLER[ =].*/CONFIG_FEATURE_INSTALLER=y/g'
431                 cd "${BUSYBOX_DIR}"
432                 print_info 1 'busybox: >> Configuring...'
433                 yes '' 2>/dev/null | compile_generic oldconfig utils
434                 print_info 1 'busybox: >> Compiling...'
435                 compile_generic all utils
436                 print_info 1 'busybox: >> Copying to cache...'
437                 [ -f "${TEMP}/${BUSYBOX_DIR}/busybox" ] ||
438                         gen_die 'Busybox executable does not exist!'
439                 strip "${TEMP}/${BUSYBOX_DIR}/busybox" ||
440                         gen_die 'Could not strip busybox binary!'
441                 bzip2 "${TEMP}/${BUSYBOX_DIR}/busybox" ||
442                         gen_die 'bzip2 compression of busybox failed!'
443                 mv "${TEMP}/${BUSYBOX_DIR}/busybox.bz2" "${BUSYBOX_BINCACHE}" ||
444                         gen_die 'Could not copy the busybox binary to the package directory, does the directory exist?'
445
446                 cd "${TEMP}"
447                 rm -rf "${BUSYBOX_DIR}" > /dev/null
448         fi
449 }
450
451 compile_lvm2() {
452         compile_device_mapper
453         if [ ! -f "${LVM2_BINCACHE}" ]
454         then
455                 [ -f "${LVM2_SRCTAR}" ] ||
456                         gen_die "Could not find LVM2 source tarball: ${LVM2_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
457                 cd "${TEMP}"
458                 rm -rf ${LVM2_DIR} > /dev/null
459                 /bin/tar -zxpf ${LVM2_SRCTAR} ||
460                         gen_die 'Could not extract LVM2 source tarball!'
461                 [ -d "${LVM2_DIR}" ] ||
462                         gen_die 'LVM2 directory ${LVM2_DIR} is invalid!'
463                 rm -rf "${TEMP}/device-mapper" > /dev/null
464                 /bin/tar -jxpf "${DEVICE_MAPPER_BINCACHE}" -C "${TEMP}" ||
465                         gen_die "Could not extract device-mapper binary cache!";
466                 
467                 cd "${LVM2_DIR}"
468                 print_info 1 'lvm2: >> Configuring...'
469                         LDFLAGS="-L${TEMP}/device-mapper/lib" \
470                         CFLAGS="-I${TEMP}/device-mapper/include" \
471                         CPPFLAGS="-I${TEMP}/device-mapper/include" \
472                         ./configure --enable-static_link --prefix=${TEMP}/lvm2 >> ${DEBUGFILE} 2>&1 ||
473                                 gen_die 'Configure of lvm2 failed!'
474                 print_info 1 'lvm2: >> Compiling...'
475                         compile_generic '' utils
476                         compile_generic 'install' utils
477
478                 cd "${TEMP}/lvm2"
479                 print_info 1 '      >> Copying to bincache...'
480                 strip "sbin/lvm.static" ||
481                         gen_die 'Could not strip lvm.static!'
482                 /bin/tar -cjf "${LVM2_BINCACHE}" sbin/lvm.static ||
483                         gen_die 'Could not create binary cache'
484
485                 cd "${TEMP}"
486                 rm -rf "${TEMP}/device-mapper" > /dev/null
487                 rm -rf "${LVM2_DIR}" lvm2
488         fi
489 }
490
491 compile_dmraid() {
492         compile_device_mapper
493         if [ ! -f "${DMRAID_BINCACHE}" ]
494         then
495                 [ -f "${DMRAID_SRCTAR}" ] ||
496                         gen_die "Could not find DMRAID source tarball: ${DMRAID_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
497                 cd "${TEMP}"
498                 rm -rf ${DMRAID_DIR} > /dev/null
499                 /bin/tar -jxpf ${DMRAID_SRCTAR} ||
500                         gen_die 'Could not extract DMRAID source tarball!'
501                 [ -d "${DMRAID_DIR}" ] ||
502                         gen_die 'DMRAID directory ${DMRAID_DIR} is invalid!'
503                 rm -rf "${TEMP}/device-mapper" > /dev/null
504                 /bin/tar -jxpf "${DEVICE_MAPPER_BINCACHE}" -C "${TEMP}" ||
505                         gen_die "Could not extract device-mapper binary cache!";
506                 
507                 cd "${DMRAID_DIR}"
508                 print_info 1 'dmraid: >> Configuring...'
509                 
510                         LDFLAGS="-L${TEMP}/device-mapper/lib" \
511                         CFLAGS="-I${TEMP}/device-mapper/include" \
512                         CPPFLAGS="-I${TEMP}/device-mapper/include" \
513                         ./configure --enable-static_link --prefix=${TEMP}/dmraid >> ${DEBUGFILE} 2>&1 ||
514                                 gen_die 'Configure of dmraid failed!'
515                                 
516                         # We dont necessarily have selinux installed yet... look into
517                         # selinux global support in the future.
518                         sed -i tools/Makefile -e "s|DMRAIDLIBS += -lselinux||g"
519                         ###echo "DMRAIDLIBS += -lselinux -lsepol" >> tools/Makefile
520                 mkdir -p "${TEMP}/dmraid"
521                 print_info 1 'dmraid: >> Compiling...'
522                         compile_generic '' utils
523                         #compile_generic 'install' utils
524                         mkdir ${TEMP}/dmraid/sbin
525                         install -m 0755 -s tools/dmraid "${TEMP}/dmraid/sbin/dmraid"
526                 print_info 1 '      >> Copying to bincache...'
527                 cd "${TEMP}/dmraid"
528                 /bin/tar -cjf "${DMRAID_BINCACHE}" sbin/dmraid ||
529                         gen_die 'Could not create binary cache'
530
531                 cd "${TEMP}"
532                 rm -rf "${TEMP}/device-mapper" > /dev/null
533                 rm -rf "${DMRAID_DIR}" dmraid
534         fi
535 }
536
537 compile_suspend() {
538         [ -f "${SUSPEND_BINCACHE}" ] && return
539         [ -f "${SUSPEND_SRCTAR}" ] ||
540                 gen_die "Could not find SUSPEND source tarball: ${SUSPEND_SRCTAR}! Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
541         cd ${TEMP}
542         rm -rf ${SUSPEND_DIR} > /dev/null
543         /bin/tar -zxpf ${SUSPEND_SRCTAR} ||
544                 gen_die 'Could not extract SUSPEND source tarball!'
545         [ -d "${SUSPEND_DIR}" ] ||
546                 gen_die "SUSPEND directory ${DMRAID_DIR} is invalid!"
547
548         cd "${SUSPEND_DIR}"
549         if [ -f "${GK_SHARE}/pkg/suspend-0.5-Makefile.patch" ]
550         then
551                 patch -p1 -i \
552                         ${GK_SHARE}/pkg/suspend-0.5-Makefile.patch \
553                         || gen_die "Failed patching suspend"
554         fi
555
556         print_info 1 'suspend: >> Compiling...'
557         compile_generic '' utils CC_FLAGS= LD_FLAGS=
558
559         print_info 1 '         >> Copying to bincache...'
560         mkdir -p "${TEMP}/bincache/sbin"
561         cp -f resume "${TEMP}/bincache/sbin" ||
562                 gen_die 'Could not copy resume binary'
563         cd "${TEMP}/bincache"
564         /bin/tar -cjf "${SUSPEND_BINCACHE}" * ||
565                 gen_die 'Could not create suspend binary cache'
566         cd "${TEMP}"
567         rm -rf bincache suspend-0.5
568 }
569
570 compile_modutils() {
571         # I've disabled dietlibc support for the time being since the
572         # version we use misses a few needed system calls.
573
574         local ARGS
575         if [ ! -f "${MODUTILS_BINCACHE}" ]
576         then
577                 [ ! -f "${MODUTILS_SRCTAR}" ] &&
578                         gen_die "Could not find modutils source tarball: ${MODUTILS_SRCTAR}!"
579                 cd "${TEMP}"
580                 rm -rf "${MODUTILS_DIR}"
581                 /bin/tar -jxpf "${MODUTILS_SRCTAR}"
582                 [ ! -d "${MODUTILS_DIR}" ] &&
583                         gen_die "Modutils directory ${MODUTILS_DIR} invalid!"
584                 cd "${MODUTILS_DIR}"
585                 print_info 1 "modutils: >> Configuring..."
586
587 #               if [ "${USE_DIETLIBC}" -eq '1' ]
588 #               then
589 #                       extract_dietlibc_bincache
590 #                       OLD_CC="${UTILS_CC}"
591 #                       UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
592 #               fi
593
594                 export_utils_args
595                 export ARCH=${ARCH}
596                 ./configure --disable-combined --enable-insmod-static >> ${DEBUGFILE} 2>&1 ||
597                         gen_die 'Configuring modutils failed!'
598                 unset_utils_args
599
600                 print_info 1 'modutils: >> Compiling...'
601                 compile_generic all utils
602
603 #               if [ "${USE_DIETLIBC}" -eq '1' ]
604 #               then
605 #                       clean_dietlibc_bincache
606 #                       UTILS_CC="${OLD_CC}"
607 #               fi
608
609                 print_info 1 'modutils: >> Copying to cache...'
610                 [ -f "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" ] ||
611                         gen_die 'insmod.static does not exist after the compilation of modutils!'
612                 strip "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" ||
613                         gen_die 'Could not strip insmod.static!'
614                 bzip2 "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static" ||
615                         gen_die 'Compression of insmod.static failed!'
616                 mv "${TEMP}/${MODUTILS_DIR}/insmod/insmod.static.bz2" "${MODUTILS_BINCACHE}" ||
617                         gen_die 'Could not move the compressed insmod binary to the package cache!'
618
619                 cd "${TEMP}"
620                 rm -rf "${MODULE_INIT_TOOLS_DIR}" > /dev/null
621         fi
622 }
623
624 compile_module_init_tools() {
625         # I've disabled dietlibc support for the time being since the
626         # version we use misses a few needed system calls.
627
628         local ARGS
629         if [ ! -f "${MODULE_INIT_TOOLS_BINCACHE}" ]
630         then
631                 [ ! -f "${MODULE_INIT_TOOLS_SRCTAR}" ] &&
632                         gen_die "Could not find module-init-tools source tarball: ${MODULE_INIT_TOOLS_SRCTAR}"
633                 cd "${TEMP}"
634                 rm -rf "${MODULE_INIT_TOOLS_DIR}"
635                 /bin/tar -jxpf "${MODULE_INIT_TOOLS_SRCTAR}"
636                 [ ! -d "${MODULE_INIT_TOOLS_DIR}" ] &&
637                         gen_die "Module-init-tools directory ${MODULE_INIT_TOOLS_DIR} is invalid"
638                 cd "${MODULE_INIT_TOOLS_DIR}"
639                 print_info 1 'module-init-tools: >> Configuring'
640
641 #               if [ "${USE_DIETLIBC}" -eq '1' ]
642 #               then
643 #                       extract_dietlibc_bincache
644 #                       OLD_CC="${UTILS_CC}"
645 #                       UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
646 #               fi
647
648                 export_utils_args
649                 ./configure >> ${DEBUGFILE} 2>&1 ||
650                         gen_die 'Configure of module-init-tools failed!'
651                 unset_utils_args
652                 print_info 1 '                   >> Compiling...'
653                 compile_generic "all" utils
654
655 #               if [ "${USE_DIETLIBC}" -eq '1' ]
656 #               then
657 #                       clean_dietlibc_bincache
658 #                       UTILS_CC="${OLD_CC}"
659 #               fi
660
661                 print_info 1 '                   >> Copying to cache...'
662                 [ -f "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" ] ||
663                         gen_die 'insmod.static does not exist after the compilation of module-init-tools!'
664                 strip "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" ||
665                         gen_die 'Could not strip insmod.static!'
666                 bzip2 "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static" ||
667                         gen_die 'Compression of insmod.static failed!'
668                 [ -f "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static.bz2" ] ||
669                         gen_die 'Could not find compressed insmod.static.bz2 binary!'
670                 mv "${TEMP}/${MODULE_INIT_TOOLS_DIR}/insmod.static.bz2" "${MODULE_INIT_TOOLS_BINCACHE}" ||
671                         gen_die 'Could not move the compressed insmod binary to the package cache!'
672
673                 cd "${TEMP}"
674                 rm -rf "${MODULE_INIT_TOOLS_DIR}" > /dev/null
675         fi
676 }
677
678 compile_devfsd() {
679         # I've disabled dietlibc support for the time being since the
680         # version we use misses a few needed system calls.
681
682         local ARGS
683         if [ ! -f "${DEVFSD_BINCACHE}" ]
684         then
685                 [ ! -f "${DEVFSD_SRCTAR}" ] &&
686                         gen_die "Could not find devfsd source tarball: ${DEVFSD_SRCTAR}"
687                 cd "${TEMP}"
688                 rm -rf "${DEVFSD_DIR}"
689                 /bin/tar -jxpf "${DEVFSD_SRCTAR}"
690                 [ ! -d "${DEVFSD_DIR}" ] &&
691                         gen_die "Devfsd directory ${DEVFSD_DIR} invalid"
692                 cd "${DEVFSD_DIR}"
693
694 #               if [ "${USE_DIETLIBC}" -eq '1' ]
695 #               then
696 #                       extract_dietlibc_bincache
697 #                       OLD_CC="${UTILS_CC}"
698 #                       UTILS_CC="${TEMP}/diet/bin/diet ${UTILS_CC}"
699 #               fi
700
701                 print_info 1 'devfsd: >> Compiling...'
702 #               if [ "${USE_DIETLIBC}" -eq '1' ]
703 #               then
704 #                       compile_generic 'has_dlopen=0 has_rpcsvc=0' utils
705 #               else
706                         compile_generic 'LDFLAGS=-static' utils
707 #               fi
708
709 #               if [ "${USE_DIETLIBC}" -eq '1' ]
710 #               then
711 #                       clean_dietlibc_bincache
712 #                       UTILS_CC="${OLD_CC}"
713 #               fi
714
715                 print_info 1 '        >> Copying to cache...'
716                 [ -f "${TEMP}/${DEVFSD_DIR}/devfsd" ] || gen_die 'The devfsd executable does not exist after the compilation of devfsd!'
717                 strip "${TEMP}/${DEVFSD_DIR}/devfsd" || gen_die 'Could not strip devfsd!'
718                 bzip2 "${TEMP}/${DEVFSD_DIR}/devfsd" || gen_die 'Compression of devfsd failed!'
719                 [ -f "${TEMP}/${DEVFSD_DIR}/devfsd.bz2" ] || gen_die 'Could not find compressed devfsd.bz2 binary!'
720                 mv "${TEMP}/${DEVFSD_DIR}/devfsd.bz2" "${DEVFSD_BINCACHE}" || gen_die 'Could not move compressed binary to the package cache!'
721
722 #               [ -f "${TEMP}/${DEVFSD_DIR}/devfsd.conf" ] || gen_die 'devfsd.conf does not exist after the compilation of devfsd!'
723 #               bzip2 "${TEMP}/${DEVFSD_DIR}/devfsd.conf" || gen_die 'Compression of devfsd.conf failed!'
724 #               mv "${TEMP}/${DEVFSD_DIR}/devfsd.conf.bz2" "${DEVFSD_CONF_BINCACHE}" || gen_die 'Could not move the compressed configuration to the package cache!'
725
726                 cd "${TEMP}"
727                 rm -rf "${DEVFSD_DIR}" > /dev/null
728         fi
729 }
730
731 compile_device_mapper() {
732         if [ ! -f "${DEVICE_MAPPER_BINCACHE}" ]
733         then
734                 [ ! -f "${DEVICE_MAPPER_SRCTAR}" ] &&
735                         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!"
736                 cd "${TEMP}"
737                 rm -rf "${DEVICE_MAPPER_DIR}"
738                 /bin/tar -zxpf "${DEVICE_MAPPER_SRCTAR}"
739                 [ ! -d "${DEVICE_MAPPER_DIR}" ] &&
740                         gen_die "device-mapper directory ${DEVICE_MAPPER_DIR} invalid"
741                 cd "${DEVICE_MAPPER_DIR}"
742                 mymapperconf="--enable-static_link"
743                 ###mymapperconf="--disable-selinux ${mymapperconf}"
744                 ./configure  --prefix=${TEMP}/device-mapper ${mymapperconf} \
745                         >> ${DEBUGFILE} 2>&1 || gen_die 'Configuring device-mapper failed!'
746                 print_info 1 'device-mapper: >> Compiling...'
747                 compile_generic '' utils
748                 compile_generic 'install' utils
749                 print_info 1 '        >> Copying to cache...'
750                 cd "${TEMP}"
751                 rm -r "${TEMP}/device-mapper/man" ||
752                         gen_die 'Could not remove manual pages!'
753                 strip "${TEMP}/device-mapper/sbin/dmsetup" ||
754                         gen_die 'Could not strip dmsetup binary!'
755                 /bin/tar -jcpf "${DEVICE_MAPPER_BINCACHE}" device-mapper ||
756                         gen_die 'Could not tar up the device-mapper binary!'
757                 [ -f "${DEVICE_MAPPER_BINCACHE}" ] ||
758                         gen_die 'device-mapper cache not created!'
759                 cd "${TEMP}"
760                 rm -rf "${DEVICE_MAPPER_DIR}" > /dev/null
761                 rm -rf "${TEMP}/device-mapper" > /dev/null
762         fi
763 }
764
765 compile_dietlibc() {
766         local BUILD_DIETLIBC
767         local ORIGTEMP
768
769         BUILD_DIETLIBC=0
770         [ ! -f "${DIETLIBC_BINCACHE}" ] && BUILD_DIETLIBC=1
771         [ ! -f "${DIETLIBC_BINCACHE_TEMP}" ] && BUILD_DIETLIBC=1
772         if ! isTrue "${BUILD_DIETLIBC}"
773         then
774                 ORIGTEMP=`cat "${DIETLIBC_BINCACHE_TEMP}"`
775                 if [ "${TEMP}" != "${ORIGTEMP}" ]
776                 then
777                         print_warning 1 'dietlibc: Bincache exists, but the current temporary directory'
778                         print_warning 1 '          is different to the original. Rebuilding.'
779                         BUILD_DIETLIBC=1
780                 fi
781         fi
782
783         if [ "${BUILD_DIETLIBC}" -eq '1' ]
784         then
785                 [ -f "${DIETLIBC_SRCTAR}" ] ||
786                         gen_die "Could not find dietlibc source tarball: ${DIETLIBC_SRCTAR}"
787                 cd "${TEMP}"
788                 rm -rf "${DIETLIBC_DIR}" > /dev/null
789                 /bin/tar -jxpf "${DIETLIBC_SRCTAR}" ||
790                         gen_die 'Could not extract dietlibc source tarball'
791                 [ -d "${DIETLIBC_DIR}" ] ||
792                         gen_die "Dietlibc directory ${DIETLIBC_DIR} is invalid!"
793                 cd "${DIETLIBC_DIR}"
794                 print_info 1 "dietlibc: >> Compiling..."
795                 compile_generic "prefix=${TEMP}/diet" utils
796                 print_info 1 "          >> Installing..."
797                 compile_generic "prefix=${TEMP}/diet install" utils
798                 print_info 1 "          >> Copying to bincache..."
799                 cd ${TEMP}
800                 /bin/tar -jcpf "${DIETLIBC_BINCACHE}" diet ||
801                         gen_die 'Could not tar up the dietlibc binary!'
802                 [ -f "${DIETLIBC_BINCACHE}" ] ||
803                         gen_die 'Dietlibc cache not created!'
804                 echo "${TEMP}" > "${DIETLIBC_BINCACHE_TEMP}"
805
806                 cd "${TEMP}"
807                 rm -rf "${DIETLIBC_DIR}" > /dev/null
808                 rm -rf "${TEMP}/diet" > /dev/null
809         fi
810 }
811 compile_klibc() {
812         cd "${TEMP}"
813         rm -rf "${KLIBC_DIR}" klibc-build
814         [ ! -f "${KLIBC_SRCTAR}" ] &&
815                 gen_die "Could not find klibc tarball: ${KLIBC_SRCTAR}"
816         /bin/tar jxpf "${KLIBC_SRCTAR}" ||
817                 gen_die 'Could not extract klibc tarball'
818         [ ! -d "${KLIBC_DIR}" ] &&
819                 gen_die "klibc tarball ${KLIBC_SRCTAR} is invalid"
820         cd "${KLIBC_DIR}"
821         if [ -f ${GK_SHARE}/pkg/klibc-1.1.16-sparc2.patch ]
822         then
823                 patch -p1 -i \
824                         ${GK_SHARE}/pkg/klibc-1.1.16-sparc2.patch \
825                         || gen_die "Failed patching klibc"
826         fi
827         if [ -f "${GK_SHARE}/pkg/klibc-1.2.1-nostdinc-flags.patch" ]
828         then
829                 patch -p1 -i \
830                         ${GK_SHARE}/pkg/klibc-1.2.1-nostdinc-flags.patch \
831                         || gen_die "Failed patching klibc"
832         fi
833
834         # Don't install to "//lib" fix
835         sed -e 's:SHLIBDIR = /lib:SHLIBDIR = $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib:' -i scripts/Kbuild.install
836         print_info 1 'klibc: >> Compiling...'
837         ln -snf "${KERNEL_DIR}" linux || gen_die "Could not link to ${KERNEL_DIR}"
838         sed -i Makefile -e "s|prefix      = /usr|prefix      = ${TEMP}/klibc-build|g"
839         if [ "${UTILS_ARCH}" != '' ]
840         then
841                 sed -i Makefile -e "s|export ARCH.*|export ARCH := ${UTILS_ARCH}|g"
842         fi
843         if [ "${ARCH}" = 'um' ]
844         then
845                 compile_generic "ARCH=um" utils
846         elif [ "${ARCH}" = 'x86' ]
847         then
848                 compile_generic "ARCH=i386" utils
849         elif [ "${UTILS_CROSS_COMPILE}" != '' ]
850         then
851                 compile_generic "CROSS=${UTILS_CROSS_COMPILE}" utils
852         else
853                 compile_generic "" utils
854         fi
855
856         compile_generic "install" utils
857         
858 }
859 compile_udev() {
860         if [ ! -f "${UDEV_BINCACHE}" ]
861         then
862                 # PPC fixup for 2.6.14
863                 # Headers are moving around .. need to make them available
864                 if [ "${VER}" -eq '2' -a "${PAT}" -eq '6' -a "${SUB}" -ge '14' ]
865                 then
866                     if [ "${ARCH}" = 'ppc' -o "${ARCH}" = 'ppc64' ]
867                     then
868                         cd ${KERNEL_DIR}
869                         echo 'Applying hack to workaround 2.6.14+ PPC header breakages...'
870                         compile_generic 'include/asm' kernel
871                     fi
872                 fi
873                 compile_klibc
874                 cd "${TEMP}"
875                 rm -rf "${UDEV_DIR}" udev
876                 [ ! -f "${UDEV_SRCTAR}" ] &&
877                         gen_die "Could not find udev tarball: ${UDEV_SRCTAR}"
878                 /bin/tar -jxpf "${UDEV_SRCTAR}" ||
879                         gen_die 'Could not extract udev tarball'
880                 [ ! -d "${UDEV_DIR}" ] &&
881                         gen_die "Udev tarball ${UDEV_SRCTAR} is invalid"
882
883                 cd "${UDEV_DIR}"
884                 local extras="extras/scsi_id extras/volume_id extras/ata_id extras/run_directory extras/usb_id extras/floppy extras/cdrom_id extras/firmware"
885                 # No selinux support yet .. someday maybe
886                 #use selinux && myconf="${myconf} USE_SELINUX=true"
887                 print_info 1 'udev: >> Compiling...'
888                 # SPARC fixup
889                 if [ "${UTILS_ARCH}" = 'sparc' ]
890                 then
891                         echo "CFLAGS += -mcpu=v8 -mtune=v8" >> Makefile
892                 fi
893                 # PPC fixup for 2.6.14
894                 if [ "${VER}" -eq '2' -a "${PAT}" -eq '6' -a "${SUB}" -ge '14' ]
895                 then
896                         if [ "${ARCH}" = 'ppc' -o "${ARCH}" = 'ppc64' ]
897                         then
898                                 # Headers are moving around .. need to make them available
899                                 echo "CFLAGS += -I${KERNEL_DIR}/arch/${ARCH}/include" >> Makefile
900                         fi
901                 fi
902
903                 if [ "${ARCH}" = 'um' ]
904                 then
905                         compile_generic "EXTRAS=\"${extras}\" ARCH=um USE_KLIBC=true KLCC=${TEMP}/klibc-build/bin/klcc USE_LOG=false DEBUG=false udevdir=/dev all" utils
906                 else
907                         # This *needs* to be runtask, or else it breakson most
908                         # architectures.  -- wolf31o2
909                         compile_generic "EXTRAS=\"${extras}\" USE_KLIBC=true KLCC=${TEMP}/klibc-build/bin/klcc USE_LOG=false DEBUG=false udevdir=/dev all" runtask
910                 fi
911
912
913                 print_info 1 '      >> Installing...'
914                 install -d "${TEMP}/udev/etc/udev" "${TEMP}/udev/sbin" "${TEMP}/udev/etc/udev/scripts" "${TEMP}/udev/etc/udev/rules.d" "${TEMP}/udev/etc/udev/permissions.d" "${TEMP}/udev/etc/udev/extras" "${TEMP}/udev/etc" "${TEMP}/udev/sbin" "${TEMP}/udev/usr/" "${TEMP}/udev/usr/bin" "${TEMP}/udev/usr/sbin"||
915                         gen_die 'Could not create directory hierarchy'
916                 
917                 install -c etc/udev/gentoo/udev.rules "${TEMP}/udev/etc/udev/rules.d/50-udev.rules" ||
918                     gen_die 'Could not copy gentoo udev.rules to 50-udev.rules'
919
920 #               compile_generic "EXTRAS=\"${extras}\" DESTDIR=${TEMP}/udev install-config" utils
921 #               compile_generic "EXTRAS=\"${extras}\" DESTDIR=${TEMP}/udev install-bin" utils
922                 # We are going to install our files by hand.  Why are we doing this?
923                 # Well, the udev ebuild does so, and I tend to think that Greg
924                 # Kroah-Hartman knows what he's doing with regards to udev.
925                 for i in udev udevd udevsend udevstart udevtrigger
926                 do
927                         install -D $i "${TEMP}/udev/sbin"
928                 done
929                 install -c extras/ide-devfs.sh "${TEMP}/udev/etc/udev/scripts" 
930                 install -c extras/scsi-devfs.sh "${TEMP}/udev/etc/udev/scripts" 
931                 install -c extras/raid-devfs.sh "${TEMP}/udev/etc/udev/scripts" 
932
933                 cd "${TEMP}/udev"
934                 print_info 1 '      >> Copying to bincache...'
935                 /bin/tar -cjf "${UDEV_BINCACHE}" * ||
936                         gen_die 'Could not create binary cache'
937
938                 cd "${TEMP}"
939                 rm -rf "${UDEV_DIR}" udev
940                 
941                 # PPC fixup for 2.6.14
942                 if [ "${VER}" -eq '2' -a "${PAT}" -eq '6' -a "${SUB}" -ge '14' ]
943                 then
944                     if [ "${ARCH}" = 'ppc' -o "${ARCH}" = 'ppc64' ]
945                     then
946                         cd ${KERNEL_DIR}
947                         compile_generic 'archclean' kernel
948                         cd "${TEMP}"
949                     fi
950                 fi
951         fi
952 }
953
954 compile_e2fsprogs() {
955         if [ ! -f "${BLKID_BINCACHE}" ]
956         then
957                 [ ! -f "${E2FSPROGS_SRCTAR}" ] &&
958                         gen_die "Could not find e2fsprogs source tarball: ${E2FSPROGS_SRCTAR}. Please place it there, or place another version, changing /etc/genkernel.conf as necessary!"
959                 cd "${TEMP}"
960                 rm -rf "${E2FSPROGS_DIR}"
961                 tar -zxpf "${E2FSPROGS_SRCTAR}"
962                 [ ! -d "${E2FSPROGS_DIR}" ] &&
963                         gen_die "e2fsprogs directory ${E2FSPROGS_DIR} invalid"
964                 cd "${E2FSPROGS_DIR}"
965                 print_info 1 'e2fsprogs: >> Configuring...'
966                 ./configure  --with-ldopts=-static >> ${DEBUGFILE} 2>&1 ||
967                         gen_die 'Configuring e2fsprogs failed!'
968                 print_info 1 'e2fsprogs: >> Compiling...'
969                 MAKE=${UTILS_MAKE} compile_generic "" ""
970                 print_info 1 'blkid: >> Copying to cache...'
971                 [ -f "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ] ||
972                         gen_die 'Blkid executable does not exist!'
973                 strip "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ||
974                         gen_die 'Could not strip blkid binary!'
975                 bzip2 "${TEMP}/${E2FSPROGS_DIR}/misc/blkid" ||
976                         gen_die 'bzip2 compression of blkid failed!'
977                 mv "${TEMP}/${E2FSPROGS_DIR}/misc/blkid.bz2" "${BLKID_BINCACHE}" ||
978                         gen_die 'Could not copy the blkid binary to the package directory, does the directory exist?'
979
980                 cd "${TEMP}"
981                 rm -rf "${E2FSPROGS_DIR}" > /dev/null
982         fi
983 }