Merge branch 'master' into devel
[tools/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 recipe file.
5 #
6 # BUILD_ROOT        here the packages will be built
7 #
8 ################################################################
9 #
10 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License version 2 or 3 as
14 # published by the Free Software Foundation.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program (see the file COPYING); if not, write to the
23 # Free Software Foundation, Inc.,
24 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 #
26 ################################################################
27
28 # some VMs do not allow to specify the init process...
29 if test "$0" = /sbin/init ; then
30     exec /.build/build "$@"
31 fi
32
33 test -z "$BUILD_DIR" -a -e /.build/build.data -a -z "$BUILD_IGNORE_2ND_STAGE" && BUILD_DIR=/.build
34 test -z "$BUILD_DIR" && BUILD_DIR=/usr/lib/build
35 test -z "$BUILD_ROOT" && BUILD_ROOT=/var/tmp/build-root
36 test -z "$CONFIG_DIR" && CONFIG_DIR="$BUILD_DIR/configs"
37
38 export BUILD_ARCH BUILD_HOST_ARCH BUILD_ROOT BUILD_RPMS BUILD_DIR BUILD_DEBUG
39 export BUILD_DIST
40
41 icecream=0
42 definesnstuff=()
43 repos=()
44 old_packages=()
45
46 # slurp in vm support
47 . "$BUILD_DIR/build-vm"
48
49 # slurp in recipe support
50 . "$BUILD_DIR/build-recipe"
51
52 # slurp in package binary support
53 . "$BUILD_DIR/build-pkg"
54
55 # need to restore build root owner for non-root builds
56 browner=
57
58 # Default uid:gid for the build user
59 ABUILD_UID=399
60 ABUILD_GID=399
61
62 DO_INIT=true
63 DO_BUILD=true
64 DO_LINT=
65 DO_CHECKS=true
66 SHORT_CIRCUIT=false
67 NO_TOPDIR_CLEANUP=false
68 CLEAN_BUILD=
69 KEEP_PACKS=
70 RECIPEFILES=()
71 SRCDIR=
72 BUILD_JOBS=
73 ABUILD_TARGET=
74 CREATE_BASELIBS=
75 USEUSEDFORBUILD=
76 LIST_STATE=
77
78 RUNNING_IN_VM=
79 RPMLIST=
80 RELEASE=
81 REASON=
82 NOROOTFORBUILD=
83 LOGFILE=
84 KILL=
85 CHANGELOG=
86 BUILD_DEBUG=
87 INCARNATION=
88 DISTURL=
89 LINKSOURCES=
90 OVERLAY=
91 RSYNCSRC=
92 RSYNCDEST=
93 RSYNCDONE=
94 SIGNDUMMY=
95 DO_STATISTICS=
96 RUN_SHELL=
97 CCACHE=
98 DLNOSIGNATURE=
99 CACHE_DIR=/var/cache/build
100 USEHIGHERDEPS=
101
102
103 # This is for insserv
104 export YAST_IS_RUNNING=instsys
105
106 unset LANGUAGE
107 unset LANG
108 export LC_ALL=POSIX
109 umask 022
110
111 echo_help () {
112     cat << EOT
113
114 Some comments for build
115 -----------------------
116
117 With build you can create binary packages.  They will be built in a chroot
118 system.  This chroot system will be setup automatically.  Normally you can
119 simply call build with a spec file as parameter - nothing else has to be
120 set.
121
122 If you want to set the directory were the chroot system will be setup
123 (at the moment it uses $BUILD_ROOT),
124 simply set the the environment variable BUILD_ROOT.
125
126 Example:
127
128   export BUILD_ROOT=/var/tmp/mybuildroot
129
130
131 Normally build builds the complete package including src.rpm (rpmbuild -ba).
132 If you want let build only make the binary package, simply set
133
134    export BUILD_RPM_BUILD_STAGE=-bb
135
136 (or -bc, -bp, -bi, ...  see "Maximum RPM" for more details [*]).
137
138 When the build command succeeds, the rpm files can be found under
139 $BUILD_ROOT/usr/src/packages/RPMS/
140
141
142 Known Parameters:
143
144   --help      You already got it :)
145
146   --kill      Instead of starting a build kill the one currently
147               running.
148
149   --shell     Instead of starting a build start a root shell in
150               the build root.
151
152   --clean     Delete old build root before initializing it
153
154   --no-init   Skip initialization of build root and start with build
155               immediately.
156
157   --no-checks Do not run checks (postbuild and %check)
158
159   --lint      Run rpmlint after build.
160
161   --logfile logfile
162               Capture build output to logfile. Defaults to
163               .build.log in the build root for non-VM builds.
164
165   --repo PATH_OR_URL
166               Use package repository at PATH_OR_URL. Supported formats
167               are rpm-md, yast2, debian, and arch linux.
168               Alternatively zypp://NAME specifies the zypp
169               repository NAME. The repo must be refreshed with zypp
170               so package meta data is available locally. With emtpy
171               NAME all enabled repositories are used.
172
173   --rpms path1:path2:...
174               Specify path where to find the RPMs for the build system
175
176   --arch arch1:arch2:...
177               Specify what architectures to select from the RPMs
178
179   --verify    Run verify when initializing the build root
180
181   --extra-packs pack
182   -X pack
183               Also install package 'pack'
184
185   --root rootdir
186               Use 'rootdir' to setup chroot environment
187
188   --cachedir cachedir
189               Use 'cachedir' to cache remote repo's packages. The
190               default cache dir is /var/cache/build, every repo
191               given by --repo corresponds to a subdir named
192               as md5sum of its repo url, for example:
193                  /var/cache/build/3e8ea9b47808629414a0cebc33ea285e
194
195   --oldpackages oldpackagesdir
196               Define a directory with a former build
197
198   --baselibs  Create -32bit/-64bit/-x86 rpms for other architectures
199
200   --list-state
201               List rpms that would be used to create a fresh build root.
202               Does not create the build root or perform a build.
203
204   --dist dist
205               Distribution to use
206
207   --with X
208               Enable feature X for build
209
210   --without X
211               Disable feature X for build
212
213   --define 'X Y'
214               Define macro X with value Y
215
216   --release release
217               Override Release in spec file
218
219   --stage -bSTAGE
220               Set stage for rpmbuild. Defaults to -ba.
221
222   --target platform
223               Set target platform for rpmbuild
224
225   --jobs N    Use N parallel processes during build.
226               Sets %jobs and %_smp_mflags macros and
227               defines the number of CPUs to use for
228               VMs.
229
230   --threads N sets number of threads for VM
231
232   --ccache
233               Use ccache to speed up rebuilds
234
235   --icecream N
236               Use N parallel build jobs with icecream
237
238   --overlay OVERLAY
239               Copy overlay filesystem to buildroot after installing
240               all RPMs. This must be a valid directory.
241
242   --rsync-src RSYNCSRC
243               Copy overlay folder (RSYNCSRC) to a folder (RSYNCDEST)
244               inside the buildroot using rsync.
245               It will "%define RSYNCDONE 1" for handling %setup in your
246               specfile. E.g.:
247               %prep
248               %if 0%{?RSYNCDONE}
249               %setup -n aaa_base -T -D -b 5 -b 7
250               %else
251               %setup -n aaa_base -b 5 -b 7
252               %endif
253
254   --rsync-dest RSYNCDEST
255               Copy overlay folder (RSYNCSRC) to a folder (RSYNCDEST)
256               inside the buildroot using rsync.
257
258   --uid uid:gid
259               Specify the uid and gid to use for the abuild user.
260               This is useful if you are hacking in the buildroot.
261               This must be set to the same value if the buildroot is re-used.
262
263   --statistics
264               monitor used resources during build inside VM
265
266   --kvm
267               Use KVM to secure build process. Your hardware needs to support
268               virtualization
269
270   --xen
271               Use XEN to secure build process, you to run in a XEN Dom0 environment.
272
273   --lxc
274               Use Linux Containers to isolate the process. This may not be 100% safe.
275
276   --openstack 
277               Cloud build
278
279   --ec2 
280               Cloud build
281
282   --emulator 
283               Use any generic emulator to isolate the build process. You need to write
284               an emulator/emulator.sh script and put it next to the build script sources.
285
286   --emulator-script SCRIPT
287               specify another emulator instead of emulator.sh
288
289   --vm-type TYPE
290               Use virtual machine instead of chroot
291               TYPE is one of xen|kvm|uml|qemu|lxc|zvm|openstack|ec2
292
293   --vm-worker GUEST
294               GUEST is a z/VM build worker controlled by the controlling
295               z/VM build machine. 
296
297   --vm-worker-nr N
298               Each worker in z/VM needs a uniq number. This is needed to 
299               calculate uniq device addresses for root and swap device.
300
301   --vm-region NAME
302               EC2 only: defines amazon control server
303
304   --vm-server NAME
305               openstack only: defines control server name
306
307   --vm-disk FILE
308               Use FILE as disk for virtual machine.
309               Defaults to \$BUILD_ROOT.img if unset
310
311   --vm-swap FILE
312               Use FILE as swap space for virtual machine. The swap space is
313               also used for retrieving packages from the VM so its size must
314               be sufficiently large
315
316   --vm-disk-size SIZEINMB
317   --vm-swap-size SIZEINMB
318   --vm-disk-filesystem TYPE
319               Defaults for automatic setup of VM root/swap files
320
321   --vm-memory SIZEINMB
322               Set amount of RAM for VMs
323
324   --vm-hugetlbfs HUGETLBFSPATH
325               Use hugetlb for memory management, path to mounted hugetlbfs.
326
327   --vm-kernel FILE
328   --vm-initrd FILE
329               Kernel and initrd to use for VM (kvm and qemu only)
330
331   --debug
332               Enable creation of a debuginfo package
333
334 Remember to have fun!
335
336 [*] Maximum RPM: http://www.rpm.org/max-rpm/
337 EOT
338 }
339
340 usage () {
341     echo "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|recipe-to-build]"
342     cleanup_and_exit 1
343 }
344
345 #
346 #  cleanup_and_exit
347 #  return values: 0 -> success, new packages built
348 #                 1 -> error, build failed
349 #                 2 -> successfull build, but no changes to former built packages
350 #                 3 -> something wrong with build host
351 #
352 cleanup_and_exit () {
353     trap EXIT
354     test -z "$1" && set 0
355     rm -f $BUILD_ROOT/.repo.config
356     rm -f $BUILD_ROOT/exit
357     if test "$1" -eq 1 -a -x /bin/df ; then
358         # okay, it failed, but maybe because disk space?
359         if df $BUILD_ROOT 2>/dev/null | grep -q "100%"; then
360             set 3
361         fi
362     fi
363     if test -n "$RUNNING_IN_VM" ; then
364         echo "$1" >  /.build/_exitcode
365         test -n "$browner" && chown "$browner" $BUILD_ROOT
366         vm_shutdown "$1"
367     else
368         umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
369         umount -n $BUILD_ROOT/proc 2>/dev/null || true
370         while true
371         do
372                 umount -n $BUILD_ROOT/dev/pts 2>/dev/null
373                 if test $? -ne 0; then
374                         break
375                 fi
376         done
377
378         umount -n $BUILD_ROOT/dev/shm 2>/dev/null || true
379         umount -n $BUILD_ROOT/sys 2>/dev/null || true
380         test -n "$VM_IMAGE" -a "$VM_IMAGE" != 1 && umount $BUILD_ROOT 2>/dev/null || true
381         test -n "$VM_TYPE" && vm_cleanup
382     fi
383     exit $1
384 }
385
386 fail_exit() {
387     cleanup_and_exit 1
388 }
389
390 shellquote() {
391     for arg ; do
392         arg=${arg/\\/\\\\}
393         arg=${arg/\$/\\\$}
394         arg=${arg/\"/\\\"}
395         arg=${arg/\`/\\\`}
396         echo -n " \"$arg\""
397     done
398 }
399
400 # create a shell script from command line. Used for preserving arguments
401 # through /bin/su -c
402 toshellscript() {
403     echo "#!/bin/sh -x"
404     echo -n exec
405     shellquote "$@"
406     echo
407 }
408
409 setupccache() {
410     if test -n "$CCACHE" ; then
411         if mkdir -p $BUILD_ROOT/var/lib/build/ccache/bin; then
412             for i in $(ls $BUILD_ROOT/usr/bin | grep -E '^(cc|gcc|[cg][+][+])([-]?[234][.]?[0-9])*$'); do
413 #                ln -sf /usr/bin/ccache $BUILD_ROOT/var/lib/build/ccache/bin/$i
414                 rm -f $BUILD_ROOT/var/lib/build/ccache/bin/$i
415                 test -e $BUILD_ROOT/usr/bin/$i || continue
416                 echo '#! /bin/sh' > $BUILD_ROOT/var/lib/build/ccache/bin/$i
417                 echo "test -e /usr/bin/$i || exit 1" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
418                 echo 'export PATH=/usr/lib/icecc/bin:/opt/icecream/bin:/usr/bin:$PATH' >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
419                 echo "ccache $i \"\$@\"" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
420                 chmod 755 $BUILD_ROOT/var/lib/build/ccache/bin/$i
421                 echo "Installed ccache wrapper as $BUILD_ROOT/var/lib/build/ccache/bin/$i"
422             done
423         fi
424         mkdir -p "$BUILD_ROOT/.ccache"
425         chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT/.ccache"
426         echo "export CCACHE_DIR=/.ccache" > "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
427         echo 'export PATH=/var/lib/build/ccache/bin:$PATH' >> "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
428     else
429         rm -f "$BUILD_ROOT"/var/lib/build/ccache/bin/{gcc,g++,cc,c++}
430     fi
431 }
432
433 setupicecream()
434 {
435     local icecreamdir="/var/run/icecream"
436     if test `readlink "$BUILD_ROOT/var/run"` = '/run' ; then
437         icecreamdir="/run/icecream"
438     fi
439     if [ "$icecream" -eq 0 ]; then
440         rm -rf "$BUILD_ROOT$icecreamdir"
441         rm -f "$BUILD_ROOT/etc/profile.d/build_icecream.sh"
442         return 0
443     fi
444
445     if ! chroot "$BUILD_ROOT" rpm -q icecream >/dev/null 2>/dev/null; then
446         echo "*** icecream package not installed ***"
447         return 1
448     fi
449
450     echo "using icecream with $icecream jobs"
451
452     if test -z "$CCACHE" ; then
453         echo 'export PATH=/usr/lib/icecc/bin:/opt/icecream/bin:$PATH' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
454     else
455         echo 'export CCACHE_PATH=/usr/lib/icecc/bin:/opt/icecream/bin' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
456     fi
457
458     local icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT$icecreamdir/*.tar.{bz2,gz}`)
459     icecc_vers=${icecc_vers//$BUILD_ROOT/}
460
461     # XXX use changelog like autobuild does instead?
462     # only run create-env if compiler or glibc changed
463     if test -z "$icecc_vers" \
464         -o ! -e "$BUILD_ROOT/$icecc_vers" \
465         -o "$BUILD_ROOT/usr/bin/gcc" -nt "$BUILD_ROOT/$icecc_vers" \
466         -o "$BUILD_ROOT/usr/bin/g++" -nt "$BUILD_ROOT/$icecc_vers" \
467         -o "$BUILD_ROOT/usr/bin/as" -nt "$BUILD_ROOT/$icecc_vers" \
468         -o "$BUILD_ROOT/lib/libc.so.6" -nt "$BUILD_ROOT/$icecc_vers"
469     then
470         rm -rf "$BUILD_ROOT$icecreamdir"
471         mkdir -p "$BUILD_ROOT$icecreamdir"
472         if [ -e "$BUILD_ROOT"/usr/bin/create-env ]; then
473           createenv=/usr/bin/create-env
474         elif [ -e "$BUILD_ROOT"/usr/lib/icecc/icecc-create-env ]; then
475           createenv="/usr/lib/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
476         elif [ -e "$BUILD_ROOT"/usr/lib64/icecc/icecc-create-env ]; then
477           createenv="/usr/lib64/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
478         else
479             echo "create-env not found"
480             return 1
481         fi
482         echo "creating new env in '$icecreamdir'"
483         chroot $BUILD_ROOT bash -c "cd $icecreamdir; $createenv" || cleanup_and_exit 1
484         icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/$icecreamdir/*.tar.{bz2,gz}`)
485         icecc_vers=${icecc_vers//$BUILD_ROOT/}
486         echo "created icecream environment $icecc_vers"
487     else
488         echo "reusing existing icecream environment $icecc_vers"
489     fi
490     if test -n "$icecc_vers" ; then
491         echo "export ICECC_VERSION=$icecc_vers" >> "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
492     fi
493 }
494
495 setmemorylimit() {
496     if test -n "$VM_IMAGE" -o -n "$RUNNING_IN_VM" ; then
497         return
498     fi
499     local mem
500     local limit
501     while read mem; do
502         case "$mem" in
503             MemTotal:*)
504                 set -- $mem
505                 eval "limit=\$(($2/3*4))"
506             ;;
507             SwapTotal:*)
508                 set -- $mem
509                 eval "limit=\$(($2/3*4+$limit))"
510             ;;
511         esac
512     done < <(cat /proc/meminfo) # cat for proc stuff
513
514     ulimit -v $limit
515     echo "Memory limit set to ${limit}KB"
516 }
517
518 create_baselibs() {
519     local pkgs=()
520     local line
521
522     BASELIBS_CFG=
523
524     if test "$BUILDTYPE" == arch ; then
525         return
526     fi
527     if test "$BUILDTYPE" == dsc ; then
528         pkgs=($DEBS)
529     else # spec and kiwi
530         if test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs.conf ; then
531             BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs.conf"
532         fi
533         if test -e $BUILD_ROOT/usr/lib/build/baselibs_global.conf; then
534             BASELIBS_GLOBAL="-c /usr/lib/build/baselibs_global.conf"
535         fi
536         pkgs=($RPMS)
537     fi
538
539     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
540     # don't use -R as extracted sources, build root etc might be below $TOPDIR
541     chown "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"/* "$BUILD_ROOT$TOPDIR"/RPMS/* || true
542
543     local mkbaselibs="/usr/lib/build/mkbaselibs"
544     local whichone=''
545     # $BUILD_DIR is set to /.build when using a vm. So we need to
546     # hardcode /usr/lib/build instead of $BUILD_DIR to prefer
547     # mkbaselibs from the distro.
548     if test -f $BUILD_ROOT$mkbaselibs; then
549         if test -z "$BASELIBS_CFG" -a -e $BUILD_ROOT/usr/lib/build/baselibs.conf ; then
550             BASELIBS_CFG="-c /usr/lib/build/baselibs.conf"
551         fi
552     else
553         if test "$CREATE_BASELIBS" = 'internal'; then
554             echo "Warning: mkbaselibs missing in build root, skipping baselibs"
555             return
556         fi
557         # use external version
558         whichone=" (external)"
559         mkbaselibs="/.mkbaselibs/mkbaselibs"
560         rm -rf "$BUILD_ROOT/.mkbaselibs"
561         mkdir -p "$BUILD_ROOT/.mkbaselibs"
562         cp -f $BUILD_DIR/mkbaselibs $BUILD_ROOT/.mkbaselibs/
563         if test "$BUILDTYPE" == "dsc" ; then
564             cp -f $BUILD_DIR/baselibs_global-deb.conf $BUILD_ROOT/.mkbaselibs/baselibs_g.conf
565             cp -f $BUILD_ROOT$TOPDIR/SOURCES/baselibs-deb.conf $BUILD_ROOT/.mkbaselibs/baselibs-deb.conf
566             BASELIBS_CFG="-c /.mkbaselibs/baselibs-deb.conf"
567         else
568             cp -f $BUILD_DIR/baselibs_global.conf $BUILD_ROOT/.mkbaselibs/baselibs_g.conf
569             if test -z "$BASELIBS_CFG" -a -e $BUILD_DIR/baselibs.conf; then
570                 cp -f $BUILD_DIR/baselibs.conf $BUILD_ROOT/.mkbaselibs/baselibs.conf
571                 BASELIBS_CFG="-c /.mkbaselibs/baselibs.conf"
572             fi
573         fi
574         if test -e $BUILD_ROOT/.mkbaselibs/baselibs_g.conf; then
575             BASELIBS_GLOBAL="-c /.mkbaselibs/baselibs_g.conf"
576         fi
577     fi
578     echo "... creating baselibs$whichone"
579     while read line
580     do
581         chroot $BUILD_ROOT su -c "$mkbaselibs $BASELIBS_GLOBAL $BASELIBS_CFG $line" - $BUILD_USER || cleanup_and_exit 1
582     done < <(IFS=$'\n'; echo "${pkgs[*]#$BUILD_ROOT}" | xargs -n 1024)
583     rm -rf "$BUILD_ROOT/.mkbaselibs"
584 }
585
586 copy_oldpackages() {
587     local i=0
588     local d
589     local dest
590     test -z "$RUNNING_IN_VM" || return 0
591     if test -z "$old_packages" ; then
592         rm -rf "$BUILD_ROOT"/.build.oldpackages*
593         return 0
594     fi
595     for d in "${old_packages[@]}"; do
596         dest="$BUILD_ROOT/.build.oldpackages"
597         test "$i" = 0 || dest="$dest$i"
598         if test -d "$d" -a "$d" != "$dest" ; then
599             rm -rf "$dest"
600             mkdir -p "$dest"
601             cp -L $d/* "$dest"
602             : $((++i))
603         fi
604     done
605 }
606
607 become_root_or_fail() {
608     if test ! -w /root ; then
609         echo "You have to be root to use $0" >&2
610         exit 1
611     fi
612     cleanup_and_exit 1
613 }
614
615 mkdir_build_root() {
616     # strip trailing slash
617     test "$BUILD_ROOT" != / && BUILD_ROOT="${BUILD_ROOT%/}"
618     if test -d "$BUILD_ROOT" ; then
619         # check if it is owned by root
620         if test -z "$RUNNING_IN_VM" -a \! -O "$BUILD_ROOT" -a "`stat -c %u $BUILD_ROOT`" -ne 0 ; then
621             echo "BUILD_ROOT=$BUILD_ROOT must be owned by root. Exit..."
622             cleanup_and_exit 1
623         fi
624     else
625         test "$BUILD_ROOT" != "${BUILD_ROOT%/*}" && mkdir -p "${BUILD_ROOT%/*}"
626         if ! mkdir $BUILD_ROOT ; then
627             echo "can not create BUILD_ROOT=$BUILD_ROOT. Exit..."
628             cleanup_and_exit 1
629         fi
630     fi
631
632     if test ! -w "$BUILD_ROOT" ; then
633         echo "Error: BUILD_ROOT=$BUILD_ROOT not writeable, try --clean."
634         cleanup_and_exit 3
635     fi
636
637     rm -rf "$BUILD_ROOT/.build.packages"
638     if test -z "$VM_TYPE" -a -z "$RUNNING_IN_VM" ; then
639          # don't touch this in VM
640          rm -rf "$BUILD_ROOT/.build"
641          mkdir -p "$BUILD_ROOT/.build"
642     fi
643 }
644
645 copy_overlay() {
646     if test -d "$OVERLAY"; then
647         pushd $OVERLAY
648         echo "Copying overlay to BUILD_ROOT"
649         tar -cpf - . | (cd $BUILD_ROOT ; tar -xvf -)
650         popd
651     else
652         echo "OVERLAY ($OVERLAY) is no directory - skipping"
653     fi
654 }
655
656 run_rsync() {
657     if test -n "$RSYNCDEST" ; then
658         if test -d "$RSYNCSRC" ; then
659             if ! test -d "$BUILD_ROOT/$RSYNCDEST" ; then
660                 echo "ATTENTION! Creating missing target directory ($BUILD_ROOT/$RSYNCDEST)."
661                 mkdir -p $BUILD_ROOT/$RSYNCDEST
662             fi
663             echo "Running rsync ..."
664             rsync -av $RSYNCSRC/* $BUILD_ROOT/$RSYNCDEST/
665             chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT/$RSYNCDEST"
666             RSYNCDONE=true
667             echo "... done"
668         else
669             echo "RSYNCSRC is not a directory - skipping"
670         fi
671     else
672         echo "RSYNCSRC given, but not RSYNCDEST - skipping"
673     fi
674 }
675
676 hide_passwords()
677 {
678     local i j
679     r=()
680     rm -f $BUILD_ROOT/.repo.config
681     for i in "${repos[@]}"; do
682         if [ "$i" == "--repo" -o "$i" == "--repository" ]; then
683             r=("${r[@]}" "$i")
684         else
685             echo $i |grep -E "^http[s]?:\/\/[^\/]*\/?" >/dev/null && echo $i >> $BUILD_ROOT/.repo.config
686             j=$(echo $i | sed -e "s#://[^@]*@#://#")
687             r=("${r[@]}" "$j")
688         fi
689     done
690     repos=("${r[@]}")
691     echo ${repos[@]}
692 }
693
694
695 #### main ####
696
697 trap fail_exit EXIT
698
699 shopt -s nullglob
700
701 export PATH=$BUILD_DIR:/sbin:/usr/sbin:/bin:/usr/bin:$PATH
702
703 if vm_detect_2nd_stage ; then
704     set "/.build-srcdir/$RECIPEFILE"
705     export PATH=/.build:$PATH
706 fi
707
708 . $BUILD_DIR/common_functions || exit 1
709
710 export HOST
711
712 needarg() {
713     if test -z "$ARG" ; then
714         echo "$PARAM needs an agrument" >&2
715         cleanup_and_exit 1
716     fi
717 }
718
719 while test -n "$1"; do
720     PARAM="$1"
721     ARG="$2"
722     test "$ARG" = "${ARG#-}" || ARG=
723     shift
724     case $PARAM in
725       *-*=*)
726         ARG=${PARAM#*=}
727         PARAM=${PARAM%%=*}
728         set -- "----noarg=$PARAM" "$@"
729         ;;
730     esac
731     case ${PARAM/#--/-} in
732       -help|-h)
733         echo_help
734         cleanup_and_exit
735       ;;
736       -noinit|-no-init)
737         DO_INIT=false
738       ;;
739       -no-build)
740        DO_BUILD=false
741       ;;
742       -nochecks|-no-checks)
743         DO_CHECKS=false
744       ;;
745       -clean)
746         CLEAN_BUILD='--clean'
747       ;;
748       -kill)
749         KILL=true
750       ;;
751       -rpms)
752         needarg
753         BUILD_RPMS="$ARG"
754         shift
755       ;;
756       -arch)
757         needarg
758         BUILD_ARCH="$ARG"
759         shift
760       ;;
761       -verify)
762         export VERIFY_BUILD_SYSTEM=true
763       ;;
764       -target)
765         needarg
766         ABUILD_TARGET="$ARG"
767         shift
768       ;;
769       -jobs)
770         needarg
771         BUILD_JOBS="$ARG"
772         shift
773       ;;
774       -threads)
775         needarg
776         BUILD_THREADS="$ARG"
777         shift
778       ;;
779       -extrapacks|-extra-packs|-X)
780         needarg
781         BUILD_EXTRA_PACKS="$BUILD_EXTRA_PACKS $ARG"
782         shift
783       ;;
784       -lint)
785         DO_LINT=true
786         ;;
787       -baselibs)
788         CREATE_BASELIBS=true
789         ;;
790       -baselibs-internal)
791         CREATE_BASELIBS=internal
792         ;;
793       -keep-packs)
794         KEEP_PACKS="--keep-packs"
795         ;;
796       -root)
797         needarg
798         BUILD_ROOT="$ARG"
799         shift
800       ;;
801       -cachedir)
802         needarg
803         CACHE_DIR="$ARG"
804         shift
805       ;;
806       -oldpackages)
807         needarg
808         old_packages=("${old_packages[@]}" "$ARG")
809         shift
810       ;;
811       -dist)
812         needarg
813         BUILD_DIST="$ARG"
814         shift
815       ;;
816       -release)
817         needarg
818         RELEASE="$ARG"
819         shift
820       ;;
821       -logfile)
822         needarg
823         LOGFILE="$ARG"
824         shift
825       ;;
826       -reason)
827         needarg
828         REASON="$ARG"
829         shift
830       ;;
831       -norootforbuild)
832         NOROOTFORBUILD=true
833       ;;
834       -short-circuit)
835         SHORT_CIRCUIT=true
836       ;;
837       -no-topdir-cleanup)
838         NO_TOPDIR_CLEANUP=true
839       ;;
840       -useusedforbuild)
841         USEUSEDFORBUILD="--useusedforbuild"
842       ;;
843       -configdir)
844         needarg
845         CONFIG_DIR="$ARG"
846         shift
847       ;;
848       -list*state)
849         LIST_STATE=true
850       ;;
851       -define|-with|-without)
852         needarg
853         PARAM="-${PARAM/#--/-}"
854         definesnstuff[${#definesnstuff[@]}]="$PARAM"
855         definesnstuff[${#definesnstuff[@]}]="$ARG"
856         shift
857       ;;
858       -repository|-repo)
859         needarg
860         repos[${#repos[@]}]="--repository"
861         repos[${#repos[@]}]="$ARG"
862         shift
863       ;;
864       -icecream)
865         needarg
866         icecream="$ARG"
867         test "$icecream" -gt 0 && BUILD_JOBS="$ARG"
868         shift
869       ;;
870       -ccache)
871         CCACHE=true
872       ;;
873       -statistics)
874         DO_STATISTICS=1
875       ;;
876       -debug)
877         BUILD_DEBUG=1
878       ;;
879       -incarnation)
880         needarg
881         INCARNATION=$ARG
882         shift
883       ;;
884       -disturl)
885         needarg
886         DISTURL=$ARG
887         shift
888       ;;
889       -linksources)
890         LINKSOURCES=true
891       ;;
892       -changelog)
893         CHANGELOG=true
894       ;;
895       -overlay)
896         needarg
897         OVERLAY=$ARG
898         shift
899       ;;
900       -rsync-src)
901         needarg
902         RSYNCSRC=$ARG
903         shift
904       ;;
905       -rsync-dest)
906         needarg
907         RSYNCDEST=$ARG
908         shift
909       ;;
910       -uid)
911         needarg
912         ABUILD_ID="$ARG"
913         if test -n "${ABUILD_ID//[0-9:]/}" ; then
914             echo "--uid argument must be uid:gid"
915             cleanup_and_exit
916         fi
917         ABUILD_UID=${ABUILD_ID%:*}
918         ABUILD_GID=${ABUILD_ID#*:}
919         shift
920       ;;
921       -rpmlist)
922         needarg
923         RPMLIST="--rpmlist $ARG"
924         BUILD_RPMS=
925         shift
926       ;;  
927       -shell)
928         RUN_SHELL=1
929         shift
930       ;;
931       -signdummy)
932         SIGNDUMMY=1
933       ;;
934       -nosignature)
935         DLNOSIGNATURE="--nosignature"
936       ;;
937       ---noarg)
938         echo "$ARG does not take an argument"
939         cleanup_and_exit
940       ;;
941       -use-higher-deps)
942         USEHIGHERDEPS="--use-higher-deps"
943       ;;
944       -*)
945         if vm_parse_options "$@" ; then
946             set -- "${nextargs[@]}"
947         elif recipe_parse_options "$@" ; then
948             set -- "${nextargs[@]}"
949         else
950             echo "Unknown option '$PARAM'. Exit."
951             cleanup_and_exit 1
952         fi
953       ;;
954       *)
955         RECIPEFILES[${#RECIPEFILES[@]}]="$PARAM"
956       ;;
957     esac
958 done
959
960 if [ "$SHORT_CIRCUIT" = true -a -z "$BUILD_RPM_BUILD_STAGE" ]; then
961     echo "--short-circuit needs a stage (use --stage)"
962     cleanup_and_exit 1
963 fi
964
965 if test -n "$KILL" ; then
966     test -z "$SRCDIR" || usage
967     if test -n "$VM_IMAGE" -a -n "$VM_SWAP" -a -n "$VM_TYPE"; then
968         # mark job as failed so that we don't extract packages
969         if test "$VM_TYPE" != zvm ; then
970             echo -n "BUILDSTATUS1" >"$VM_SWAP"
971         fi
972     fi
973     (set -C; > "$BUILD_ROOT/exit" 2>/dev/null || true)
974     if test -n "$VM_TYPE" ; then
975         vm_kill
976     else
977         if ! $BUILD_DIR/killchroot -s 9 $BUILD_ROOT ; then
978             echo "could not kill build in $BUILD_ROOT"
979             cleanup_and_exit 1
980         fi
981     fi
982     cleanup_and_exit 0
983 fi
984
985 if test -n "$CLEAN_BUILD" ; then
986     DO_INIT=true
987 fi
988
989 if test -n "$VM_TYPE" -a -z "$RUNNING_IN_VM" ; then
990     vm_verify_options
991 fi
992
993 if test -z "$RPMLIST" -a -z "$RUNNING_IN_VM" ; then
994     if test -z "$repos" -a -z "$BUILD_RPMS" ; then
995         repos=(--repository 'zypp://')
996     fi
997 else
998     repos=()
999 fi
1000
1001 set_build_arch
1002
1003 expand_recipe_directories
1004
1005 if test -n "$LIST_STATE" ; then
1006     BUILD_ROOT=`mktemp -d /var/tmp/build-list-state-XXXXXX`
1007     test -d "$BUILD_ROOT" || cleanup_and_exit 3
1008     RECIPEFILE=$RECIPEFILES # only one specified anyways
1009     if test "$RECIPEFILE" != "${RECIPEFILE%.src.rpm}" ; then
1010         MYSRCDIR="$BUILD_ROOT/usr/src/packages/SOURCES"
1011         recipe_unpack_srcrpm
1012         RECIPEFILE="$MYSRCDIR/$RECIPEFILE"
1013     fi
1014     init_buildsystem --configdir "$CONFIG_DIR" --cachedir "$CACHE_DIR" --list-state "${definesnstuff[@]}" "${repos[@]}" $DLNOSIGNATURE $KEEP_PACKS $USEHIGHERDEPS $USEUSEDFORBUILD $RECIPEFILE $BUILD_EXTRA_PACKS
1015     ERR=$?
1016     rm -rf "$BUILD_ROOT"
1017     cleanup_and_exit $ERR
1018 fi
1019
1020 # do vm setup if needed
1021 if test -z "$RUNNING_IN_VM" -a -n "$VM_TYPE" -a -n "$VM_IMAGE" ; then
1022     vm_setup
1023 fi
1024
1025 mkdir_build_root
1026 hide_passwords
1027
1028 if test "$BUILD_ROOT" = / ; then
1029     browner="$(stat -c %u /)"
1030 fi
1031
1032 rm -f $BUILD_ROOT/exit
1033
1034 if test -w /root ; then
1035     mkdir -p $BUILD_ROOT/proc
1036     mkdir -p $BUILD_ROOT/sys
1037     mkdir -p $BUILD_ROOT/dev/pts
1038     mount -n -tproc none $BUILD_ROOT/proc || true
1039     mount -n -tdevpts -omode=0620,gid=5 none $BUILD_ROOT/dev/pts
1040 fi
1041
1042 if test -z "$VM_IMAGE" -a -z "$LOGFILE" ; then
1043     LOGFILE="$BUILD_ROOT/.build.log"
1044 fi
1045
1046 if test -n "$LOGFILE" -a -z "$RUN_SHELL" ; then
1047     echo "logging output to $LOGFILE..."
1048     rm -f $LOGFILE
1049     touch $LOGFILE
1050     # set start time, to be substracted for build log timestamps
1051     STARTTIME=`perl -e 'print time()'`
1052
1053     if test -n "$RUNNING_IN_VM" ; then
1054         # no additional timestamps in inner vm build system
1055         exec 1> >(exec -a 'build logging' tee -a $LOGFILE) 2>&1
1056     elif test -n "$VM_IMAGE" ; then
1057         # external run of virtualization build
1058         exec 1> >(exec -a 'build logging' perl -e 'open(F,">>",$ARGV[0])||die("$ARGV[0]: $!\n");$|=1;select(F);$|=1;while(<STDIN>){my $p=sprintf("[%5ds] ", time()-'$STARTTIME');print STDOUT $p.$_;s/^\r//s;s/\r\n/\n/gs;print F $p.$_}' $LOGFILE) 2>&1
1059     else
1060         # plain chroot
1061         exec 1> >(exec -a 'build logging' perl -e 'open(F,">>",$ARGV[0])||die("$ARGV[0]: $!\n");$|=1;select(F);$|=1;while(<STDIN>){my $p=sprintf("[%5ds] ", time()-'$STARTTIME');print STDOUT $p.$_;print F $p.$_}' $LOGFILE) 2>&1
1062     fi
1063 fi
1064
1065 setmemorylimit
1066
1067 #
1068 # say hello
1069 #
1070 test -z "$HOST" && HOST=`hostname`
1071
1072 if test -z "$RUNNING_IN_VM" ; then
1073     echo Using BUILD_ROOT=$BUILD_ROOT
1074     test -n "$BUILD_RPMS" && echo Using BUILD_RPMS=$BUILD_RPMS
1075     echo Using BUILD_ARCH=$BUILD_ARCH
1076     test -n "$VM_TYPE" && echo "Doing $VM_TYPE build${VM_IMAGE:+ in $VM_IMAGE}"
1077     echo
1078 fi
1079
1080 test "$BUILD_ARCH" = all && BUILD_ARCH=
1081 BUILD_USER_ABUILD_USED=
1082
1083 for RECIPEFILE in "${RECIPEFILES[@]}" ; do
1084
1085     SRCDIR="${RECIPEFILE%/*}"
1086     RECIPEFILE="${RECIPEFILE##*/}"
1087
1088     recipe_set_buildtype
1089
1090     if test -z "$RUNNING_IN_VM" ; then
1091         echo
1092         echo "$HOST started \"build $RECIPEFILE\" at `date --utc`."
1093         echo
1094         test -n "$REASON" && echo "$REASON"
1095         echo
1096         TIME_START_TIME=`date +%s` # for statistics
1097     fi
1098
1099     #
1100     # first setup building directory...
1101     #
1102     cd "$SRCDIR"
1103     if ! test -s "$RECIPEFILE" ; then
1104          echo "$RECIPEFILE is empty.  This should not happen..."
1105          cleanup_and_exit 1
1106     fi
1107     MYSRCDIR="$SRCDIR"
1108
1109     # special hack to build from a .src.rpm
1110     test "$RECIPEFILE" != "${RECIPEFILE%.src.rpm}" && recipe_unpack_srcrpm
1111
1112     echo "processing recipe $MYSRCDIR/$RECIPEFILE ..."
1113
1114     ADDITIONAL_PACKS=
1115     test -z "$BUILD_EXTRA_PACKS" || ADDITIONAL_PACKS="$ADDITIONAL_PACKS $BUILD_EXTRA_PACKS"
1116     test -z "$CREATE_BASELIBS" || ADDITIONAL_PACKS="$ADDITIONAL_PACKS build"
1117     test -z "$CCACHE" || ADDITIONAL_PACKS="$ADDITIONAL_PACKS ccache"
1118     test "$icecream" = 0 || ADDITIONAL_PACKS="$ADDITIONAL_PACKS icecream gcc-c++"
1119     test -z "$DO_LINT" || ADDITIONAL_PACKS="$ADDITIONAL_PACKS rpmlint-Factory"
1120     test "$VMDISK_FILESYSTEM" = xfs && ADDITIONAL_PACKS="$ADDITIONAL_PACKS libblkid1"
1121     test "$VM_TYPE" = zvm && ADDITIONAL_PACKS="$ADDITIONAL_PACKS udev libcap2"
1122
1123     # we need to do this before the vm is started
1124     if test -n "$CHANGELOG" -a -z "$RUNNING_IN_VM" ; then
1125         rm -f $BUILD_ROOT/.build-changelog
1126         case $RECIPEFILE in
1127             *.dsc) CFFORMAT=debian ;;
1128             *) CFFORMAT=rpm ;;
1129         esac
1130         echo "running changelog2spec --target $CFFORMAT --file $MYSRCDIR/$RECIPEFILE"
1131         if ! $BUILD_DIR/changelog2spec --target $CFFORMAT --file "$MYSRCDIR/$RECIPEFILE" > $BUILD_ROOT/.build-changelog ; then
1132             rm -f $BUILD_ROOT/.build-changelog
1133         fi
1134     fi
1135
1136     if test -n "$VM_TYPE" -a -z "$RUNNING_IN_VM"; then
1137         vm_first_stage
1138         cleanup_and_exit 0
1139     fi
1140
1141     if test "$DO_INIT" = true ; then
1142         start_time=`date +%s`
1143         #
1144         # create legacy .buildenv file
1145         #
1146         test -z "$INCARNATION" && INCARNATION=0
1147         echo "BUILD_INCARNATION=$INCARNATION" > $BUILD_ROOT/.buildenv
1148         CREATE_BUILD_BINARIES=
1149         test "$BUILDTYPE" = preinstallimage && mkdir -p $BUILD_ROOT/.preinstall_image
1150         egrep '^#[       ]*needsbinariesforbuild[       ]*$' >/dev/null <$MYSRCDIR/$RECIPEFILE && CREATE_BUILD_BINARIES=--create-build-binaries
1151         test "$BUILDTYPE" = mock && CREATE_BUILD_BINARIES=--create-build-binaries
1152         test "$BUILDTYPE" = debootstrap && CREATE_BUILD_BINARIES=--create-build-binaries
1153         test "$BUILDTYPE" = livebuild && CREATE_BUILD_BINARIES=--create-build-binaries
1154         set -- init_buildsystem --configdir "$CONFIG_DIR" --cachedir "$CACHE_DIR" "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $DLNOSIGNATURE $KEEP_PACKS $USEHIGHERDEPS $USEUSEDFORBUILD $CREATE_BUILD_BINARIES $RPMLIST "$MYSRCDIR/$RECIPEFILE" $ADDITIONAL_PACKS
1155         echo "$* ..."
1156         start_time=`date +%s`
1157         "$@" || cleanup_and_exit 1
1158         check_exit
1159         TIME_INSTALL=$(( `date +%s` - $start_time ))
1160         unset start_time
1161         # arbitrary limit of 10MB
1162         if test $((`stat -f -c "%a*%S/1024/1024" $BUILD_ROOT`)) -lt 10; then
1163             # ensure that old stat is not failing (RHEL4)
1164             if df $BUILD_ROOT 2>/dev/null | grep -q "100%"; then
1165                 df -h $BUILD_ROOT
1166                 echo "build does not work on a completely full filesystem"
1167                 cleanup_and_exit 1
1168             fi
1169         fi
1170         mount -n -tproc none $BUILD_ROOT/proc || true
1171         mount -n -tdevpts -omode=0620,gid=5 none $BUILD_ROOT/dev/pts
1172
1173         copy_oldpackages
1174     fi
1175
1176     # hack to process preinstallimages early
1177     if test "$BUILDTYPE" = preinstallimage ; then
1178         recipe_build
1179         continue
1180     fi
1181
1182     if test -z "$BUILD_DIST" -a -e "$BUILD_ROOT/.guessed_dist" ; then
1183         read BUILD_DIST < $BUILD_ROOT/.guessed_dist
1184     fi
1185
1186     #
1187     # install dummy sign program if needed
1188     #
1189     test -f $BUILD_ROOT/usr/bin/sign_installed && mv $BUILD_ROOT/usr/bin/sign_installed $BUILD_ROOT/usr/bin/sign
1190     if test -n "$SIGNDUMMY" ; then
1191         test -f $BUILD_ROOT/usr/bin/sign && mv $BUILD_ROOT/usr/bin/sign $BUILD_ROOT/usr/bin/sign_installed
1192         cp $BUILD_DIR/signdummy $BUILD_ROOT/usr/bin/sign
1193         chmod 755 $BUILD_ROOT/usr/bin/sign
1194     fi
1195
1196     #
1197     # check if we want to build with the abuild user
1198     #
1199     BUILD_USER=abuild
1200     if test -x $BUILD_ROOT/bin/rpm ; then
1201         SUSE_VERSION=`chroot $BUILD_ROOT /bin/rpm --eval '%{?suse_version}' 2>/dev/null`
1202         test -n "$SUSE_VERSION" -a "${SUSE_VERSION:-0}" -le 1020 && BUILD_USER=root
1203     fi
1204     if test "$BUILD_USER" = abuild ; then
1205         egrep '^#[       ]*needsrootforbuild[       ]*$' >/dev/null <$RECIPEFILE && BUILD_USER=root
1206     else
1207         egrep '^#[       ]*norootforbuild[       ]*$' >/dev/null <$RECIPEFILE && BUILD_USER=abuild
1208     fi
1209     test -n "$NOROOTFORBUILD" && BUILD_USER=abuild
1210
1211     # appliance builds must run as root
1212     if test "$BUILDTYPE" = kiwi ; then
1213         imagetype=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $RECIPEFILE imagetype)
1214         test "$imagetype" = product || BUILD_USER=root
1215     fi
1216
1217     # fixup passwd/group
1218     if test $BUILD_USER = abuild ; then
1219         if ! egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1220             echo "abuild:x:${ABUILD_UID}:${ABUILD_GID}:Autobuild:/home/abuild:/bin/bash" >>$BUILD_ROOT/etc/passwd
1221             echo 'abuild:*:::::::' >>$BUILD_ROOT/etc/shadow # This is needed on Mandriva 2009
1222             echo 'abuild:*::' >>$BUILD_ROOT/etc/gshadow # This is needed on Ubuntu
1223             echo "abuild:x:${ABUILD_GID}:" >>$BUILD_ROOT/etc/group
1224             mkdir -p $BUILD_ROOT/home/abuild
1225             chown "$ABUILD_UID:$ABUILD_GID" $BUILD_ROOT/home/abuild
1226         else
1227             if ! egrep "^abuild:x?:${ABUILD_UID}:${ABUILD_GID}" >/dev/null <$BUILD_ROOT/etc/passwd ; then
1228                 sed -i '/^abuild:/d' $BUILD_ROOT/etc/passwd
1229                 sed -i '/^abuild:/d' $BUILD_ROOT/etc/group
1230                 echo "abuild:x:${ABUILD_UID}:${ABUILD_GID}:Autobuild:/home/abuild:/bin/bash" >>$BUILD_ROOT/etc/passwd
1231                 echo "abuild:x:${ABUILD_GID}:" >>$BUILD_ROOT/etc/group
1232                 chown "$ABUILD_UID:$ABUILD_GID" $BUILD_ROOT/home/abuild -R
1233             fi
1234         fi
1235         if test -f $BUILD_ROOT/etc/shadow ; then
1236             sed -i -e "s@^root::@root:*:@" $BUILD_ROOT/etc/shadow
1237         fi
1238         if test -f $BUILD_ROOT/etc/gshadow ; then
1239             sed -i -e "s@^root::@root:*:@" $BUILD_ROOT/etc/gshadow
1240         fi
1241         BUILD_USER_ABUILD_USED=true
1242     else
1243         # building as root
1244         ABUILD_UID=0
1245         ABUILD_GID=0
1246         if egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1247             rm -rf "$BUILD_ROOT/home/abuild"
1248             sed -i -e '/^abuild:/d' $BUILD_ROOT/etc/passwd
1249             sed -i -e '/^abuild:/d' $BUILD_ROOT/etc/group
1250             if test -f $BUILD_ROOT/etc/shadow ; then
1251                 sed -i -e '/^abuild:/d' $BUILD_ROOT/etc/shadow
1252             fi
1253             if test -f $BUILD_ROOT/etc/gshadow ; then
1254                 sed -i -e '/^abuild:/d' $BUILD_ROOT/etc/gshadow
1255             fi
1256         fi
1257     fi
1258
1259     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1260     mount -n -tdevpts -omode=0620,gid=5 none $BUILD_ROOT/dev/pts 2> /dev/null
1261     # needed for POSIX semaphores
1262     test -d $BUILD_ROOT/dev/shm || rm -f $BUILD_ROOT/dev/shm
1263     mkdir -p $BUILD_ROOT/dev/shm
1264     mount -n -ttmpfs none $BUILD_ROOT/dev/shm 2> /dev/null
1265
1266     if test -n "$RUNNING_IN_VM" ; then
1267         if test -x /sbin/ip ; then
1268             ip addr add 127.0.0.1/8 dev lo
1269             ip addr add ::1/128 dev lo
1270             ip link set lo up
1271         else
1272             ifconfig lo 127.0.0.1 up
1273             ifconfig lo add ::1/128
1274         fi
1275         if test -n "$MYHOSTNAME" ; then
1276             hostname "$MYHOSTNAME"
1277         fi
1278     fi
1279
1280     setupicecream
1281     setupccache
1282
1283     # fill build directories with sources. Also sets TOPDIR
1284     recipe_setup
1285     
1286     # strip prefix from autogenerated files of source services.
1287     for i in $BUILD_ROOT$TOPDIR/SOURCES/_service\:* ; do
1288         mv "$i" "${i%/*}/${i##*:}"
1289     done
1290     RECIPEFILE="${RECIPEFILE##*:}"
1291
1292     # create .build.packages link
1293     rm -f $BUILD_ROOT/.build.packages
1294     ln -s ${TOPDIR#/} $BUILD_ROOT/.build.packages
1295
1296     # nasty hack to prevent rpath on known paths
1297     # FIXME: do this only for suse
1298     if test -d "$BUILD_ROOT/etc/profile.d" ; then
1299         echo "export SUSE_IGNORED_RPATHS=/etc/ld.so.conf" > "$BUILD_ROOT/etc/profile.d/buildsystem.sh"
1300     fi
1301
1302     # get rid of old src dir, it is no longer needed and just wastes space
1303     test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir && rm -rf "$MYSRCDIR"
1304
1305     # patch recipes
1306     recipe_prepare
1307
1308     # hmmm
1309     chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
1310
1311     cd $BUILD_ROOT$TOPDIR/SOURCES || cleanup_and_exit 1
1312
1313     echo -----------------------------------------------------------------
1314     if test "$BUILD_USER" = root ; then
1315         echo ----- building $RECIPEFILE
1316     else
1317         echo ----- building $RECIPEFILE "(user $BUILD_USER)"
1318     fi
1319     echo -----------------------------------------------------------------
1320     echo -----------------------------------------------------------------
1321     BUILD_SUCCEEDED=false
1322
1323     if test -n "$OVERLAY" ; then
1324         copy_overlay
1325     fi
1326
1327     if test -n "$RSYNCSRC" ; then
1328         run_rsync
1329     fi
1330
1331     start_time=`date +%s`
1332     recipe_build
1333     if test "$DO_STATISTICS" = 1; then
1334         mkdir -p $TOPDIR/OTHER
1335         echo "TIME_main_build: $(( `date +%s` - $start_time ))"  >> $TOPDIR/OTHER/_statistics
1336     fi
1337     unset start_time
1338
1339     test "$BUILD_SUCCEEDED" = true || cleanup_and_exit 1
1340     test -d "$SRCDIR" && cd "$SRCDIR"
1341 done
1342
1343 if test -n "$RUNNING_IN_VM" -a -n "$DO_STATISTICS" ; then
1344     touch /.build/_statistics.exit
1345 fi
1346
1347 RPMS=`find $BUILD_ROOT/$TOPDIR/RPMS -type f -name "*.rpm" 2>/dev/null || true`
1348 DEBS=`find $BUILD_ROOT/$TOPDIR/DEBS -type f -name "*.deb" 2>/dev/null || true`
1349
1350 if test -n "$RPMS" -a -n "$BUILD_USER_ABUILD_USED" ; then
1351     recipe_check_file_owners
1352 fi
1353
1354 if test -n "$RPMS" -a -d "$BUILD_ROOT/usr/lib/build/checks" ; then
1355     export DO_RPM_REMOVE=true
1356     # find package name
1357     export PNAME=
1358     for SRPM in $BUILD_ROOT/$TOPDIR/SRPMS/*src.rpm ; do
1359         test -f "$SRPM" && PNAME=`rpm --nodigest --nosignature -qp --qf "%{NAME}" $SRPM`
1360     done
1361     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1362     for CHECKSCRIPT in $BUILD_ROOT/usr/lib/build/checks/* ; do
1363         echo "... running ${CHECKSCRIPT##*/}"
1364         $CHECKSCRIPT || cleanup_and_exit 1
1365     done
1366     umount -n $BUILD_ROOT/proc 2>/dev/null || true
1367 fi
1368
1369 # checkscripts may have deleted some binaries
1370 RPMS=`find $BUILD_ROOT/$TOPDIR/RPMS -type f -name "*.rpm" 2>/dev/null || true`
1371 DEBS=`find $BUILD_ROOT/$TOPDIR/DEBS -type f -name "*.deb" 2>/dev/null || true`
1372
1373 if test -n "$RPMS" -a "$DO_CHECKS" != false ; then
1374     recipe_run_rpmlint
1375 fi
1376
1377 if test \( -n "$RPMS" -o -n "$DEBS" \) -a -n "$CREATE_BASELIBS"; then
1378     create_baselibs
1379 fi
1380
1381 exitcode=0
1382
1383 # post build work
1384 # TODO: don't hardcode. instead run scripts in a directory as it's done for the checks
1385 if test -n "$RPMS" -a -d "$BUILD_ROOT/.build.oldpackages" ; then
1386     recipe_compare_oldpackages
1387     # no need to create deltas if the build is the same
1388     if test ! -e $BUILD_ROOT/.build/.same_result_marker ; then
1389         recipe_create_deltarpms
1390     fi
1391 fi
1392
1393 if test -n "$RUNNING_IN_VM" ; then
1394     vm_wrapup_build $(recipe_resultdirs) OTHER
1395 fi
1396
1397 echo
1398 echo "$HOST finished \"build $RECIPEFILE\" at `date --utc`."
1399 echo
1400
1401 cleanup_and_exit "$exitcode"