Whamcloud - gitweb
add RHBUILD to targets and spec to distinguish files that come from using a redhat...
[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         -e "s/@RHBUILD@/$RHBUILD/g" \
312         < $TOPDIR/lustre/scripts/lustre-kernel-2.4.spec.in \
313         > lustre-kernel-2.4.spec
314     [ -d SRPMS ] || mkdir SRPMS
315     [ -d RPMS ] || mkdir RPMS
316     [ -d BUILD ] || mkdir BUILD
317     [ -d SOURCES ] || mkdir SOURCES
318     cp $TOPDIR/lustre/scripts/linux-rhconfig.h SOURCES
319     cp $TOPDIR/lustre/scripts/linux-merge-config.awk SOURCES
320     cp $TOPDIR/lustre/scripts/linux-merge-modules.awk SOURCES
321     cp "$LUSTRE" "$KERNEL_FILE" SOURCES
322 }
323
324 clean_lustre()
325 {
326     [ -d lustre ] || return 0
327     echo "Cleaning lustre..."
328     [ -L lustre ] && rm -rf $(readlink lustre)
329     rm -rf lustre
330 }
331
332 build()
333 {
334     echo "Building rpms for: $BUILD_ARCHS..."
335     targets=
336     for arch in $BUILD_ARCHS ; do
337         targets="--target $arch $targets"
338     done
339
340     rpmbuild $targets -bb lustre-kernel-2.4.spec \
341         --define "_topdir $TOPDIR" || \
342         fatal 1 "Error building rpms for $arch."
343
344     (( $DO_SRC )) && rpmbuild -bs lustre-kernel-2.4.spec \
345         --define "_topdir $TOPDIR" || \
346         fatal 1 "Error building .src.rpm."
347 }
348
349 publish()
350 {
351     publishing || return 0
352 }
353
354 [ -r ~/.lbuildrc ] && . ~/.lbuildrc
355
356 options=$(getopt -o d:D:h -l extraversion:,kerneldir:,lustre:,nosrc,publish,release,tag:,target:,target-archs: -- "$@")
357
358 eval set -- "$options"
359     
360 while [ "$1" ] ; do
361     case "$1" in
362         '')
363             usage 1
364             ;;
365         -d)
366             CVSROOT=$2
367             shift 2
368             ;;
369         -D)
370             DATE=$2
371             shift 2
372             ;;
373         --extraversion)
374             EXTRA_VERSION=$2
375             shift 2
376             ;;
377         --help | -h)
378             usage 0
379             ;;
380         --kerneldir)
381             KERNELDIR=$2
382             shift 2
383             ;;
384         --lustre)
385             LUSTRE=$2
386             shift 2
387             ;;
388         --nosrc)
389             DO_SRC=0
390             shift 1
391             ;;
392         --publish)
393             PUBLISH=1
394             shift
395             ;;
396         --release)
397             RELEASE=1
398             shift
399             ;;
400         --tag)
401             TAG=$2
402             shift 2
403             ;;
404         --target)
405             TARGET=$2
406             shift 2
407             ;;
408         --target-archs)
409             TARGET_ARCHS=$2
410             shift 2
411             ;;
412         --)
413             shift
414             CONFIGURE_FLAGS=$@
415             break
416             ;; 
417         *)
418             usage 1 "Unrecognized option: $1"
419             ;;
420     esac
421 done
422
423 check_options
424
425 unpack_lustre
426 load_target
427
428 if (( $DO_SRC )) ; then
429     unpack_linux
430     patch_linux
431     pack_linux
432     clean_linux
433 fi
434
435 # prep_build needs the .spec.in from the lustre source
436 prep_build
437 clean_lustre
438
439 build
440 publish