Whamcloud - gitweb
- add a helper to call bc with a specified precision
[fs/lustre-release.git] / lustre / scripts / lbuild
1 #!/bin/sh
2
3 TOPDIR=$PWD
4
5 # CVSROOT is inherited from the environment
6 KERNELDIR=
7 LUSTRE=
8 PUBLISH=0
9 RELEASE=0
10 DO_SRC=1
11 TAG=
12 TARGET=
13 TARGET_ARCHS=
14 CONFIGURE_FLAGS=
15 EXTRA_VERSION=
16
17 # from target file
18 KERNEL=
19 SERIES=
20 CONFIG=
21 VERSION=
22
23 BASE_ARCH=
24 BIGMEM_ARCHS=
25 BOOT_ARCHS=
26 JENSEN_ARCHS=
27 SMP_ARCHS=
28 UP_ARCHS=
29
30 DATE=$(date)
31
32 cleanup()
33 {
34     true
35 }
36
37 fatal()
38 {
39     cleanup
40     [ "$2" ] && echo
41     [ "$2" ] && echo "${0##*/}: $2"
42     exit $1
43 }
44
45 publishing()
46 {
47     (( $PUBLISH )) || return 0
48 }
49
50 is_release()
51 {
52     (( $RELEASE )) || return 0
53 }
54
55 list_targets()
56 {
57     echo -n "Available targets:"
58     for target in $TOPDIR/lustre/kernel_patches/targets/*.target ; do
59         target_file=${target##*/}
60         echo -n " ${target_file%%.target}"
61     done
62     echo
63 }
64
65 usage()
66 {
67     cat <<EOF
68 Usage: ${0##*/} [OPTION]... [-- <lustre configure options>]
69
70   -d CVSROOT
71     Specifies the CVS Root to use when pulling files from CVS.  The
72     environment variable \$CVSROOT is used if this option is not
73     present.
74
75   --extraversion=EXTRAVERSION
76     Text to use for the rpm release and kernel extraversion.
77
78   --kerneldir=KERNELDIR
79     Directory containing Linux source tarballs referenced by target
80     files.
81
82   --lustre=LUSTRE
83     Path to an existing lustre source tarball to use instead of
84     pulling from CVS.
85
86   --nosrc
87     Do not build a .src.rpm, a full kernel patch, or a patched kernel
88     tarball.
89
90   --publish
91     Publish the packages, patches, and tarballs on the ftp server.
92
93   --release
94     Specifies that the files generated do not include timestamps, and
95     that this is an official release.
96
97   --tag=TAG
98     A CVS branch/tag name to build from when pulling from CVS.
99
100   --target=TARGET
101     The name of the target to build.  The available targets are listed
102     below.
103
104   --target-archs=TARGET_ARCHS
105     A (space delimited) list of architectures to build.  By default,
106     all of the archs supported by the TARGET will be built, in
107     addition to a .src.rpm.  This option can limit those, for machines
108     that can only build certain archs or if you only want a certain
109     arch built (for testing, or a one-off kernel).
110
111     Also note that by using a non-"base" arch (eg, i386) only kernels
112     will be built - there will be no lustre-lite-utils package.
113
114 EOF
115
116 #   list_targets
117
118     fatal "$1" "$2"
119 }
120
121 check_options()
122 {
123     if [ "$LUSTRE" ] ; then
124         [ -r "$LUSTRE" ] || \
125             usage 1 "Could not find Lustre source tarball '$LUSTRE'."
126     else
127         [ "$CVSROOT" ] || \
128             usage 1 "Either specify a CVS Root with -d, or a Lustre source tarball with --lustre."
129         [ "$TAG" ] || \
130             usage 1 "A branch/tag name must be specified with --tag when not building from a tarball."
131     fi
132
133     [ "$KERNELDIR" ] || \
134         usage 1 "A kernel directory must be specified with --kerneldir."
135
136     [ -d "$KERNELDIR" ] || \
137         usage 1 "$KERNELDIR is not a directory."
138
139     if ! (( $RELEASE )) ; then
140         [ "$TAG" ] || \
141             usage 1 "When building a snapshot, a tag name must be used."
142     fi
143
144     TIMESTAMP=$(date -d "$DATE" "+%Y%m%d%H%M")
145
146     [ "$TARGET" ] || usage 1 "A target must be specified with --target."
147 #    TARGET_FILE="$TOPDIR/kernel_patches/targets/$TARGET.target"
148 #    [ -r "$TARGET_FILE" ] || \
149 #       usage 1 "Target '$TARGET' was not found."
150 }
151
152 uniqify()
153 {
154     echo $(echo "$*" | xargs -n 1 | sort -u)
155 }
156
157 load_target()
158 {
159     EXTRA_VERSION_save="$EXTRA_VERSION"
160     TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target"
161     [ -r "$TARGET_FILE" ] || \
162         fatal 1 "Target $TARGET was not found."
163
164     echo "Loading target config file $TARGET.target..." 
165
166     . "$TARGET_FILE"
167
168     [ "$KERNEL"  ] || fatal 1 "Target $TARGET did not specify a kernel."
169     [ "$SERIES"  ] || fatal 1 "Target $TARGET did not specify a kernel patch series."
170 #    [ "$CONFIG"  ] || fatal 1 "Target $TARGET did not specify a kernel config."
171     [ "$VERSION" ] || fatal 1 "Target $TARGET did not specify a kernel version."
172
173     if [ "$KERNELDIR" ] ; then
174         KERNEL_FILE="$KERNELDIR/$KERNEL"
175         [ -r "$KERNELDIR/$KERNEL" ] || \
176             fatal 1 "Target $TARGET's kernel file $KERNEL not found in kernel directory $KERNELDIR."
177     fi
178
179     SERIES_FILE="$TOPDIR/lustre/kernel_patches/series/$SERIES"
180     [ -r "$SERIES_FILE" ] || \
181         fatal 1 "Target $TARGET's series $SERIES missing from $TOPDIR/lustre/kernel_patches/series."
182
183     CONFIG_FILE="$TOPDIR/lustre/kernel_patches/kernel_configs/$CONFIG"
184     [ -r "$CONFIG_FILE" ] || \
185         fatal 1 "Target $TARGET's config file $CONFIG missing from $TOPDIR/lustre/kernel_patches/kernel_configs/configs."
186
187     if [ "$EXTRA_VERSION_save" ] ; then
188         EXTRA_VERSION="$EXTRA_VERSION_save"
189     elif ! (( $RELEASE )) ; then
190         EXTRA_VERSION="${EXTRA_VERSION}-${TAG//_/}.${TIMESTAMP}"
191     fi
192     # EXTRA_VERSION=${EXTRA_VERSION//-/_}
193
194     ALL_ARCHS="$BASE_ARCHS $BIGMEM_ARCHS $BOOT_ARCHS $JENSEN_ARCHS $SMP_ARCHS $UP_ARCHS"
195
196     BUILD_ARCHS=
197     for arch in $(uniqify "$ALL_ARCHS") ; do
198         if [ -z "$TARGET_ARCHS" ] || echo "$TARGET_ARCHS" | grep -s "$arch" ; then
199             BUILD_ARCHS="$BUILD_ARCHS $arch"
200         fi
201     done
202     [ "$BUILD_ARCHS" ] || usage 1 "No available target archs to build."
203     echo "Building for: $BUILD_ARCHS"
204 }
205
206 tarflags()
207 {
208     case "$1" in
209         '')
210             fatal 1 "tarflags(): File name argument missing."
211             ;;
212         *.tar.gz)
213             echo 'zxf'
214             ;;
215         *.tar.bz2)
216             echo 'jxf'
217             ;;
218         *)
219             fatal 1 "tarflags(): Unrecognized tar extension in file: $1"
220             ;;
221     esac
222 }
223
224 untar()
225 {
226     echo "Untarring ${1##*/}..."
227     tar $(tarflags "$1") "$1"
228 }
229
230 unpack_lustre()
231 {
232     DIRNAME="lustre-$TAG-$TIMESTAMP"
233     if [ "$LUSTRE" ] ; then
234         untar "$LUSTRE"
235         [ -d lustre ] || ln -sf lustre* lustre
236     else
237         cvs -d "$CVSROOT" -qz3 co -D "$DATE" "-r$TAG" -d "$DIRNAME" lustre || \
238             fatal 1 "There was an error checking out Lustre from CVS."
239         echo "Creating lustre tarball..."
240         tar zcf "$DIRNAME.tar.gz" "$DIRNAME" \
241             --exclude "CVS" --exclude "*~" --exclude ".cvsignore" || \
242             fatal 1 "Could not create Lustre tarball."
243         LUSTRE="$PWD/$DIRNAME.tar.gz"
244         ln -sf "$DIRNAME" lustre
245     fi
246 }
247
248 unpack_linux()
249 {
250     untar "$KERNEL_FILE"
251     [ -d linux ] || ln -sf linux* linux
252 }
253
254 patch_linux()
255 {
256     FULL_PATCH="$PWD/lustre-kernel-${TARGET}-${EXTRA_VERSION}.patch"
257     [ -f "$FULL_PATCH" ] && rm -f "$FULL_PATCH"
258     pushd linux >/dev/null
259     echo -n "Applying patches:"
260     for patch in $(<"$SERIES_FILE") ; do
261         echo -n " $patch"
262         PATCH_FILE="$TOPDIR/lustre/kernel_patches/patches/$patch"
263         [ -r "$PATCH_FILE" ] || \
264             fatal 1 "Patch $patch does not exist in Lustre tree."
265         cat "$PATCH_FILE" >> "$FULL_PATCH" || \
266             fatal 1 "Error adding patch $patch to full patch."
267         patch -s -p1 < "$PATCH_FILE" || fatal 1 "Error applying patch $patch."
268     done
269     echo
270     popd >/dev/null
271     echo "Full patch has been saved in ${FULL_PATCH##*/}."
272     echo "Replacing .config files..."
273     rm -f linux/configs/*
274     cp -v lustre/kernel_patches/kernel_configs/kernel-${VERSION}-${TARGET}*.config linux/configs/
275 }
276
277 pack_linux()
278 {
279     TARBALL="$(readlink linux)-$EXTRA_VERSION.tar.gz"
280     echo "Creating patched linux tarball $TARBALL..."
281     tar zcf "$TARBALL" "$(readlink linux)" \
282         --exclude "CVS" --exclude ".cvsignore" || \
283         --exclude "*.orig" --exclude "*~" --exclude "*.rej" || \
284         fatal 1 "Error creating patched Linux tarball."
285 }
286
287 clean_linux()
288 {
289     [ -d linux ] || return 0
290     echo "Cleaning linux..."
291     [ -L linux ] && rm -rf $(readlink linux)
292     rm -rf linux
293 }
294
295 prep_build()
296 {
297     # make .spec file
298     sed -e "s/@KERNEL_VERSION@/$VERSION/g" \
299         -e "s/@KERNEL_EXTRA_VERSION@/$EXTRA_VERSION/g" \
300         -e "s^@KERNEL_RELEASE@^${EXTRA_VERSION//-/_}^g" \
301         -e "s/@KERNEL_SOURCE@/$KERNEL/g" \
302         -e "s/@LUSTRE_SOURCE@/${LUSTRE##*/}/g" \
303         -e "s/@LUSTRE_TARGET@/$TARGET/g" \
304         -e "s/@CONFIGURE_FLAGS@/$CONFIGURE_FLAGS/g" \
305         -e "s/@BASE_ARCHS@/$BASE_ARCHS/g" \
306         -e "s/@BIGMEM_ARCHS@/$BIGMEM_ARCHS/g" \
307         -e "s/@BOOT_ARCHS@/$BOOT_ARCHS/g" \
308         -e "s/@JENSEN_ARCHS@/$BOOT_ARCHS/g" \
309         -e "s/@SMP_ARCHS@/$SMP_ARCHS/g" \
310         -e "s/@UP_ARCHS@/$UP_ARCHS/g" \
311         < $TOPDIR/lustre/scripts/lustre-kernel-2.4.spec.in \
312         > lustre-kernel-2.4.spec
313     [ -d SRPMS ] || mkdir SRPMS
314     [ -d RPMS ] || mkdir RPMS
315     [ -d BUILD ] || mkdir BUILD
316     [ -d SOURCES ] || mkdir SOURCES
317     cp $TOPDIR/lustre/scripts/linux-rhconfig.h SOURCES
318     cp $TOPDIR/lustre/scripts/linux-merge-config.awk SOURCES
319     cp $TOPDIR/lustre/scripts/linux-merge-modules.awk SOURCES
320     cp "$LUSTRE" "$KERNEL_FILE" SOURCES
321 }
322
323 clean_lustre()
324 {
325     [ -d lustre ] || return 0
326     echo "Cleaning lustre..."
327     [ -L lustre ] && rm -rf $(readlink lustre)
328     rm -rf lustre
329 }
330
331 build()
332 {
333     echo "Building rpms for: $BUILD_ARCHS..."
334     targets=
335     for arch in $BUILD_ARCHS ; do
336         targets="--target $arch $targets"
337     done
338
339     rpmbuild $targets -bb lustre-kernel-2.4.spec \
340         --define "_topdir $TOPDIR" \
341         fatal 1 "Error building rpms for $arch."
342
343     (( $DO_SRC )) && rpmbuild -bs lustre-kernel-2.4.spec \
344         --define "_topdir $TOPDIR" \
345         fatal 1 "Error building .src.rpm."
346 }
347
348 publish()
349 {
350     publishing || return 0
351 }
352
353 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
354
355 options=$(getopt -o d:D:h -l extraversion:,kerneldir:,lustre:,nosrc,publish,release,tag:,target:,target-archs: -- "$@")
356
357 eval set -- "$options"
358     
359 while [ "$1" ] ; do
360     case "$1" in
361         '')
362             usage 1
363             ;;
364         -d)
365             CVSROOT=$2
366             shift 2
367             ;;
368         -D)
369             DATE=$2
370             shift 2
371             ;;
372         --extraversion)
373             EXTRA_VERSION=$2
374             shift 2
375             ;;
376         --help | -h)
377             usage 0
378             ;;
379         --kerneldir)
380             KERNELDIR=$2
381             shift 2
382             ;;
383         --lustre)
384             LUSTRE=$2
385             shift 2
386             ;;
387         --nosrc)
388             DO_SRC=0
389             shift 1
390             ;;
391         --publish)
392             PUBLISH=1
393             shift
394             ;;
395         --release)
396             RELEASE=1
397             shift
398             ;;
399         --tag)
400             TAG=$2
401             shift 2
402             ;;
403         --target)
404             TARGET=$2
405             shift 2
406             ;;
407         --target-archs)
408             TARGET_ARCHS=$2
409             shift 2
410             ;;
411         --)
412             shift
413             CONFIGURE_FLAGS=$@
414             break
415             ;; 
416         *)
417             usage 1 "Unrecognized option: $1"
418             ;;
419     esac
420 done
421
422 check_options
423
424 unpack_lustre
425 load_target
426
427 if (( $DO_SRC )) ; then
428     unpack_linux
429     patch_linux
430     pack_linux
431     clean_linux
432 fi
433
434 # prep_build needs the .spec.in from the lustre source
435 prep_build
436 clean_lustre
437
438 build
439 publish