make sure proc is mounted for build-compare (bnc#590462)
[opensuse:dl9pfs-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_HOST_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 #vm_img_mkfs='mkreiserfs -q -f'
23 #vm_img_mkfs='mkfs.ext2 -m 0 -q -F'
24 vm_img_mkfs='mkfs.ext3 -m 0 -q -F'
25 vm_img_tunefs='tune2fs -c 0'
26 qemu_kernel=/boot/vmlinuz
27 qemu_initrd=/boot/initrd
28 qemu_bin=/usr/bin/qemu
29 uml_kernel=/boot/vmlinux-um
30 uml_initrd=/boot/initrd-um
31
32 kvm_kernel=/boot/vmlinuz
33 kvm_initrd=/boot/initrd
34 kvm_bin=/usr/bin/qemu-kvm
35 mkinitrd_virtio_cmd=(env rootfstype=ext3 mkinitrd -d /dev/null -m "virtio_pci virtio_blk" -k $kvm_kernel -i $kvm_initrd-virtio)
36 # whether we have virtio support
37 kvm_virtio=
38
39 # Default uid:gid for the build user
40 ABUILD_UID=399
41 ABUILD_GID=399
42
43 DO_INIT=true
44 DO_LINT=
45 DO_CHECKS=true
46 CLEAN_BUILD=
47 SPECFILES=()
48 SRCDIR=
49 BUILD_JOBS=
50 ABUILD_TARGET_ARCH=
51 CREATE_BASELIBS=
52 USEUSEDFORBUILD=
53 LIST_STATE=
54 VM_IMAGE=
55 VM_SWAP=
56 MEMSIZE=
57 RUNNING_IN_VM=
58 RPMLIST=
59 RELEASE=
60 REASON=
61 NOROOTFORBUILD=
62 LOGFILE=
63 KILL=
64 CHANGELOG=
65 BUILD_DEBUG=
66 PERSONALITY_SYSCALL=
67 INCARNATION=
68 DISTURL=
69 LINKSOURCES=
70 CHANGETARGET=
71 OVERLAY=
72 RSYNCSRC=
73 RSYNCDEST=
74 RSYNCDONE=
75
76 # This is for insserv
77 export YAST_IS_RUNNING=instsys
78
79 unset LANGUAGE
80 unset LANG
81 export LC_ALL=POSIX
82 umask 022
83
84 echo_help () {
85     cat << EOT
86
87 Some comments for build
88 -----------------------
89
90 With build you can create rpm packages.  They will be built in a chroot
91 system.  This chroot system will be setup automatically.  Normally you can
92 simply call build with a spec file as parameter - nothing else has to be
93 set.
94
95 If you want to set the directory were the chroot system will be setup
96 (at the moment it uses $BUILD_ROOT),
97 simply set the the environment variable BUILD_ROOT.
98
99 Example:
100
101   export BUILD_ROOT=/var/tmp/mybuildroot
102
103
104 Normally build builds the complete package including src.rpm (rpmbuild -ba).
105 If you want let build only make the binary package, simply set
106
107    export BUILD_RPM_BUILD_STAGE=-bb
108
109 (or -bc, -bp, -bi, ...  see "Maximum RPM" for more details [*]).
110
111 When the build command succeeds, the rpm files can be found under
112 $BUILD_ROOT/usr/src/packages/RPMS/
113
114
115 Known Parameters:
116
117   --help      You already got it :)
118
119   --clean     Delete old build root before initializing it
120
121   --no-init   Skip initialization of build root and start with build
122               immediately.
123
124   --no-checks Do not run post-build checks
125
126   --repository PATH
127               Use package repository at PATH. Supported formats are
128               rpm-md and yast2.
129               Alternatively zypp://NAME specifies the zypp
130               repository NAME. The repo must be refreshed with zypp
131               so package meta data is available locally. With emtpy
132               NAME all enabled repositories are used.
133
134   --rpms path1:path2:...
135               Specify path where to find the RPMs for the build system
136
137   --arch arch1:arch2:...
138               Specify what architectures to select from the RPMs
139
140   --verify    Run verify when initializing the build root
141
142   --extra-packs pack
143               Also install package 'pack'
144
145   --root rootdir
146               Use 'rootdir' to setup chroot environment
147
148   --oldpackages oldpackagesdir
149               Define a directory with a former build
150
151   --baselibs  Create -32bit/-64bit/-x86 rpms for other architectures
152
153   --list-state
154               List rpms that would be used to create a fresh build root.
155               Does not create the build root or perform a build.
156
157   --with X
158               enable feature X for build
159
160   --without X
161               disable feature X for build
162
163   --define 'X Y'
164               define macro X with value Y
165
166   --ccache
167               use ccache to speed up rebuilds
168
169   --icecream N
170               use N parallel build jobs with icecream
171   --overlay OVERLAY
172               Copy overlay filesystem to buildroot after installing
173               all RPMs. This must be a valid directory.
174
175   --rsync-src RSYNCSRC
176               Copy overlay folder (RSYNCSRC) to a folder (RSYNCDEST)
177               inside the buildroot using rsync.
178               It will "%define RSYNCDONE 1" for handling %setup in your
179               specfile. E.g.:
180               %prep
181               %if 0%{?RSYNCDONE}
182               %setup -n aaa_base -T -D -b 5 -b 7
183               %else
184               %setup -n aaa_base -b 5 -b 7
185               %endif
186
187   --rsync-dest RSYNCDEST
188               Copy overlay folder (RSYNCSRC) to a folder (RSYNCDEST)
189               inside the buildroot using rsync.
190
191   --uid uid:gid
192               Specify the uid and gid to use for the abuild user.
193               This is useful if you are hacking in the buildroot.
194               This must be set to the same value if the buildroot is re-used.
195
196   --debug
197               enable creation of a debuginfo package
198
199 Remember to have fun!
200
201 [*] Maximum RPM: http://www.rpm.org/max-rpm/
202 EOT
203 }
204 usage () {
205     echo "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|spec-to-build]"
206     cleanup_and_exit 1
207 }
208
209 #
210 #  cleanup_and_exit
211 #  return values: 0 -> success, new packages built
212 #                 1 -> error, build failed
213 #                 2 -> successfull build, but no changes to former built packages
214 #                 3 -> something wrong with build host
215 #
216 cleanup_and_exit () {
217     trap EXIT
218     test -z "$1" && set 0
219     if test -n "$RUNNING_IN_VM" ; then
220         cd /
221         if test -n "$VM_SWAP" -a -e "$VM_SWAP" ; then
222             swapoff "$VM_SWAP" 2>/dev/null
223             echo -n "BUILDSTATUS$1" >"$VM_SWAP"
224         fi
225         exec >&0 2>&0        # so that the logging tee finishes
226         sleep 1                # wait till tee terminates
227         if test "$VM_TYPE" != lxc; then
228             kill -9 -1        # goodbye cruel world
229             exec /bin/bash -c 'mount -n -o remount,ro / ; halt -f -p'
230             halt -f -p
231         fi
232     else
233         umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
234         umount -n $BUILD_ROOT/proc 2>/dev/null || true
235         umount -n $BUILD_ROOT/dev/pts 2>/dev/null || true
236         test "$VM_IMAGE" = 1 && VM_IMAGE=
237         [ -n "$VM_IMAGE" ] && umount $BUILD_ROOT 2>/dev/null || true
238     fi
239 #    echo "pid $$ exit $1"
240     exit $1
241 }
242
243 fail_exit()
244 {
245   cleanup_and_exit 1
246 }
247
248 shellquote()
249 {
250     for arg; do
251         arg=${arg/\\/\\\\}
252         arg=${arg/\$/\\\$}
253         arg=${arg/\"/\\\"}
254         arg=${arg/\`/\\\`}
255         echo -n " \"$arg\""
256     done
257 }
258
259 # create a shell script from command line. Used for preserving arguments
260 # through /bin/su -c
261 toshellscript()
262 {
263         echo "#!/bin/sh -x"
264         echo -n exec
265         shellquote "$@"
266         echo
267 }
268
269 setupccache()
270 {
271     if [ "$ccache" = 1 ]; then
272         if mkdir -p $BUILD_ROOT/var/lib/build/ccache/bin; then
273             for i in gcc g++ cc c++; do
274 #                ln -sf /usr/bin/ccache $BUILD_ROOT/var/lib/build/ccache/bin/$i
275                 rm -f $BUILD_ROOT/var/lib/build/ccache/bin/$i
276                 test -e $BUILD_ROOT/usr/bin/$i || continue
277                 echo '#! /bin/sh' > $BUILD_ROOT/var/lib/build/ccache/bin/$i
278                 echo "test -e /usr/bin/$i || exit 1" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
279                 echo 'export PATH=/opt/icecream/bin:/usr/bin:$PATH' >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
280                 echo "ccache $i \"\$@\"" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
281                 chmod 755 $BUILD_ROOT/var/lib/build/ccache/bin/$i
282                 echo "Installed ccache wrapper as $BUILD_ROOT/var/lib/build/ccache/bin/$i"
283             done
284         fi
285         mkdir -p "$BUILD_ROOT"/.ccache
286         chroot "$BUILD_ROOT" chown -R "$BUILD_USER" "/.ccache"
287         echo "export CCACHE_DIR=/.ccache" > "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
288         echo 'export PATH=/var/lib/build/ccache/bin:$PATH' >> "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
289     else
290         rm -f "$BUILD_ROOT$builduserhome"/bin/{gcc,g++,cc,c++}
291         rm -f "$BUILD_ROOT"/var/lib/build/ccache/bin/{gcc,g++,cc,c++}
292     fi
293 }
294
295 setupicecream()
296 {
297     if [ "$icecream" -eq 0 ]; then
298         rm -rf "$BUILD_ROOT"/var/run/icecream
299         rm -f "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
300         return
301     fi
302
303     if ! chroot "$BUILD_ROOT" rpm -q icecream >/dev/null 2>/dev/null; then
304         echo "*** icecream package not installed ***"
305         false
306         return
307     fi
308
309     echo "using icecream with $icecream jobs"
310
311     if [ "$ccache" -ne 1 ]; then
312         echo 'export PATH=/opt/icecream/bin:$PATH' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
313     else
314         echo 'export CCACHE_PATH=/opt/icecream/bin' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
315     fi
316
317     local icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
318     icecc_vers=${icecc_vers//$BUILD_ROOT/}
319
320     # XXX use changelog like autobuild does instead?
321     # only run create-env if compiler or glibc changed
322     if [ -z "$icecc_vers" \
323         -o ! -e "$BUILD_ROOT/$icecc_vers" \
324         -o "$BUILD_ROOT/usr/bin/gcc" -nt "$BUILD_ROOT/$icecc_vers" \
325         -o "$BUILD_ROOT/usr/bin/g++" -nt "$BUILD_ROOT/$icecc_vers" \
326         -o "$BUILD_ROOT/usr/bin/as" -nt "$BUILD_ROOT/$icecc_vers" \
327         -o "$BUILD_ROOT/lib/libc.so.6" -nt "$BUILD_ROOT/$icecc_vers" ]
328     then
329         rm -rf $BUILD_ROOT/var/run/icecream
330         mkdir -p $BUILD_ROOT/var/run/icecream
331         if [ -e "$BUILD_ROOT"/usr/bin/create-env ]; then
332           createenv=/usr/bin/create-env
333         elif [ -e "$BUILD_ROOT"/usr/lib/icecc/icecc-create-env ]; then
334           createenv="/usr/lib/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
335         elif [ -e "$BUILD_ROOT"/usr/lib64/icecc/icecc-create-env ]; then
336           createenv="/usr/lib64/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
337         else
338           echo "create-env not found"
339           false
340           return
341         fi
342         chroot $BUILD_ROOT bash -c "cd /var/run/icecream; $createenv" || cleanup_and_exit 1
343         icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
344         icecc_vers=${icecc_vers//$BUILD_ROOT/}
345     else
346         echo "reusing existing icecream environment $icecc_vers"
347     fi
348     if [ -n "$icecc_vers" ]; then
349       echo "export ICECC_VERSION=$icecc_vers" >> "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
350     fi
351 }
352
353 setmemorylimit()
354 {
355     if [ -n "$VM_IMAGE" -o -n "$RUNNING_IN_VM" ]; then
356         return
357     fi
358     local mem
359     while read mem; do
360         case "$mem" in
361             MemTotal:*)
362                 set -- $mem
363                 eval "mem=\$(($2/3*4))"
364                 ulimit -v $mem
365                 echo "Memory limit set to ${mem}KB"
366                 break;
367             ;;
368         esac
369     done < <(cat /proc/meminfo) # cat for proc stuff
370 }
371
372 function create_baselibs {
373     echo "... creating baselibs"
374     BRPMS=
375     for RPM in $BUILD_ROOT$TOPDIR/RPMS/*/*.rpm ; do
376         BRPMS="$BRPMS ${RPM#$BUILD_ROOT}"
377     done
378     BDEBS=
379     for DEB in $BUILD_ROOT$TOPDIR/DEBS/*.deb ; do
380         BDEBS="$BDEBS ${DEB#$BUILD_ROOT}"
381     done
382     BASELIBS_CFG=
383 ## Nb REPO is the name of the project instance
384 ## DISTRO is the underlying (remote?) distro
385 #    if test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs_${REPO}.conf ; then
386 #        BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs_${REPO}.conf"
387 #    elif test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs_${DISTRO}.conf ; then
388 #        BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs_${DISTRO}.conf"
389 #    elif test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs.conf ; then
390
391     if test "$BUILDTYPE" == "dsc" ; then
392         BPKGS=$BDEBS
393     else # spec and kiwi
394         if test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs.conf ; then
395             BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs.conf"
396         fi
397         if test -e $BUILD_ROOT/usr/lib/build/baselibs_global.conf; then
398             BASELIBS_GLOBAL="-c /usr/lib/build/baselibs_global.conf"
399         fi
400         BPKGS=$BRPMS
401     fi
402     if test -f $BUILD_ROOT/usr/lib/build/mkbaselibs ; then
403         if test -z "$BASELIBS_CFG" -a -e $BUILD_ROOT/usr/lib/build/baselibs.conf ; then
404             BASELIBS_CFG="-c /usr/lib/build/baselibs.conf"
405         fi
406         chroot $BUILD_ROOT /usr/lib/build/mkbaselibs $BASELIBS_GLOBAL $BASELIBS_CFG $BPKGS || cleanup_and_exit 1
407     else
408         # use external version
409         rm -rf $BUILD_ROOT/.mkbaselibs
410         mkdir -p $BUILD_ROOT/.mkbaselibs
411         cp -f $BUILD_DIR/mkbaselibs $BUILD_ROOT/.mkbaselibs/
412         if test "$BUILDTYPE" == "dsc" ; then
413             cp -f $BUILD_DIR/baselibs_global-deb.conf $BUILD_ROOT/.mkbaselibs/baselibs_g.conf
414             cp -f $BUILD_ROOT$TOPDIR/SOURCES/baselibs-deb.conf $BUILD_ROOT/.mkbaselibs/baselibs-deb.conf
415             BASELIBS_CFG="-c /.mkbaselibs/baselibs-deb.conf"
416         else
417             cp -f $BUILD_DIR/baselibs_global.conf $BUILD_ROOT/.mkbaselibs/baselibs_g.conf
418             if test -z "$BASELIBS_CFG" -a -e $BUILD_DIR/baselibs.conf; then
419                 cp -f $BUILD_DIR/baselibs.conf $BUILD_ROOT/.mkbaselibs/baselibs.conf
420                 BASELIBS_CFG="-c /.mkbaselibs/baselibs.conf"
421             fi
422         fi
423         if test -e $BUILD_ROOT/.mkbaselibs/baselibs_g.conf; then
424             BASELIBS_GLOBAL="-c /.mkbaselibs/baselibs_g.conf"
425         fi
426         chroot $BUILD_ROOT su -c "/.mkbaselibs/mkbaselibs $BASELIBS_GLOBAL $BASELIBS_CFG $BPKGS" - $BUILD_USER || cleanup_and_exit 1
427         rm -rf $BUILD_ROOT/.mkbaselibs
428     fi
429 }
430
431 detect_vm_2nd_stage()
432 {
433     if ! test "$0" = "/.build/build" ; then
434         return 1
435     fi
436     if test $$ -eq 1 ; then
437         # ignore special init signals if we're init
438         # we're using ' ' instead of '' so that the signal handlers
439         # are reset in the child processes
440         trap ' ' HUP TERM
441         $0 "$@"
442         cleanup_and_exit $?
443     fi
444     echo "2nd stage started in virtual machine"
445     BUILD_ROOT=/
446     BUILD_DIR=/.build
447     . $BUILD_DIR/build.data
448     echo "machine type: `uname -m`"
449     if test "$PERSONALITY" != 0 -a -z "$PERSONALITY_SET" ; then
450         export PERSONALITY_SET=true
451         echo "switching personality to $PERSONALITY..."
452         # this is 32bit perl/glibc, thus the 32bit syscall number
453         exec perl -e 'syscall(136, '$PERSONALITY') == -1 && warn("personality: $!\n");exec "/.build/build" || die("/.build/build: $!\n")'
454     fi
455     RUNNING_IN_VM=true
456     mount -orw -n -tproc none /proc
457     if test "$VM_TYPE" != 'lxc'; then
458         mount -n -o remount,rw /
459     fi
460 # qemu inside of xen does not work, check again with kvm later before enabling this
461 #    if [ -e /dev/kqemu ]; then
462 #        # allow abuild user to run qemu
463 #        chmod 0666 /dev/kqemu
464 #    fi
465     if test -n "$VM_SWAP" ; then
466         for i in 1 2 3 4 5 6 7 8 9 10 ; do
467             test -e "$VM_SWAP" && break
468             test $i = 1 && echo "waiting for $VM_SWAP to appear"
469             echo -n .
470             sleep 1
471         done
472         test $i = 1 || echo
473         # recreate the swap device manually if it didn't exist for some
474         # reason, hardcoded to hda2 atm
475         if ! test -b "$VM_SWAP" ; then
476             rm -f "$VM_SWAP"
477             umask 027
478             mknod "$VM_SWAP" b 3 2
479             umask 022
480         fi
481         swapon -v "$VM_SWAP" || exit 1
482     fi
483     HOST="$MYHOSTNAME"
484
485     return 0
486 }
487
488 find_spec_files()
489 {
490     local spec files
491     if [ -z "$SPECFILES" ]; then
492         set -- "`pwd`"
493     else
494         set -- "${SPECFILES[@]}"
495     fi
496     SPECFILES=()
497     for spec in "$@"; do
498         if [ "$spec" = "${spec#/}" ]; then
499             spec="`pwd`/$spec"
500         fi
501
502         if [ -d "$spec" ]; then
503             specs=("$spec"/*.spec)
504             if [ -n "$specs" ]; then
505                 SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
506             else
507                 specs=("$spec"/*.spec)
508                 if [ -n "$specs" ]; then
509                     SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
510                 fi
511             fi
512         else
513             SPECFILES[${#SPECFILES[@]}]="$spec";
514         fi
515     done
516
517     if test -z "$SPECFILES"; then
518         echo no spec files or src rpms found in $@. exit...
519         cleanup_and_exit 1
520     fi
521 }
522
523 become_root_or_fail()
524 {
525     if [ ! -w /root ]; then
526         echo "You have to be root to use $0" >&2
527         exit 1
528     fi
529     cleanup_and_exit 1
530 }
531
532 mkdir_build_root()
533 {
534     if [ -d "$BUILD_ROOT" ]; then
535         # check if it is owned by root
536         if [ -z "$RUNNING_IN_VM" -a \! -O "$BUILD_ROOT" -a "`stat -c %u $BUILD_ROOT`" -ne 0 ]; then
537             echo "BUILD_ROOT=$BUILD_ROOT must be owned by root. Exit..."
538             cleanup_and_exit 1
539         fi
540     else
541         test "$BUILD_ROOT" != "${BUILD_ROOT%/*}" && mkdir -p "${BUILD_ROOT%/*}"
542         if ! mkdir $BUILD_ROOT; then
543             echo "can not create BUILD_ROOT=$BUILD_ROOT. Exit..."
544             cleanup_and_exit 1
545         fi
546     fi
547
548     rm -rf "$BUILD_ROOT"/.build.packages
549 }
550
551 linux64()
552 {
553         perl -e 'syscall('$PERSONALITY_SYSCALL', 0); exec(@ARGV) || die("$ARGV[0]: $!\n")' "$@"
554 }
555
556 #### main ####
557
558 trap fail_exit EXIT
559
560 case `perl -V:archname` in
561     *x86_64*) PERSONALITY_SYSCALL=135 ;;
562     *i?86*)   PERSONALITY_SYSCALL=136 ;;
563 esac
564
565 shopt -s nullglob
566
567 if detect_vm_2nd_stage ; then
568     set "/.build-srcdir/$SPECFILE"
569
570 fi
571
572 export PATH=$BUILD_DIR:/sbin:/usr/sbin:$PATH
573
574 . $BUILD_DIR/common_functions || exit 1
575
576 export HOST
577
578 while test -n "$1"; do
579   PARAM="$1"
580   ARG="$2"
581   shift
582   case $PARAM in
583     *-*=*)
584       ARG=${PARAM#*=}
585       PARAM=${PARAM%%=*}
586       set -- "----noarg=$PARAM" "$@"
587   esac
588   case $PARAM in
589       *-help|-h)
590         echo_help
591         cleanup_and_exit
592       ;;
593       *-no*init)
594         DO_INIT=false
595       ;;
596       *-no*checks)
597         DO_CHECKS=false
598       ;;
599       *-clean)
600         CLEAN_BUILD='--clean'
601       ;;
602       *-kill)
603         KILL=true
604       ;;
605       *-rpms)
606         BUILD_RPMS="$ARG"
607         if [ -z "$BUILD_RPMS" ] ; then
608           echo_help
609           cleanup_and_exit
610         fi
611         shift
612       ;;
613       *-arch)
614         BUILD_ARCH="$ARG"
615         shift
616       ;;
617       *-verify)
618         export VERIFY_BUILD_SYSTEM=true
619       ;;
620       *-target)
621         ABUILD_TARGET_ARCH="$ARG"
622         shift
623       ;;
624       *-jobs)
625         BUILD_JOBS="$ARG"
626         shift
627       ;;
628       *-extra*packs|-X)
629         BUILD_EXTRA_PACKS="$BUILD_EXTRA_PACKS $ARG"
630         shift
631       ;;
632       *-lint)
633         DO_LINT=true
634         ;;
635       *-baselibs)
636         CREATE_BASELIBS=true
637         ;;
638       *-baselibs-internal)
639         CREATE_BASELIBS=internal
640         ;;
641       *-root)
642         BUILD_ROOT="$ARG"
643         shift
644       ;;
645       *-oldpackages)
646         OLD_PACKAGES="$ARG"
647         shift
648       ;;
649       *-dist)
650         BUILD_DIST="$ARG"
651         export BUILD_DIST
652         shift
653       ;;
654       *-xen|*-kvm|--uml|--qemu)
655         VM_TYPE=${PARAM##*-}
656         if [ -n "$ARG" -a "$ARG" = "${ARG#-}" ]; then
657             VM_IMAGE="$ARG"
658             shift
659         else
660             VM_IMAGE=1
661         fi
662       ;;
663       --lxc)
664         VM_TYPE=${PARAM##*-}
665       ;;
666       *-xenswap|*-swap)
667         VM_SWAP="$ARG"
668         shift
669       ;;
670       *-xenmemory|*-memory)
671         MEMSIZE="$ARG"
672         shift
673       ;;
674       *-rpmlist)
675         RPMLIST="--rpmlist $ARG"
676         BUILD_RPMS=
677         shift
678       ;;
679       *-release)
680         RELEASE="$ARG"
681         shift
682       ;;
683       *-logfile)
684         LOGFILE="$ARG"
685         shift
686       ;;
687       *-reason)
688         REASON="$ARG"
689         shift
690       ;;
691       *-norootforbuild)
692         NOROOTFORBUILD=true
693       ;;
694       *-stage)
695         BUILD_RPM_BUILD_STAGE="$ARG"
696         shift
697       ;;
698       *-useusedforbuild)
699         USEUSEDFORBUILD="--useusedforbuild"
700       ;;
701       *-list*state)
702         LIST_STATE=true
703       ;;
704       --define|--with|--without)
705         definesnstuff[${#definesnstuff[@]}]="$PARAM";
706         definesnstuff[${#definesnstuff[@]}]="$ARG";
707         shift
708       ;;
709       --repository|--repo)
710         if [ -z "$ARG" ] ; then
711           echo_help
712           cleanup_and_exit
713         fi
714         repos[${#repos[@]}]="$PARAM";
715         repos[${#repos[@]}]="$ARG";
716         shift
717       ;;
718       --icecream)
719         if [ -z "$ARG" ] ; then
720           echo "--icecream needs an argument" >&2
721           echo_help
722           cleanup_and_exit 1
723         fi
724         icecream="$ARG"
725         if [ "$icecream" -gt 0 ]; then
726                 BUILD_JOBS="$ARG"
727         fi
728         shift
729       ;;
730       --ccache)
731         ccache=1
732       ;;
733       --debug)
734         BUILD_DEBUG=1
735       ;;
736       --incarnation)
737         INCARNATION=$ARG
738         shift
739       ;;
740       --disturl)
741         DISTURL=$ARG
742         shift
743       ;;
744       --linksources)
745         LINKSOURCES=true
746       ;;
747       ----noarg)
748         echo "$ARG does not take an argument"
749         cleanup_and_exit
750       ;;
751       *-changelog)
752         CHANGELOG=true
753       ;;
754       --overlay)
755         OVERLAY=$ARG
756         shift
757       ;;
758       --rsync-src)
759         RSYNCSRC=$ARG
760         shift
761       ;;
762       --rsync-dest)
763         RSYNCDEST=$ARG
764         shift
765       ;;
766       --uid)
767         ABUILD_ID="$ARG"
768         if test -n "${ABUILD_ID//[0-9:]/}"; then
769             echo "--uid argument must be uid:gid"
770             cleanup_and_exit
771         fi
772         ABUILD_UID=${ABUILD_ID%:*}
773         ABUILD_GID=${ABUILD_ID#*:}
774         shift
775       ;;
776       -*)
777         echo Unknown Option "$PARAM". Exit.
778         cleanup_and_exit 1
779       ;;
780       *)
781         SPECFILES[${#SPECFILES[@]}]="$PARAM";
782       ;;
783     esac
784 done
785
786 if test -n "$KILL" ; then
787     test -z "$SRCDIR" || usage
788     if test -n "$VM_IMAGE" -a -n "$VM_SWAP" -a -n "$VM_TYPE"; then
789         # mark job as failed so that we don't extract packages
790         echo -n "BUILDSTATUS1" >"$VM_SWAP"
791     fi
792     (set -C; > "$BUILD_ROOT/exit" || true)
793     if test "$VM_TYPE" = 'lxc'; then
794         LXCID=${BUILD_ROOT##*/}
795         lxc-stop -n "$LXCID" || true
796         lxc-destroy -n "$LXCID"
797     elif test -z "$VM_IMAGE" ; then
798         if ! $BUILD_DIR/killchroot -s 9 $BUILD_ROOT ; then
799             echo "could not kill build in $BUILD_ROOT"
800             cleanup_and_exit 1
801         fi
802     elif test "$VM_TYPE" = 'xen'; then
803         XENID="${VM_IMAGE%/root}"
804         XENID="${XENID%/tmpfs}"
805         XENID="${XENID##*/}"
806         if xm list "build:$XENID" >/dev/null 2>&1 ; then
807             if ! xm destroy "build:$XENID" ; then
808                 echo "could not kill xen build $XENID"
809                 cleanup_and_exit 1
810             fi
811         fi
812     elif test -n "$VM_TYPE"; then
813         if ! fuser -k -TERM "$VM_IMAGE"; then
814             echo "could not kill build in $VM_IMAGE"
815             cleanup_and_exit 1
816         fi
817     else
818         echo "don't know how to kill this build job"
819         cleanup_and_exit 1
820     fi
821     cleanup_and_exit 0
822 fi
823
824 if [ "$VM_TYPE" = 'kvm' -a -z "$RUNNING_IN_VM" ]; then
825     if [ ! -r /dev/kvm -o ! -x "$qemu_bin"-kvm ]; then
826         echo "host doesn't support kvm"
827         cleanup_and_exit 3
828     fi
829     qemu_bin="$kvm_bin"
830     qemu_kernel="$kvm_kernel"
831     qemu_initrd="$kvm_initrd"
832     if [ ! -e $qemu_initrd-virtio -o $qemu_kernel -nt $qemu_initrd-virtio ]; then
833         if [ ! -w /root ]; then
834             echo "No initrd that provides virtio support found. virtio accelleration disabled."
835             echo "Run the following command as root to enable virtio:"
836             shellquote "${mkinitrd_virtio_cmd[@]}"
837             echo
838         elif /sbin/modinfo virtio_pci >/dev/null 2>&1; then
839             echo "creating $qemu_initrd-virtio"
840             "${mkinitrd_virtio_cmd[@]}" || cleanup_and_exit 1
841             kvm_virtio=1
842         fi
843     else
844         kvm_virtio=1
845     fi
846
847     if [ "$kvm_virtio" = 1 ]; then
848         qemu_initrd="$qemu_initrd-virtio"
849         VM_SWAPDEV=/dev/vdb
850         qemu_rootdev=/dev/vda
851     else
852         VM_SWAPDEV=/dev/sdb
853         qemu_rootdev=/dev/sda
854     fi
855 fi
856
857 if [ "$VM_TYPE" = 'qemu' ]; then
858     VM_SWAPDEV=/dev/sdb
859     qemu_rootdev=/dev/sda
860 fi
861
862 if [ -z "$RPMLIST" ]; then
863     if [ -z "$repos" -a -z "$BUILD_RPMS" ]; then
864         repos=(--repository 'zypp://')
865     fi
866 else
867     repos=()
868 fi
869
870 set_build_arch
871
872 if [ -n "$CLEAN_BUILD" ]; then
873     DO_INIT=true
874 fi
875
876 find_spec_files
877
878 if test -n "$LIST_STATE" ; then
879     BUILD_ROOT=`mktemp -d /var/tmp/build-list-state-XXXXXX`
880     test -d "$BUILD_ROOT" || cleanup_and_exit 3
881     SPECFILE=$SPECFILES # only one specified anyways
882     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
883        rm -rf $BUILD_ROOT/usr/src/packages
884        mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES $BUILD_ROOT/usr/src/packages/SPECS
885        rpm -i --nodigest --nosignature --root $BUILD_ROOT $SPECFILE || {
886            echo "could not install $SPECFILE." 2>&1
887            rm -rf $BUILD_ROOT
888            cleanup_and_exit 3
889        }
890        for SPECFILE in $BUILD_ROOT/usr/src/packages/SPECS/*.spec ; do : ; done
891     fi
892     init_buildsystem --list-state "${definesnstuff[@]}" "${repos[@]}" $USEUSEDFORBUILD $SPECFILE $BUILD_EXTRA_PACKS
893     ERR=$?
894     rm -rf $BUILD_ROOT
895     cleanup_and_exit $ERR
896 fi
897
898 if test -z "$RUNNING_IN_VM" ; then
899     if test -n "$VM_IMAGE" ; then
900         if test "$VM_IMAGE" = 1 ; then
901             VM_IMAGE="$BUILD_ROOT.img"
902         fi
903         if test ! -e "$VM_IMAGE" ; then
904             echo "you need to create a file system on $VM_IMAGE first"
905             cleanup_and_exit 3
906         fi
907     fi
908     if test -n "$VM_IMAGE" ; then
909         if test -n "$CLEAN_BUILD" ; then
910             echo "Creating filesystem on $VM_IMAGE"
911             $vm_img_mkfs $VM_IMAGE || cleanup_and_exit 3
912             if test -n "$vm_img_tunefs" ; then
913                 $vm_img_tunefs $VM_IMAGE || cleanup_and_exit 3
914             fi
915         fi
916         mkdir_build_root
917         if [ -w /root ]; then
918             mount -o loop $VM_IMAGE $BUILD_ROOT || cleanup_and_exit 3
919         else
920             mount $BUILD_ROOT || cleanup_and_exit 3
921         fi
922     else
923         test -w /root || become_root_or_fail
924     fi
925     if test -n "$VM_SWAP" ; then
926         dd if=/dev/zero of="$VM_SWAP" bs=12 count=1 conv=notrunc 2>/dev/null
927         mkswap "$VM_SWAP"
928     fi
929 fi
930
931 mkdir_build_root
932
933 rm -f $BUILD_ROOT/exit
934
935 if [ -w /root ]; then
936     mkdir -p $BUILD_ROOT/proc
937     mkdir -p $BUILD_ROOT/dev/pts
938     mount -n -tproc none $BUILD_ROOT/proc || true
939     mount -n -tdevpts none $BUILD_ROOT/dev/pts
940 fi
941
942 if test -z "$VM_IMAGE" -a -z "$LOGFILE"; then
943     LOGFILE="$BUILD_ROOT/.build.log"
944 fi
945
946 if test -n "$LOGFILE" ; then
947     echo  logging output to $LOGFILE...
948     rm -f $LOGFILE
949     touch $LOGFILE
950     if test -n "$VM_IMAGE" ; then
951         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
952     else
953         exec 1> >(exec -a 'build logging tee' tee -a $LOGFILE) 2>&1
954     fi
955 fi
956
957 setmemorylimit
958
959 #
960 # say hello
961 #
962 test -z "$HOST" && HOST=`hostname`
963
964 if [ -z "$RUNNING_IN_VM" ]; then
965     echo Using BUILD_ROOT=$BUILD_ROOT
966     test -n "$BUILD_RPMS" && echo Using BUILD_RPMS=$BUILD_RPMS
967     echo Using BUILD_ARCH=$BUILD_ARCH
968     test -n "$VM_TYPE" && echo "Doing $VM_TYPE build${VM_IMAGE:+ in $VM_IMAGE}"
969     echo
970 fi
971
972 test "$BUILD_ARCH" = all && BUILD_ARCH=
973 BUILD_USER_ABUILD_USED=
974
975 for SPECFILE in "${SPECFILES[@]}" ; do
976
977     SRCDIR="${SPECFILE%/*}"
978     SPECFILE="${SPECFILE##*/}"
979
980     BUILDTYPE=
981     case $SPECFILE in
982       *.spec|*.src.rpm) BUILDTYPE=spec ;;
983       *.dsc) BUILDTYPE=dsc ;;
984       *.kiwi) BUILDTYPE=kiwi ;;
985     esac
986     if test -z "$BUILDTYPE" ; then
987        echo "don't know how to build $SPECFILE"
988        cleanup_and_exit 1
989     fi
990
991     cd "$SRCDIR"
992
993     if [ -z "$RUNNING_IN_VM" ]; then
994         echo
995         echo "$HOST started \"build $SPECFILE\" at `date`."
996         echo
997         test -n "$REASON" && echo "$REASON"
998         echo
999     fi
1000
1001     #
1002     # first setup building directory...
1003     #
1004     test -s "$SPECFILE" || {
1005        echo "$SPECFILE" is empty.  This should not happen...
1006        cleanup_and_exit 1
1007     }
1008
1009     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
1010         echo processing src rpm $SRCDIR/$SPECFILE ...
1011         MYSRCDIR=$BUILD_ROOT/.build-srcdir
1012         rm -rf $MYSRCDIR
1013         mkdir -p $MYSRCDIR
1014         cd $MYSRCDIR || cleanup_and_exit 1
1015         $BUILD_DIR/unrpm -q $SRCDIR/$SPECFILE || {
1016             echo "could not install $SPECFILE."
1017             cleanup_and_exit 1
1018         }
1019         for SPECFILE in *.spec ; do : ; done
1020     else
1021         MYSRCDIR="$SRCDIR"
1022         # strip prefix from autogenerated files from OBS.
1023         for i in $MYSRCDIR/_service\:*; do
1024           mv "$i" "${i##*:}"
1025         done
1026         SPECFILE="${SPECFILE##*:}"
1027     fi
1028
1029     # FIX to work with baselibs_$PROJ etc
1030     if test "$BUILDTYPE" == "dsc" -a -e ${SRCDIR}/baselibs-deb.conf ; then
1031         # Set CREATE_BASELIBS if not set
1032         echo "dsc build and baselibs-deb.conf present: forcing --baselibs to true"
1033         CREATE_BASELIBS=true
1034     fi
1035
1036 # Currently local osc build does not allow extra .deb packages to be
1037 # specified on the command line. Both init_buildsystem and expanddeps
1038 # need to handle .deb dependencies first
1039 #    if test -n "$CREATE_BASELIBS" ; then
1040 #        case $BUILDTYPE in
1041 #            spec) ;;
1042 #            dsc) BUILD_EXTRA_PACKS="$BUILD_EXTRA_PACKS libparse-debcontrol-perl" ;;
1043 #        esac
1044 #    fi
1045
1046     echo processing specfile $MYSRCDIR/$SPECFILE ...
1047
1048     ADDITIONAL_PACKS=""
1049     test -n "$BUILD_EXTRA_PACKS" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS $BUILD_EXTRA_PACKS"
1050     test -n "$CREATE_BASELIBS" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS build"
1051     test "$ccache" = '1' && ADDITIONAL_PACKS="$ADDITIONAL_PACKS ccache"
1052     test "$icecream" -gt 1 && ADDITIONAL_PACKS="$ADDITIONAL_PACKS icecream gcc-c++"
1053     test -n "$DO_LINT" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS rpmlint-Factory"
1054     test -n "$OLD_PACKAGES" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS build-compare"
1055
1056     if test -n "$CHANGELOG" -a -z "$RUNNING_IN_VM" ; then
1057         rm -f $BUILD_ROOT/.build-changelog
1058         case $SPECFILE in
1059           *.dsc) CFFORMAT=debian ;;
1060           *) CFFORMAT=rpm ;;
1061         esac
1062         echo "running changelog2spec --target $CFFORMAT --file $MYSRCDIR/$SPECFILE"
1063         if ! $BUILD_DIR/changelog2spec --target $CFFORMAT --file "$MYSRCDIR/$SPECFILE" > $BUILD_ROOT/.build-changelog ; then
1064             rm -f $BUILD_ROOT/.build-changelog
1065         fi
1066     fi
1067
1068     if test -n "$VM_TYPE" -a -z "$RUNNING_IN_VM"; then
1069         rm -rf $BUILD_ROOT/.build
1070         mkdir -p $BUILD_ROOT/.build
1071         if test "$DO_INIT" = true ; then
1072             # do fist stage of init_buildsystem
1073             rm -f $BUILD_ROOT/.build.success
1074             set -- init_buildsystem --prepare "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS
1075             echo "$* ..."
1076             "$@" || cleanup_and_exit 1
1077             check_exit
1078             if [ ! -w /root ]; then
1079                 # remove setuid bit if files belong to user to make e.g. mount work
1080                 find $BUILD_ROOT/{bin,sbin,usr/bin,usr/sbin} -type f -uid $UID -perm +4000 -print0 | xargs -0 --no-run-if-empty chmod -s
1081             fi
1082             if [ -d "$OLD_PACKAGES" ]; then
1083                rm -rf $BUILD_ROOT/.build.oldpackages
1084                cp -a $OLD_PACKAGES $BUILD_ROOT/.build.oldpackages
1085             fi
1086         fi
1087         # start up xen, rerun ourself
1088         cp -a $BUILD_DIR/. $BUILD_ROOT/.build
1089         if ! test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
1090             rm -rf $BUILD_ROOT/.build-srcdir
1091             mkdir $BUILD_ROOT/.build-srcdir
1092             if test "$BUILDTYPE" = kiwi ; then
1093                 cp -pRL "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
1094             else
1095                 cp -p "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
1096             fi
1097             MYSRCDIR=$BUILD_ROOT/.build-srcdir
1098         else
1099             # cwd is at $BUILD_ROOT/.build-srcdir which we want to
1100             # umount later so step aside
1101             cd "$SRCDIR"
1102         fi
1103         Q="'\''"
1104         echo "SPECFILE='${SPECFILE//"'"/$Q}'" > $BUILD_ROOT/.build/build.data
1105         echo "BUILD_JOBS='${BUILD_JOBS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1106         echo "BUILD_ARCH='${BUILD_ARCH//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1107         echo "BUILD_RPMS='${BUILD_RPMS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1108         case $BUILD_DIST in
1109             */*)
1110                 cp $BUILD_DIST $BUILD_ROOT/.build/build.dist
1111                 BUILD_DIST=/.build/build.dist
1112                 ;;
1113         esac
1114         echo "BUILD_DIST='${BUILD_DIST//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1115         echo "RELEASE='${RELEASE//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1116         echo "BUILD_DEBUG='${BUILD_DEBUG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1117         echo "DO_LINT='${DO_LINT//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1118         echo "DO_CHECKS='${DO_CHECKS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1119         echo "NOROOTFORBUILD='${NOROOTFORBUILD//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1120         echo "CREATE_BASELIBS='$CREATE_BASELIBS'" >> $BUILD_ROOT/.build/build.data
1121         echo "REASON='${REASON//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1122         echo "CHANGELOG='${CHANGELOG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1123         echo "INCARNATION='${INCARNATION//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1124         echo "DISTURL='${DISTURL//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1125         # FIXME: this depends on the kernel and vm.
1126         # could be hda2, sda2 for xen or hdb/sdb for qemu
1127         test -n "$VM_SWAP" && echo "VM_SWAP='${VM_SWAPDEV:-/dev/hda2}'" >> $BUILD_ROOT/.build/build.data
1128         PERSONALITY=0
1129         if test "$VM_TYPE" != 'lxc'; then
1130             test -n "$PERSONALITY_SYSCALL" && PERSONALITY=`perl -e 'print syscall('$PERSONALITY_SYSCALL', 0)."\n"'`
1131         fi
1132         echo "PERSONALITY='$PERSONALITY'" >> $BUILD_ROOT/.build/build.data
1133         echo "OLD_PACKAGES='$OLD_PACKAGES'" >> $BUILD_ROOT/.build/build.data
1134         echo "MYHOSTNAME='`hostname`'" >> $BUILD_ROOT/.build/build.data
1135         echo -n "definesnstuff=(" >> $BUILD_ROOT/.build/build.data
1136         shellquote "${definesnstuff[@]}" >> $BUILD_ROOT/.build/build.data
1137         echo ")" >> $BUILD_ROOT/.build/build.data
1138         echo -n "repos=(" >> $BUILD_ROOT/.build/build.data
1139         shellquote "${repos[@]}" >> $BUILD_ROOT/.build/build.data
1140         echo ")" >> $BUILD_ROOT/.build/build.data
1141         echo "VM_TYPE='$VM_TYPE'" >> $BUILD_ROOT/.build/build.data
1142         umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
1143         umount -n $BUILD_ROOT/proc 2> /dev/null || true
1144         umount -n $BUILD_ROOT/dev/pts 2> /dev/null || true
1145         umount -n $BUILD_ROOT/mnt 2> /dev/null || true
1146
1147         if [ -n "$VM_IMAGE" ]; then
1148             check_exit
1149             # needs to work otherwise we have a corrupted file system
1150             umount $BUILD_ROOT || cleanup_and_exit 1
1151         fi
1152
1153         if [ "$VM_TYPE" = 'xen' ]; then
1154                 XMROOT=file:$VM_IMAGE
1155                 XMROOT=${XMROOT/#file:\/dev/phy:/dev}
1156                 XMROOT="disk=$XMROOT,hda1,w"
1157                 XMSWAP=
1158                 if test -n "$VM_SWAP" ; then
1159                     XMSWAP=file:$VM_SWAP
1160                     XMSWAP=${XMSWAP/#file:\/dev/phy:/dev}
1161                     XMSWAP="disk=$XMSWAP,hda2,w"
1162                 fi
1163                 XENID="${VM_IMAGE%/root}"
1164                 XENID="${XENID%/tmpfs}"
1165                 XENID="${XENID##*/}"
1166                 CROSS_INIT_SCRIPT="/.build/build"
1167                 # to run the qemu initialization in the XEN chroot, we need to register it with a statically build shell
1168                 if [ x"$BUILD_ARCH" == xarmv4l -o x"$BUILD_ARCH" == xarmv5el -o x"$BUILD_ARCH" == xarmv7el -o x"$BUILD_ARCH" == xsh4 -o x"$BUILD_ARCH" == xppc ]; then
1169                     if [  -n "$(uname -m | grep '[x3-6]86')" ]; then
1170                         if [ -e /bin/bash-static -a -e /bin/mount-static ]; then
1171                             CROSS_INIT_SCRIPT="/.build/initscript_qemu_vm"
1172                         fi
1173                     fi
1174                 fi
1175
1176                 echo "booting XEN kernel ..."
1177                 if xm list "build:$XENID" >/dev/null 2>&1 ; then
1178                    echo "Instance already exist, something really went wrong..."
1179                    echo "Please report to your server admin, there might be multiple services running for same domain"
1180                    cleanup_and_exit 3
1181                 fi
1182                 set -- xm create -c $BUILD_DIR/xen.conf name="build:$XENID" ${MEMSIZE:+memory=$MEMSIZE} $XMROOT $XMSWAP extra="quiet init="$CROSS_INIT_SCRIPT" panic=1 console=ttyS0"
1183                 if test "$PERSONALITY" != 0 ; then
1184                     # have to switch back to PER_LINUX to make xm work
1185                     set -- linux64 "$@"
1186                 fi
1187                 echo "$@"
1188                 "$@" || cleanup_and_exit 3
1189         elif [ "$VM_TYPE" = 'uml' ]; then
1190                 echo "booting UML kernel ..."
1191                 set -- $uml_kernel initrd=$uml_initrd root=/ubda init=/.build/build panic=1 quiet ubd0=$VM_IMAGE ${MEMSIZE:+mem=$MEMSIZE}
1192                 echo "$@"
1193                 "$@"
1194         elif [ "$VM_TYPE" = 'qemu' -o "$VM_TYPE" = 'kvm' ]; then
1195                 echo "booting $VM_TYPE ..."
1196                 if [ "$kvm_virtio" = 1 ]; then
1197                     qemu_disks=(-drive file="$VM_IMAGE",if=virtio -hda "$VM_IMAGE")
1198                     if [ -n "$VM_SWAP" ]; then
1199                         qemu_disks[${#qemu_disks[@]}]="-drive"
1200                         qemu_disks[${#qemu_disks[@]}]="file=$VM_SWAP,if=virtio"
1201                     fi
1202                 else
1203                     qemu_disks=(-hda "$VM_IMAGE")
1204                     if [ -n "$VM_SWAP" ]; then
1205                         qemu_disks[${#qemu_disks[@]}]="-hdb"
1206                         qemu_disks[${#qemu_disks[@]}]="$VM_SWAP"
1207                     fi
1208                 fi
1209                 set -- $qemu_bin -no-reboot -nographic -net none -serial stdio \
1210                     -kernel $qemu_kernel \
1211                     -initrd $qemu_initrd \
1212                     -append "root=$qemu_rootdev panic=1 quiet noapic rw console=ttyS0 init=/.build/build" \
1213                     ${MEMSIZE:+-m $MEMSIZE} \
1214                     "${qemu_disks[@]}"
1215
1216                 if test "$PERSONALITY" != 0 ; then
1217                     # have to switch back to PER_LINUX to make qemu work
1218                     set -- linux64 "$@"
1219                 fi
1220                 echo "$@"
1221                 "$@"
1222         elif [ "$VM_TYPE" = 'lxc' ]; then
1223                 echo "booting $VM_TYPE ..."
1224                 LXCCONF="$BUILD_ROOT/.build.lxc.conf"
1225                 rm -f "$LXCCONF"
1226                 cat $BUILD_DIR/lxc.conf > "$LXCCONF"
1227                 cat >> "$LXCCONF" <<-EOF
1228                 lxc.rootfs = $BUILD_ROOT
1229                 EOF
1230                 # XXX: do this always instead of leaking the hosts' one?
1231                 echo "rootfs / rootfs rw 0 0" > $BUILD_ROOT/etc/mtab
1232                 LXCID=${BUILD_ROOT##*/}
1233                 lxc-destroy -n "$LXCID" >/dev/null 2>&1 || true
1234                 lxc-create -n "$LXCID" -f "$LXCCONF" || cleanup_and_exit 1
1235                 lxc-start -n "$LXCID" /.build/build
1236                 BUILDSTATUS="$?"
1237                 test "$BUILDSTATUS" != 255 || BUILDSTATUS=3
1238                 cleanup_and_exit "$BUILDSTATUS"
1239         fi
1240         if test -n "$VM_SWAP" ; then
1241             BUILDSTATUS=`dd if="$VM_SWAP" bs=12 count=1 2>/dev/null`
1242             case $BUILDSTATUS in
1243               BUILDSTATUS[02])
1244                 mkdir -p $BUILD_ROOT/.build.packages
1245                 cd $BUILD_ROOT/.build.packages || cleanup_and_exit 1
1246                 echo "build: extracting built packages..."
1247                 extractbuild --disk "$VM_IMAGE" --input "$VM_SWAP" --skip 512 -v || cleanup_and_exit 3
1248                 # create same layout as with plain chroot
1249                 if test "$BUILDTYPE" = spec ; then
1250                   mkdir -p SRPMS
1251                   for i in *src.rpm; do mv "$i" SRPMS/; done
1252                   for i in *.rpm; do
1253                     arch=${i%.rpm}
1254                     arch=${arch##*\.}
1255                     mkdir -p RPMS/$arch
1256                     mv "$i" RPMS/$arch/;
1257                   done
1258                 elif test "$BUILDTYPE" = dsc ; then
1259                   mkdir -p DEBS
1260                   find . -type f | while read i; do mv "$i" DEBS/; done
1261                 else
1262                   mkdir -p KIWI
1263                   find . -type f | while read i; do mv "$i" KIWI/; done
1264                 fi
1265                 cleanup_and_exit ${BUILDSTATUS#BUILDSTATUS}
1266                 ;;
1267               BUILDSTATUS*)
1268                 cleanup_and_exit ${BUILDSTATUS#BUILDSTATUS}
1269                 ;;
1270               *)
1271                 echo "No buildstatus set, packager broke either the base system (glibc/bash) or the build host has a problem, server will retry..."
1272                 cleanup_and_exit 3
1273                 ;;
1274             esac
1275             cleanup_and_exit 1
1276         fi
1277         cleanup_and_exit 0
1278     fi
1279
1280     if test "$DO_INIT" = true ; then
1281         #
1282         # create legacy .buildenv file
1283         #
1284         test -z "$INCARNATION" && INCARNATION=0
1285         echo "BUILD_INCARNATION=$INCARNATION" > $BUILD_ROOT/.buildenv
1286         CREATE_BUILD_BINARIES=
1287         egrep '^#[       ]*needsbinariesforbuild[       ]*$' >/dev/null <$MYSRCDIR/$SPECFILE && CREATE_BUILD_BINARIES=--create-build-binaries
1288         set -- init_buildsystem "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $CREATE_BUILD_BINARIES $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS
1289         echo "$* ..."
1290         "$@" || cleanup_and_exit 1
1291         check_exit
1292         if df $BUILD_ROOT 2>/dev/null | grep -q "100%"; then
1293             df -h $BUILD_ROOT
1294             echo "build does not work on a completely full filesystem"
1295             cleanup_and_exit 1
1296         fi
1297         mount -n -tproc none $BUILD_ROOT/proc || true
1298         mount -n -tdevpts none $BUILD_ROOT/dev/pts
1299         if [ -d "$OLD_PACKAGES" ]; then
1300            rm -rf "$BUILD_ROOT/.build.oldpackages"
1301            cp -r $OLD_PACKAGES $BUILD_ROOT/.build.oldpackages
1302         fi
1303     fi
1304
1305     if test -z "$BUILD_DIST" -a -e "$BUILD_ROOT/.guessed_dist" ; then
1306         read BUILD_DIST < $BUILD_ROOT/.guessed_dist
1307     fi
1308
1309     #
1310     # fix rpmrc if we are compiling for i686
1311     #
1312     test -f $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 && mv $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 $BUILD_ROOT/usr/lib/rpm/rpmrc
1313     if test -e $BUILD_ROOT/usr/lib/rpm/rpmrc -a "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
1314         mv $BUILD_ROOT/usr/lib/rpm/rpmrc $BUILD_ROOT/usr/lib/rpm/rpmrc_i586
1315         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
1316     fi
1317
1318     #
1319     # check if we want to build with the abuild user
1320     #
1321     BUILD_USER=abuild
1322     if test -x $BUILD_ROOT/bin/rpm ; then
1323         SUSE_VERSION=`chroot $BUILD_ROOT /bin/rpm --eval '%{?suse_version}' 2>/dev/null`
1324         if test -n "$SUSE_VERSION" && test "$SUSE_VERSION" -le 1020 ; then
1325             BUILD_USER=root
1326         fi
1327     fi
1328     if test "$BUILD_USER" = abuild ; then
1329         egrep '^#[       ]*needsrootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=root
1330     else
1331         egrep '^#[       ]*norootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=abuild
1332     fi
1333     test -n "$NOROOTFORBUILD" && BUILD_USER=abuild
1334
1335     if test $BUILD_USER = abuild ; then
1336         if ! egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1337             echo "abuild::${ABUILD_UID}:${ABUILD_GID}:Autobuild:/home/abuild:/bin/bash" >>$BUILD_ROOT/etc/passwd
1338             echo 'abuild:*:::::::' >>$BUILD_ROOT/etc/shadow # This is needed on Mandriva 2009
1339             echo 'abuild:*::' >>$BUILD_ROOT/etc/gshadow # This is needed on Ubuntu
1340             echo "abuild::${ABUILD_GID}:" >>$BUILD_ROOT/etc/group
1341             mkdir -p $BUILD_ROOT/home/abuild
1342             chown ${ABUILD_UID}:${ABUILD_GID} $BUILD_ROOT/home/abuild
1343         else
1344             if ! egrep "^abuild::${ABUILD_UID}:${ABUILD_GID}" >/dev/null <$BUILD_ROOT/etc/passwd ; then
1345                 echo "abuild user present in the buildroot ($BUILD_ROOT) but uid:gid does not match"
1346                 echo "buildroot currently using:"
1347                 egrep "^abuild:" <$BUILD_ROOT/etc/passwd
1348                 echo "build script attempting to use:"
1349                 echo "abuild::${ABUILD_UID}:${ABUILD_GID}:..."
1350                 echo "build aborting"
1351                 cleanup_and_exit 1
1352             fi
1353         fi
1354         if test -f $BUILD_ROOT/etc/shadow ; then
1355             sed -e "s@^root::@root:*:@" < $BUILD_ROOT/etc/shadow > $BUILD_ROOT/etc/shadow.t && mv $BUILD_ROOT/etc/shadow.t $BUILD_ROOT/etc/shadow
1356         fi
1357         if test -f $BUILD_ROOT/etc/gshadow ; then
1358             sed -e "s@^root::@root:*:@" < $BUILD_ROOT/etc/gshadow > $BUILD_ROOT/etc/gshadow.t && mv $BUILD_ROOT/etc/gshadow.t $BUILD_ROOT/etc/gshadow
1359         fi
1360         BUILD_USER_ABUILD_USED=true
1361     else
1362         if egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1363             rm -rf $BUILD_ROOT/home/abuild
1364             egrep -v '^abuild:' <$BUILD_ROOT/etc/passwd >$BUILD_ROOT/etc/passwd.new
1365             mv $BUILD_ROOT/etc/passwd.new $BUILD_ROOT/etc/passwd
1366             egrep -v '^abuild:' <$BUILD_ROOT/etc/group >$BUILD_ROOT/etc/group.new
1367             mv $BUILD_ROOT/etc/group.new $BUILD_ROOT/etc/group
1368             if test -f $BUILD_ROOT/etc/shadow ; then
1369               egrep -v '^abuild:' <$BUILD_ROOT/etc/shadow >$BUILD_ROOT/etc/shadow.new
1370               mv $BUILD_ROOT/etc/shadow.new $BUILD_ROOT/etc/shadow
1371             fi
1372             if test -f $BUILD_ROOT/etc/gshadow ; then
1373               egrep -v '^abuild:' <$BUILD_ROOT/etc/gshadow >$BUILD_ROOT/etc/gshadow.new
1374               mv $BUILD_ROOT/etc/gshadow.new $BUILD_ROOT/etc/gshadow
1375             fi
1376         fi
1377     fi
1378
1379     if test "$BUILDTYPE" = spec ; then
1380         TOPDIR=`chroot $BUILD_ROOT su -c "rpm --eval '%_topdir'" - $BUILD_USER`
1381     else
1382         TOPDIR=/usr/src/packages
1383         mkdir -p $BUILD_ROOT$TOPDIR
1384     fi
1385
1386     rm -f $BUILD_ROOT/.build.packages
1387     ln -s ${TOPDIR#/} $BUILD_ROOT/.build.packages
1388
1389     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1390     mount -n -tdevpts none $BUILD_ROOT/dev/pts 2> /dev/null
1391
1392     setupicecream
1393
1394     setupccache
1395
1396     # nasty hack to prevent rpath on known paths
1397     # FIXME: do this only for suse
1398     if test -d "$BUILD_ROOT/etc/profile.d" ; then
1399         echo "export SUSE_IGNORED_RPATHS=/etc/ld.so.conf" > "$BUILD_ROOT/etc/profile.d/buildsystem.sh"
1400     fi
1401
1402     #
1403     # now clean up RPM building directories
1404     #
1405     rm -rf $BUILD_ROOT$TOPDIR
1406     for i in BUILD RPMS/`uname -m` RPMS/i386 RPMS/noarch SOURCES SPECS SRPMS BUILDROOT; do
1407         mkdir -p $BUILD_ROOT$TOPDIR/$i
1408         test $BUILD_USER = abuild && chown ${ABUILD_UID}:${ABUILD_GID} $BUILD_ROOT$TOPDIR/$i
1409     done
1410     check_exit
1411
1412     mkdir -p $BUILD_ROOT$TOPDIR/SOURCES
1413     if test "$BUILDTYPE" = kiwi ; then
1414         mkdir -p $BUILD_ROOT$TOPDIR/KIWI
1415         if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
1416             mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1417         else
1418             if test -z "$LINKSOURCES" ; then
1419                 cp -dLR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1420             else
1421                 cp -lR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1422             fi
1423             if test "$?" != 0 ; then
1424                 echo "source copy failed"
1425                 cleanup_and_exit 1
1426             fi
1427         fi
1428     else
1429         cp -p "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1430     fi
1431     test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir && rm -rf "$MYSRCDIR"
1432     CHANGELOGARGS=
1433     test -n "$CHANGELOG" -a -f "$BUILD_ROOT/.build-changelog" && CHANGELOGARGS="--changelog $BUILD_ROOT/.build-changelog"
1434
1435     if test "$BUILDTYPE" = spec ; then
1436         # do buildrequires/release substitution
1437         args=()
1438         if test -n "$RELEASE"; then
1439                 args=(--release "$RELEASE")
1440         fi
1441         substitutedeps "${args[@]}" --root "$BUILD_ROOT" --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" $CHANGELOGARGS "$BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE" "$BUILD_ROOT/.spec.new" || cleanup_and_exit 1
1442         # extract macros from configuration
1443         getmacros --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" > $BUILD_ROOT/root/.rpmmacros
1444         if test -n "$BUILD_DEBUG" ; then
1445             echo '
1446 %prep %?_suse_insert_debug_package%%prep
1447 %package %?_suse_insert_debug_package%%package
1448 %suse_insert_debug_package \
1449   %global _suse_insert_debug_package \\\
1450     %%undefine _suse_insert_debug_package \\\
1451     %%debug_package
1452
1453 ' >> $BUILD_ROOT/root/.rpmmacros
1454         fi
1455
1456         if [ -n "$BUILD_JOBS" ]; then
1457                 cat >> $BUILD_ROOT/root/.rpmmacros <<-EOF
1458                 %jobs $BUILD_JOBS
1459                 %_smp_mflags -j$BUILD_JOBS
1460                 EOF
1461         fi
1462         test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmmacros $BUILD_ROOT/home/abuild/.rpmmacros
1463         # extract optflags from configuration
1464         getoptflags --dist "$BUILD_DIST" --configdir "$BUILD_DIR/configs" --archpath "$BUILD_ARCH" ${BUILD_DEBUG:+--debug} > $BUILD_ROOT/root/.rpmrc
1465         test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmrc $BUILD_ROOT/home/abuild/.rpmrc
1466         # case arm, extract changetarget
1467         # this is restricted to arm to make sure it doesn't interfere with x86
1468         if [ "$BUILD_ARCH" != "${BUILD_ARCH#arm}" ]; then
1469             echo "changing targetarch"
1470             CHANGETARGET=$(getchangetarget --dist "$BUILD_DIST" --configdir "$BUILD_DIR/configs" --archpath "$BUILD_ARCH" )
1471             echo "NEW TARGET: $CHANGETARGET"
1472         fi
1473     fi
1474     if test -f $BUILD_ROOT/.spec.new ; then
1475         if ! cmp -s $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT/.spec.new ; then
1476             echo -----------------------------------------------------------------
1477             echo I have the following modifications for $SPECFILE:
1478             sed -e "/^%changelog/q" $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE > $BUILD_ROOT/.spec.t1
1479             sed -e "/^%changelog/q" $BUILD_ROOT/.spec.new > $BUILD_ROOT/.spec.t2
1480             diff $BUILD_ROOT/.spec.t1 $BUILD_ROOT/.spec.t2
1481             rm -f $BUILD_ROOT/.spec.t1 $BUILD_ROOT/.spec.t2
1482             mv $BUILD_ROOT/.spec.new $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE
1483         else
1484             rm -f $BUILD_ROOT/.spec.new
1485         fi
1486     fi
1487
1488     if test "$BUILDTYPE" = dsc ; then
1489         rm -rf $BUILD_ROOT$TOPDIR/BUILD
1490         mkdir -p $BUILD_ROOT$TOPDIR/SOURCES.DEB
1491         test $BUILD_USER = abuild && chown -R ${ABUILD_UID}:${ABUILD_GID} $BUILD_ROOT$TOPDIR
1492         DEB_TRANSFORM=
1493         DEB_SOURCEDIR=$TOPDIR/SOURCES
1494         DEB_DSCFILE=$SPECFILE
1495         for f in $BUILD_ROOT$TOPDIR/SOURCES/debian.* ; do
1496             test -f $f && DEB_TRANSFORM=true
1497         done
1498         if test -n "$DEB_TRANSFORM" ; then
1499             echo "running debian transformer..."
1500             if ! debtransform $CHANGELOGARGS $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT$TOPDIR/SOURCES.DEB ; then
1501                 echo "debian transforming failed."
1502                 cleanup_and_exit 1
1503             fi
1504             DEB_SOURCEDIR=$TOPDIR/SOURCES.DEB
1505             for DEB_DSCFILE in $BUILD_ROOT/$DEB_SOURCEDIR/*.dsc ; do : ; done
1506             DEB_DSCFILE="${DEB_DSCFILE##*/}"
1507         fi
1508         chroot $BUILD_ROOT su -c "dpkg-source -x $DEB_SOURCEDIR/$DEB_DSCFILE $TOPDIR/BUILD" - $BUILD_USER
1509     fi
1510
1511     if test $BUILD_USER = abuild ; then
1512         chown -R ${ABUILD_UID}:${ABUILD_GID} $BUILD_ROOT$TOPDIR/*
1513     else
1514         chown -R root:root $BUILD_ROOT$TOPDIR/*
1515     fi
1516     cd $BUILD_ROOT$TOPDIR/SOURCES || cleanup_and_exit 1
1517
1518     echo -----------------------------------------------------------------
1519     if test "$BUILD_USER" = root ; then
1520         echo ----- building $SPECFILE
1521     else
1522         echo ----- building $SPECFILE "(user $BUILD_USER)"
1523     fi
1524     echo -----------------------------------------------------------------
1525     echo -----------------------------------------------------------------
1526     if [ -n "$RUNNING_IN_VM" ]; then
1527         if [ -x /sbin/ip ]; then
1528             ip addr add 127.0.0.1/8 dev lo
1529             ip link set lo up
1530         else
1531             ifconfig lo 127.0.0.1 up
1532         fi
1533         if [ -n "$MYHOSTNAME" ]; then
1534             hostname "$MYHOSTNAME"
1535         fi
1536     fi
1537
1538     BUILD_SUCCEEDED=false
1539
1540     if test -n "$OVERLAY" ; then
1541         if -d "$OVERLAY"; then
1542             pushd $OVERLAY
1543             echo "Copying overlay to BUILD_ROOT"
1544             tar -cpf - . | (cd $BUILD_ROOT ; tar -xvf -)
1545             popd
1546         else
1547             echo "OVERLAY ($OVERLAY) is no directory - skipping"
1548         fi
1549     fi
1550
1551     if test -n "$RSYNCSRC" ; then
1552         if test -n "$RSYNCDEST"; then
1553             if test -d "$RSYNCSRC"; then
1554                 if ! test -d "$BUILD_ROOT/$RSYNCDEST"; then
1555                     echo "ATTENTION! Creating target directory ($BUILD_ROOT/$RSYNCDEST) as its not there."
1556                     mkdir -p $BUILD_ROOT/$RSYNCDEST
1557                 fi
1558                 echo "Running rsync ..."
1559                 rsync -av $RSYNCSRC/* $BUILD_ROOT/$RSYNCDEST/
1560                 chown -R ${ABUILD_UID}:${ABUILD_GID} $BUILD_ROOT/$RSYNCDEST
1561                 RSYNCDONE=true
1562                 echo "... done"
1563             else
1564                 echo "RSYNCSRC is no directory - skipping"
1565             fi
1566         else
1567             echo "RSYNCSRC given, but not RSYNCDEST - skipping"
1568         fi
1569     fi
1570
1571     if test "$BUILDTYPE" = spec ; then
1572         test -z "$BUILD_RPM_BUILD_STAGE" && BUILD_RPM_BUILD_STAGE=-ba
1573
1574         # XXX: move _srcdefattr to macro file?
1575         rpmbopts=("$BUILD_RPM_BUILD_STAGE" "--define" "_srcdefattr (-,root,root)")
1576         if [ -n "$ABUILD_TARGET_ARCH" ]; then
1577                 rpmbopts[${#rpmbopts[@]}]="--target=$ABUILD_TARGET_ARCH"
1578         fi
1579         if [ -n "$DISTURL" ]; then
1580                 rpmbopts[${#rpmbopts[@]}]='--define'
1581                 rpmbopts[${#rpmbopts[@]}]="disturl $DISTURL"
1582         fi
1583         if test -s "$BUILD_ROOT/usr/lib/rpm/mandriva/macros" ; then
1584                 rpmbopts[${#rpmbopts[@]}]='--eval'
1585                 rpmbopts[${#rpmbopts[@]}]="%undefine _enable_debug_packages"
1586         fi
1587         if [ -n "$BUILD_DEBUG" ]; then
1588                 rpmbopts[${#rpmbopts[@]}]='--eval'
1589                 rpmbopts[${#rpmbopts[@]}]="%suse_insert_debug_package"
1590         fi
1591         if [ -n "$CHANGETARGET" ]; then
1592                 rpmbopts[${#rpmbopts[@]}]='--target'
1593                 rpmbopts[${#rpmbopts[@]}]="$CHANGETARGET"
1594         fi
1595         if [ -n "$RSYNCDONE" ] ; then
1596                 rpmbopts[${#rpmbopts[@]}]='--define'
1597                 rpmbopts[${#rpmbopts[@]}]="RSYNCDONE 1"
1598         fi
1599
1600         rpmbuild=rpmbuild
1601
1602         test -x $BUILD_ROOT/usr/bin/rpmbuild || rpmbuild=rpm
1603         # su involves a shell which would require even more
1604         # complicated quoting to bypass than this
1605         toshellscript $rpmbuild \
1606                 "${definesnstuff[@]}" \
1607                 "${rpmbopts[@]}" \
1608                 "$TOPDIR/SOURCES/$SPECFILE" \
1609                 > $BUILD_ROOT/.build.command
1610         chmod 755 $BUILD_ROOT/.build.command
1611         check_exit
1612         chroot $BUILD_ROOT su -c /.build.command - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1613     fi
1614
1615     if test "$BUILDTYPE" = dsc ; then
1616         # Checks to see if a build script should be used
1617         # this allows the build environment to be manipulated
1618         # and alternate build commands can be used
1619         DSC_BUILD_CMD="dpkg-buildpackage -us -uc -rfakeroot-tcp"
1620         if test -e $BUILD_ROOT/$TOPDIR/SOURCES/build.script ; then
1621             echo "Sourcing build.script to build - it should normally run 'dpkg-buildpackage -us -uc -rfakeroot-tcp'"
1622             DSC_BUILD_CMD="source $TOPDIR/SOURCES/build.script"
1623             chmod +x $BUILD_ROOT/$TOPDIR/SOURCES/build.script
1624         fi
1625         chroot $BUILD_ROOT su -c "cd $TOPDIR/BUILD && $DSC_BUILD_CMD" - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1626
1627         mkdir -p $BUILD_ROOT/$TOPDIR/DEBS
1628         for DEB in $BUILD_ROOT/$TOPDIR/*.deb ; do
1629             test -e "$DEB" && mv "$DEB" "$BUILD_ROOT/$TOPDIR/DEBS"
1630         done
1631         # link sources over
1632         ln $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE $BUILD_ROOT/$TOPDIR/DEBS/
1633         while read f ; do
1634             ln $BUILD_ROOT/$DEB_SOURCEDIR/$f $BUILD_ROOT/$TOPDIR/DEBS/
1635         done < <(sed -ne '/^Files:/,$s/^ ................................ [0-9][0-9]* //p' < $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE)
1636     fi
1637
1638     if test "$BUILDTYPE" = kiwi ; then
1639         . $BUILD_DIR/build_kiwi.sh
1640         run_kiwi
1641     fi
1642
1643     test "$BUILD_SUCCEEDED" = true || cleanup_and_exit 1
1644     test -d "$SRCDIR" && cd "$SRCDIR"
1645 done
1646
1647 RPMS=`find $BUILD_ROOT/$TOPDIR/RPMS -type f -name "*.rpm" 2>/dev/null || true`
1648 DEBS=`find $BUILD_ROOT/$TOPDIR/DEBS -type f -name "*.deb" 2>/dev/null || true`
1649
1650 if test -n "$RPMS" -a -n "$BUILD_USER_ABUILD_USED" ; then
1651     echo "... checking for files with abuild user/group"
1652     BADFILE=
1653     while read un gn fn ; do
1654         if test "$un" = abuild -o "$gn" = abuild -o "$un" = ${ABUILD_UID} -o "$gn" = ${ABUILD_GID} ; then
1655             echo "  $un $gn $fn"
1656             BADFILE=true
1657         fi
1658     done < <(rpm -qp --qf '[%{FILEUSERNAME} %{FILEGROUPNAME} %{FILENAMES}\n]' $RPMS)
1659     if test -n "$BADFILE" ; then
1660         echo "please fix your filelist (e.g. add defattr)"
1661         cleanup_and_exit 1
1662     fi
1663 fi
1664
1665 if test -n "$RPMS" -a -d "$BUILD_ROOT/usr/lib/build/checks" ; then
1666     export PNAME=""
1667     export DO_RPM_REMOVE=true
1668     for SRPM in $BUILD_ROOT/$TOPDIR/SRPMS/*src.rpm ; do
1669         test -f "$SRPM" && PNAME=`rpm --nodigest --nosignature -qp --qf "%{NAME}" $SRPM`
1670     done
1671     for CHECKSCRIPT in $BUILD_ROOT/usr/lib/build/checks/* ; do
1672         echo "... running `basename $CHECKSCRIPT`"
1673         $CHECKSCRIPT || cleanup_and_exit 1
1674     done
1675 fi
1676
1677 if test -n "$RPMS" -a "$DO_CHECKS" != "false" -a -x "$BUILD_ROOT/opt/testing/bin/rpmlint" -a -d "$BUILD_ROOT/$TOPDIR/RPMS" ; then
1678     LINT_RPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/RPMS \
1679         \( -name "*-debuginfo-*" -o -name "*-debugsource-*" \
1680         -o -name "*-32bit-*" -o -name "*-64bit-*" \
1681         -o -name "*-x86-*" -o -name "*-ia32-*" \) -prune \
1682         -o -type f -name '*.rpm' -print))
1683     SRPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/SRPMS -type f -name "*.rpm"))
1684     echo
1685     echo "RPMLINT report:"
1686     echo "==============="
1687     chroot $BUILD_ROOT /opt/testing/bin/rpmlint --info ${LINT_RPM_FILE_LIST[*]#$BUILD_ROOT} ${SRPM_FILE_LIST[*]#$BUILD_ROOT} || cleanup_and_exit 1
1688 fi
1689
1690 if test \( -n "$RPMS" -o -n "$DEBS" \) -a -n "$CREATE_BASELIBS" -a \( -d "$BUILD_ROOT/$TOPDIR/RPMS" -o -d "$BUILD_ROOT/$TOPDIR/DEBS" \)  ; then
1691     # $BUILD_DIR is set to /.build when using a vm. So we need to
1692     # hardcode /usr/lib/build instead of $BUILD_DIR to prefer
1693     # mkbaselibs from the distro.
1694     # (and force distro version if CREATE_BASELIBS=internal)
1695     if test -f $BUILD_ROOT/usr/lib/build/mkbaselibs -o "$CREATE_BASELIBS" != internal ; then
1696         mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1697         create_baselibs
1698     fi
1699 fi
1700
1701 # IMPLEMENT ME: place code for creating binary package deltas here
1702
1703 if test -n "$RUNNING_IN_VM" -a -n "$VM_SWAP"; then
1704     echo "... saving built packages"
1705     swapoff "$VM_SWAP"
1706     args="--padstart 512 --padend 512 -v"
1707     case "$BUILDTYPE" in
1708         spec)
1709             computeblocklists $args $TOPDIR/RPMS/*/*.rpm $TOPDIR/SRPMS/* > "$VM_SWAP"
1710             ;;
1711         dsc)
1712             computeblocklists $args $TOPDIR/DEBS/*.deb $TOPDIR/SOURCES.DEB/* > "$VM_SWAP"
1713             ;;
1714         kiwi)
1715             computeblocklists $args $TOPDIR/KIWI/* > "$VM_SWAP"
1716             ;;
1717     esac || cleanup_and_exit 1
1718 fi
1719
1720 if test -n "$RPMS" -a -d "$BUILD_ROOT/$TOPDIR/RPMS" -a -d "$BUILD_ROOT/.build.oldpackages" -a -x "$BUILD_ROOT/usr/lib/build/same-build-result.sh" ; then
1721     echo "... comparing built packages with the former built"
1722     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1723     # exit with 2, if packages built successfull, but have no changes to former built packages.
1724     if chroot $BUILD_ROOT /usr/lib/build/same-build-result.sh /.build.oldpackages "$TOPDIR/RPMS" "$TOPDIR/SRPMS"; then
1725         cleanup_and_exit 2
1726     fi
1727 fi
1728
1729 echo
1730 echo "$HOST finished \"build $SPECFILE\" at `date`."
1731 echo
1732
1733 cleanup_and_exit 0