Whamcloud - gitweb
b=11752
[fs/lustre-release.git] / build / lmake
1 #!/bin/sh
2
3 # option variables
4 DESTDIR=
5 KERNELDIR=
6 TARGET=
7 # Not sure what to put here
8 # TARGET_ARCH=$(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
9 TARGET_ARCH=
10 TARGET_CONFIG=
11 JOBS=1
12 CONFIGURE_FLAGS=
13 TMPDIR=${TMPDIR:-"/var/tmp"}
14
15 # commands to run
16 BUILD_LUSTRE=0
17 BUILD_KERNEL=0
18 DEPEND_KERNEL=0
19 INSTALL_LUSTRE=0
20 INSTALL_KERNEL=0
21 SAVE_HEADERS=0
22 UNPACK_KERNEL=0
23
24 # provided by target file
25 KERNEL=
26 SERIES=
27 CONFIG=
28 VERSION=
29 EXTRA_VERSION=
30
31 BASE_ARCHS=
32 BIGMEM_ARCHS=
33 BOOT_ARCHS=
34 JENSEN_ARCHS=
35 SMP_ARCHS=
36 BIGSMP_ARCHS=
37 PSERIES64_ARCHS=
38 UP_ARCHS=
39
40 RHBUILD=0
41 SUSEBUILD=0
42
43 # flat-out globals
44 TOPDIR=
45 TARGET_FILE=
46 KERNEL_FILE=
47 SERIES_FILE=
48 CONFIG_FILE=
49 RPMBUILD=
50
51 canon()
52 {
53     pushd $1 >/dev/null
54     echo $PWD
55     popd >/dev/null
56 }
57 TOPDIR="${0%%${0##*/}}"
58 if [ "${TOPDIR}" ] ; then
59     TOPDIR=$(canon "${TOPDIR}/..")
60 else
61     TOPDIR=$(canon "..")
62 fi
63
64 lbuild_topdir()
65 {
66     retdir=$TOPDIR
67     while [ ! -d $retdir/BUILD ] ; do
68         retdir=$(canon "$retdir/..")
69         if [ "$retdir" = "/" ] ; then
70             break;
71         fi
72     done
73     echo "$retdir"
74 }
75
76 cleanup()
77 {
78     true
79 }
80
81 fatal()
82 {
83     cleanup
84     [ "$2" ] && echo
85     [ "$2" ] && echo "${0##*/}: $2"
86     exit $1
87 }
88
89 list_targets()
90 {
91     echo -n "Available targets:"
92     for target in $TOPDIR/lustre/kernel_patches/targets/*.target ; do
93         target_file=${target##*/}
94         echo -n " ${target_file%%.target}"
95     done
96     echo
97 }
98
99
100 usage()
101 {
102     cat <<EOF
103 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
104
105 Options:
106
107   --build
108     same as --build-kernel --build-lustre --unpack-kernel
109
110   --build-lustre
111     configure and compile lustre.  Requires that --build-kernel was
112     already run.
113
114   --build-kernel
115     configure and compile a kernel.  Implies --depend-kernel.
116     Requires that --unpack-kernel was already run.
117
118   --depend-kernel)
119     Prepares a kernel tree for building (similar to make mrproper
120     oldconfig dep).  Requires that --unpack-kernel was already run.
121
122   --destdir=DESTDIR
123     Root directory to install into (like DESTDIR with auto*).
124
125   --extraversion=EXTRAVERSION
126     Overrides the target kernel\'s EXTRAVERSION text.
127
128   -h, --help
129     Display this message.
130
131   --install
132     same as --install-kernel --install-lustre
133
134   --install-lustre
135     run make install in the Lustre tree.
136
137   --install-kernel
138     install the kernel image and modules.
139
140   -j jobs
141     This works just like the -j option to make, and is passed to make
142     when building.
143
144   --kerneldir=KERNELDIR
145     Directory containing linux source tarballs.
146
147   --target=TARGET
148     Name of the configuration to use.  The available targets are
149     listed below.
150
151   --target-arch=ARCH
152     Specifies an architecture to use when choosing a kernel config
153     file.  Default is i386.
154
155   --target-config=CONFIG
156     Specifies a special option (such as smp, bigsmp, bigmem, or BOOT)
157     to use when choosing a kernel config file.  This also modifies the
158     kernel version and modules directory.
159
160   --unpack-kernel
161     Untars and patches the kernel source.
162
163   The order that commands (--build-lustre, --unpack-kernel) are
164   specified on the command line is ignored; ${0##*/} will always
165   execute them in the correct order (unpack, then build, then install
166   etc.).
167
168 EOF
169     list_targets
170
171     fatal "$1" "$2"
172 }
173
174 check_options()
175 {
176     (( $BUILD_LUSTRE || $BUILD_KERNEL || $DEPEND_KERNEL || \
177             $INSTALL_LUSTRE || $INSTALL_KERNEL || $SAVE_HEADERS || \
178             $UNPACK_KERNEL )) || \
179                 fatal 1 "No commands specified."
180
181     if (( $UNPACK_KERNEL )) ; then
182         [ "$KERNELDIR" ] || \
183             fatal 1 "A kernel directory must be specified with --kerneldir."
184         [ -d "$KERNELDIR" ] || \
185             fatal 1 "$KERNELDIR is not a directory."
186     fi
187
188     if (( $INSTALL_LUSTRE || $INSTALL_KERNEL || $SAVE_HEADERS )) ; then
189         [ -z "$DESTDIR" -o -d "$DESTDIR" ] || \
190             fatal 1 "$DESTDIR is not a directory."
191     fi
192
193     [ "$TARGET" ] || usage 1 "A target must be specified with --target."
194     TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
195     [ -r "$TARGET_FILE" ] || \
196         fatal 1 "Target '$TARGET' was not found.  Try --list-targets."
197
198     if [ -z "$JOBS" -o "$JOBS" -lt "1" ] ; then
199         JOBS=1
200     fi
201
202     RPMBUILD=$(which rpmbuild 2>/dev/null | head -1)
203     if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
204         RPMBUILD=$(which rpm 2>/dev/null | head -1)
205         if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
206             usage 1 "Could not find binary for making rpms (tried rpmbuild and rpm)."
207         fi
208     fi
209 }
210
211 get_lustre_version()
212 {
213     for series in $SERIES ; do
214         SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
215         lustre_patch=$(grep lustre_version "$SERIES_FILE" 2>/dev/null)
216         [ "$lustre_patch" ] && break
217     done
218     [ "$lustre_patch" ] || \
219         fatal 1 "Could not determine Lustre version from $SERIES series."
220
221     awk '/^\+#define LUSTRE_KERNEL_VERSION /{ print $3 }' \
222         "$TOPDIR/lustre/kernel_patches/patches/$lustre_patch" 2>/dev/null
223 }
224
225 load_target()
226 {
227     EXTRA_VERSION_save="$EXTRA_VERSION"
228
229     . "$TARGET_FILE"
230
231     [ "$KERNEL" ] || fatal 1 "Target $TARGET did not specify a kernel."
232 # Suse 2.6 has our patches in already
233 #    [ "$SERIES" ] || fatal 1 "Target $TARGET did not specify a patch series."
234 #    [ "$CONFIG" ] || fatal 1 "Target $TARGET did not specify a kernel config."
235     [ "$VERSION" ] || fatal 1 "Target $TARGET did not specify the kernel version."
236     
237     if [ "$KERNELDIR" ] ; then
238         KERNEL_FILE="$KERNELDIR/$KERNEL"
239         [ -r "$KERNELDIR/$KERNEL" ] || \
240             fatal 1 "Target $TARGET's kernel file $KERNEL not found in kernel directory $KERNELDIR."
241     fi
242
243     if [ "$SERIES" ] ; then
244         for series in $SERIES ; do
245             SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
246             [ -r "$SERIES_FILE" ] || \
247                 fatal 1 "Target $TARGET's series $SERIES missing from $TOPDIR/lustre/kernel_patches/series."
248         done
249     fi
250
251     TARGET_ARCH=${TARGET_ARCH:-$BASE_ARCHS}
252     CONFIG_TARGET="$TARGET-${TARGET_ARCH}${TARGET_CONFIG:+-$TARGET_CONFIG}"
253     CONFIG_FILE="$TOPDIR/lustre/kernel_patches/kernel_configs/kernel-$VERSION-$CONFIG_TARGET.config"
254     [ -r "$CONFIG_FILE" ] ||
255         fatal 1 "Target $TARGET's config file $CONFIG_FILE missing from $TOPDIR/lustre/kernel_patches/configs."
256
257     if [ "$EXTRA_VERSION_save" ] ; then
258         EXTRA_VERSION="$EXTRA_VERSION_save"
259     else
260         EXTRA_VERSION="${EXTRA_VERSION}_lustre.$(get_lustre_version)"
261     fi
262 }
263
264 # do these after load_target(), which maybe export CC
265 setup_ccache_distcc()
266 {
267     # distcc can't handle ".incbin"
268     if [ "$TARGET" == "2.6-suse" -o "$TARGET" == "2.6-rhel4" ]; then
269         if [ "$TARGET_ARCH" == "x86_64" ]; then
270             unset DISTCC
271         fi
272     fi
273     
274     CC=${CC:-gcc}
275     if [ "$CCACHE" ]; then
276         CC="$CCACHE $CC"
277         [ "$DISTCC" ] && export CCACHE_PREFIX="$DISTCC"
278     else
279         [ "$DISTCC" ] && CC="$DISTCC $CC"
280     fi
281 }
282
283 tarflags()
284 {
285     case "$1" in
286         '')
287             fatal 1 "tarflags(): File name argument missing."
288             ;;
289         *.tar.gz | *.tgz)
290             echo 'zxf'
291             ;;
292         *.tar.bz2)
293             echo 'jxf'
294             ;;
295         *.tar)
296             echo 'xf'
297             ;;
298         *)
299             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
300             ;;
301     esac
302 }
303
304 untar()
305 {
306     echo "Untarring ${1##*/}..."
307     tar $(tarflags $1) $1
308 }
309
310
311 extract_kernel()
312 {
313     (( $UNPACK_KERNEL )) || return 0
314     pushd "$TOPDIR" >/dev/null
315     if [ -d linux ] ; then
316         [ -L linux ] && rm -rf $(readlink linux)
317         rm -rf linux
318     fi
319     untar "$KERNEL_FILE"
320     [ -d linux ] || ln -sf linux* linux
321     popd >/dev/null
322 }
323
324 patch_kernel()
325 {
326     (( $UNPACK_KERNEL )) || return 0
327     [ "$SERIES" ] || return 0
328     pushd "$TOPDIR/linux" >/dev/null
329     for series in $SERIES ; do
330         echo -n "Applying series $series:"
331         SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
332         for patch in $(<"$SERIES_FILE") ; do
333             PATCH_FILE="$TOPDIR/lustre/kernel_patches/patches/$patch"
334             [ -r "$PATCH_FILE" ] || \
335                 fatal 1 "Patch file not found: $patch"
336             echo -n " $patch"
337             patch -s -p1 < "$PATCH_FILE" || fatal 1 "Error applying patch $patch."
338         done
339         echo
340     done
341     popd >/dev/null
342 }
343
344 set_make()
345 {
346     MAKE="make -s"
347     if [ "$CC" ] ; then
348         MAKE_CC="CC=$CC"
349     fi
350     if [ "$ARCH" ] ; then
351         MAKE_ARCH="$MAKE ARCH=$ARCH"
352     else
353         case $TARGET_ARCH in
354             i?86)
355                 ;;
356             *)
357                 MAKE_ARCH="$MAKE ARCH=$TARGET_ARCH"
358                 ;;
359         esac
360     fi
361     MAKE_J="$MAKE -j $JOBS"
362 }
363
364 timed_run() {
365     SLEEP_TIME=$1
366     shift
367
368     set -o monitor
369
370     #bash -c "$@" &
371     ("$@") &
372     child_pid=$!
373     
374     (sleep $SLEEP_TIME
375     kill -TERM -$child_pid 2>/dev/null
376     sleep 5
377     kill -KILL -$child_pid 2>/dev/null
378     echo "$1 was killed due to timeout") &
379     dog_pid=$!
380
381     wait $child_pid
382     # status will be set to 143 if the process had to be killed due to timeout
383     status=${PIPESTATUS[0]}
384     kill -KILL -$dog_pid
385     return $status
386 }
387
388 depend_kernel()
389 {
390     (( $DEPEND_KERNEL )) || return 0
391     # we need to override $CC at make time, since there is no
392     # configure
393     set_make
394     pushd "$TOPDIR/linux" >/dev/null
395     echo "Overriding EXTRAVERSION in kernel..."
396     perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -${EXTRA_VERSION}${TARGET_CONFIG}/" Makefile
397     echo "Making depend in $PWD..."
398     $MAKE "$MAKE_CC" mrproper || fatal 1 "Error running make mrproper"
399     rm -f rpm-release
400     # remove localversion-* files to avoid kernel release string 
401     # srewing up by the top-level Makefile
402     rm -f localversion-*
403     cp "$CONFIG_FILE" .config
404     local UPDATE_OLDCONFIG=
405     for oc in oldconfig_nonint silentoldconfig oldconfig ; do                   
406         if grep -q "$oc" Makefile ; then                                        
407             timed_run 300 $MAKE "$MAKE_CC" $oc || UPDATE_OLDCONFIG=1
408             break
409         fi
410     done
411
412     if [ "$UPDATE_OLDCONFIG" ] ; then
413         # use the expect script to "make oldconfig" and answer the questions for
414         # new items conservatively.  QA will get notified on anything newly added
415         # for them to review and adjust accordingly.
416         local logfile=$(mktemp /tmp/XXXXXX)
417         #timed_run 300 $TOPDIR/build/update_oldconfig $logfile
418         #local RC=${PIPESTATUS[0]}
419         #local RC=$(strace -f -o update_oldconfig.strace bash -c "$TOPDIR/build/update_oldconfig $logfile; echo \$?")
420         $TOPDIR/build/update_oldconfig $logfile
421         local RC=${PIPESTATUS[0]}
422         #$TOPDIR/build/update_oldconfig $logfile
423         #local RC=${PIPESTATUS[0]}
424         if [ $RC -eq 143 ]; then
425             fatal 1 "update_oldconfig timed out"
426         elif [ $RC -ne 0 ]; then
427             # dump the log
428             cat $logfile
429             rm -f $logfile
430             if [ -f update_oldconfig.strace ]; then
431                 cat update_oldconfig.strace
432                 rm -f update_oldconfig.strace
433             fi
434             fatal 1 "update_oldconfig failed: $RC. See log above."
435         fi
436         rm -f $logfile
437         # now notify if resulting .config is different than $CONFIG_FILE
438         local tmpfile=$(mktemp /tmp/XXXXXX)
439         diff -I '^#.*' -u "$CONFIG_FILE" .config >$tmpfile
440         if [ -s $tmpfile ]; then
441             { cat <<EOF
442 To: qa@lists.clusterfs.com
443 Subject: kernel_config change 
444
445 The result of a make oldconfig on file $CONFIG_FILE resulted in a
446 difference when compared to .config in the following way:
447
448 EOF
449             cat $tmpfile
450             echo -e "\n\nPlease consider updating $CONFIG_FILE."
451             # not sure these are entirely useful.  the above and "patch" are good
452             #echo -e "\nThe entire new .config file:\n"
453             #cat .config
454             } | sendmail -fqa@clusterfs.com -t
455         fi
456         rm -f $tmpfile
457     fi
458
459     case "$VERSION" in
460         2.6*)
461             $MAKE "$MAKE_CC" include/asm
462             ;;
463         2.4*)
464             $MAKE "$MAKE_CC" symlinks
465             $MAKE "$MAKE_CC" dep || fatal 1 "Error running make dep"
466             ;;
467     esac
468     $MAKE "$MAKE_CC" include/linux/version.h || fatal 1 "Error making include/linux/version.h"
469 }
470
471 build_kernel()
472 {
473     (( $BUILD_KERNEL )) || return 0
474     set_make
475     echo "Building kernel in $PWD..."
476     case "$TARGET_ARCH" in
477         i386 | i586 | i686 | athlon | x86_64)
478             $MAKE_J "$MAKE_CC" bzImage || fatal 1 "Error making bzImage."
479             ;;
480     ia64 | ppc | ppc64)
481             $MAKE_J "$MAKE_CC" vmlinux || fatal 1 "Error making vmlinux."
482             ;;
483         *)
484             $MAKE_J "$MAKE_CC" boot || fatal 1 "Error making boot."
485             ;;
486     esac
487     $MAKE_J "$MAKE_CC" modules || fatal 1 "Error building modules."
488
489     popd >/dev/null
490 }
491
492 configure_lustre()
493 {
494     return 0
495     (( $BUILD_LUSTRE )) || return 0
496     pushd "$TOPDIR" >/dev/null
497     [ -f Makefile ] && make -s clean
498     [ -f configure ] || sh ./autogen.sh
499     ./configure --with-linux=$PWD/linux $CONFIGURE_FLAGS || \
500         fatal 1 "Error configuring Lustre."
501     popd >/dev/null
502 }
503
504 build_lustre()
505 {
506     (( $BUILD_LUSTRE )) || return 0
507     set_make
508     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
509     pushd "$TOPDIR" >/dev/null
510     sed \
511         -e s^@VERSION@^${LUSTRE_VERSION}^g \
512         -e s^@LINUXRELEASE@^${FULL_VERSION}^g \
513         -e s^@RELEASE@^${FULL_VERSION//-/_}^g \
514         -e s^@ac_configure_args@^"--with-linux=${PWD}/linux ${CONFIGURE_FLAGS}"^g \
515         < build/lustre.spec.in \
516         > build/lustre.spec
517     $RPMBUILD --target ${TARGET_ARCH} -bb build/lustre.spec \
518         --define "_tmppath $TMPDIR" \
519         --define "_topdir $(lbuild_topdir)" || \
520         fatal 1 "Error building Lustre rpms."
521     # $MAKE_J "$MAKE_CC" || fatal 1 "Error building Lustre."
522     popd >/dev/null
523 }
524
525 install_kernel()
526 {
527     (( $INSTALL_KERNEL )) || return 0
528     set_make
529     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
530     pushd "$TOPDIR/linux" >/dev/null
531     mkdir -p "$DESTDIR/boot"
532
533     install -m 644 System.map "$DESTDIR/boot/System.map-${FULL_VERSION}"
534     # install -m 644 module-info ...
535     install -m 644 "$CONFIG_FILE" "$DESTDIR/boot/config-${FULL_VERSION}"
536
537     mkdir -p "$DESTDIR/dev/shm"
538     mkdir -p "$DESTDIR/lib/modules/${FULL_VERSION}"
539
540     $MAKE "$MAKE_CC" INSTALL_MOD_PATH="$DESTDIR" KERNELRELEASE="$FULL_VERSION" \
541         -s modules_install || \
542         fatal 1 "Error installing modules."
543
544     case "$TARGET_ARCH" in
545         i386 | i586 | i686 | athlon)
546             cp arch/i386/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
547             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
548             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
549             ;;
550         x86_64)
551             cp arch/x86_64/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
552             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
553             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
554             ;;
555         ppc | ppc64)
556             cp vmlinux "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
557             ln -sf "$DESTDIR/boot/vmlinux-${FULL_VERSION}" "../lib/modules/${FULL_VERSION}/vmlinux" 
558             ;;
559         ia64)
560             gzip -cfv vmlinux > vmlinuz
561             mkdir -p "$DESTDIR/boot/efi/redhat"
562             install -m 755 vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
563             install -m 755 vmlinuz "$DESTDIR/boot/efi/redhat/vmlinuz-${FULL_VERSION}"
564             ln -sf "../../../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/efi/redhat/vmlinux-${FULL_VERSION}"
565             ln -sf "efi/redhat/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
566             ln -sf "efi/redhat/vmlinuz-${FULL_VERSION}" "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
567             ;;
568         *)
569             cp vmlinuz "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
570             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}"
571             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
572
573             ;;
574     esac
575     if [ -e init/kerntypes.o ] ; then
576         cp init/kerntypes.o "$DESTDIR/boot/Kerntypes-${FULL_VERSION}"
577     fi
578
579     popd >/dev/null
580 }
581
582 cleanup_libmodules()
583 {
584     (( $INSTALL_LUSTRE )) || return 0
585
586     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
587     KVERREL="${VERSION}-${EXTRA_VERSION}"
588     i="$DESTDIR/lib/modules/${FULL_VERSION}"
589
590     rm -f $i/build
591     rm -f $i/source
592
593     if (( $LINUX26 )) ; then
594         ln -sf ../../../usr/src/linux-${KVERREL}-obj/${TARGET_ARCH}/${TARGET_CONFIG} $i/build
595         ln -sf ../../../usr/src/linux-${KVERREL} $i/source
596     else
597         ln -sf ../../../usr/src/linux-${KVERREL} $i/build
598     fi
599 }
600
601 install_lustre()
602 {
603     (( $INSTALL_LUSTRE )) || return 0
604     return 0
605     set_make
606     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
607     pushd "$TOPDIR" >/dev/null
608     $MAKE "$MAKE_CC" -s install "DESTDIR=$DESTDIR" KERNELRELEASE="$FULL_VERSION" || fatal 1 "Error installing Lustre."
609     popd >/dev/null
610 }
611
612 build_kms()
613 {
614     (( $BUILD_KERNEL )) || return 0
615     (( $SUSEBUILD )) || return 0
616     set_make
617     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
618     mkdir -p "${TOPDIR}/modules-${FULL_VERSION}"
619     for dir in /usr/src/kernel-modules/* ; do
620         # we are replacing lustre-lite, so don't include it
621         if [ "${dir##*/}" != "lustre-lite" -a -e $dir/Makefile ]; then
622             build_dir="${TOPDIR}/modules-${FULL_VERSION}/${dir##*/}"
623             cp -a $dir $build_dir
624             # these modules are terrible, and don't all build
625             $MAKE_J "$MAKE_CC" -C $build_dir modules KERNEL_SOURCE="${TOPDIR}/linux"
626         fi
627     done
628 }
629
630 symver()
631 {
632     local file=$1 name=${1%.ko}
633     nm $file \
634     | sed -ne 's,^0*\([0-9a-f]\{8\}\) A __crc_\(.*\),0x\1\t\2\t'"$name"',p'
635 }
636
637 install_kms()
638 {
639     (( $INSTALL_KERNEL )) || return 0
640     (( $LINUX26 )) || return 0
641     set_make
642     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
643     for build_dir in "${TOPDIR}/modules-${FULL_VERSION}/*" ; do
644         [ -d $build_dir ] || continue
645         # these modules are terrible, and don't all build
646         $MAKE "$MAKE_CC" -C $build_dir KERNEL_SOURCE="${TOPDIR}/linux" INSTALL_MOD_PATH="$DESTDIR" 
647     done
648     (   symver vmlinux
649         moddir="${DESTDIR}/lib/modules/${FULL_VERSION}"
650         cd $moddir/kernel
651         for module in $(find * -name '*.ko'); do
652             symver $module
653         done
654         cd $moddir
655         for module in $(find * -path 'kernel/*' -prune -o \
656                            -name '*.ko' -print); do
657             symver $module
658         done
659     ) | sort -u -k2 \
660         | gzip -c9 > "${DESTDIR}/boot/symvers-${VERSION}-${EXTRA_VERSION}-${TARGET_ARCH}${TARGET_CONFIG}.gz"
661 }
662
663 save_headers()
664 {
665     (( $SAVE_HEADERS )) || return 0
666
667     echo "Saving headers for ${TARGET_CONFIG:-up} ${TARGET_ARCH}..."
668     pushd linux >/dev/null
669
670     KVERREL="${VERSION}-${EXTRA_VERSION}"
671     # deal with the kernel headers that are version specific
672     
673     saveddir="$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/${TARGET_ARCH}/${TARGET_CONFIG:-up}"
674     mkdir -p "$saveddir"
675     install -m 644 include/linux/autoconf.h "$saveddir/autoconf.h"
676     install -m 644 include/linux/version.h  "$saveddir/version.h"
677     mv include/linux/modules "$saveddir/"
678     echo ${TARGET_ARCH} ${TARGET_CONFIG} ../../savedheaders/${TARGET_ARCH}/${TARGET_CONFIG:-up}/ \
679         >>  "$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/list"
680     popd >/dev/null
681 }
682
683 save_all_headers()
684 {
685     (( $SAVE_HEADERS )) || return 0
686
687     for arch in $BIGMEM_ARCHS ; do
688         save_headers bigmem $arch
689     done
690
691     for arch in $BOOT_ARCHS ; do
692         save_headers BOOT $arch
693     done
694
695     for arch in $JENSEN_ARCHS ; do
696         save_headers jensen $arch
697     done
698
699     for arch in $SMP_ARCHS ; do
700         save_headers smp $arch
701     done
702
703     for arch in $BIGSMP_ARCHS ; do
704         save_headers bigsmp $arch
705     done
706     for arch in $PSERIES64_ARCHS ; do
707         save_headers pseries64 $arch
708     done
709     for arch in $UP_ARCHS ; do
710         save_headers up $arch
711     done
712 }
713
714 longopts="build,build-lustre,build-kernel,depend-kernel,destdir:,extraversion:"
715 longopts="$longopts,help,install,install-lustre,install-kernel,kerneldir:"
716 longopts="$longopts,save-headers,target:,target-arch:,target-config:,unpack-kernel"
717
718 options=$(getopt -o hj: -l "$longopts" -- "$@")
719
720 eval set -- "$options"
721     
722 while [ "$1" ] ; do
723     case "$1" in
724         '')
725             usage 1
726             ;;
727         --build)
728             BUILD_LUSTRE=1
729             BUILD_KERNEL=1
730             DEPEND_KERNEL=1
731             UNPACK_KERNEL=1
732             shift
733             ;;
734         --build-lustre)
735             BUILD_LUSTRE=1
736             shift
737             ;;
738         --build-kernel)
739             BUILD_KERNEL=1
740             DEPEND_KERNEL=1
741             shift
742             ;;
743         --depend-kernel)
744             DEPEND_KERNEL=1
745             shift
746             ;;
747         --destdir)
748             DESTDIR=$2
749             shift 2
750             ;;
751         --extraversion)
752             EXTRA_VERSION=$2
753             shift 2
754             ;;
755         --help | -h)
756             usage 0
757             ;;
758         --install)
759             INSTALL_LUSTRE=1
760             INSTALL_KERNEL=1
761             shift
762             ;;
763         --install-lustre)
764             INSTALL_LUSTRE=1
765             shift
766             ;;
767         --install-kernel)
768             INSTALL_KERNEL=1
769             shift
770             ;;
771         -j)
772             JOBS=$2
773             shift 2
774             ;;
775         --kerneldir)
776             KERNELDIR=$2
777             shift 2
778             ;;
779         --save-headers)
780             SAVE_HEADERS=1
781             shift
782             ;;
783         --target)
784             TARGET=$2
785             shift 2
786             ;;
787         --target-arch)
788             TARGET_ARCH=$2
789             shift 2
790             ;;
791         --target-config)
792             TARGET_CONFIG=$2
793             shift 2
794             ;;
795         --unpack-kernel)
796             UNPACK_KERNEL=1
797             shift
798             ;;
799         --)
800             shift
801             CONFIGURE_FLAGS=$@
802             break
803             ;; 
804         *)
805             usage 1 "Unrecognized option: $1"
806             ;;
807     esac
808 done
809
810 check_options
811 load_target
812 setup_ccache_distcc
813
814 extract_kernel
815 patch_kernel
816
817 depend_kernel
818 build_kernel
819
820 configure_lustre
821 build_lustre
822
823 build_kms
824
825 install_kernel
826 install_lustre
827
828 install_kms
829
830 cleanup_libmodules
831
832 save_headers
833
834 exit 0