Whamcloud - gitweb
594d7779b194a8c744245e71e426861eedbc259d
[fs/lustre-release.git] / build / lbuild
1 #!/bin/bash
2
3 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
4
5 #set -x
6 shopt -s extdebug
7
8 TOPDIR=$PWD
9
10 # CVSROOT is inherited from the environment
11 KERNELDIR=
12 LINUX=
13 LUSTRE=
14 RELEASE=false
15 # XXX - some recent hacking has pretty much neutered this option.
16 #       search through this file (and lbuild.old_school -- but that will
17 #       be going away soon) for "-bb" and see how many places
18 #       simply don't account for this option
19 DO_SRC=false
20 DOWNLOAD=true
21 TAG=
22 CANONICAL_TARGET=
23 TARGET=
24 TARGET_ARCH=$(uname -m)
25 TARGET_ARCHS=
26 TARGET_ARCHS_ALL=$TARGET_ARCH
27 [ "$TARGET_ARCH" = "i686" ] && TARGET_ARCHS_ALL="i686 i586 i386"
28 CONFIGURE_FLAGS=
29 EXTERNAL_PATCHES=
30 EXTRA_VERSION=
31 LUSTRE_EXTRA_VERSION=
32 STAGEDIR=
33 TMPDIR=${TMPDIR:-"/var/tmp"}
34 TIMESTAMP=
35 # XXX - i think these two parameters/arguments/variables need to be
36 #       cleaned up and merged.  they effectively do the same thing
37 REUSERPM=
38 REUSEBUILD=
39 # what does this do exactly?  does it imply no kernel build?
40 NORPM=false
41 LDISKFSRPM=true
42 SKIPLDISKFSRPM="v1_4_* b1_4"
43 SMPTYPES="smp bigsmp default ''"
44 KERNCONFSMPTYPE=
45 PATCHLESS=false
46 XEN=false
47 LINUXOBJ=
48 DISTRO=
49 KERNELTREE=
50
51 # patchless build
52 KERNELRPMSBASE=
53 RPMSMPTYPE=
54
55 # from target file
56 SERIES=
57 BASE_ARCHS=
58 BIGMEM_ARCHS=
59 BOOT_ARCHS=
60 JENSEN_ARCHS=
61 SMP_ARCHS=
62 BIGSMP_ARCHS=
63 PSERIES64_ARCHS=
64 UP_ARCHS=
65
66 # not in the target file any more
67 CONFIG=
68
69 DATE=$(date)
70
71 USE_DATESTAMP=1
72 RPMBUILD=
73
74 OLD_SCHOOL=false
75
76 export CC=${CC:-gcc}
77
78 # Readlink is not present on some older distributions: emulate it.
79 readlink() {
80     local path=$1 ll
81
82     if [ -L "$path" ]; then
83         ll="$(LC_ALL=C ls -l "$path" 2> /dev/null)" &&
84         echo "${ll/* -> }"
85     else
86         return 1
87     fi
88 }
89
90 cleanup() {
91
92     true
93 }
94
95 error() {
96     local msg="$1"
97
98     [ -n "$msg" ] && echo -e "\n${0##*/}: $msg" >&2
99
100 }
101
102 fatal() {
103
104     cleanup
105     error "$2"
106     exit $1
107
108 }
109
110 usage() {
111     cat <<EOF
112 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
113
114   -d CVSROOT
115     Specifies the CVS Root to use when pulling files from CVS.  The
116     environment variable \$CVSROOT is used if this option is not
117     present.
118
119   --external-patches=EXTERNAL_PATCHES
120     Directory similar to lustre/lustre/kernel_patches/ that lbuild should
121     look for seres and config files in before looking in the lustre
122     tree.
123
124   --extraversion=EXTRAVERSION
125     Text to use for the rpm release and kernel extraversion.
126
127   --timestamp=TIMESTAMP
128     Date of building lustre in format YYYYMMDDhhmmss
129
130   --reuserpm=DIR
131     Try to reuse old kernel RPMs from DIR
132
133   --reusebuild=DIR
134     Try to reuse old kernel builds from DIR
135
136   --kernelrpm=DIR
137     Path to distro kernel RPM collection
138
139   --ccache
140     Use ccache
141
142   --norpm
143     Do not build RPMs (compile only mode)
144
145   --patchless
146     Build lustre client only
147
148   --distro=DISTRO
149     Which distro using. Autodetect by default
150
151   --kerneldir=KERNELDIR
152     Directory containing Linux source tarballs referenced by target
153     files.
154
155   --kerneltree=KERNELTREE
156     Directory containing dirs with Linux source tarballs referenced by target
157     files. Dir names in format kernel version ('2.6.9', etc.)
158
159   --linux=LINUX --with-linux=LINUX
160     Directory of Linux kernel sources.  When this option is used, only
161     Lustre modules and userspace are built.
162
163   --lustre=LUSTRE
164     Path to an existing lustre source tarball to use instead of
165     pulling from CVS.
166
167   --nodownload
168     Do not try to download a kernel from downloads.lustre.org
169
170   --nosrc
171     Do not build a .src.rpm, a full kernel patch, or a patched kernel
172     tarball.
173
174   --ldiskfs
175     Do ldiskfs RPM. Now true by default
176
177   --publish
178     Unused.
179
180   --release
181     Specifies that the files generated do not include timestamps, and
182     that this is an official release.
183
184   --src
185     Build a .src.rpm, a full kernel patch, and a patched kernel tarball.
186
187   --stage=DIR
188     Directory used to stage packages for release.  RPMs will be placed
189     more or less in DIR/<target>-<arch>, and the tarball will be
190     placed in DIR.
191
192   --tag=TAG
193     A CVS branch/tag name to build from when pulling from CVS.
194
195   --target=TARGET
196     The name of the target to build.  The available targets are listed
197     below.
198
199   --target-archs=TARGET_ARCHS
200     A (space delimited) list of architectures to build.  By default,
201     all of the archs supported by the TARGET will be built, in
202     addition to a .src.rpm.  This option can limit those, for machines
203     that can only build certain archs or if you only want a certain
204     arch built (for testing, or a one-off kernel).
205
206     Also note that by using a non-"base" arch (eg, i386) only kernels
207     will be built - there will be no lustre-lite-utils package.
208
209   --disable-datestamp
210     Prevents the datestamp flag (-D) from being passed to cvs for
211     checkouts. This is a workaround for a problem encountered when
212     using lbuild with tinderbox.
213
214   --xen
215     Builds a Xen domX kernel.
216
217 EOF
218
219 #   list_targets
220
221     fatal "$1" "$2"
222 }
223
224 # canonicalize a relative path
225 canon_path() {
226     local PATH="$1"
227
228     if [ ! -d "$PATH" ]; then
229         return 1
230     fi
231
232     pushd "$PATH" >/dev/null || return 1
233     local CANONPATH=$PWD
234     popd >/dev/null
235
236     echo "$CANONPATH"
237     return 0
238 }
239
240 check_options() {
241
242     if [ "$LUSTRE" ]; then
243         [ -r "$LUSTRE" ] || \
244             usage 1 "Could not find Lustre source tarball '$LUSTRE'."
245     else
246         [ "$CVSROOT" ] || \
247             usage 1 "Either specify a CVS Root with -d, or a Lustre source tarball with --lustre."
248         [ "$TAG" ] || \
249             usage 1 "A branch/tag name must be specified with --tag when not building from a tarball."
250     fi
251
252     if [ -z "$LINUX" ]; then
253         [ "$KERNELDIR" -o "$KERNELTREE" ] || \
254             usage 1 "A kernel directory must be specified with --kerneldir or --kerneltree."
255
256         [ -d "$KERNELDIR" -o -d "$KERNELTREE" ] || \
257             usage 1 "$KERNELDIR and $KERNELTREE are not a directory."
258
259         if ! $RELEASE; then
260             [ "$TAG" ] || \
261                 usage 1 "When building a snapshot, a tag name must be used."
262         fi
263
264         [ "$TARGET" ] || usage 1 "A target must be specified with --target."
265 #       TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
266 #       [ -r "$TARGET_FILE" ] || \
267 #               usage 1 "Target '$TARGET' was not found."
268     fi
269
270     case $TARGET in
271         2.6-rhel5)
272             CANONICAL_TARGET="rhel5"
273             ;;
274         2.6-rhel4)
275             CANONICAL_TARGET="rhel-2.6"
276             ;;
277         2.6-suse)
278             CANONICAL_TARGET="sles-2.6"
279             ;;
280         2.6-sles10)
281             CANONICAL_TARGET="sles10-2.6"
282             ;;
283         hp_pnnl-2.4)
284             CANONICAL_TARGET="hp-pnnl-2.4"
285             ;;
286         2.6-vanilla \
287             | suse-2.4.21-2 \
288             | rh-2.4 \
289             | rhel-2.4 \
290             | sles-2.4 \
291             | 2.6-patchless)
292                 CANONICAL_TARGET="$TARGET"
293                 ;;
294     esac
295
296     local timestampnodig=$(echo $TIMESTAMP | sed -e s/[0-9]*//g)
297     [ "$timestampnodig" = "" ] || TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M%S")
298     local timestamplength="${#TIMESTAMP}"
299     if [ $timestamplength -eq 12 ]; then
300         TIMESTAMP="${TIMESTAMP}00"
301     elif [ $timestamplength -ne 14 ]; then
302         TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M%S")
303     fi
304
305     RPMBUILD=$(which rpmbuild 2>/dev/null | head -1)
306     if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
307         RPMBUILD=$(which rpm 2>/dev/null | head -1)
308         if [ ! "$RPMBUILD" -o "$RPMBUILD" == "" ]; then
309             usage 1 "Could not find binary for making rpms (tried rpmbuild and rpm)."
310         fi
311     fi
312
313     if [ -n "$CCACHE" ]; then
314         which "$DISTCC" 2>/dev/null && export DISTCC RPM_BUILD_NCPUS
315
316         if which "$CCACHE" 2>/dev/null; then
317             local ccache=$(which "$CCACHE")
318             local bindir="$TOPDIR/bin"
319
320             [ -d $bindir ] || mkdir -p $bindir
321             if [ -d $bindir ]; then
322                 rm ${bindir}/* > /dev/null 2>&1
323                 ln -s "$ccache" ${bindir}/ccache
324                 ln -s "$ccache" ${bindir}/cc
325                 ln -s "$ccache" ${bindir}/$CC
326                 export PATH=$bindir:$PATH
327             fi
328             export CCACHE && export CC="ccache $CC"
329             # zero the cache so we can see how effective we are being with it
330             ccache -z
331         fi
332     fi
333
334     [ -z "$DISTRO" ] && DISTRO=$(autodetect_distro)
335 }
336
337 # autodetect used Distro
338 autodetect_distro() {
339
340     local name
341     local version
342
343     if [ -f /etc/SuSE-release ]; then
344         name=sles
345         version=$(grep ^VERSION /etc/SuSE-release)
346         version=${version#*= }
347     elif [ -f /etc/redhat-release ]; then
348         name=$(head -1 /etc/redhat-release)
349         version=$(echo "$distroname" |
350                   sed -e 's/^[^0-9.]*//g' | sed -e 's/[ \.].*//')
351     fi
352     if [ -z "$name" -o -z "$version" ]; then
353         fatal 1 "I don't know how to determine distro type/version.\n" \
354                 "Either update autodetect_distro() or use the --distro argument"
355     fi
356
357     echo ${name}${version}
358     return 0
359
360 }
361
362 uniqify() {
363
364     echo $(echo "$*" | xargs -n 1 | sort -u)
365
366 }
367
368 download_srpm() {
369     local target=$1
370     local srpm=$2
371
372     echo "Downloading http://downloads.lustre.org/public/kernels/$target/old/$srpm..."
373     if ! wget -nv "http://downloads.lustre.org/public/kernels/$target/old/$srpm" \
374         -O "$KERNELDIR/$srpm"; then
375         fatal 1 "Could not download target $target's kernel SRPM $srpm from downloads.lustre.org."
376     fi
377     [ -s "$KERNELDIR/$srpm" ] || {
378         rm -rf $KERNELDIR/$srpm
379         fatal 1 "Could not download target $target's kernel SRPM $srpm from downloads.lustre.org."
380     }
381
382 }
383
384 download_ofed() {
385
386     if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ] && \
387        [ ! -r "$KERNELTREE/OFED-${OFED_VERSION}.tgz" ]; then
388         if $DOWNLOAD; then
389             local location="http://downloads.lustre.org/public/OFED/"
390             echo "Downloading $location/OFED-${OFED_VERSION}.tgz..."
391             if ! wget -nv "$location/OFED-${OFED_VERSION}.tgz" \
392                 -O "$KERNELTREE/OFED-${OFED_VERSION}.tgz"; then
393                 fatal 1 "Could not download OFED-${OFED_VERSION}.tgz" \
394                         "from downloads.lustre.org."
395             fi
396         else
397             fatal 1 "OFED-${OFED_VERSION}.tgz not found in kernel" \
398                     "directory $KERNELTREE."
399         fi
400     fi
401
402 }
403
404 load_target() {
405
406     EXTRA_VERSION_save="$EXTRA_VERSION"
407     for patchesdir in "$EXTERNAL_PATCHES" \
408                       "$TOPDIR/lustre/lustre/kernel_patches"; do
409         TARGET_FILE="$patchesdir/targets/$TARGET.target"
410         [ -r "$TARGET_FILE" ] && break
411     done
412     [ -r "$TARGET_FILE" ] || fatal 1 "Target $TARGET was not found."
413
414     echo "Loading target config file $TARGET.target..."
415
416     . "$TARGET_FILE"
417
418     # doesn't make any sense to build OFED for xen domX's
419     if $XEN; then
420         OFED_VERSION=""
421     fi
422
423     # XXX - set_rpm_smp_type is an ugly undeterministic hack.  it needs to
424     #       go away and the target just specify the $RPMSMPTYPE
425     [ -z "$RPMSMPTYPE" ] && set_rpm_smp_type
426
427     # CC might have been overwriten in TARGET_FILE
428     if [[ $CC != ccache\ * ]] && which "$CCACHE" 2>/dev/null; then
429         export CCACHE && export CC="ccache $CC"
430     fi
431
432     if [ ! "$KERNELTREE" = "" ] && [ -d "$KERNELTREE" ]; then
433         KERNELDIR="$KERNELTREE/${lnxmaj}"
434         [ -d "$KERNELDIR" ] || mkdir "$KERNELDIR"
435     fi
436
437     # verify the series is available
438     if [ "$SERIES" ]; then
439         for series in $SERIES; do
440             for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches"; do
441                 [ -r "$patchesdir/series/$series" ] && continue 2
442             done
443             fatal 1 "Target $TARGET's series $SERIES could not be" \
444                     "found.\nSearched:\n\t$EXTERNAL_PATCHES/series\n" \
445                     "\t$TOPDIR/lustre/lustre/kernel_patches/series."
446         done
447     fi
448
449     # set the location of the .config file
450     local XENPOSTFIX=""
451     if $XEN; then
452         XENPOSTFIX="-xen"
453     fi
454     if [ -f $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET-$TARGET_ARCH.config ]; then
455         CONFIG_FILE="$TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET$XENPOSTFIX-$TARGET_ARCH.config"
456     fi
457
458     # figure out our smp kernel type and set the .config if we have one
459     local smptype
460     for smptype in $SMPTYPES; do
461         [ "$smptype" = "''" ] && smptype=
462         if [ -f $TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET-$TARGET_ARCH-${smptype}.config ]; then
463             CONFIG_FILE="$TOPDIR/lustre/lustre/kernel_patches/kernel_configs/kernel-$lnxmaj-$TARGET$XENPOSTFIX-$TARGET_ARCH-${smptype}.config"
464             KERNCONFSMPTYPE=$smptype
465     fi
466     done
467
468     local lnxrelnew=${lnxrel//-/_}
469
470     # remember the EXTRA_VERSION before we diddle it here
471     # XXX - we really should not diddle with any values read in from the
472     #       target file.  if we want to modify a value, we should create
473     #       a new variable.
474     PRISTINE_EXTRA_VERSION=$EXTRA_VERSION
475
476     if ! $PATCHLESS && [ ! -f "$CONFIG_FILE" ]; then
477         fatal 1 "Config file for target $TARGET missing from" \
478                 "$TOPDIR/lustre/lustre/kernel_patches/kernel_configs/."
479     fi
480
481     if [ "$EXTRA_VERSION_save" ]; then
482         EXTRA_VERSION="$EXTRA_VERSION_save"
483     elif ! $RELEASE; then
484         # if there is no patch series, then this is not a lustre specific
485         # kernel.  don't make it look like one
486         if [ -n "$SERIES" ]; then
487             #remove the @VERSION@ (lustre version)
488 #            EXTRA_VERSION=$(echo $EXTRA_VERSION | sed -e "s/\(.*_lustre\)\..*/\1/")
489 #            EXTRA_VERSION="${EXTRA_VERSION}-${TAG}.${TIMESTAMP}"
490             ! ( $PATCHLESS ) && EXTRA_VERSION="${EXTRA_VERSION}.${TIMESTAMP}"
491         fi
492     fi
493     # EXTRA_VERSION=${EXTRA_VERSION//-/_}
494
495     ALL_ARCHS="$BASE_ARCHS $BIGMEM_ARCHS $BOOT_ARCHS $JENSEN_ARCHS $SMP_ARCHS $BIGSMP_ARCHS $PSERIES64_ARCHS $UP_ARCHS"
496
497     BUILD_ARCHS=
498     for arch in $(uniqify "$ALL_ARCHS"); do
499         if [ -z "$TARGET_ARCHS" ] ||
500            [[ \ $TARGET_ARCHS\  = *\ $arch\ * ]]; then
501             BUILD_ARCHS="$BUILD_ARCHS $arch"
502         fi
503     done
504     [ "$BUILD_ARCHS" ] || usage 1 "No available target archs to build."
505     echo "Building for: $BUILD_ARCHS"
506 }
507
508 tarflags() {
509     local file="$1"
510
511     case "$file" in
512         '')
513             fatal 1 "tarflags(): File name argument missing."
514             ;;
515         *.tar.gz | *.tgz)
516             echo 'zxf'
517             ;;
518         *.tar.bz2)
519             echo 'jxf'
520             ;;
521         *.tar)
522             echo 'xf'
523             ;;
524         *)
525             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
526             ;;
527     esac
528
529 }
530
531 untar() {
532     local file="$1"
533
534     echo "Untarring ${file##*/}..."
535     tar $(tarflags "$file") "$file"
536
537 }
538
539 unpack_ofed() {
540
541     untar "$KERNELTREE/OFED-${OFED_VERSION}.tgz" || fatal 1 "Error unpacking OFED tarball"
542     [ -d OFED ] || ln -sf OFED-[0-9].[0-9]* OFED
543
544 }
545
546 unpack_lustre() {
547
548     if [ -z "$LUSTRE" ]; then
549         local DATESTAMP=""
550
551         if [ -n "$USE_DATESTAMP" ]; then
552             DATESTAMP="-D '$DATE'"
553         fi
554
555         local DIRNAME="lustre-$TAG-$TIMESTAMP"
556
557         cvs -d "$CVSROOT" -qz3 co $DATESTAMP -d "$DIRNAME" lustre || \
558             fatal 1 "There was an error checking out toplevel Lustre from CVS."
559         pushd "$DIRNAME" > /dev/null
560         ./lustrecvs "$TAG" || \
561             fatal 1 "There was an error checking out Lustre/Portals/Build from CVS."
562         echo "Creating lustre tarball..."
563         sh autogen.sh || fatal 1 "There was an error running autogen.sh."
564         ./configure --disable-{modules,utils,liblustre,tests,doc} || \
565             fatal 1 "There was an error running ./configure to create makefiles."
566         make dist || fatal 1 "There was an error running 'make dist'."
567         LUSTRE=$PWD/lustre-*.tar.gz
568         popd > /dev/null
569     fi
570
571     untar "$LUSTRE" || fatal 1 "Error unpacking Lustre tarball"
572     [ -d lustre ] || ln -sf lustre-[0-9].[0-9]* lustre
573
574 }
575
576 do_patch_linux() {
577
578     local do_patch=${1:-true}
579
580     FULL_PATCH="$PWD/lustre-kernel-${TARGET}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}.patch"
581     [ -f "$FULL_PATCH" ] && rm -f "$FULL_PATCH"
582     $do_patch && pushd linux >/dev/null
583     for series in $SERIES; do
584         echo -n "Applying series $series:"
585         for patchesdir in "$EXTERNAL_PATCHES" "$TOPDIR/lustre/lustre/kernel_patches"; do
586             [ -r "$patchesdir/series/$series" ] || continue
587             SERIES_FILE="$patchesdir/series/$series"
588             for patch in $(<"$SERIES_FILE"); do
589                 echo -n " $patch"
590                 PATCH_FILE="$patchesdir/patches/$patch"
591                 [ -r "$PATCH_FILE" ] || \
592                     fatal 1 "Patch $patch does not exist in Lustre tree."
593                 cat "$PATCH_FILE" >> "$FULL_PATCH" || {
594                     rm -f $FULL_PATCH
595                     fatal 1 "Error adding patch $patch to full patch."
596                 }
597                 if $do_patch; then
598                     patch -s -p1 < "$PATCH_FILE" || {
599                         rm -f $FULL_PATCH
600                         fatal 1 "Error applying patch $patch."
601                     }
602                 fi
603             done
604             break
605         done
606         echo
607     done
608     $do_patch && popd >/dev/null
609     echo "Full patch has been saved in ${FULL_PATCH##*/}."
610
611 }
612
613 build_lustre() {
614
615     cp "$LUSTRE" SOURCES
616
617     pushd lustre >/dev/null
618
619     echo "Building Lustre RPMs for: $BUILD_ARCHS..."
620     targets=
621     for arch in $BUILD_ARCHS; do
622         targets="--target $arch $targets"
623     done
624
625     local confoptions="--with-linux=${LINUX}"
626     if $PATCHLESS; then
627         confoptions="--with-linux=${LINUX} --disable-server"
628     fi
629     if [ ! "$LINUXOBJ" = "" ]; then
630         confoptions="$confoptions --with-linux-obj=${LINUXOBJ}"
631     fi
632
633     ./configure $confoptions ${CONFIGURE_FLAGS}
634     if [ "$?" != "0" ]; then
635         local saved_config="../config.log.$(date +%s)"
636         cp config.log $saved_config
637         chmod a+r $saved_config
638         echo "Saved config.log is at $saved_config"
639         popd
640         return 255
641     fi
642
643     gen_lustre_version
644
645     # hack. Somebody move build/lustre.spec to lustre.spec for b1_6
646     local lustre_spec
647     [ -f lustre.spec ] && lustre_spec=lustre.spec
648     [ -f build/lustre.spec ] && lustre_spec=build/lustre.spec
649
650     [ -f "$lustre_spec" ] && sed \
651         -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
652         < $lustre_spec \
653         > ../lustre.spec
654
655     local rpmbuildopt='-bb'
656     if $NORPM; then
657         rpmbuildopt='-bc'
658         echo NORPM mode. Only compiling.
659     fi
660
661     # convert the $PATCHLESS boolean to an empty/no-empty boolean
662     # as silly as this seems, it makes the syntax of the rpmbuild command
663     # simpler and not need an eval to deal with the quotes in the quotes 
664     local is_patchless=""
665     if $PATCHLESS; then
666         is_patchless="yes"
667     fi
668     $RPMBUILD $targets $rpmbuildopt ../lustre.spec \
669         ${is_patchless:+--define "lustre_name lustre-client"} \
670         --define "_tmppath $TMPDIR" \
671         --define "_topdir $TOPDIR" || \
672         fatal 1 "Error building rpms for $BUILD_ARCHS."
673
674     popd >/dev/null
675     ( $(skeep_ldiskfs_rpm $TAG) ) && return
676
677     pushd lustre/ldiskfs || return 255
678     make dist
679     if [ "$?" != "0" ]; then
680         popd
681         return 255
682     fi
683     cp lustre-ldiskfs*.tar.gz $TOPDIR/SOURCES
684
685     gen_lustre_version
686
687     local ldiskfs_spec=lustre-ldiskfs.spec
688     [ -f "$ldiskfs_spec" ] && sed \
689     -e "s^Release: .*$^Release: $LUSTRE_EXTRA_VERSION^" \
690     < $ldiskfs_spec \
691     > ../lustre-ldiskfs.spec
692
693     $RPMBUILD $targets $rpmbuildopt ../lustre-ldiskfs.spec \
694         --define "_tmppath /var/tmp" \
695         --define "_topdir $TOPDIR"
696     if [ "$?" != "0" ]; then
697         popd
698         return 255
699     fi
700
701     if $DO_SRC; then
702             $RPMBUILD -bs ../lustre-ldiskfs.spec \
703             --define "_tmppath /var/tmp" \
704             --define "_topdir $TOPDIR"
705         if [ "$?" != "0" ]; then
706             popd
707             return 255
708         fi
709     fi
710     popd
711
712 }
713
714 stage() {
715
716     [ "$STAGEDIR" ] || return 0
717
718     for arch in $BUILD_ARCHS; do
719         rpmdir="${STAGEDIR}/${CANONICAL_TARGET}-${arch}"
720         echo "${0##*/}: Copying RPMs into ${rpmdir}"
721         mkdir -p "${rpmdir}"
722         cp -v RPMS/${arch}/*.rpm "${rpmdir}"
723         if [ -d RPMS/noarch ]; then
724             cp -v RPMS/noarch/*.rpm "${rpmdir}"
725         fi
726     done
727
728     cp -v "$LUSTRE" "$STAGEDIR"
729 }
730
731 #check if we need to build separate ldiskfs RPM
732 skeep_ldiskfs_rpm() {
733         local tag="$1"
734
735         local skip=false
736
737         if ! $LDISKFSRPM; then
738             skip=true
739         elif $PATCHLESS; then
740             skip=true
741         else
742             for skiptag in $SKIPLDISKFSRPM; do
743                 [[ $tag == $skiptag ]] && skip=true && break
744             done
745         fi
746         echo $skip
747
748 }
749
750 #generate LUSTRE_EXTRA_VERSION from EXTRA_VERSION
751 gen_lustre_version() {
752
753     LUSTRE_EXTRA_VERSION="${lnxmaj}${EXTRA_VERSION_DELIMITER}${EXTRA_VERSION}${TARGET_DELIMITER}${RPMSMPTYPE:-${KERNCONFSMPTYPE:-"smp"}}"
754     LUSTRE_EXTRA_VERSION=${LUSTRE_EXTRA_VERSION//-/_}
755
756 }
757
758 set_rpm_smp_type() {
759
760     local infact_arch="${TARGET_ARCH}"
761
762     RPMSMPTYPE=default
763     [ "$infact_arch" == "i586" ] && infact_arch="i686"
764
765     local smp_type
766     for smp_type in $SMP_ARCHS; do
767         [ $infact_arch == $smp_type ] && RPMSMPTYPE=smp && break
768     done
769
770     for smp_type in $BIGSMP_ARCHS; do
771         [ $infact_arch == $smp_type ] && RPMSMPTYPE=bigsmp && break
772     done
773
774 }
775
776 # This function takes a linux source pool and digs out the linux release
777 # from it
778 find_linux_release() {
779     local SRCDIR="$1"
780
781     local LINUXRELEASEHEADER=$SRCDIR/include/linux/version.h
782     if [ -s $SRCDIR/include/linux/utsrelease.h ]; then
783         LINUXRELEASEHEADER=$SRCDIR/include/linux/utsrelease.h
784     fi
785
786     sed -ne 's/#define UTS_RELEASE "\(.*\)"$/\1/p' $LINUXRELEASEHEADER
787
788 }
789
790 find_linux_rpm() {
791     local prefix="$1"
792     local delimiter=${2:-"-"}
793
794     local pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
795     [ -d $pathtorpms ] || return 255
796
797     local kernelbinaryrpm rpmfile
798     local wanted_kernel="${lnxmaj}${delimiter}${lnxrel}"
799
800     local arch ret=1
801     for arch in $TARGET_ARCHS_ALL; do
802         local found_rpm="" rpm
803         for rpm in ${pathtorpms}/${arch}/*.rpm; do
804             if rpm -q --provides -p "$rpm" | grep -q "kernel${prefix} = $wanted_kernel"; then
805                 found_rpm="$rpm"
806                 ret=0
807                 break
808             fi
809         done
810         [ -f "$found_rpm" ] && TARGET_ARCH="$arch" && BUILD_ARCHS="$arch" && break
811     done
812
813     echo "$found_rpm"
814     return $ret
815
816 }
817
818 #unpack kernel(/source/devel) RPM
819 #
820 # This function and it's setting of $LINUX and $LINUXOBJ is a total hack that
821 # needs to completely refactored.  It completely ingores that $BUILD_ARCHS may
822 # contain a list of arches for which rpmbuild commands (including the one for
823 # lustre itself)
824 unpack_linux_rpm() {
825     local kernelrpm="${1}"
826     local delimiter=${2:-"-"}
827
828     [ -f "$kernelrpm" ] || return 255
829     [ -d $TOPDIR/reused ] || mkdir $TOPDIR/reused
830
831     pushd $TOPDIR/reused || return 255
832
833     local RC=0
834
835     rpm2cpio < "$kernelrpm" | cpio -id > /dev/null 2>&1
836     if [ ${PIPESTATUS[0]} -eq 0 ]; then
837         # RHEL-style and SLES-style rpms
838         local paths="kernels/${lnxmaj}${delimiter}${lnxrel}-${TARGET_ARCH} linux-${lnxmaj}${delimiter}${lnxrel}"
839
840         local path
841         for path in $paths; do
842             local src='usr/src'
843
844             if [ -d "$src/$path/" ]; then
845                 LINUX="$(pwd)/$src/$path"
846             fi
847             # SLES has a separate -obj tree
848             if [ -d "$src/${path}-obj" ]; then
849                 local src="$src/${path}-obj"
850                 local objects="$TARGET_ARCH/$RPMSMPTYPE"
851
852                 # Novell, are you *TRYING* to make life hard for me?
853                 if [ -d "$src/powerpc" ]; then
854                     objects="powerpc/$TARGET_ARCH"
855                 elif [ $TARGET_ARCH == 'i686' ]; then
856                     objects="i386/$RPMSMPTYPE"
857                 fi
858
859                 LINUXOBJ="$(pwd)/$src/$objects"
860             fi
861         done
862         if [ -z "$LINUX" ]; then
863             RC=255
864         else
865             # dig out the release version
866             LINUXRELEASE=$(find_linux_release ${LINUXOBJ:-$LINUX})
867             if [ -z "$LINUXRELEASE" ]; then
868                 echo "Failed to find linux release in ${LINUXOBJ:-$LINUX}"
869                 RC=255
870             fi
871         fi
872     else
873         RC=255
874     fi
875     popd
876     return $RC
877 }
878
879 build_kernel_ib() {
880     # build kernel-ib{,-devel}
881     # some I/B drivers are architecture dependent and kernel-ib's configure
882     # does not figure it out for us ~sigh~
883     local configure_options=""
884     case "$TARGET_ARCH" in
885         x86_64 | ia64)
886             configure_options="--with-ipath_inf-mod"
887             ;;
888         ppc64)
889             configure_options="--with-ipath_inf-mod --with-ehca-mod"
890             ;;
891     esac
892     local K_SRC="K_SRC"
893     # ofed 1.3 had a bug in the rpm spec
894     if [ "$OFED_VERSION" = "1.3" ]; then
895         K_SRC="KSRC"
896     fi
897     $RPMBUILD --rebuild --define 'build_kernel_ib 1' --define 'build_kernel_ib_devel 1' \
898               --define "_topdir ${TOPDIR}" --target ${TARGET_ARCH} \
899               --define "KVERSION ${LINUXRELEASE}" \
900               --define "$K_SRC ${LINUXOBJ:-${LINUX}}" \
901               --define "LIB_MOD_DIR /lib/modules/${LINUXRELEASE}/updates" \
902               --define "configure_options --without-quilt --with-core-mod --with-user_mad-mod --with-user_access-mod --with-addr_trans-mod --with-srp-target-mod --with-core-mod --with-mthca-mod --with-mlx4-mod --with-cxgb3-mod --with-nes-mod --with-ipoib-mod --with-sdp-mod --with-srp-mod --without-srp-target-mod --with-rds-mod --with-iser-mod --with-qlgc_vnic-mod --with-madeye-mod $configure_options" ${TOPDIR}/OFED/SRPMS/ofa_kernel-${OFED_VERSION}-ofed${OFED_VERSION}.src.rpm
903
904     if [ ${PIPESTATUS[0]} != 0 ]; then
905         fatal 1 "Error building kernel-ib"
906     fi
907
908     pushd "$TOPDIR" >/dev/null
909     rm -rf kernel-ib-devel
910     mkdir kernel-ib-devel
911     cd kernel-ib-devel
912     local rpm=$(ls $TOPDIR/RPMS/*/kernel-ib-devel-${OFED_VERSION}-${LINUXRELEASE//-/_}.*.rpm)
913     rpm2cpio -itv < $rpm | cpio -id
914     CONFIGURE_FLAGS="--with-o2ib=$(pwd)/usr/src/ofa_kernel ${CONFIGURE_FLAGS}"
915     popd >/dev/null
916 }
917
918 store_for_reuse() {
919         local articles="$1"
920         local module="$2"
921         local location="$3"
922         local signature="$4"
923         local use_links="$5"
924
925         local linkflag=""
926         if $use_links; then
927             linkflag="l"
928         fi
929
930         location="$location"/"$signature"/"$module"
931         mkdir -p "$location"
932         ## use eval/echo here to make sure shell expansions are performed
933         #if ! cp -a${linkflag} $(eval echo $articles) "$location"; then
934         local article
935         for article in $(eval echo $articles); do
936             if ! cp -a${linkflag} "$article" "$location"; then
937                 error "Failed to copy \"$article\" to \"$location\" in store_for_reuse()"
938                 # rename the cache location so that it's not cached
939                 # product, but is around for analysis
940                 mv "$location"{,-bad-$(date +%s)} || 
941                     error "failed to clean up a failed cache attempt" \
942                           "in \"$location\" -- manual cleanup will be" \
943                           "necessary"
944                 return 1
945             fi
946         done
947
948         return 0
949
950 }
951
952 reuse() {
953     local module="$1"
954     local dest="$2"
955     local use_links="${3:-false}"
956     local signature="$4"
957
958     if [ -n "$REUSEBUILD" ] && [ -d $REUSEBUILD/$signature/$module ]; then
959         # so that we know how stale this entry is
960         touch $REUSEBUILD/$signature/$module/.lastused
961         if $use_links; then
962             if ls $REUSEBUILD/$signature/$module/* >/dev/null 2>&1; then
963                 cp -al $REUSEBUILD/$signature/$module/* $dest/
964             fi
965         else
966             # copying is pretty heavy
967             # cp -a $REUSEBUILD/$signature/$module/* $dest/
968             # do some creative symlinking instead
969             local dir
970             for dir in BUILD SRPMS SPECS; do
971                 if ls $REUSEBUILD/$signature/$module/$dir/* >/dev/null 2>&1; then
972                     ln -s $REUSEBUILD/$signature/$module/$dir/* $dest/$dir
973                 fi
974             done
975             # sources have to be copied by file because we need SOURCES to
976             # be a dir we can write into
977 # could overrun ls's arg list here
978             #ls $REUSEBUILD/$signature/$module/SOURCES/* |
979             find $REUSEBUILD/$signature/$module/SOURCES/ -type f |
980                 xargs ln -t $dest/SOURCES -s
981
982             # same for RPMS/* dirs
983 # could overrun ls's arg list here
984             #ls $REUSEBUILD/$signature/$module/RPMS/$TARGET_ARCH/* |
985             local dir
986             for dir in $REUSEBUILD/$signature/$module/RPMS/*; do
987                 mkdir -p $dest/RPMS/${dir##*/}
988                 find $dir -type f |
989                   xargs ln -t $dest/RPMS/${dir##*/} -s
990             done
991                      
992         fi
993         return 0
994     else
995         return 1
996     fi
997 }
998
999 basearch() {
1000     local arch="$1"
1001
1002     if [[ $arch = i[3456]86 ]]; then
1003         echo "i386"
1004     else
1005         echo "$arch"
1006     fi
1007
1008 }
1009
1010 build_kernel_with_srpm() {
1011
1012     # need to generate the patch for this target
1013     do_patch_linux false >&2    # sets global $FULL_PATCH (yeah, yuck)
1014
1015     # get an md5sum of the kernel patch + config for reuse check
1016     # XXX really, there needs to be a signature and a CONFIG_FILE per arch
1017     #     in BUILD_ARCHS
1018     local REUSE_SIGNATURE=$(cat $CONFIG_FILE $TARGET_FILE $FULL_PATCH | md5sum | cut -d" " -f1)
1019
1020     # see if we can link to the reuse pool
1021     # XXX - hrm.  i'm not convinced this doesn't belong in the reuse "library"
1022     local CAN_LINK_FOR_REUSE=false
1023     touch $REUSEBUILD/$$
1024     if cp -al $REUSEBUILD/$$ $TOPDIR/; then
1025         CAN_LINK_FOR_REUSE=true
1026     fi
1027     rm $REUSEBUILD/$$
1028
1029     # the extra version string to use for the kernel (which might be a reused
1030     # kernel, remember)
1031     local kernel_extra_version=""
1032     if $REUSERPM && reuse kernel "$TOPDIR" "$CAN_LINK_FOR_REUSE" \
1033                                  "$REUSE_SIGNATURE"; then
1034         # figure out the EXTRA_VERSION of the kernel we are re-using
1035         kernel_extra_version=$(ls $TOPDIR/RPMS/$TARGET_ARCH/kernel-lustre-*${lnxmaj}${EXTRA_VERSION_DELIMITER}${PRISTINE_EXTRA_VERSION}.*.$TARGET_ARCH.rpm)
1036         kernel_extra_version=${kernel_extra_version##*kernel-lustre-*${lnxmaj}${EXTRA_VERSION_DELIMITER}}
1037         kernel_extra_version=${kernel_extra_version%%.$TARGET_ARCH.rpm}
1038     else
1039         # nothing cached, build from scratch
1040         if [ ! -r "$KERNELDIR/$KERNEL_SRPM" ]; then
1041             download_srpm "$CANONICAL_TARGET" "$KERNEL_SRPM"
1042         fi
1043
1044         rpm -ivh $KERNELDIR/$KERNEL_SRPM --define "_topdir $TOPDIR" >&2 || {
1045         # should we clean this up or leave it for analysis?
1046             #rm -rf $RPMTOPDIR
1047             fatal 1 "Error installing kernel SRPM."
1048         }
1049
1050         # put the Lustre kernel patch into the RPM build tree
1051         cp $FULL_PATCH $TOPDIR/SOURCES/linux-${lnxmaj}-lustre.patch
1052         prepare_and_build_srpm
1053
1054         # store the resulting kernel RPM build tree for future use
1055         if ! store_for_reuse "$TOPDIR/{SPECS,SOURCES,BUILD,SRPMS,RPMS}" \
1056                              "kernel" "$REUSEBUILD" "$REUSE_SIGNATURE" \
1057                              "$CAN_LINK_FOR_REUSE"; then
1058             error "Failed to store kernel RPMS for reuse"
1059             echo "unknown"
1060             return 1
1061         fi
1062         kernel_extra_version=$EXTRA_VERSION
1063     fi  # build reuse
1064
1065     # should now have the following RPMs
1066     # $TOPDIR/RPMS/$arch/kernel-lustre-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1067     # $TOPDIR/RPMS/$arch/kernel-lustre-devel-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1068     # $TOPDIR/RPMS/$arch/kernel-lustre-headers-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1069     # $TOPDIR/RPMS/$arch/kernel-lustre-debuginfo-common-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1070     # $TOPDIR/RPMS/$arch/kernel-lustre-debuginfo-2.6.18-53.1.21.el5_lustre.1.6.5.1.$arch.rpm
1071
1072     echo $kernel_extra_version
1073     return 0
1074
1075 }
1076
1077 build_with_srpm() {
1078
1079     if ! $PATCHLESS; then
1080         local kernel_extra_version
1081         if ! kernel_extra_version=$(build_kernel_with_srpm); then
1082             fatal 1 "Failed to build the kernel from it's SRPM"
1083         fi
1084 #local kernel_extra_version="60-0.33_lustre.1.8.0.50.20090311172757"
1085
1086         for arch in $BUILD_ARCHS; do
1087
1088             local kernel_devel_rpm="$TOPDIR/RPMS/$arch/$(devel_kernel_name true)-$lnxmaj$EXTRA_VERSION_DELIMITER$kernel_extra_version.$arch.rpm"
1089
1090             # install the -devel RPM in preparation for the lustre build
1091             if ! lnxrel="$kernel_extra_version" unpack_linux_rpm \
1092                            $kernel_devel_rpm $EXTRA_VERSION_DELIMITER; then
1093                 fatal 1 "Could not find the Linux tree in $kernel_devel_rpm"
1094             fi
1095
1096             # XXX - superhack of all superhacks!  kernel-lustre-devel doesn't
1097             #       have have sources in it, so we need to pull them out of
1098             #       the SRPM
1099             #       yeah.  blech.
1100             cp BUILD/kernel-lustre-${lnxmaj}/linux-${lnxmaj}.$arch/fs/ext3/*.[ch] \
1101                $TOPDIR/reused/usr/src/kernels/${lnxmaj}${EXTRA_VERSION_DELIMITER}${kernel_extra_version}-${arch}/fs/ext3
1102         done
1103     else
1104         # need to find and unpack the vendor's own kernel-devel for patchless
1105         # client build
1106         local kernelrpm
1107         if ! kernelrpm=$(find_linux_rpm "-$DEVEL_KERNEL_TYPE" ${EXTRA_VERSION_DELIMITER:-"-"}); then
1108             fatal 1 "Could not find the kernel-$DEVEL_KERNEL_TYPE RPM in ${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}"
1109         fi
1110         if ! lnxrel="$lnxrel" unpack_linux_rpm "$kernelrpm" \
1111                                                "${EXTRA_VERSION_DELIMITER:--}"; then
1112             fatal 1 "Could not find the Linux tree in $kernelrpm"
1113         fi
1114     fi
1115
1116     # before lustre, build kernel-ib
1117     if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1118         # see if we can link to the reuse pool
1119         # XXX - hrm.  i'm not convinced this doesn't belong in the reuse "library"
1120         local CAN_LINK_FOR_REUSE=false
1121         touch $REUSEBUILD/$$
1122         if cp -al $REUSEBUILD/$$ $TOPDIR/; then
1123             CAN_LINK_FOR_REUSE=true
1124         fi
1125         rm $REUSEBUILD/$$
1126
1127         local REUSE_SIGNATURE=$({ echo "$OFED_VERSION"; echo "$(find_linux_release ${LINUXOBJ:-$LINUX})"; cat "${LINUXOBJ:-${LINUX}}/include/linux/autoconf.h"; } | md5sum | cut -d" " -f1)
1128         if ! reuse ofed "$TOPDIR" "$CAN_LINK_FOR_REUSE" \
1129                         "$REUSE_SIGNATURE"; then
1130             # stash away the existing built articles for a moment
1131             mkdir bak
1132             mv {BUILD,{S,}RPMS,S{OURCE,PEC}S} bak
1133             function mv_back {
1134                 pushd bak
1135                 find . | cpio -pudlm ..
1136                 popd
1137                 rm -rf bak
1138             }
1139             create_rpmbuild_dirs
1140             # build it
1141             build_kernel_ib
1142             if ! store_for_reuse "$TOPDIR/{SPECS,SOURCES,BUILD,SRPMS,RPMS}" \
1143                                  "ofed" "$REUSEBUILD" "$REUSE_SIGNATURE" \
1144                                  "$CAN_LINK_FOR_REUSE"; then
1145                 error "Failed to store OFED RPMS for reuse"
1146                 mv_back
1147                 return 1
1148             fi
1149             # put the stuff we stashed away back
1150             mv_back
1151         fi
1152     fi
1153
1154     # now build Lustre
1155     if build_lustre; then
1156         # the build worked.  resolve any symlinked files (i.e. from reuse)
1157         # in RPMS/$arch to real files so that that that huge mess of
1158         # complication knows as LTS can copy them yet somewhere else.
1159         # is it any wonder this whole process is so damn so?  anyone ever
1160         # heard of hardlinks?  it this cool new thing that allows you save
1161         # tons of time and space by creating... well you can go read about
1162         # them if you have not heard about them yet.
1163         # can i say how much the implemenation of all of this really impedes
1164         # RPM reuse?
1165         pushd RPMS/$TARGET_ARCH
1166             for file in *; do
1167                 if [ -h $file ]; then
1168                     cp $file foo
1169                     mv foo $file
1170                 fi
1171             done
1172         popd
1173     fi
1174 }
1175
1176 create_rpmbuild_dirs() {
1177
1178     if [ ! -d RPMS ]; then
1179         mkdir -p RPMS
1180         for arch in $BUILD_ARCHS; do
1181             mkdir RPMS/$arch
1182         done
1183     fi
1184     [ -d BUILD ] || mkdir BUILD
1185     [ -d SOURCES ] || mkdir SOURCES
1186     [ -d SPECS ] || mkdir SPECS
1187     [ -d SRPMS ] || mkdir SRPMS
1188
1189 }
1190
1191 new_list() {
1192
1193     echo ""
1194
1195 }
1196
1197 add_list() {
1198     local list="$1"
1199     local item="$2"
1200
1201     echo "$list $item"
1202
1203 }
1204
1205 is_list_member() {
1206     local list="$1"
1207     local item="$2"
1208
1209     [[ $list\  == *\ $item\ * ]]
1210
1211 }
1212
1213 #########################################################################
1214 # Generate a backtrace through the call stack.
1215 #
1216 # Input: None
1217 # Output: None
1218 #########################################################################
1219 backtrace() {
1220     local strip=${1:-1}
1221
1222     local funcname="" sourcefile="" lineno="" n
1223
1224     echo "Call stack: (most recent first)"
1225     for (( n = $strip ; n < ${#FUNCNAME[@]} ; ++n )) ; do
1226         funcname=${FUNCNAME[$n - 1]}
1227         sourcefile=$(basename ${BASH_SOURCE[$n]})
1228         lineno=${BASH_LINENO[$n - 1]}
1229         # Display function arguments
1230         if [[ ! -z "${BASH_ARGV[@]}" ]]; then
1231             local args newarg j p=0
1232             for (( j = ${BASH_ARGC[$n - 1]}; j > 0; j-- )); do
1233                 newarg=${BASH_ARGV[$j + $p - 1]}
1234                 args="${args:+${args} }'${newarg}'"
1235             done
1236             let p+=${BASH_ARGC[$n - 1]}
1237         fi
1238         echo "  ${funcname} ${args:+${args} }at ${sourcefile}:${lineno}"
1239     done
1240
1241     echo
1242     echo "BEGIN BACKTRACE"
1243
1244     #echo ${BASH_LINENO[*]}
1245     #echo ${BASH_SOURCE[*]}
1246     #echo ${FUNCNAME[*]}
1247     local i=$((${#FUNCNAME[@]} - 1))
1248     while [ $i -ge 0 ]; do
1249         local SOURCELINE="${BASH_SOURCE[$i + 1]}:${BASH_LINENO[$i]}"
1250         # Can't figure out how to get function args from other frames...
1251         local FUNCTION="${FUNCNAME[$i]}()"
1252         echo "$SOURCELINE:$FUNCTION"
1253         i=$((i - 1))
1254     done
1255
1256     echo "END BACKTRACE"
1257
1258     echo $BACKTRACE
1259
1260 }
1261
1262 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
1263
1264 options=$(getopt -o d:D:h -l kerneltree:,distro:,kernelrpm:,reusebuild:,patchless,ldiskfs,ccache,reuse:,norpm,disable-datestamp,external-patches:,timestamp:,extraversion:,kerneldir:,linux:,lustre:,nodownload,nosrc,publish,release,src,stage:,tag:,target:,target-archs:,with-linux:,xen -- "$@")
1265
1266 if [ $? != 0 ]; then
1267     usage 1
1268 fi
1269
1270 eval set -- "$options"
1271
1272 while [ "$1" ]; do
1273     case "$1" in
1274         '')
1275             usage 1
1276             ;;
1277         --ccache)
1278             CCACHE='ccache'
1279             shift
1280             ;;
1281         -d)
1282             CVSROOT=$2
1283             shift 2
1284             ;;
1285         -D)
1286             DATE=$2
1287             shift 2
1288             ;;
1289         --external-patches)
1290             EXTERNAL_PATCHES=$2
1291             shift 2
1292             ;;
1293         --extraversion)
1294             EXTRA_VERSION=$2
1295             shift 2
1296             ;;
1297         --help | -h)
1298             usage 0
1299             ;;
1300         --kerneldir)
1301             KERNELDIR=$2
1302             shift 2
1303             ;;
1304         --kerneltree)
1305             if ! KERNELTREE=$(canon_path "$2"); then
1306                 fatal 1 "Could not determine the canonical location of $2"
1307             fi
1308             shift 2
1309             ;;
1310         --linux | --with-linux)
1311             if ! LINUX=$(canon_path "$2"); then
1312                 fatal 1 "Could not determine the canonical location of $2"
1313             fi
1314             shift 2
1315             ;;
1316         --distro)
1317             DISTRO=$2
1318             shift 2
1319             ;;
1320         --reuserpm)
1321             REUSERPM=$2
1322             shift 2
1323             ;;
1324         --reusebuild)
1325             if ! REUSEBUILD=$(canon_path "$2"); then
1326                 fatal 1 "Could not determine the canonical location of $2"
1327             fi
1328             shift 2
1329             ;;
1330         --norpm)
1331             NORPM=true
1332             shift
1333             ;;
1334         --ldiskfs)
1335             LDISKFSRPM=true
1336             shift
1337             ;;
1338         --patchless)
1339             PATCHLESS=true
1340             shift
1341             ;;
1342         --kernelrpm)
1343             if ! KERNELRPMSBASE=$(canon_path "$2"); then
1344                 fatal 1 "Could not determine the canonical location of $2"
1345             fi
1346             shift 2
1347             ;;
1348         --timestamp)
1349             TIMESTAMP=$2
1350             shift 2
1351             ;;
1352         --lustre)
1353             LUSTRE=$2
1354             shift 2
1355             ;;
1356         --nodownload)
1357             DOWNLOAD=false
1358             shift 1
1359             ;;
1360         --nosrc)
1361             DO_SRC=false
1362             shift 1
1363             ;;
1364         --publish)
1365             shift
1366             ;;
1367         --release)
1368             RELEASE=true
1369             shift
1370             ;;
1371         --src)
1372             DO_SRC=true
1373             shift 1
1374             ;;
1375         --stage)
1376             STAGEDIR=$2
1377             shift 2
1378             ;;
1379         --tag)
1380             TAG=$2
1381             shift 2
1382             ;;
1383         --target)
1384             TARGET=$2
1385             shift 2
1386             ;;
1387         --target-archs)
1388             TARGET_ARCHS=$2
1389             shift 2
1390             ;;
1391         --disable-datestamp)
1392             USE_DATESTAMP=
1393             shift
1394             ;;
1395         --xen)
1396             XEN=true
1397             shift
1398             ;;
1399         --)
1400             shift
1401             CONFIGURE_FLAGS=$@
1402             CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-liblustre --enable-liblustre-tests"
1403             break
1404             ;;
1405         *)
1406             usage 1 "Unrecognized option: $1"
1407             ;;
1408     esac
1409 done
1410
1411 check_options
1412
1413 unpack_lustre
1414
1415 load_target
1416 EXTRA_VERSION_DELIMITER=${EXTRA_VERSION_DELIMITER:-"-"}
1417
1418 if [ -n "$OFED_VERSION" -a "$OFED_VERSION" != "inkernel" ]; then
1419     download_ofed
1420     unpack_ofed
1421 fi
1422
1423 # make sure the RPM build environment is set up
1424 create_rpmbuild_dirs
1425
1426 trap '[ -n "$CCACHE" ] && ccache -s' EXIT
1427
1428 # if an unpacked kernel source tree was given on the command line
1429 # just build lustre with it (nothing distro kernel specific here)
1430 if [ -n "$LINUX" ]; then
1431     build_lustre
1432 else
1433     if [ -f "${0%/*}/lbuild-$DISTRO" ]; then
1434         seen_list=$(new_list)
1435         trap '(echo "Untrapped error"
1436 echo
1437 # have we seen this one
1438 echo "checking seen list for ${BASH_SOURCE[0]}:${BASH_LINENO[0]}"
1439
1440 if is_list_member "$seen_list" "${BASH_SOURCE[0]}:${BASH_LINENO[0]}"; then
1441   echo "seen this one already"
1442 else
1443   seen_list=$(add_list "$seen_list" "${BASH_SOURCE[0]}:${BASH_LINENO[0]}")
1444 fi
1445 backtrace
1446 echo
1447 echo "Environment:"
1448 set
1449 ) | tee >(mail -s "Untrapped error at ${BASH_SOURCE[0]##*/}:${BASH_LINENO[0]} on $HOSTNAME" brian@sun.com) >&2' ERR
1450         set -E
1451
1452         source ${0%/*}/lbuild-$DISTRO
1453
1454         build_with_srpm || fatal 1 "Failed to build_with_srpm"
1455     else
1456         source ${0%/*}/lbuild.old_school
1457
1458         old_school_download_kernel
1459
1460         build_success=false
1461         if $PATCHLESS; then
1462             patchless_build_sequence && build_success=true
1463         else
1464             [ "$DISTRO" = "sles9" ] && build_sequence_rpm_reuse && build_success=true
1465             if ! $build_success; then
1466                 build_sequence_reuse && build_success=true
1467                 if ! $build_success; then
1468                     build_sequence && build_success=true
1469                     if $build_success; then
1470                         store_for_reuse || echo "Cannot store for future reuse"
1471                     fi
1472                 fi
1473             fi
1474         fi
1475         ( $build_success ) || fatal 1 "Cannot build lustre"
1476     fi
1477 fi
1478
1479 stage