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