Whamcloud - gitweb
Don't run make dep for 2.6
[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
14 # commands to run
15 BUILD_LUSTRE=0
16 BUILD_KERNEL=0
17 DEPEND_KERNEL=0
18 INSTALL_LUSTRE=0
19 INSTALL_KERNEL=0
20 SAVE_HEADERS=0
21 UNPACK_KERNEL=0
22
23 # provided by target file
24 KERNEL=
25 SERIES=
26 CONFIG=
27 VERSION=
28 EXTRA_VERSION=
29
30 BASE_ARCHS=
31 BIGMEM_ARCHS=
32 BOOT_ARCHS=
33 JENSEN_ARCHS=
34 SMP_ARCHS=
35 BIGSMP_ARCHS=
36 UP_ARCHS=
37
38 RHBUILD=0
39 SUSEBUILD=0
40
41 # flat-out globals
42 TOPDIR=
43 TARGET_FILE=
44 KERNEL_FILE=
45 SERIES_FILE=
46 CONFIG_FILE=
47
48 canon()
49 {
50     pushd $1 >/dev/null
51     echo $PWD
52     popd >/dev/null
53 }
54 TOPDIR=$(canon "${0%%${0##*/}}/..")
55
56 cleanup()
57 {
58     true
59 }
60
61 fatal()
62 {
63     cleanup
64     [ "$2" ] && echo
65     [ "$2" ] && echo "${0##*/}: $2"
66     exit $1
67 }
68
69 list_targets()
70 {
71     echo -n "Available targets:"
72     for target in $TOPDIR/lustre/kernel_patches/targets/*.target ; do
73         target_file=${target##*/}
74         echo -n " ${target_file%%.target}"
75     done
76     echo
77 }
78
79
80 usage()
81 {
82     cat <<EOF
83 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
84
85 Options:
86
87   --build
88     same as --build-kernel --build-lustre --unpack-kernel
89
90   --build-lustre
91     configure and compile lustre.  Requires that --build-kernel was
92     already run.
93
94   --build-kernel
95     configure and compile a kernel.  Implies --depend-kernel.
96     Requires that --unpack-kernel was already run.
97
98   --depend-kernel)
99     Prepares a kernel tree for building (similar to make mrproper
100     oldconfig dep).  Requires that --unpack-kernel was already run.
101
102   --destdir=DESTDIR
103     Root directory to install into (like DESTDIR with auto*).
104
105   --extraversion=EXTRAVERSION
106     Overrides the target kernel\'s EXTRAVERSION text.
107
108   -h, --help
109     Display this message.
110
111   --install
112     same as --install-kernel --install-lustre
113
114   --install-lustre
115     run make install in the Lustre tree.
116
117   --install-kernel
118     install the kernel image and modules.
119
120   -j jobs
121     This works just like the -j option to make, and is passed to make
122     when building.
123
124   --kerneldir=KERNELDIR
125     Directory containing linux source tarballs.
126
127   --target=TARGET
128     Name of the configuration to use.  The available targets are
129     listed below.
130
131   --target-arch=ARCH
132     Specifies an architecture to use when choosing a kernel config
133     file.  Default is i386.
134
135   --target-config=CONFIG
136     Specifies a special option (such as smp, bigsmp, bigmem, or BOOT)
137     to use when choosing a kernel config file.  This also modifies the
138     kernel version and modules directory.
139
140   --unpack-kernel
141     Untars and patches the kernel source.
142
143   The order that commands (--build-lustre, --unpack-kernel) are
144   specified on the command line is ignored; ${0##*/} will always
145   execute them in the correct order (unpack, then build, then install
146   etc.).
147
148 EOF
149     list_targets
150
151     fatal "$1" "$2"
152 }
153
154 check_options()
155 {
156     (( $BUILD_LUSTRE || $BUILD_KERNEL || $DEPEND_KERNEL || \
157             $INSTALL_LUSTRE || $INSTALL_KERNEL || $SAVE_HEADERS || \
158             $UNPACK_KERNEL )) || \
159                 fatal 1 "No commands specified."
160
161     if (( $UNPACK_KERNEL )) ; then
162         [ "$KERNELDIR" ] || \
163             fatal 1 "A kernel directory must be specified with --kerneldir."
164         [ -d "$KERNELDIR" ] || \
165             fatal 1 "$KERNELDIR is not a directory."
166     fi
167
168     if (( $INSTALL_LUSTRE || $INSTALL_KERNEL || $SAVE_HEADERS )) ; then
169         [ -z "$DESTDIR" -o -d "$DESTDIR" ] || \
170             fatal 1 "$DESTDIR is not a directory."
171     fi
172
173     [ "$TARGET" ] || usage 1 "A target must be specified with --target."
174     TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
175     [ -r "$TARGET_FILE" ] || \
176         fatal 1 "Target '$TARGET' was not found.  Try --list-targets."
177
178     if [ -z "$JOBS" -o "$JOBS" -lt "1" ] ; then
179         JOBS=1
180     fi
181 }
182
183 get_lustre_version()
184 {
185     for series in $SERIES ; do
186         SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
187         lustre_patch=$(grep lustre_version "$SERIES_FILE" 2>/dev/null)
188         [ "$lustre_patch" ] && break
189     done
190     [ "$lustre_patch" ] || \
191         fatal 1 "Could not determine Lustre version from $SERIES series."
192
193     awk '/^\+#define LUSTRE_KERNEL_VERSION /{ print $3 }' \
194         "$TOPDIR/lustre/kernel_patches/patches/$lustre_patch" 2>/dev/null
195 }
196
197 load_target()
198 {
199     EXTRA_VERSION_save="$EXTRA_VERSION"
200
201     . "$TARGET_FILE"
202
203     [ "$KERNEL" ] || fatal 1 "Target $TARGET did not specify a kernel."
204 # Suse 2.6 has our patches in already
205 #    [ "$SERIES" ] || fatal 1 "Target $TARGET did not specify a patch series."
206 #    [ "$CONFIG" ] || fatal 1 "Target $TARGET did not specify a kernel config."
207     [ "$VERSION" ] || fatal 1 "Target $TARGET did not specify the kernel version."
208     
209     if [ "$KERNELDIR" ] ; then
210         KERNEL_FILE="$KERNELDIR/$KERNEL"
211         [ -r "$KERNELDIR/$KERNEL" ] || \
212             fatal 1 "Target $TARGET's kernel file $KERNEL not found in kernel directory $KERNELDIR."
213     fi
214
215     if [ "$SERIES" ] ; then
216         for series in $SERIES ; do
217             SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
218             [ -r "$SERIES_FILE" ] || \
219                 fatal 1 "Target $TARGET's series $SERIES missing from $TOPDIR/lustre/kernel_patches/series."
220         done
221     fi
222
223     TARGET_ARCH=${TARGET_ARCH:-$BASE_ARCHS}
224     CONFIG_TARGET="$TARGET-${TARGET_ARCH}${TARGET_CONFIG:+-$TARGET_CONFIG}"
225     CONFIG_FILE="$TOPDIR/lustre/kernel_patches/kernel_configs/kernel-$VERSION-$CONFIG_TARGET.config"
226     [ -r "$CONFIG_FILE" ] ||
227         fatal 1 "Target $TARGET's config file $CONFIG_FILE missing from $TOPDIR/lustre/kernel_patches/configs."
228
229     if [ "$EXTRA_VERSION_save" ] ; then
230         EXTRA_VERSION="$EXTRA_VERSION_save"
231     else
232         EXTRA_VERSION="${EXTRA_VERSION}_lustre.$(get_lustre_version)"
233     fi
234 }
235
236 tarflags()
237 {
238     case "$1" in
239         '')
240             fatal 1 "tarflags(): File name argument missing."
241             ;;
242         *.tar.gz)
243             echo 'zxf'
244             ;;
245         *.tar.bz2)
246             echo 'jxf'
247             ;;
248         *)
249             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
250             ;;
251     esac
252 }
253
254 untar()
255 {
256     echo "Untarring ${1##*/}..."
257     tar $(tarflags $1) $1
258 }
259
260
261 extract_kernel()
262 {
263     (( $UNPACK_KERNEL )) || return 0
264     pushd "$TOPDIR" >/dev/null
265     if [ -d linux ] ; then
266         [ -L linux ] && rm -rf $(readlink linux)
267         rm -rf linux
268     fi
269     untar "$KERNEL_FILE"
270     [ -d linux ] || ln -sf linux* linux
271     popd >/dev/null
272 }
273
274 patch_kernel()
275 {
276     (( $UNPACK_KERNEL )) || return 0
277     [ "$SERIES" ] || return 0
278     pushd "$TOPDIR/linux" >/dev/null
279     for series in $SERIES ; do
280         echo -n "Applying series $series:"
281         SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$series"
282         for patch in $(<"$SERIES_FILE") ; do
283             PATCH_FILE="$TOPDIR/lustre/kernel_patches/patches/$patch"
284             [ -r "$PATCH_FILE" ] || \
285                 fatal 1 "Patch file not found: $patch"
286             echo -n " $patch"
287             patch -s -p1 < "$PATCH_FILE" || fatal 1 "Error applying patch $patch."
288         done
289         echo
290     done
291     popd >/dev/null
292 }
293
294 set_make()
295 {
296     MAKE="make -s"
297     if [ "$CC" ] ; then
298         MAKE="$MAKE CC=$CC"
299     fi
300     if [ "$ARCH" ] ; then
301         MAKE_ARCH="$MAKE ARCH=$ARCH"
302     else
303         case $TARGET_ARCH in
304             i?86)
305                 ;;
306             *)
307                 MAKE_ARCH="$MAKE ARCH=$TARGET_ARCH"
308                 ;;
309         esac
310     fi
311     MAKE_J="$MAKE -j $JOBS"
312 }
313
314 depend_kernel()
315 {
316     (( $DEPEND_KERNEL )) || return 0
317     # we need to override $CC at make time, since there is no
318     # configure
319     set_make
320     pushd "$TOPDIR/linux" >/dev/null
321     echo "Overriding EXTRAVERSION in kernel..."
322     perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -${EXTRA_VERSION}${TARGET_CONFIG}/" Makefile
323     echo "Making depend in $PWD..."
324     $MAKE mrproper || fatal 1 "Error running make mrproper"
325     cp "$CONFIG_FILE" .config
326     for oc in oldconfig_nonint silentoldconfig oldconfig ; do
327         if grep -q "$oc" Makefile ; then
328             $MAKE $oc || fatal 1 "Error running make oldconfig"
329             break
330         fi
331     done
332     case "$VERSION" in
333         2.6*)
334             $MAKE include/asm
335             ;;
336         2.4*)
337             $MAKE symlinks
338             $MAKE dep || fatal 1 "Error running make dep"
339             ;;
340     esac
341     $MAKE include/linux/version.h || fatal 1 "Error making include/linux/version.h"
342 }
343
344 build_kernel()
345 {
346     (( $BUILD_KERNEL )) || return 0
347     set_make
348     echo "Building kernel in $PWD..."
349     case "$TARGET_ARCH" in
350         i386 | i586 | i686 | athlon | x86_64)
351             $MAKE_J bzImage || fatal 1 "Error making bzImage."
352             ;;
353         ppc | ppc64)
354             $MAKE_J vmlinux || fatal 1 "Error making vmlinux."
355             ;;
356         *)
357             $MAKE_J boot || fatal 1 "Error making boot."
358             ;;
359     esac
360     $MAKE_J modules || fatal 1 "Error building modules."
361
362     popd >/dev/null
363 }
364
365 configure_lustre()
366 {
367     (( $BUILD_LUSTRE )) || return 0
368     pushd "$TOPDIR" >/dev/null
369     [ -f Makefile ] && make -s clean
370     [ -f configure ] || sh ./autogen.sh
371     ./configure --with-linux=$PWD/linux $CONFIGURE_FLAGS || \
372         fatal 1 "Error configuring Lustre."
373     popd >/dev/null
374 }
375
376 build_lustre()
377 {
378     (( $BUILD_LUSTRE )) || return 0
379     set_make
380     pushd "$TOPDIR" >/dev/null
381     $MAKE_J || fatal 1 "Error building Lustre."
382     popd >/dev/null
383 }
384
385 install_kernel()
386 {
387     (( $INSTALL_KERNEL )) || return 0
388     set_make
389     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
390     pushd "$TOPDIR/linux" >/dev/null
391     mkdir -p "$DESTDIR/boot"
392
393     install -m 644 System.map "$DESTDIR/boot/System.map-${FULL_VERSION}"
394     # install -m 644 module-info ...
395     install -m 644 "$CONFIG_FILE" "$DESTDIR/boot/config-${FULL_VERSION}"
396
397     mkdir -p "$DESTDIR/dev/shm"
398     mkdir -p "$DESTDIR/lib/modules/${FULL_VERSION}"
399
400     $MAKE INSTALL_MOD_PATH="$DESTDIR" KERNELRELEASE="$FULL_VERSION" \
401         -s modules_install || \
402         fatal 1 "Error installing modules."
403
404     case "$TARGET_ARCH" in
405         i386 | i586 | i686 | athlon)
406             cp arch/i386/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
407             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
408             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
409             ;;
410         x86_64)
411             cp arch/x86_64/boot/bzImage "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
412             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
413             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
414             ;;
415         ppc | ppc64)
416             cp vmlinux "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
417             ln -sf "$DESTDIR/boot/vmlinux-${FULL_VERSION}" "../lib/modules/${FULL_VERSION}/vmlinux" 
418             ;;
419         ia64)
420             gzip -cfv vmlinux > vmlinuz
421             mkdir -p "$DESTDIR/boot/efi/redhat"
422             install -m 755 vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/"
423             install -m 755 vmlinuz "$DESTDIR/boot/efi/redhat/vmlinuz-${FULL_VERSION}"
424             ln -sf "../../../lib/modules/${FULL_VERSION}/vmlinux" "$DESTDIR/boot/efi/redhat/vmlinux-${FULL_VERSION}"
425             ln -sf "efi/redhat/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
426             ln -sf "efi/redhat/vmlinuz-${FULL_VERSION}" "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
427             ;;
428         *)
429             cp vmlinuz "$DESTDIR/boot/vmlinuz-${FULL_VERSION}"
430             cp vmlinux "$DESTDIR/lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}"
431             ln -sf "../lib/modules/${FULL_VERSION}/vmlinux-${FULL_VERSION}" "$DESTDIR/boot/vmlinux-${FULL_VERSION}"
432
433             ;;
434     esac
435     if [ -e init/kerntypes.o ] ; then
436         cp init/kerntypes.o "$DESTDIR/boot/Kerntypes-${FULL_VERSION}"
437     fi
438
439     popd >/dev/null
440 }
441
442 install_lustre()
443 {
444     (( $INSTALL_LUSTRE )) || return 0
445     set_make
446     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
447     pushd "$TOPDIR" >/dev/null
448     $MAKE -s install "DESTDIR=$DESTDIR" KERNELRELEASE="$FULL_VERSION" || fatal 1 "Error installing Lustre."
449     popd >/dev/null
450 }
451
452 build_kms()
453 {
454     (( $BUILD_KERNEL )) || return 0
455     (( $SUSEBUILD )) || return 0
456     set_make
457     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
458     mkdir -p "${TOPDIR}/modules-${FULL_VERSION}"
459     for dir in /usr/src/kernel-modules/* ; do
460         # we are replacing lustre-lite, so don't include it
461         if [ ${dir##*/} != "lustre-lite" -a -e $dir/Makefile ]; then
462             build_dir="${TOPDIR}/modules-${FULL_VERSION}/${dir##*/}"
463             cp -a $dir $build_dir
464             # these modules are terrible, and don't all build
465             $MAKE_J -C $build_dir modules KERNEL_SOURCE="${TOPDIR}/linux"
466         fi
467     done
468 }
469
470 symver()
471 {
472     local file=$1 name=${1%.ko}
473     nm $file \
474     | sed -ne 's,^0*\([0-9a-f]\{8\}\) A __crc_\(.*\),0x\1\t\2\t'"$name"',p'
475 }
476
477 install_kms()
478 {
479     (( $INSTALL_KERNEL )) || return 0
480     (( $SUSEBUILD )) || return 0
481     set_make
482     FULL_VERSION="${VERSION}-${EXTRA_VERSION}${TARGET_CONFIG}"
483     for build_dir in "${TOPDIR}/modules-${FULL_VERSION}/*" ; do
484         [ -d $build_dir ] || continue
485         # these modules are terrible, and don't all build
486         $MAKE -C $build_dir KERNEL_SOURCE="${TOPDIR}/linux" INSTALL_MOD_PATH="$DESTDIR" 
487     done
488     (   symver vmlinux
489         moddir="${DESTDIR}/lib/modules/${FULL_VERSION}"
490         cd $moddir/kernel
491         for module in $(find * -name '*.ko'); do
492             symver $module
493         done
494         cd $moddir
495         for module in $(find * -path 'kernel/*' -prune -o \
496                            -name '*.ko' -print); do
497             symver $module
498         done
499     ) | sort -u -k2 \
500         | gzip -c9 > "${DESTDIR}/boot/symvers-${VERSION}-${EXTRA_VERSION}-${TARGET_ARCH}${TARGET_CONFIG}.gz"
501 }
502
503 save_headers()
504 {
505     echo "Saving headers for $1 $2..."
506     pushd linux >/dev/null
507
508     KVERREL="${VERSION}-${EXTRA_VERSION}"
509     # deal with the kernel headers that are version specific
510     
511     saveddir="$RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/$2/$1"
512     mkdir -p "$saveddir"
513     install -m 644 include/linux/autoconf.h "$saveddir/autoconf.h"
514     install -m 644 include/linux/version.h  "$saveddir/version.h"
515     mv include/linux/modules "$saveddir/"
516     echo $2 $1 ../../savedheaders/$2/$1/ >>  $RPM_BUILD_ROOT/usr/src/linux-${KVERREL}/savedheaders/list
517     popd >/dev/null
518 }
519
520 save_all_headers()
521 {
522     (( $SAVE_HEADERS )) || return 0
523
524     for arch in $BIGMEM_ARCHS ; do
525         save_headers bigmem $arch
526     done
527
528     for arch in $BOOT_ARCHS ; do
529         save_headers BOOT $arch
530     done
531
532     for arch in $JENSEN_ARCHS ; do
533         save_headers jensen $arch
534     done
535
536     for arch in $SMP_ARCHS ; do
537         save_headers smp $arch
538     done
539
540     for arch in $BIGSMP_ARCHS ; do
541         save_headers bigsmp $arch
542     done
543     for arch in $UP_ARCHS ; do
544         save_headers up $arch
545     done
546 }
547
548 longopts="build,build-lustre,build-kernel,depend-kernel,destdir:,extraversion:"
549 longopts="$longopts,help,install,install-lustre,install-kernel,kerneldir:"
550 longopts="$longopts,save-headers,target:,target-arch:,target-config:,unpack-kernel"
551
552 options=$(getopt -o hj: -l "$longopts" -- "$@")
553
554 eval set -- "$options"
555     
556 while [ "$1" ] ; do
557     case "$1" in
558         '')
559             usage 1
560             ;;
561         --build)
562             BUILD_LUSTRE=1
563             BUILD_KERNEL=1
564             DEPEND_KERNEL=1
565             UNPACK_KERNEL=1
566             shift
567             ;;
568         --build-lustre)
569             BUILD_LUSTRE=1
570             shift
571             ;;
572         --build-kernel)
573             BUILD_KERNEL=1
574             DEPEND_KERNEL=1
575             shift
576             ;;
577         --depend-kernel)
578             DEPEND_KERNEL=1
579             shift
580             ;;
581         --destdir)
582             DESTDIR=$2
583             shift 2
584             ;;
585         --extraversion)
586             EXTRA_VERSION=$2
587             shift 2
588             ;;
589         --help | -h)
590             usage 0
591             ;;
592         --install)
593             INSTALL_LUSTRE=1
594             INSTALL_KERNEL=1
595             shift
596             ;;
597         --install-lustre)
598             INSTALL_LUSTRE=1
599             shift
600             ;;
601         --install-kernel)
602             INSTALL_KERNEL=1
603             shift
604             ;;
605         -j)
606             JOBS=$2
607             shift 2
608             ;;
609         --kerneldir)
610             KERNELDIR=$2
611             shift 2
612             ;;
613         --save-headers)
614             SAVE_HEADERS=1
615             shift
616             ;;
617         --target)
618             TARGET=$2
619             shift 2
620             ;;
621         --target-arch)
622             TARGET_ARCH=$2
623             shift 2
624             ;;
625         --target-config)
626             TARGET_CONFIG=$2
627             shift 2
628             ;;
629         --unpack-kernel)
630             UNPACK_KERNEL=1
631             shift
632             ;;
633         --)
634             shift
635             CONFIGURE_FLAGS=$@
636             break
637             ;; 
638         *)
639             usage 1 "Unrecognized option: $1"
640             ;;
641     esac
642 done
643
644 check_options
645 load_target
646
647 extract_kernel
648 patch_kernel
649
650 depend_kernel
651 build_kernel
652
653 configure_lustre
654 build_lustre
655
656 build_kms
657
658 install_kernel
659 install_lustre
660
661 install_kms
662
663 save_all_headers
664
665 exit 0