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