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