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