234574d01403752c1931a18f3e014c604e843372
[tools/obs-build.git] / build
1 #!/bin/bash
2 # Script to build a package.  It uses init_buildsystem to setup a chroot
3 # building tree.  This script needs a directory as parameter.  This directory
4 # has to include sources and a spec file.
5 #
6 # BUILD_ROOT        here the packages will be built
7 #
8 # (c) 1997-2008 SuSE GmbH Nuernberg, Germany
9
10 test -z "$BUILD_DIR" && BUILD_DIR=/usr/lib/build
11 test -z "$BUILD_ROOT" && BUILD_ROOT=/var/tmp/build-root
12
13 export BUILD_ARCH BUILD_ROOT BUILD_RPMS BUILD_DIR
14
15 ccache=0
16 icecream=0
17 definesnstuff=()
18 repos=()
19
20 # mkreiserfs only works with qemu/uml if it is able to create a file
21 # system that is owned by the calling user (bnc#369006)
22 #xen_img_mkfs='mkreiserfs -q -f'
23 #xen_img_mkfs='mkfs.ext2 -m 0 -q -F'
24 xen_img_mkfs='mkfs.ext3 -m 0 -q -F'
25 qemu_kernel=/boot/vmlinuz
26 qemu_initrd=/boot/initrd
27 qemu_bin=/usr/bin/qemu
28 uml_kernel=/boot/vmlinux-um
29 uml_initrd=/boot/initrd-um
30
31 kvm_kernel=/boot/vmlinuz
32 kvm_initrd=/boot/initrd
33 kvm_bin=/usr/bin/qemu-kvm
34 mkinitrd_virtio_cmd=(env rootfstype=ext2 mkinitrd -d /dev/null -m "virtio_pci virtio_blk" -k $kvm_kernel -i $kvm_initrd-virtio)
35 # whether we have virtio support
36 kvm_virtio=
37
38 DO_INIT=true
39 DO_LINT=
40 DO_CHECKS=true
41 CLEAN_BUILD=
42 SPECFILES=()
43 SRCDIR=
44 BUILD_JOBS=
45 ABUILD_TARGET_ARCH=
46 CREATE_BASELIBS=
47 USEUSEDFORBUILD=
48 LIST_STATE=
49 VM_IMAGE=
50 VM_SWAP=
51 MEMSIZE=
52 RUNNING_IN_VM=
53 RPMLIST=
54 RELEASE=
55 REASON=
56 NOROOTFORBUILD=
57 LOGFILE=
58 KILL=
59 CHANGELOG=
60 BUILD_DEBUG=
61 PERSONALITY_SYSCALL=
62 INCARNATION=
63 DISTURL=
64
65 export PATH=$BUILD_DIR:$PATH
66
67 # This is for insserv
68 export YAST_IS_RUNNING=instsys
69
70 unset LANGUAGE
71 unset LANG
72 export LC_ALL=POSIX
73 umask 022
74
75 echo_help () {
76     cat << EOT
77
78 Some comments for build
79 -----------------------
80
81 With build you can create rpm packages.  They will be built in a chroot
82 system.  This chroot system will be setup automatically.  Normally you can
83 simply call build with a spec file as parameter - nothing else has to be
84 set.
85
86 If you want to set the directory were the chroot system will be setup
87 (at the moment it uses $BUILD_ROOT),
88 simply set the the environment variable BUILD_ROOT.
89
90 Example:
91
92   export BUILD_ROOT=/var/tmp/mybuildroot
93
94
95 Normally build builds the complete package including src.rpm (rpmbuild -ba).
96 If you want let build only make the binary package, simply set
97
98    export BUILD_RPM_BUILD_STAGE=-bb
99
100 (or -bc, -bp, -bi, ...  see "Maximum RPM" for more details [*]).
101
102 When the build command succeeds, the rpm files can be found under
103 $BUILD_ROOT/usr/src/packages/RPMS/
104
105
106 Known Parameters:
107
108   --help      You already got it :)
109
110   --clean     Delete old build root before initializing it
111
112   --no-init   Skip initialization of build root and start with build
113               immediately.
114
115   --no-checks Do not run post-build checks
116
117   --rpms path1:path2:...
118               Specify path where to find the RPMs for the build system
119
120   --arch arch1:arch2:...
121               Specify what architectures to select from the RPMs
122
123   --useusedforbuild
124               Do not expand dependencies but search the specfile for
125               usedforbuild lines.
126
127   --verify    Run verify when initializing the build root
128
129   --extra-packs pack
130               Also install package 'pack'
131
132   --root rootdir
133               Use 'rootdir' to setup chroot environment
134
135   --baselibs  Create -32bit/-64bit/-x86 rpms for other architectures
136
137   --list-state
138               List rpms that would be used to create a fresh build root.
139               Does not create the build root or perform a build.
140
141   --with X
142               enable feature X for build
143
144   --without X
145               disable feature X for build
146
147   --define 'X Y'
148               define macro X with value Y
149
150   --ccache
151               use ccache to speed up rebuilds
152
153   --icecream N
154               use N parallel build jobs with icecream
155
156   --debug
157               enable creation of a debuginfo package
158
159 Remember to have fun!
160
161 [*] Maximum RPM: http://www.rpm.org/max-rpm/
162 EOT
163 }
164 usage () {
165     echo "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|spec-to-build]"
166     cleanup_and_exit 1
167 }
168
169 #
170 #  cleanup_and_exit
171 #
172 cleanup_and_exit () {
173     trap EXIT
174     test -z "$1" && set 0
175     if test -n "$RUNNING_IN_VM" ; then
176         cd /
177         if test -n "$VM_SWAP" -a -e "$VM_SWAP" ; then
178             swapoff "$VM_SWAP" 2>/dev/null
179             echo -n "BUILDSTATUS$1" >"$VM_SWAP"
180         fi
181         exec >&0 2>&0   # so that the logging tee finishes
182         sleep 1         # wait till tee terminates
183         kill -9 -1      # goodbye cruel world
184         exec /bin/bash -c 'mount -n -o remount,ro / ; halt -f -p'
185         halt -f -p
186     else
187         umount -n $BUILD_ROOT/proc 2>/dev/null || true
188         umount -n $BUILD_ROOT/dev/pts 2>/dev/null || true
189         test "$VM_IMAGE" = 1 && VM_IMAGE=
190         [ -n "$VM_IMAGE" ] && umount $BUILD_ROOT 2>/dev/null || true
191     fi
192     exit $1
193 }
194
195 fail_exit()
196 {
197   cleanup_and_exit 1
198 }
199
200 shellquote()
201 {
202     for arg; do
203         arg=${arg/\\/\\\\}
204         arg=${arg/\$/\\\$}
205         arg=${arg/\"/\\\"}
206         arg=${arg/\`/\\\`}
207         echo -n " \"$arg\""
208     done
209 }
210
211 # create a shell script from command line. Used for preserving arguments
212 # through /bin/su -c
213 toshellscript()
214 {
215         echo "#!/bin/sh -x"
216         echo -n exec
217         shellquote "$@"
218         echo
219 }
220
221 setupccache()
222 {
223     if [ "$ccache" = 1 ]; then
224         if mkdir -p $BUILD_ROOT/var/lib/build/ccache/bin; then
225             for i in gcc g++ cc c++; do
226 #               ln -sf /usr/bin/ccache $BUILD_ROOT/var/lib/build/ccache/bin/$i
227                 rm -f $BUILD_ROOT/var/lib/build/ccache/bin/$i
228                 test -e $BUILD_ROOT/usr/bin/$i || continue
229                 echo '#! /bin/sh' > $BUILD_ROOT/var/lib/build/ccache/bin/$i
230                 echo "test -e /usr/bin/$i || exit 1" > $BUILD_ROOT/var/lib/build/ccache/bin/$i
231                 echo 'export PATH=/opt/icecream/bin:/usr/bin:$PATH' >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
232                 echo "ccache $i \"\$@\"" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
233                 chmod 755 $BUILD_ROOT/var/lib/build/ccache/bin/$i
234                 echo "Installed ccache wrapper as $BUILD_ROOT/var/lib/build/ccache/bin/$i"
235             done
236         fi
237         mkdir -p "$BUILD_ROOT"/.ccache
238         chroot "$BUILD_ROOT" chown -R "$BUILD_USER" "/.ccache"
239         echo "export CCACHE_DIR=/.ccache" > "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
240         echo 'export PATH=/var/lib/build/ccache/bin:$PATH' >> "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
241     else
242         rm -f "$BUILD_ROOT$builduserhome"/bin/{gcc,g++,cc,c++}
243         rm -f "$BUILD_ROOT"/var/lib/build/ccache/bin/{gcc,g++,cc,c++}
244     fi
245 }
246
247 setupicecream()
248 {
249     if [ "$icecream" -eq 0 ]; then
250         rm -rf "$BUILD_ROOT"/var/run/icecream
251         rm -f "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
252         return
253     fi
254
255     if ! chroot "$BUILD_ROOT" rpm -q icecream >/dev/null 2>/dev/null; then
256         echo "*** icecream package not installed ***"
257         false
258         return
259     fi
260
261     echo "using icecream with $icecream jobs"
262
263     if [ "$ccache" -ne 1 ]; then
264         echo 'export PATH=/opt/icecream/bin:$PATH' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
265     else
266         echo 'export CCACHE_PATH=/opt/icecream/bin' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
267     fi
268
269     local icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
270     icecc_vers=${icecc_vers//$BUILD_ROOT/}
271
272     # XXX use changelog like autobuild does instead?
273     # only run create-env if compiler or glibc changed
274     if [ -z "$icecc_vers" \
275         -o ! -e "$BUILD_ROOT/$icecc_vers" \
276         -o "$BUILD_ROOT/usr/bin/gcc" -nt "$BUILD_ROOT/$icecc_vers" \
277         -o "$BUILD_ROOT/usr/bin/g++" -nt "$BUILD_ROOT/$icecc_vers" \
278         -o "$BUILD_ROOT/usr/bin/as" -nt "$BUILD_ROOT/$icecc_vers" \
279         -o "$BUILD_ROOT/lib/libc.so.6" -nt "$BUILD_ROOT/$icecc_vers" ]
280     then
281         rm -rf $BUILD_ROOT/var/run/icecream
282         mkdir -p $BUILD_ROOT/var/run/icecream
283         if [ -e "$BUILD_ROOT"/usr/bin/create-env ]; then
284           createenv=/usr/bin/create-env
285         elif [ -e "$BUILD_ROOT"/usr/lib/icecc/icecc-create-env ]; then
286           createenv="/usr/lib/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
287         else
288           echo "create-env not found"
289           false
290           return
291         fi
292         chroot $BUILD_ROOT bash -c "cd /var/run/icecream; $createenv"
293         icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
294         icecc_vers=${icecc_vers//$BUILD_ROOT/}
295     else
296         echo "reusing existing icecream environment $icecc_vers"
297     fi
298     if [ -n "$icecc_vers" ]; then
299       echo "export ICECC_VERSION=$icecc_vers" >> "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
300     fi
301 }
302
303 setmemorylimit()
304 {
305     if [ -n "$VM_IMAGE" -o -n "$RUNNING_IN_VM" ]; then
306         return
307     fi
308     local mem
309     while read mem; do
310         case "$mem" in
311             MemTotal:*)
312                 set -- $mem
313                 eval "mem=\$(($2/3*4))"
314                 ulimit -v $mem
315                 echo "Memory limit set to ${mem}KB"
316                 break;
317             ;;
318         esac
319     done < <(cat /proc/meminfo) # cat for proc stuff
320 }
321
322 function create_baselibs {
323     echo "... creating baselibs"
324     BRPMS=
325     for RPM in $BUILD_ROOT$TOPDIR/RPMS/*/*.rpm ; do
326         BRPMS="$BRPMS ${RPM#$BUILD_ROOT}"
327     done
328     BASELIBS_CFG=
329     if test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs.conf ; then
330         BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs.conf"
331     fi
332     if test -f $BUILD_ROOT/usr/lib/build/mkbaselibs ; then
333         if test -z "$BASELIBS_CFG" -a -e $BUILD_ROOT/usr/lib/build/baselibs.conf ; then
334             BASELIBS_CFG="-c /usr/lib/build/baselibs.conf"
335         fi
336         chroot $BUILD_ROOT /usr/lib/build/mkbaselibs -c /usr/lib/build/baselibs_global.conf $BASELIBS_CFG $BRPMS || cleanup_and_exit 1
337     else
338         # use external version
339         rm -rf $BUILD_ROOT/.mkbaselibs
340         mkdir -p $BUILD_ROOT/.mkbaselibs
341         cp -f $BUILD_DIR/mkbaselibs $BUILD_ROOT/.mkbaselibs
342         cp -f $BUILD_DIR/baselibs_global.conf $BUILD_ROOT/.mkbaselibs
343         if test -z "$BASELIBS_CFG" -a -e $BUILD_DIR/baselibs.conf ; then
344             cp -f $BUILD_DIR/baselibs.conf $BUILD_ROOT/.mkbaselibs/baselibs.conf
345             BASELIBS_CFG="-c /.mkbaselibs/baselibs.conf"
346         fi
347         chroot $BUILD_ROOT /.mkbaselibs/mkbaselibs -c /.mkbaselibs/baselibs_global.conf $BASELIBS_CFG $BRPMS || cleanup_and_exit 1
348         rm -rf $BUILD_ROOT/.mkbaselibs
349     fi
350 }
351
352 detect_vm_2nd_stage()
353 {
354     if ! test "$0" = "/.build/build" ; then
355         return 1
356     fi
357     echo "2nd stage started in virtual machine"
358     BUILD_ROOT=/
359     BUILD_DIR=/.build
360     . $BUILD_DIR/build.data
361     echo "machine type: `uname -m`"
362     if test "$PERSONALITY" != 0 -a -z "$PERSONALITY_SET" ; then
363         export PERSONALITY_SET=true
364         echo "switching personality to $PERSONALITY..."
365         # this is 32bit perl/glibc, thus the 32bit syscall number
366         exec perl -e 'syscall(136, '$PERSONALITY') == -1 && warn("personality: $!\n");exec "/.build/build" || die("/.build/build: $!\n")'
367     fi
368     PATH=$BUILD_DIR:$PATH
369     RUNNING_IN_VM=true
370     mount -orw -n -tproc none /proc
371     mount -n -o remount,rw /
372     if test -n "$VM_SWAP" ; then
373         for i in 1 2 3 4 5 6 7 8 9 10 ; do
374             test -e "$VM_SWAP" && break
375             test $i = 1 && echo "waiting for $VM_SWAP to appear"
376             echo -n .
377             sleep 1
378         done
379         test $i = 1 || echo
380         # recreate the swap device manually if it didn't exist for some
381         # reason, hardcoded to hda2 atm
382         if ! test -b "$VM_SWAP" ; then
383             rm -f "$VM_SWAP"
384             umask 027
385             mknod "$VM_SWAP" b 3 2
386             umask 022
387         fi
388         swapon -v "$VM_SWAP" || exit 1
389     fi
390     HOST="$MYHOSTNAME"
391
392     return 0
393 }
394
395 set_build_arch()
396 {
397     if [ -z "$BUILD_ARCH" ]; then
398         BUILD_ARCH=`uname -m`
399         test i686 = "$BUILD_ARCH" && BUILD_ARCH=i586 # XXX: why?
400     fi
401
402     case $BUILD_ARCH in
403       i686) BUILD_ARCH="i686:i586:i486:i386" ;;
404       i586) BUILD_ARCH="i586:i486:i386" ;;
405       i486) BUILD_ARCH="i486:i386" ;;
406       x86_64) BUILD_ARCH="x86_64:i686:i586:i486:i386" ;;
407     esac
408     if test "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
409         cpuflags=`grep ^flags /proc/cpuinfo`
410         cpuflags="$cpuflags "
411         test "$cpuflags" = "${cpuflags/ cx8 /}" -o "$cpuflags" = "${cpuflags/ cmov /}" && {
412             echo "Your cpu doesn't support i686 rpms. Exit."
413             exit 1
414         }
415     fi
416 }
417
418 find_spec_files()
419 {
420     local spec files
421     if [ -z "$SPECFILES" ]; then
422         set -- "`pwd`"
423     else
424         set -- "${SPECFILES[@]}"
425     fi
426     SPECFILES=()
427     for spec in "$@"; do
428         if [ "$spec" = "${spec#/}" ]; then
429             spec="`pwd`/$spec"
430         fi
431
432         if [ -d "$spec" ]; then
433             specs=("$spec"/*.spec)
434             if [ -n "$specs" ]; then
435                 SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
436             else
437                 specs=("$spec"/*.spec)
438                 if [ -n "$specs" ]; then
439                     SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
440                 fi
441             fi
442         else
443             SPECFILES[${#SPECFILES[@]}]="$spec";
444         fi
445     done
446
447     if test -z "$SPECFILES"; then
448         echo no spec files or src rpms found in $@. exit...
449         cleanup_and_exit 1
450     fi
451 }
452
453 become_root_or_fail()
454 {
455     if [ ! -w /root ]; then
456         echo "You have to be root to use $0" >&2
457         exit 1
458     fi
459     cleanup_and_exit 1
460 }
461
462 mkdir_build_root()
463 {
464     if [ -d "$BUILD_ROOT" ]; then
465         # check if it is owned by root
466         if [ -z "$RUNNING_IN_VM" -a \! -O "$BUILD_ROOT" -a "`stat -c %u $BUILD_ROOT`" -ne 0 ]; then
467             echo "BUILD_ROOT=$BUILD_ROOT must be owned by $USER. Exit..."
468             cleanup_and_exit 1
469         fi
470     else
471         test "$BUILD_ROOT" != "${BUILD_ROOT%/*}" && mkdir -p "${BUILD_ROOT%/*}"
472         if ! mkdir $BUILD_ROOT; then
473             echo "can not create BUILD_ROOT=$BUILD_ROOT. Exit..."
474             cleanup_and_exit 1
475         fi
476     fi
477     
478     rm -rf "$BUILD_ROOT"/.build.packages
479 }
480
481 linux64()
482 {
483         perl -e 'syscall('$PERSONALITY_SYSCALL', 0); exec(@ARGV) || die("$ARGV[0]: $!\n")' "$@"
484 }
485
486 #### main ####
487
488 trap fail_exit EXIT
489
490 case `perl -V:archname` in
491     *x86_64*) PERSONALITY_SYSCALL=135 ;;
492     *i?86*)   PERSONALITY_SYSCALL=136 ;;
493 esac
494
495 shopt -s nullglob
496
497 if detect_vm_2nd_stage ; then
498     set "/.build-srcdir/$SPECFILE"
499 fi
500 export HOST
501
502 while test -n "$1"; do
503   PARAM="$1"
504   ARG="$2"
505   shift
506   case $PARAM in
507     *-*=*)
508       ARG=${PARAM#*=}
509       PARAM=${PARAM%%=*}
510       set -- "----noarg=$PARAM" "$@"
511   esac
512   case $PARAM in
513       *-help|-h)
514         echo_help
515         cleanup_and_exit
516       ;;
517       *-no*init)
518         DO_INIT=false
519       ;;
520       *-no*checks)
521         DO_CHECKS=false
522       ;;
523       *-clean)
524         CLEAN_BUILD='--clean'
525       ;;
526       *-kill)
527         KILL=true
528       ;;
529       *-rpms)
530         BUILD_RPMS="$ARG"
531         if [ -z "$BUILD_RPMS" ] ; then
532           echo_help
533           cleanup_and_exit
534         fi
535         shift
536       ;;
537       *-arch)
538         BUILD_ARCH="$ARG"
539         shift
540       ;;
541       *-verify)
542         export VERIFY_BUILD_SYSTEM=true
543       ;;
544       *-target)
545         ABUILD_TARGET_ARCH="$ARG"
546         shift
547       ;;
548       *-jobs) 
549         BUILD_JOBS="$ARG"
550         shift
551       ;;
552       *-extra*packs|-X)
553         BUILD_EXTRA_PACKS="$BUILD_EXTRA_PACKS $ARG"
554         shift
555       ;;
556       *-lint)
557         DO_LINT=true
558         ;;
559       *-baselibs)
560         CREATE_BASELIBS=true
561         ;;
562       *-baselibs-internal)
563         CREATE_BASELIBS=internal
564         ;;
565       *-root)
566         BUILD_ROOT="$ARG"
567         shift
568       ;;
569       *-dist) 
570         BUILD_DIST="$ARG"
571         export BUILD_DIST
572         shift
573       ;;
574       *-xen|*-kvm|--uml|--qemu)
575         VM_TYPE=${PARAM##*-}
576         if [ -n "$ARG" -a "$ARG" = "${ARG#-}" ]; then
577             VM_IMAGE="$ARG"
578             shift
579         else
580             VM_IMAGE=1
581         fi
582       ;;
583       *-xenswap|*-swap)
584         VM_SWAP="$ARG"
585         shift
586       ;;
587       *-xenmemory|*-memory)
588         MEMSIZE="$ARG"
589         shift
590       ;;
591       *-rpmlist)
592         RPMLIST="--rpmlist $ARG"
593         BUILD_RPMS=
594         shift
595       ;;
596       *-release)
597         RELEASE="$ARG"
598         shift
599       ;;
600       *-logfile)
601         LOGFILE="$ARG"
602         shift
603       ;;
604       *-reason)
605         REASON="$ARG"
606         shift
607       ;;
608       *-norootforbuild)
609         NOROOTFORBUILD=true
610       ;;
611       *-stage)
612         BUILD_RPM_BUILD_STAGE="$ARG"
613         shift
614       ;;
615       *-useusedforbuild)
616         USEUSEDFORBUILD="--useusedforbuild"
617       ;;
618       *-list*state)
619         LIST_STATE=true
620       ;;
621       --define|--with|--without)
622         definesnstuff[${#definesnstuff[@]}]="$PARAM";
623         definesnstuff[${#definesnstuff[@]}]="$ARG";
624         shift
625       ;;
626       --repository|--repo)
627         if [ -z "$ARG" ] ; then
628           echo_help
629           cleanup_and_exit
630         fi
631         repos[${#repos[@]}]="$PARAM";
632         repos[${#repos[@]}]="$ARG";
633         shift
634       ;;
635       --icecream)
636         if [ -z "$ARG" ] ; then
637           echo "--icecream needs an argument" >&2
638           echo_help
639           cleanup_and_exit 1
640         fi
641         icecream="$ARG"
642         BUILD_JOBS="$ARG"
643         shift
644       ;;
645       --ccache)
646         ccache=1
647       ;;
648       --debug)
649         BUILD_DEBUG=1
650       ;;
651       --incarnation)
652         INCARNATION=$ARG
653         shift
654       ;;
655       --disturl)
656         DISTURL=$ARG
657         shift
658       ;;
659       ----noarg)
660         echo "$ARG does not take an argument"
661         cleanup_and_exit
662       ;;
663       *-changelog)
664         CHANGELOG=true
665       ;;
666       -*)
667         echo Unknown Option "$PARAM". Exit.
668         cleanup_and_exit 1
669       ;;
670       *)
671         SPECFILES[${#SPECFILES[@]}]="$PARAM";
672       ;;
673     esac
674 done
675
676 # XXX: doesn't kill vm if in prepare stage (because there is none)
677 if test -n "$KILL" ; then
678     test -z "$SRCDIR" || usage
679     if test -z "$VM_IMAGE" ; then
680         if ! $BUILD_DIR/killchroot -s 9 $BUILD_ROOT ; then
681             echo "could not kill build in $BUILD_ROOT"
682             cleanup_and_exit 1
683         fi
684     elif test "$VM_TYPE" = 'xen'; then
685         XENID="${VM_IMAGE%/root}"
686         XENID="${XENID##*/}"
687         if xm list "build:$XENID" >/dev/null 2>&1 ; then
688             if ! xm destroy "build:$XENID" ; then
689                 echo "could not kill xen build $XENID"
690                 cleanup_and_exit 1
691             fi
692         fi
693     elif test -n "$VM_TYPE"; then
694         if ! fuser -k -TERM "$VM_IMAGE"; then
695             echo "could not kill build in $VM_IMAGE"
696             cleanup_and_exit 1
697         fi
698     else
699         echo "don't know how to kill this build job"
700         cleanup_and_exit 1
701     fi
702     cleanup_and_exit 0
703 fi
704
705 if [ "$VM_TYPE" = 'kvm' ]; then
706     if [ ! -r /dev/kvm -o ! -x "$qemu_bin"-kvm ]; then
707         echo ""
708     fi
709     qemu_bin="$kvm_bin"
710     qemu_kernel="$kvm_kernel"
711     qemu_initrd="$kvm_initrd"
712     if [ ! -e $qemu_initrd-virtio -o $qemu_kernel -nt $qemu_initrd-virtio ]; then
713         if [ ! -w /root ]; then
714             echo "No initrd that provides virtio support found. virtio accelleration disabled."
715             echo "Run the following command as root to enable virtio:"
716             shellquote "${mkinitrd_virtio_cmd[@]}"
717             echo
718         elif /sbin/modinfo virtio_pci >/dev/null 2>&1; then
719             echo "creating $qemu_initrd-virtio"
720             "${mkinitrd_virtio_cmd[@]}" || cleanup_and_exit 1
721             kvm_virtio=1
722         fi
723     else
724         kvm_virtio=1
725     fi
726
727     if [ "$kvm_virtio" = 1 ]; then
728         qemu_initrd="$qemu_initrd-virtio"
729         VM_SWAPDEV=/dev/vdb
730         qemu_rootdev=/dev/vda
731     else
732         VM_SWAPDEV=/dev/sdb
733         qemu_rootdev=/dev/sda
734     fi
735 fi
736
737 if [ "$VM_TYPE" = 'qemu' ]; then
738     VM_SWAPDEV=/dev/sdb
739     qemu_rootdev=/dev/sda
740 fi
741
742 if [ -z "$RPMLIST" ]; then
743     if [ -z "$repos" -a -z "$BUILD_RPMS" ]; then
744         BUILD_RPMS="/media/dvd/suse"
745     fi
746 else
747     repos=()
748 fi
749
750 set_build_arch
751
752 if [ -n "$CLEAN_BUILD" ]; then
753     DO_INIT=true
754 fi
755
756 find_spec_files
757
758 if test -n "$LIST_STATE" ; then
759     BUILD_ROOT=`mktemp -d /var/tmp/build-list-state-XXXXXX`
760     test -d "$BUILD_ROOT" || cleanup_and_exit 1
761     SPECFILE=$SPECFILES # only one specified anyways
762     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
763        rm -rf $BUILD_ROOT/usr/src/packages
764        mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES $BUILD_ROOT/usr/src/packages/SPECS
765        rpm -i --nodigest --nosignature --root $BUILD_ROOT $SPECFILE || {
766            echo "could not install $SPECFILE." 2>&1
767            rm -rf $BUILD_ROOT
768            cleanup_and_exit 1
769        }
770        for SPECFILE in $BUILD_ROOT/usr/src/packages/SPECS/*.spec ; do : ; done
771     fi
772     init_buildsystem --list-state "${definesnstuff[@]}" "${repos[@]}" $USEUSEDFORBUILD $SPECFILE $BUILD_EXTRA_PACKS
773     ERR=$?
774     rm -rf $BUILD_ROOT
775     cleanup_and_exit $ERR
776 fi
777
778 if test -z "$RUNNING_IN_VM" ; then
779     if test -n "$VM_IMAGE" ; then
780         if test "$VM_IMAGE" = 1 ; then
781             VM_IMAGE="$BUILD_ROOT.img"
782         fi
783         if test ! -e "$VM_IMAGE" ; then
784             echo "you need to create a file system on $VM_IMAGE first"
785             cleanup_and_exit 1
786         fi
787     fi
788     if test -n "$VM_IMAGE" ; then
789         if test -n "$CLEAN_BUILD" ; then
790             echo "Creating filesystem on $VM_IMAGE"
791             $xen_img_mkfs $VM_IMAGE || become_root_or_fail
792         fi
793         mkdir_build_root
794         if [ -w /root ]; then
795             mount -o loop $VM_IMAGE $BUILD_ROOT || cleanup_and_exit 1
796         else
797             mount $BUILD_ROOT || become_root_or_fail
798         fi
799     else
800         test -w /root || become_root_or_fail
801     fi
802     if test -n "$VM_SWAP" ; then
803         dd if=/dev/zero of="$VM_SWAP" bs=12 count=1 conv=notrunc 2>/dev/null
804         mkswap "$VM_SWAP"
805     fi
806 fi
807
808 mkdir_build_root
809
810 rm -f $BUILD_ROOT/exit
811
812 if [ -w /root ]; then
813     mkdir -p $BUILD_ROOT/proc
814     mkdir -p $BUILD_ROOT/dev/pts
815     mount -n -tproc none $BUILD_ROOT/proc || true
816     mount -n -tdevpts none $BUILD_ROOT/dev/pts
817 fi
818
819 if test -z "$VM_IMAGE" -a -z "$LOGFILE"; then
820     LOGFILE="$BUILD_ROOT/.build.log"
821 fi
822
823 if test -n "$LOGFILE" ; then
824     echo  logging output to $LOGFILE...
825     rm -f $LOGFILE
826     touch $LOGFILE
827     if test -n "$VM_IMAGE" ; then
828         exec 1> >(exec -a 'build logging tee' perl -e 'open(F,">>",$ARGV[0])||die("$ARGV[0]: $!\n");$|=1;select(F);$|=1;while(<STDIN>){print STDOUT;s/^\r//s;s/\r\n/\n/gs;print F}' $LOGFILE) 2>&1
829     else
830         exec 1> >(exec -a 'build logging tee' tee -a $LOGFILE) 2>&1
831     fi
832 fi
833
834 setmemorylimit
835
836 #
837 # say hello
838 #
839 test -z "$HOST" && HOST=`hostname`
840
841 if [ -z "$RUNNING_IN_VM" ]; then
842     echo Using BUILD_ROOT=$BUILD_ROOT
843     test -n "$BUILD_RPMS" && echo Using BUILD_RPMS=$BUILD_RPMS
844     echo Using BUILD_ARCH=$BUILD_ARCH
845     test -n "$VM_TYPE" && echo "Doing $VM_TYPE build in $VM_IMAGE"
846     echo
847 fi
848
849 test "$BUILD_ARCH" = all && BUILD_ARCH=
850 BUILD_USER_ABUILD_USED=
851
852 for SPECFILE in "${SPECFILES[@]}" ; do
853
854     SRCDIR="${SPECFILE%/*}"
855     SPECFILE="${SPECFILE##*/}"
856
857     BUILDTYPE=
858     case $SPECFILE in
859       *.spec|*.src.rpm) BUILDTYPE=spec ;;
860       *.dsc) BUILDTYPE=dsc ;;
861       *.kiwi) BUILDTYPE=kiwi ;;
862     esac
863     if test -z "$BUILDTYPE" ; then
864        echo "don't know how to build $SPECFILE"
865        cleanup_and_exit 1
866     fi
867
868     cd "$SRCDIR"
869
870     if [ -z "$RUNNING_IN_VM" ]; then
871         echo
872         echo "$HOST started \"build $SPECFILE\" at `date`."
873         echo
874         test -n "$REASON" && echo "$REASON"
875         echo
876     fi
877
878     #
879     # first setup building directory...
880     #
881     test -s "$SPECFILE" || {
882        echo "$SPECFILE" is empty.  This should not happen...
883        cleanup_and_exit 1
884     }
885
886     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
887         echo processing src rpm `pwd`/$SPECFILE...
888         TOPDIR=`rpm --eval '%_topdir'`
889         rm -rf $BUILD_ROOT$TOPDIR
890         mkdir -p $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SPECS
891         rpm -i --nodigest --nosignature --root $BUILD_ROOT $SPECFILE || {
892             echo "could not install $SPECFILE."
893             cleanup_and_exit 1
894         }
895         rm -rf $BUILD_ROOT/.build-srcdir
896         mkdir -p $BUILD_ROOT/.build-srcdir
897         mv $BUILD_ROOT$TOPDIR/SOURCES/* $BUILD_ROOT/.build-srcdir
898         mv $BUILD_ROOT$TOPDIR/SPECS/* $BUILD_ROOT/.build-srcdir
899         MYSRCDIR=$BUILD_ROOT/.build-srcdir
900         cd $MYSRCDIR || cleanup_and_exit 1
901         for SPECFILE in *.spec ; do : ; done
902     else
903         echo processing specfile `pwd`/$SPECFILE...
904         MYSRCDIR="$SRCDIR"
905     fi
906
907     ADDITIONAL_PACKS=""
908     test -n "$BUILD_EXTRA_PACKS" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS $BUILD_EXTRA_PACKS"
909     test -n "$CREATE_BASELIBS" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS build"
910     test "$ccache" = '1' && ADDITIONAL_PACKS="$ADDITIONAL_PACKS ccache"
911     test "$icecream" -gt 1 && ADDITIONAL_PACKS="$ADDITIONAL_PACKS icecream gcc-c++"
912     test -n "$DO_LINT" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS rpmlint-Factory"
913
914     if test -n "$CHANGELOG" -a -z "$RUNNING_IN_VM" ; then
915         rm -f $BUILD_ROOT/.build-changelog
916         case $SPECFILE in
917           *.dsc) CFFORMAT=debian ;;
918           *) CFFORMAT=rpm ;;
919         esac
920         echo "running changelog2spec --target $CFFORMAT --file $MYSRCDIR/$SPECFILE"
921         if ! $BUILD_DIR/changelog2spec --target $CFFORMAT --file "$MYSRCDIR/$SPECFILE" > $BUILD_ROOT/.build-changelog ; then
922             rm -f $BUILD_ROOT/.build-changelog
923         fi
924     fi
925
926     if test -n "$VM_TYPE"; then
927         rm -rf $BUILD_ROOT/.build
928         mkdir -p $BUILD_ROOT/.build
929         if test "$DO_INIT" = true ; then
930             # do fist stage of init_buildsystem
931             rm -f $BUILD_ROOT/.build.success
932             set -- init_buildsystem --prepare "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS
933             echo "$* ..."
934             "$@" || cleanup_and_exit 1
935             if [ ! -w /root ]; then
936                 # remove setuid bit if files belong to user to make e.g. mount work
937                 find $BUILD_ROOT/{bin,sbin,usr/bin,usr/sbin} -type f -uid $UID -perm +4000 -print0 | xargs -0 --no-run-if-empty chmod -s
938             fi
939         fi
940         # start up xen, rerun ourself
941         cp -a $BUILD_DIR/. $BUILD_ROOT/.build
942         if ! test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
943             rm -rf $BUILD_ROOT/.build-srcdir
944             mkdir $BUILD_ROOT/.build-srcdir
945             if test "$BUILDTYPE" = kiwi ; then
946                 cp -pRL "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
947             else
948                 cp -p "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
949             fi
950             MYSRCDIR=$BUILD_ROOT/.build-srcdir
951         fi
952         Q="'\''"
953         echo "SPECFILE='${SPECFILE//"'"/$Q}'" > $BUILD_ROOT/.build/build.data
954         echo "BUILD_JOBS='${BUILD_JOBS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
955         echo "BUILD_ARCH='${BUILD_ARCH//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
956         echo "BUILD_RPMS='${BUILD_RPMS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
957         case $BUILD_DIST in
958             */*)
959                 cp $BUILD_DIST $BUILD_ROOT/.build/build.dist
960                 BUILD_DIST=/.build/build.dist
961                 ;;
962         esac
963         echo "BUILD_DIST='${BUILD_DIST//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
964         echo "RELEASE='${RELEASE//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
965         echo "BUILD_DEBUG='${BUILD_DEBUG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
966         echo "DO_LINT='${DO_LINT//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
967         echo "DO_CHECKS='${DO_CHECKS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
968         echo "NOROOTFORBUILD='${NOROOTFORBUILD//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
969         echo "CREATE_BASELIBS='$CREATE_BASELIBS'" >> $BUILD_ROOT/.build/build.data
970         echo "REASON='${REASON//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
971         echo "CHANGELOG='${CHANGELOG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
972         echo "INCARNATION='${INCARNATION//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
973         echo "DISTURL='${DISTURL//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
974         # FIXME: this depends on the kernel and vm.
975         # could be hda2, sda2 for xen or hdb/sdb for qemu
976         test -n "$VM_SWAP" && echo "VM_SWAP='${VM_SWAPDEV:-/dev/hda2}'" >> $BUILD_ROOT/.build/build.data
977         PERSONALITY=0
978         test -n "$PERSONALITY_SYSCALL" && PERSONALITY=`perl -e 'print syscall('$PERSONALITY_SYSCALL', 0)."\n"'`
979         echo "PERSONALITY='$PERSONALITY'" >> $BUILD_ROOT/.build/build.data
980         echo "MYHOSTNAME='`hostname`'" >> $BUILD_ROOT/.build/build.data
981         echo -n "definesnstuff=(" >> $BUILD_ROOT/.build/build.data
982         shellquote "${definesnstuff[@]}" >> $BUILD_ROOT/.build/build.data
983         echo ")" >> $BUILD_ROOT/.build/build.data
984         echo -n "repos=(" >> $BUILD_ROOT/.build/build.data
985         shellquote "${repos[@]}" >> $BUILD_ROOT/.build/build.data
986         echo ")" >> $BUILD_ROOT/.build/build.data
987         umount -n $BUILD_ROOT/proc 2> /dev/null || true
988         umount -n $BUILD_ROOT/dev/pts 2> /dev/null || true
989         umount -n $BUILD_ROOT/mnt 2> /dev/null || true
990         umount $BUILD_ROOT
991
992         if [ "$VM_TYPE" = 'xen' ]; then
993                 XMROOT=file:$VM_IMAGE
994                 XMROOT=${XMROOT/#file:\/dev/phy:/dev}
995                 XMROOT="disk=$XMROOT,hda1,w"
996                 XMSWAP=
997                 if test -n "$VM_SWAP" ; then
998                     XMSWAP=file:$VM_SWAP
999                     XMSWAP=${XMSWAP/#file:\/dev/phy:/dev}
1000                     XMSWAP="disk=$XMSWAP,hda2,w"
1001                 fi
1002                 XENID="${VM_IMAGE%/root}"
1003                 XENID="${XENID##*/}"
1004                 echo "booting XEN kernel ..."
1005                 set -- xm create -c $BUILD_DIR/xen.conf name="build:$XENID" ${MEMSIZE:+memory=$MEMSIZE} $XMROOT $XMSWAP extra="quiet init=/.build/build panic=1 console=ttyS0"
1006                 if test "$PERSONALITY" != 0 ; then
1007                     # have to switch back to PER_LINUX to make xm work
1008                     set -- linux64 "$@"
1009                 fi
1010                 echo "$@"
1011                 "$@" || true # somehow this is always false
1012         elif [ "$VM_TYPE" = 'uml' ]; then
1013                 echo "booting UML kernel ..."
1014                 set -- $uml_kernel initrd=$uml_initrd root=/ubda init=/.build/build panic=1 quiet ubd0=$VM_IMAGE ${MEMSIZE:+mem=$MEMSIZE}
1015                 echo "$@"
1016                 "$@"
1017         elif [ "$VM_TYPE" = 'qemu' -o "$VM_TYPE" = 'kvm' ]; then
1018                 echo "booting QEMU ..."
1019                 if [ "$kvm_virtio" = 1 ]; then
1020                     qemu_disks=(-drive file="$VM_IMAGE",if=virtio -hda /dev/null)
1021                     if [ -n "$VM_SWAP" ]; then
1022                         qemu_disks[${#qemu_disks[@]}]="-drive"
1023                         qemu_disks[${#qemu_disks[@]}]="file=$VM_SWAP,if=virtio"
1024                     fi
1025                 else
1026                     qemu_disks=(-hda "$VM_IMAGE")
1027                     if [ -n "$VM_SWAP" ]; then
1028                         qemu_disks[${#qemu_disks[@]}]="-hdb"
1029                         qemu_disks[${#qemu_disks[@]}]="$VM_SWAP"
1030                     fi
1031                 fi
1032                 set -- $qemu_bin -no-reboot -nographic -net none -serial stdio \
1033                     -kernel $qemu_kernel \
1034                     -initrd $qemu_initrd \
1035                     -append "root=$qemu_rootdev panic=1 quiet rw console=ttyS0,115200 init=/.build/build" \
1036                     ${MEMSIZE:+-m $MEMSIZE} \
1037                     "${qemu_disks[@]}"
1038
1039                 if test "$PERSONALITY" != 0 ; then
1040                     # have to switch back to PER_LINUX to make qemu work
1041                     set -- linux64 "$@"
1042                 fi
1043                 echo "$@"
1044                 "$@"
1045         fi
1046         if test -n "$VM_SWAP" ; then
1047             BUILDSTATUS=`dd if="$VM_SWAP" bs=12 count=1 2>/dev/null`
1048             case $BUILDSTATUS in
1049               BUILDSTATUS0)
1050                 mkdir -p $BUILD_ROOT/.build.packages 
1051                 cd $BUILD_ROOT/.build.packages || cleanup_and_exit 1
1052                 echo "extracting built packages..."
1053                 dd if=$VM_SWAP bs=512 skip=1 | cpio --extract --no-absolute-filenames -v
1054                 cleanup_and_exit 0
1055                 ;;
1056               BUILDSTATUS[1-9])
1057                 cleanup_and_exit ${BUILDSTATUS#BUILDSTATUS}
1058                 ;;
1059             esac
1060             cleanup_and_exit 1
1061         fi
1062         cleanup_and_exit 0
1063     fi
1064
1065     if test "$DO_INIT" = true ; then
1066         #
1067         # create legacy .buildenv file
1068         #
1069         test -z "$INCARNATION" && INCARNATION=0
1070         echo "BUILD_INCARNATION=$INCARNATION" > $BUILD_ROOT/.buildenv
1071
1072         CREATE_BUILD_BINARIES=
1073         egrep '^#[       ]*needsbinariesforbuild[       ]*$' >/dev/null <$MYSRCDIR/$SPECFILE && CREATE_BUILD_BINARIES=--create-build-binaries
1074         set -- init_buildsystem "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $CREATE_BUILD_BINARIES $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS
1075         echo "$* ..."
1076         "$@" || cleanup_and_exit 1
1077         mount -n -tproc none $BUILD_ROOT/proc || true
1078         mount -n -tdevpts none $BUILD_ROOT/dev/pts
1079     fi
1080
1081     if test -z "$BUILD_DIST" -a -e "$BUILD_ROOT/.guessed_dist" ; then
1082         BUILD_DIST=`cat $BUILD_ROOT/.guessed_dist`
1083         echo "assuming dist $BUILD_DIST"
1084     fi
1085
1086     #
1087     # fix rpmrc if we are compiling for i686
1088     #
1089     test -f $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 && mv $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 $BUILD_ROOT/usr/lib/rpm/rpmrc
1090     if test -e $BUILD_ROOT/usr/lib/rpm/rpmrc -a "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
1091         mv $BUILD_ROOT/usr/lib/rpm/rpmrc $BUILD_ROOT/usr/lib/rpm/rpmrc_i586
1092         sed -e 's/^buildarchtranslate: athlon.*/buildarchtranslate: athlon: i686/' -e 's/^buildarchtranslate: i686.*/buildarchtranslate: i686: i686/' < $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 > $BUILD_ROOT/usr/lib/rpm/rpmrc
1093     fi
1094
1095     #
1096     # check if we want to build with the abuild user
1097     #
1098     BUILD_USER=abuild
1099     if test -x $BUILD_ROOT/bin/rpm ; then
1100         SUSE_VERSION=`chroot $BUILD_ROOT /bin/rpm --eval '%{?suse_version}' 2>/dev/null`
1101         if test -n "$SUSE_VERSION" && test "$SUSE_VERSION" -le 1020 ; then
1102             BUILD_USER=root
1103         fi
1104     fi
1105     if test "$BUILD_USER" = abuild ; then
1106         egrep '^#[       ]*needsrootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=root
1107     else
1108         egrep '^#[       ]*norootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=abuild
1109     fi
1110     test -n "$NOROOTFORBUILD" && BUILD_USER=abuild
1111     test "$BUILDTYPE" = kiwi && BUILD_USER=root
1112
1113     if test $BUILD_USER = abuild ; then
1114         if ! egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1115             echo 'abuild::399:399:Autobuild:/home/abuild:/bin/bash' >>$BUILD_ROOT/etc/passwd
1116             echo 'abuild:*:::::::' >>$BUILD_ROOT/etc/shadow # This is needed on Mandriva 2009
1117             echo 'abuild::399:' >>$BUILD_ROOT/etc/group
1118             mkdir -p $BUILD_ROOT/home/abuild
1119             chown 399:399 $BUILD_ROOT/home/abuild
1120         fi
1121         if test -f $BUILD_ROOT/etc/shadow ; then
1122             sed -e "s@^root::@root:*:@" < $BUILD_ROOT/etc/shadow > $BUILD_ROOT/etc/shadow.t && mv $BUILD_ROOT/etc/shadow.t $BUILD_ROOT/etc/shadow
1123         fi
1124         BUILD_USER_ABUILD_USED=true
1125     else
1126         if egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1127             egrep -v '^abuild:' <$BUILD_ROOT/etc/passwd >$BUILD_ROOT/etc/passwd.new
1128             mv $BUILD_ROOT/etc/passwd.new $BUILD_ROOT/etc/passwd
1129             egrep -v '^abuild:' <$BUILD_ROOT/etc/shadow >$BUILD_ROOT/etc/shadow.new
1130             mv $BUILD_ROOT/etc/shadow.new $BUILD_ROOT/etc/shadow
1131             egrep -v '^abuild:' <$BUILD_ROOT/etc/group >$BUILD_ROOT/etc/group.new
1132             mv $BUILD_ROOT/etc/group.new $BUILD_ROOT/etc/group
1133             rm -rf $BUILD_ROOT/home/abuild
1134         fi
1135     fi
1136
1137     if test "$BUILDTYPE" = spec ; then
1138         TOPDIR=`chroot $BUILD_ROOT su -c "rpm --eval '%_topdir'" - $BUILD_USER`
1139     else
1140         TOPDIR=/usr/src/packages
1141         mkdir -p $BUILD_ROOT$TOPDIR
1142     fi
1143
1144     rm -f $BUILD_ROOT/.build.packages
1145     ln -s ${TOPDIR#/} $BUILD_ROOT/.build.packages
1146
1147     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1148     mount -n -tdevpts none $BUILD_ROOT/dev/pts 2> /dev/null
1149  
1150     setupicecream
1151
1152     setupccache
1153
1154     # nasty hack to prevent rpath on known paths
1155     # FIXME: do this only for suse
1156     if test -d "$BUILD_ROOT/etc/profile.d" ; then
1157         echo "export SUSE_IGNORED_RPATHS=/etc/ld.so.conf" > "$BUILD_ROOT/etc/profile.d/buildsystem.sh"
1158     fi
1159
1160     #
1161     # now clean up RPM building directories
1162     #
1163     rm -rf $BUILD_ROOT$TOPDIR
1164     for i in BUILD RPMS/`uname -m` RPMS/i386 RPMS/noarch SOURCES SPECS SRPMS BUILDROOT; do
1165         mkdir -p $BUILD_ROOT$TOPDIR/$i
1166         test $BUILD_USER = abuild && chown 399:399 $BUILD_ROOT$TOPDIR/$i
1167     done
1168     test -e $BUILD_ROOT/exit && cleanup_and_exit
1169
1170     mkdir -p $BUILD_ROOT$TOPDIR/SOURCES
1171     if test "$BUILDTYPE" = kiwi ; then
1172         mkdir -p $BUILD_ROOT$TOPDIR/KIWI
1173         if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
1174             mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1175         else
1176             cp -dLR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1177         fi
1178     else
1179         cp -p "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1180     fi
1181     test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir && rm -rf "$MYSRCDIR"
1182
1183     CHANGELOGARGS=
1184     test -n "$CHANGELOG" -a -f "$BUILD_ROOT/.build-changelog" && CHANGELOGARGS="--changelog $BUILD_ROOT/.build-changelog"
1185
1186     if test "$BUILDTYPE" = spec ; then
1187         # do buildrequires/release substitution
1188         args=()
1189         if test -n "$RELEASE"; then
1190                 args=(--release "$RELEASE")
1191         fi
1192         substitutedeps "${args[@]}" --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" $CHANGELOGARGS "$BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE" "$BUILD_ROOT/.spec.new" || cleanup_and_exit 1
1193         # extract macros from configuration
1194         getmacros --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" > $BUILD_ROOT/root/.rpmmacros
1195         if test -n "$BUILD_DEBUG" ; then
1196             echo '
1197 %prep %?_suse_insert_debug_package%%prep
1198 %package %?_suse_insert_debug_package%%package
1199 %suse_insert_debug_package \
1200   %global _suse_insert_debug_package \\\
1201     %%undefine _suse_insert_debug_package \\\
1202     %%debug_package
1203
1204 ' >> $BUILD_ROOT/root/.rpmmacros
1205         fi
1206         test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmmacros $BUILD_ROOT/home/abuild/.rpmmacros
1207         # extract optflags from configuration
1208         getoptflags --dist "$BUILD_DIST" --configdir "$BUILD_DIR/configs" --archpath "$BUILD_ARCH" ${BUILD_DEBUG:+--debug} > $BUILD_ROOT/root/.rpmrc
1209         test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmrc $BUILD_ROOT/home/abuild/.rpmrc
1210     fi
1211     if test -f $BUILD_ROOT/.spec.new ; then
1212         if ! cmp -s $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT/.spec.new ; then
1213             echo -----------------------------------------------------------------
1214             echo I have the following modifications for $SPECFILE:
1215             diff $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT/.spec.new
1216             mv $BUILD_ROOT/.spec.new $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE
1217         else
1218             rm -f $BUILD_ROOT/.spec.new
1219         fi
1220     fi
1221
1222     if test "$BUILDTYPE" = dsc ; then
1223         rm -rf $BUILD_ROOT$TOPDIR/BUILD
1224         test $BUILD_USER = abuild && chown 399:399 $BUILD_ROOT$TOPDIR
1225         DEB_TRANSFORM=
1226         DEB_SOURCEDIR=$TOPDIR/SOURCES
1227         DEB_DSCFILE=$SPECFILE
1228         for f in $BUILD_ROOT$TOPDIR/SOURCES/debian.* ; do
1229             test -f $f && DEB_TRANSFORM=true
1230         done
1231         if test -n "$DEB_TRANSFORM" ; then
1232             echo "running debian transformer..."
1233             mkdir -p $BUILD_ROOT$TOPDIR/SOURCES.DEB
1234             if ! debtransform $CHANGELOGARGS $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT$TOPDIR/SOURCES.DEB ; then
1235                 echo "debian transforming failed."
1236                 cleanup_and_exit 1
1237             fi
1238             DEB_SOURCEDIR=$TOPDIR/SOURCES.DEB
1239             for DEB_DSCFILE in $BUILD_ROOT/$DEB_SOURCEDIR/*.dsc ; do : ; done
1240             DEB_DSCFILE="${DEB_DSCFILE##*/}"
1241         fi
1242         chroot $BUILD_ROOT su -c "dpkg-source -x $DEB_SOURCEDIR/$DEB_DSCFILE $TOPDIR/BUILD" - $BUILD_USER
1243     fi
1244
1245     if test $BUILD_USER = abuild ; then
1246         chown -R 399:399 $BUILD_ROOT$TOPDIR/*
1247     else
1248         chown -R root:root $BUILD_ROOT$TOPDIR/*
1249     fi
1250     cd $BUILD_ROOT$TOPDIR/SOURCES || cleanup_and_exit 1
1251
1252     echo -----------------------------------------------------------------
1253     if test "$BUILD_USER" = root ; then
1254         echo ----- building $SPECFILE
1255     else
1256         echo ----- building $SPECFILE "(user $BUILD_USER)"
1257     fi
1258     echo -----------------------------------------------------------------
1259     echo -----------------------------------------------------------------
1260     test -n "$RUNNING_IN_VM" && ifconfig lo 127.0.0.1 up
1261     test -n "$RUNNING_IN_VM" -a -n "$MYHOSTNAME" && hostname "$MYHOSTNAME"
1262
1263     BUILD_SUCCEEDED=false
1264
1265     if test "$BUILDTYPE" = spec ; then
1266         test -z "$BUILD_RPM_BUILD_STAGE" && BUILD_RPM_BUILD_STAGE=-ba
1267
1268         # XXX: move _srcdefattr to macro file?
1269         rpmbopts=("$BUILD_RPM_BUILD_STAGE" "--define" "_srcdefattr (-,root,root)")
1270         if [ -n "$ABUILD_TARGET_ARCH" ]; then
1271                 rpmbopts[${#rpmbopts[@]}]="--target=$ABUILD_TARGET_ARCH"
1272         fi
1273         if [ -n "$BUILD_JOBS" ]; then
1274                 rpmbopts[${#rpmbopts[@]}]='--define'
1275                 rpmbopts[${#rpmbopts[@]}]="jobs $BUILD_JOBS"
1276         fi
1277         if [ -n "$DISTURL" ]; then
1278                 rpmbopts[${#rpmbopts[@]}]='--define'
1279                 rpmbopts[${#rpmbopts[@]}]="disturl $DISTURL"
1280         fi
1281         if test -s "$BUILD_ROOT/usr/lib/rpm/mandriva/macros" ; then
1282                 rpmbopts[${#rpmbopts[@]}]='--eval'
1283                 rpmbopts[${#rpmbopts[@]}]="%undefine _enable_debug_packages"
1284         fi
1285         if [ -n "$BUILD_DEBUG" ]; then
1286                 rpmbopts[${#rpmbopts[@]}]='--eval'
1287                 rpmbopts[${#rpmbopts[@]}]="%suse_insert_debug_package"
1288         fi
1289         rpmbuild=rpmbuild
1290
1291         test -x $BUILD_ROOT/usr/bin/rpmbuild || rpmbuild=rpm
1292         # su involves a shell which would require even more
1293         # complicated quoting to bypass than this
1294         toshellscript $rpmbuild \
1295                 "${definesnstuff[@]}" \
1296                 "${rpmbopts[@]}" \
1297                 "$TOPDIR/SOURCES/$SPECFILE" \
1298                 > $BUILD_ROOT/.build.command
1299         chmod 755 $BUILD_ROOT/.build.command
1300         chroot $BUILD_ROOT su -c /.build.command - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1301     fi
1302
1303     if test "$BUILDTYPE" = dsc ; then
1304         chroot $BUILD_ROOT su -c "cd $TOPDIR/BUILD && dpkg-buildpackage -us -uc -rfakeroot-tcp" - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1305         mkdir -p $BUILD_ROOT/$TOPDIR/DEBS
1306         for DEB in $BUILD_ROOT/$TOPDIR/*.deb ; do
1307             test -e "$DEB" && mv "$DEB" "$BUILD_ROOT/$TOPDIR/DEBS"
1308         done
1309         # link sources over
1310         ln $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE $BUILD_ROOT/$TOPDIR/DEBS/
1311         while read f ; do
1312             ln $BUILD_ROOT/$DEB_SOURCEDIR/$f $BUILD_ROOT/$TOPDIR/DEBS/
1313         done < <(sed -ne '/^Files:/,$s/^ ................................ [0-9][0-9]* //p' < $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE)
1314     fi
1315
1316     if test "$BUILDTYPE" = kiwi ; then
1317         # run createrepo on the repositories
1318         if test -d $BUILD_ROOT/$TOPDIR/SOURCES/repos ; then
1319             (
1320             cd $BUILD_ROOT/$TOPDIR/SOURCES/repos
1321             for r in */* ; do
1322                 test -L $r && continue
1323                 test -d $r || continue
1324                 repo="$TOPDIR/SOURCES/repos/$r/"
1325                 # create compatibility link
1326                 rc="${r//:/:/}"
1327                 if test "$rc" != "$r" ; then
1328                     rl="${rc//[^\/]}"
1329                     rl="${rl//?/../}"
1330                     mkdir -p "${rc%/*}"
1331 ls -l "${rc%/*}/"
1332                     ln -s $rl$r "${rc%/*}/${rc##*/}"
1333                     repo="$TOPDIR/SOURCES/repos/${rc%/*}/${rc##*/}/"
1334                 fi
1335                 echo "creating repodata for $repo"
1336                 chroot $BUILD_ROOT createrepo "$repo"
1337             done
1338             )
1339         fi
1340         # unpack tar files in image directories
1341         if test -d $BUILD_ROOT/$TOPDIR/SOURCES/images ; then
1342             (
1343             cd $BUILD_ROOT/$TOPDIR/SOURCES/images
1344             for r in */* ; do
1345                 test -L $r && continue
1346                 test -d $r || continue
1347                 for t in $r/root.tar* ; do
1348                     test -f $t || continue
1349                     mkdir -p $r/root
1350                     chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/images/$r/root -xf "$TOPDIR/SOURCES/images/$r/$t{##*/}"
1351                 done
1352                 # create compatibility link
1353                 rc="${r//:/:/}"
1354                 if test "$rc" != "$r" ; then
1355                     rl="${rc//[^\/]}"
1356                     rl="${rl//?/../}"
1357                     mkdir -p "${rc%/*}"
1358                     ln -s $rl$r "${rc%/*}/${rc##*/}"
1359                 fi
1360             done
1361             )
1362         fi
1363         rm -f $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
1364         ln -s $SPECFILE $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
1365         imagetype=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE imagetype)
1366         chroot $BUILD_ROOT su -c "kiwi --version" -
1367         if test "$imagetype" = product ; then
1368             echo "running kiwi --create-instsource..."
1369             chroot $BUILD_ROOT su -c "kiwi --root $TOPDIR/KIWIROOT -v -v --instsource-local --logfile terminal -p $TOPDIR/SOURCES --create-instsource $TOPDIR/SOURCES" - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1370             for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
1371                 test -d "$i" || continue
1372                 n="${i##*/}"
1373                 test "$n" = scripts && continue
1374                 test "$n" != "${n%0}" && continue
1375                 chroot $BUILD_ROOT su -c "suse-isolinux $TOPDIR/KIWIROOT/main/$n $TOPDIR/KIWI/$n.iso" - $BUILD_USER
1376             done
1377         else
1378             BUILD_SUCCEEDED=true
1379             for imagetype in $imagetype ; do
1380                 echo "running kiwi --prepare for $imagetype..."
1381                 if chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --prepare $TOPDIR/SOURCES --logfile terminal --root $TOPDIR/KIWIROOT-$imagetype" - $BUILD_USER < /dev/null ; then
1382                     echo "running kiwi --create for $imagetype..."
1383                     chroot $BUILD_ROOT su -c "kiwi --create $TOPDIR/KIWIROOT-$imagetype --logfile terminal --type $imagetype -d $TOPDIR/KIWI" - $BUILD_USER < /dev/null || BUILD_SUCCEEDED=
1384                 else
1385                     BUILD_SUCCEEDED=
1386                 fi
1387                 test -z "$BUILD_SUCCEEDED" && break
1388             done
1389         fi
1390     fi
1391
1392     test "$BUILD_SUCCEEDED" = true || cleanup_and_exit 1
1393     test -d "$SRCDIR" && cd "$SRCDIR"
1394 done
1395
1396 RPMS=`find $BUILD_ROOT/$TOPDIR/RPMS -type f -name "*.rpm"`
1397
1398 if test -n "$RPMS" -a -n "$BUILD_USER_ABUILD_USED" ; then
1399     echo "... checking for files with abuild user/group"
1400     BADFILE=
1401     while read un gn fn ; do
1402         if test "$un" = abuild -o "$gn" = abuild -o "$un" = 399 -o "$gn" = 399 ; then
1403             echo "  $un $gn $fn"
1404             BADFILE=true
1405         fi
1406     done < <(rpm -qp --qf '[%{FILEUSERNAME} %{FILEGROUPNAME} %{FILENAMES}\n]' $RPMS)
1407     if test -n "$BADFILE" ; then
1408         echo "please fix your filelist (e.g. add defattr)"
1409         cleanup_and_exit 1
1410     fi
1411 fi
1412
1413 if test -n "$RPMS" -a -d "$BUILD_ROOT/usr/lib/build/checks" ; then
1414     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1415     export PNAME=""
1416     export DO_RPM_REMOVE=true
1417     for SRPM in $BUILD_ROOT/$TOPDIR/SRPMS/*src.rpm ; do
1418         test -f "$SRPM" && PNAME=`rpm --nodigest --nosignature -qp --qf "%{NAME}" $SRPM`
1419     done
1420     for CHECKSCRIPT in $BUILD_ROOT/usr/lib/build/checks/* ; do
1421         echo "... running `basename $CHECKSCRIPT`"
1422         $CHECKSCRIPT || cleanup_and_exit 1
1423     done
1424     umount -n $BUILD_ROOT/proc 2> /dev/null
1425 fi
1426
1427 if test -n "$RPMS" -a "$DO_CHECKS" != "false" -a -x "$BUILD_ROOT/opt/testing/bin/rpmlint" -a -d "$BUILD_ROOT/$TOPDIR/RPMS" ; then
1428     LINT_RPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/RPMS \
1429         \( -name "*-debuginfo-*" -o -name "*-debugsource-*" \
1430         -o -name "*-32bit-*" -o -name "*-64bit-*" \
1431         -o -name "*-x86-*" -o -name "*-ia32-*" \) -prune \
1432         -o -type f -name '*.rpm' -print))
1433     SRPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/SRPMS -type f -name "*.rpm"))
1434     echo
1435     echo "RPMLINT report:"
1436     echo "==============="
1437     chroot $BUILD_ROOT /opt/testing/bin/rpmlint --info ${LINT_RPM_FILE_LIST[*]#$BUILD_ROOT} ${SRPM_FILE_LIST[*]#$BUILD_ROOT} || cleanup_and_exit 1
1438 fi
1439
1440 if test -n "$RPMS" -a -n "$CREATE_BASELIBS" -a -d "$BUILD_ROOT/$TOPDIR/RPMS" ; then
1441     if test -f $BUILD_ROOT/usr/lib/build/mkbaselibs -o "$CREATE_BASELIBS" != internal ; then
1442         mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1443         create_baselibs
1444         umount -n $BUILD_ROOT/proc 2> /dev/null
1445     fi
1446 fi
1447
1448 if test -n "$RUNNING_IN_VM" -a -n "$VM_SWAP"; then
1449     echo "... saving built packages"
1450     swapoff "$VM_SWAP"
1451     $BUILD_DIR/extractbuild "$VM_SWAP" || cleanup_and_exit 1
1452 fi
1453
1454 echo "... build is finished"
1455 cleanup_and_exit 0