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