summaryrefslogtreecommitdiff
path: root/build
blob: 710a9246e3cb488fa657c3c55a842edab218b803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#!/bin/bash
# Script to build a package.  It uses init_buildsystem to setup a chroot
# building tree.  This script needs a directory as parameter.  This directory
# has to include sources and a spec file.
#
# BUILD_ROOT        here the packages will be built
#
# (c) 1997-2006 SuSE GmbH Nuernberg, Germany

test -z "$BUILD_DIR" && BUILD_DIR=/usr/lib/build
test -z "$BUILD_ROOT" && BUILD_ROOT=/var/tmp/build-root
test -z "$BUILD_RPMS" && BUILD_RPMS=/media/dvd/suse
test -z "$BUILD_ARCH" && {
    BUILD_ARCH=`uname -m`
    test i686 = "$BUILD_ARCH" && BUILD_ARCH=i586
}
export BUILD_ARCH BUILD_ROOT BUILD_RPMS BUILD_DIR

export PATH=$BUILD_DIR:$PATH

# This is for insserv
export YAST_IS_RUNNING=instsys

unset LANGUAGE
unset LANG
export LC_ALL=POSIX
umask 022

echo_help () {
    cat << EOT

Some comments for build
-----------------------

With build you can create rpm packages.  They will be built in a chroot
system.  This chroot system will be setup automatically.  Normally you can
simply call build with a spec file as parameter - nothing else has to be
set.

If you want to set the directory were the chroot system will be setup
(at the moment it uses $BUILD_ROOT),
simply set the the environment variable BUILD_ROOT.

Example:

  export BUILD_ROOT=/var/tmp/mybuildroot


Normally build builds the complete package including src.rpm (rpmbuild -ba).
If you want let build only make the binary package, simply set

   export BUILD_RPM_BUILD_STAGE=-bb

(or -bc, -bp, -bi, ...  see "Maximum RPM" for more details [*]).

When the build command succeeds, the rpm files can be found under
$BUILD_ROOT/usr/src/packages/RPMS/


Known Parameters:

  --help      You already got it :)

  --clean     Delete old build root before initializing it

  --no-init   Skip initialization of build root and start with build
              immediately.

  --rpms path1:path2:...
              Specify path where to find the RPMs for the build system

  --arch arch1:arch2:...
              Specify what architectures to select from the RPMs

  --useusedforbuild
              Do not expand dependencies but search the specfile for
              usedforbuild lines.

  --verify    Run verify when initializing the build root

  --extra-packs pack
              Also install package 'pack'

  --root rootdir
              Use 'rootdir' to setup chroot environment

  --baselibs  Create -32bit/-64bit/-x86 rpms for other architectures

  --list-state
              List rpms that would be used to create a fresh build root.
              Does not create the build root or perform a build.

Remember to have fun!

[*] Maximum RPM: http://www.rpm.org/max-rpm/
EOT
}
usage () {
    echo "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|spec-to-build]"
    cleanup_and_exit
}

function clean_build_root () {
        test -n "$BUILD_ROOT" && {
            umount -n $BUILD_ROOT/proc 2> /dev/null
            umount -n $BUILD_ROOT/dev/pts 2> /dev/null
            umount -n $BUILD_ROOT/mnt 2> /dev/null
            rm -rf $BUILD_ROOT/*
        }
}

#
#  cleanup_and_exit
#
cleanup_and_exit () {
    test -z "$1" && set 0
    if test -n "$RUNNING_IN_XEN" ; then
	cd /
	if test -n "$XENSWAP" -a -e "$XENSWAP" ; then
	    swapoff "$XENSWAP"
	    echo -n "BUILDSTATUS$1" >"$XENSWAP"
	fi
	exec >&0 2>&0	# so that the logging tee finishes
	sleep 1		# wait till tee terminates
	kill -9 -1	# goodbye cruel world
	exec /bin/bash -c 'mount -n -o remount,ro / ; halt -f'
	halt -f
    fi
    exit $1
}

function create_baselibs {
    echo "... creating baselibs"
    BRPMS=
    for RPM in $BUILD_ROOT$TOPDIR/RPMS/*/*.rpm ; do
        BRPMS="$BRPMS ${RPM#$BUILD_ROOT}"
    done
    BASELIBS_CFG=
    if test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs.conf ; then
	BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs.conf"
    fi
    if test -f $BUILD_ROOT/usr/lib/build/mkbaselibs ; then
	if test -z "$BASELIBS_CFG" -a -e $BUILD_ROOT/usr/lib/build/baselibs.conf ; then
	    BASELIBS_CFG="-c /usr/lib/build/baselibs.conf"
	fi
	chroot $BUILD_ROOT /usr/lib/build/mkbaselibs -c /usr/lib/build/baselibs_global.conf $BASELIBS_CFG $BRPMS || cleanup_and_exit 1
    else
	# use external version
	rm -rf $BUILD_ROOT/.mkbaselibs
	mkdir -p $BUILD_ROOT/.mkbaselibs
	cp -f $BUILD_DIR/mkbaselibs $BUILD_ROOT/.mkbaselibs
	cp -f $BUILD_DIR/baselibs_global.conf $BUILD_ROOT/.mkbaselibs
	if test -z "$BASELIBS_CFG" -a -e $BUILD_DIR/baselibs.conf ; then
	    cp -f $BUILD_DIR/baselibs.conf $BUILD_ROOT/.mkbaselibs/baselibs.conf
	    BASELIBS_CFG="-c /.mkbaselibs/baselibs.conf"
	fi
	chroot $BUILD_ROOT /.mkbaselibs/mkbaselibs -c /.mkbaselibs/baselibs_global.conf $BASELIBS_CFG $BRPMS || cleanup_and_exit 1
	rm -rf $BUILD_ROOT/.mkbaselibs
    fi
}

DO_INIT=true
CLEAN_BUILD=false
SRCDIR=
BUILD_JOBS=
ABUILD_TARGET_ARCH=
CREATE_BASELIBS=
USEUSEDFORBUILD=
LIST_STATE=
XENIMAGE=
XENSWAP=
XENMEMORY=
BUILD_INIT_BUILDSYSTEM=init_buildsystem
RUNNING_IN_XEN=
RPMLIST=
RELEASE=
REASON=
NOROOTFORBUILD=
LOGFILE=
KILL=
CHANGELOG=

if test "$0" = "/.build/build" ; then
    BUILD_ROOT=/
    BUILD_DIR=/.build
    . $BUILD_DIR/build.data
    echo "machine type: `uname -m`"
    if test "$PERSONALITY" != 0 -a -z "$PERSONALITY_SET" ; then
	export PERSONALITY_SET=true
	echo "switching personality to $PERSONALITY..."
	# this is 32bit perl/glibc, thus the 32bit syscall number
	exec perl -e 'syscall(136, '$PERSONALITY') == -1 && warn("personality: $!\n");exec "/.build/build" || die("/.build/build: $!\n")'
    fi
    PATH=$BUILD_DIR:$PATH
    RUNNING_IN_XEN=true
    mount -orw -n -tproc none /proc 2>/dev/null
    mount -n -o remount,rw /
    if test -n "$XENSWAP" ; then
	for i in 1 2 3 4 5 6 7 8 9 10 ; do
	    test -e "$XENSWAP" && break
	    test $i = 1 && echo "waiting for $XENSWAP to appear"
	    echo -n .
	    sleep 1
	done
	test $i = 1 || echo
    fi
    umount -l /dev 2>/dev/null
    if test -n "$XENSWAP" ; then
	rm -f "$XENSWAP"
	umask 027
	mknod "$XENSWAP" b 3 2
	umask 022
	swapon -v "$XENSWAP" || exit 1
    fi
    set "/.build-srcdir/$SPECFILE"
    HOST="$MYHOSTNAME"
    export HOST
fi

while test -n "$1"; do
  PARAM="$1"
  ARG="$2"
  shift
  case $PARAM in
    *-*=*)
      ARG=${PARAM#*=}
      PARAM=${PARAM%%=*}
      set -- "----noarg=$PARAM" "$@"
  esac
  case $PARAM in
      *-help|-h)
        echo_help
        cleanup_and_exit
      ;;
      *-no*init)
        DO_INIT=false
      ;;
      *-clean)
        CLEAN_BUILD=true
      ;;
      *-kill)
        KILL=true
      ;;
      *-rpms)
        BUILD_RPMS="$ARG"
	if [ -z "$BUILD_RPMS" ] ; then
	  echo_help
	  cleanup_and_exit
	fi
        shift
      ;;
      *-arch)
        BUILD_ARCH="$ARG"
        shift
      ;;
      *-verify)
        export VERIFY_BUILD_SYSTEM=true
      ;;
      *-target)
	ABUILD_TARGET_ARCH="$ARG"
	shift
      ;;
      *-jobs) 
	BUILD_JOBS="$ARG"
	shift
      ;;
      *-extra*packs|-X)
        BUILD_EXTRA_PACKS="$BUILD_EXTRA_PACKS $ARG"
        shift
      ;;
      *-baselibs)
        CREATE_BASELIBS=true
        ;;
      *-root)
        BUILD_ROOT="$ARG"
        shift
      ;;
      *-dist) 
	BUILD_DIST="$ARG"
	export BUILD_DIST
	shift
      ;;
      *-xen)
        XENIMAGE="$ARG"
        shift
      ;;
      *-xenswap)
        XENSWAP="$ARG"
        shift
      ;;
      *-xenmemory)
        XENMEMORY="memory=$ARG"
        shift
      ;;
      *-rpmlist)
        RPMLIST="--rpmlist $ARG"
	BUILD_RPMS=
        shift
      ;;
      *-release)
        RELEASE="--release $ARG"
        shift
      ;;
      *-logfile)
        LOGFILE="$ARG"
        shift
      ;;
      *-reason)
        REASON="$ARG"
        shift
      ;;
      *-norootforbuild)
        NOROOTFORBUILD=true
      ;;
      *-stage)
        BUILD_RPM_BUILD_STAGE="$ARG"
        shift
      ;;
      *-useusedforbuild)
        USEUSEDFORBUILD="--useusedforbuild"
      ;;
      *-list*state)
	LIST_STATE=true
      ;;
      ----noarg)
        echo "$ARG does not take an argument"
        cleanup_and_exit
      ;;
      *-changelog)
        CHANGELOG=true
      ;;
      -*)
        echo Unknown Option "$PARAM". Exit.
        cleanup_and_exit 1
      ;;
      *)
        test -n "$SRCDIR" && usage
        SRCDIR="$PARAM"
      ;;
    esac
done

test -z "$LIST_STATE" -a "$UID" != 0 && {
    echo You have to be root to use $0. Exit.
    cleanup_and_exit 1
}

if test -n "$KILL" ; then
    test -z "$SRCDIR" || usage
    if test -z "$XENIMAGE" ; then
	if ! $BUILD_DIR/killchroot -s 9 $BUILD_ROOT ; then
	    echo "could not kill build in $BUILD_ROOT"
	    exit 1
	fi
    else
	XENID="${XENIMAGE%/root}"
	XENID="${XENID##*/}"
	if xm list "build:$XENID" >/dev/null 2>&1 ; then
	    if ! xm destroy "build:$XENID" ; then
		echo "could not kill xen build $XENID"
		exit 1
	    fi
	fi
    fi
    exit 0
fi

case $BUILD_ARCH in
  i686) BUILD_ARCH="i686:i586:i486:i386" ;;
  i586) BUILD_ARCH="i586:i486:i386" ;;
  i486) BUILD_ARCH="i486:i386" ;;
  x86_64) BUILD_ARCH="x86_64:i686:i586:i486:i386" ;;
esac
if test "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
    cpuflags=`grep ^flags /proc/cpuinfo`
    cpuflags="$cpuflags "
    test "$cpuflags" = "${cpuflags/ cx8 /}" -o "$cpuflags" = "${cpuflags/ cmov /}" && {
	echo "Your cpu doesn't support i686 rpms. Exit."
	exit 1
    }
fi

if test "`echo $SRCDIR | cut -c 1`" != "/" ; then
    SRCDIR=`pwd`/$SRCDIR
fi

if test -f $SRCDIR ; then
    SPECFILES=`basename $SRCDIR`
    SRCDIR=`dirname $SRCDIR`
else
    SPECFILES=""
    for i in $SRCDIR/*.spec ; do
        SPECFILES="$SPECFILES `basename $i`"
    done
    test -z "$SPECFILES" && {
	for i in $SRCDIR/*.src.rpm ; do
	    SPECFILES="$SPECFILES `basename $i`"
	done
    }
fi

test -z "$SPECFILES" && {
    echo no spec files and src rpms found in $SRCDIR. exit...
    cleanup_and_exit 1
}

if test -z "$SRCDIR" -o ! -d "$SRCDIR" ; then
    echo Usage: $0 \<src-dirctory\>
    cleanup_and_exit 1
fi

if test -n "$LIST_STATE" ; then
    BUILD_ROOT=`mktemp -d /var/tmp/build-list-state-XXXXXX`
    test -d "$BUILD_ROOT" || exit 1
    SPECFILE=$SRCDIR/$SPECFILES
    if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
       rm -rf $BUILD_ROOT/usr/src/packages
       mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES $BUILD_ROOT/usr/src/packages/SPECS
       rpm -i --nodigest --nosignature --root $BUILD_ROOT $SPECFILE || {
	   echo "could not install $SPECFILE." 2>&1
	   rm -rf $BUILD_ROOT
	   cleanup_and_exit 1
       }
       for SPECFILE in $BUILD_ROOT/usr/src/packages/SPECS/*.spec ; do : ; done
    fi
    init_buildsystem --list-state $USEUSEDFORBUILD $SPECFILE $BUILD_EXTRA_PACKS
    ERR=$?
    rm -rf $BUILD_ROOT
    cleanup_and_exit $ERR
fi

if test -d "$BUILD_ROOT" ; then
    # check if it is owned by root
    test -O "$BUILD_ROOT" || {
	echo "BUILD_ROOT=$BUILD_ROOT must be owned by root. Exit..."
	cleanup_and_exit 1
    }
else
    test "$BUILD_ROOT" != "${BUILD_ROOT%/*}" && mkdir -p "${BUILD_ROOT%/*}"
    mkdir $BUILD_ROOT || {
	echo "can not create BUILD_ROOT=$BUILD_ROOT. Exit..."
	cleanup_and_exit 1
    }
fi

if test -n "$XENIMAGE" ; then
    umount -n $BUILD_ROOT/proc 2> /dev/null
    umount -n $BUILD_ROOT/dev/pts 2> /dev/null
    umount -t loop $BUILD_ROOT 2>/dev/null
    if test "$CLEAN_BUILD" = true ; then
	echo "Creating filesystem on $XENIMAGE"
	if ! echo y | mkfs -t reiserfs -f $XENIMAGE >/dev/null 2>&1 ; then
	    if ! echo y | mkfs -t reiserfs -f $XENIMAGE ; then
		cleanup_and_exit 1
	    fi
	fi
    fi
    mount -oloop $XENIMAGE $BUILD_ROOT || cleanup_and_exit 1
    if test -n "$XENSWAP" ; then
	dd if=/dev/zero of="$XENSWAP" bs=12 count=1 conv=notrunc 2>/dev/null
	mkswap "$XENSWAP"
    fi
fi

rm -f $BUILD_ROOT/exit

if test -z "$XENIMAGE" -a -z "$LOGFILE"; then
    LOGFILE="$BUILD_ROOT/.build.log"
fi

if test -n "$LOGFILE" ; then
    echo  logging output to $LOGFILE...
    rm -f $LOGFILE
    touch $LOGFILE
    if test -n "$XENIMAGE" ; then
	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
    else
	exec 1> >(exec -a 'build logging tee' tee -a $LOGFILE) 2>&1
    fi
fi

#
# say hello
#
test -z "$HOST" && HOST=`hostname`
echo $HOST started \"build $SPECFILES\" at `date`.
echo
test -n "$REASON" && echo "$REASON"
echo Using BUILD_ROOT=$BUILD_ROOT
test -n "$BUILD_RPMS" && echo Using BUILD_RPMS=$BUILD_RPMS
echo Using BUILD_ARCH=$BUILD_ARCH
test -n "$XENIMAGE" && echo "Doing XEN build in $XENIMAGE"
echo

test "$BUILD_ARCH" = all && BUILD_ARCH=

cd $SRCDIR

for SPECFILE in $SPECFILES ; do
    #
    # first setup building directory...
    #
    test -s $SPECFILE || {
       echo $SPECFILE is empty.  This should not happen...
       continue
    }

    if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
	echo processing src rpm `pwd`/$SPECFILE...
	TOPDIR=`rpm --eval '%_topdir'`
	rm -rf $BUILD_ROOT$TOPDIR
	mkdir -p $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SPECS
	rpm -i --nodigest --nosignature --root $BUILD_ROOT $SPECFILE || {
	    echo "could not install $SPECFILE."
	    continue
	}
	rm -rf $BUILD_ROOT/.build-srcdir
	mkdir -p $BUILD_ROOT/.build-srcdir
	mv $BUILD_ROOT$TOPDIR/SOURCES/* $BUILD_ROOT/.build-srcdir
	mv $BUILD_ROOT$TOPDIR/SPECS/* $BUILD_ROOT/.build-srcdir
	MYSRCDIR=$BUILD_ROOT/.build-srcdir
	cd $MYSRCDIR || cleanup_and_exit 1
	for SPECFILE in *.spec ; do : ; done
    else
	echo processing specfile `pwd`/$SPECFILE...
	MYSRCDIR=$SRCDIR
    fi

    ADDITIONAL_PACKS=""
    test -n "$BUILD_EXTRA_PACKS" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS $BUILD_EXTRA_PACKS"
    test -n "$CREATE_BASELIBS" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS build"

    if test "$CLEAN_BUILD" = true ; then
        clean_build_root
        DO_INIT=true
    fi

    if test -n "$CHANGELOG" -a -z "$RUNNING_IN_XEN" ; then
	rm -f $BUILD_ROOT/.build-changelog
	case $SPECFILE in
	  *.dsc) CFFORMAT=debian ;;
	  *) CFFORMAT=rpm ;;
	esac
	echo "running changelog2spec --target $CFFORMAT --file $MYSRCDIR/$SPECFILE"
	if ! $BUILD_DIR/changelog2spec --target $CFFORMAT --file "$MYSRCDIR/$SPECFILE" > $BUILD_ROOT/.build-changelog ; then
	    rm -f $BUILD_ROOT/.build-changelog
	fi
    fi

    if test -n "$XENIMAGE" ; then
	# do fist stage of init_buildsystem
        echo init_buildsystem $USEUSEDFORBUILD $RPMLIST $SPECFILE $ADDITIONAL_PACKS ...
        init_buildsystem --prepare $USEUSEDFORBUILD $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS || cleanup_and_exit 1
	# start up xen, rerun ourself
	mkdir -p $BUILD_ROOT/.build
	cp -a $BUILD_DIR/. $BUILD_ROOT/.build
	if ! test $MYSRCDIR = $BUILD_ROOT/.build-srcdir ; then
	    mkdir $BUILD_ROOT/.build-srcdir
	    cp -p $MYSRCDIR/* $BUILD_ROOT/.build-srcdir
	    MYSRCDIR=$BUILD_ROOT/.build-srcdir
	fi
	Q="'\''"
	echo "SPECFILE='${SPECFILE//\'/$Q}'" > $BUILD_ROOT/.build/build.data
	echo "BUILD_JOBS='${BUILD_JOBS//\'/$Q}'" >> $BUILD_ROOT/.build/build.data
	echo "BUILD_ARCH='${BUILD_ARCH//\'/$Q}'" >> $BUILD_ROOT/.build/build.data
	echo "BUILD_RPMS='${BUILD_RPMS//\'/$Q}'" >> $BUILD_ROOT/.build/build.data
	case $BUILD_DIST in
	    */*)
		cp $BUILD_DIST $BUILD_ROOT/.build/build.dist
		BUILD_DIST=/.build/build.dist
		;;
	esac
	echo "BUILD_DIST='${BUILD_DIST//\'/$Q}'" >> $BUILD_ROOT/.build/build.data
	echo "RELEASE='${RELEASE//\'/$Q}'" >> $BUILD_ROOT/.build/build.data
	echo "NOROOTFORBUILD='${NOROOTFORBUILD//\'/$Q}'" >> $BUILD_ROOT/.build/build.data
	echo "CREATE_BASELIBS='$CREATE_BASELIBS'" >> $BUILD_ROOT/.build/build.data
	echo "REASON='${REASON//\'/$Q}'" >> $BUILD_ROOT/.build/build.data
	echo "CHANGELOG='${CHANGELOG//\'/$Q}'" >> $BUILD_ROOT/.build/build.data
	test -n "$XENSWAP" && echo "XENSWAP='/dev/hda2'" >> $BUILD_ROOT/.build/build.data
	PERSONALITY_SYSCALL=
	PERSONALITY=0
	case `perl -V:archname` in
	    *x86_64*) PERSONALITY_SYSCALL=135 ;;
	    *i?86*)   PERSONALITY_SYSCALL=136 ;;
	esac
	test -n "$PERSONALITY_SYSCALL" && PERSONALITY=`perl -e 'print syscall('$PERSONALITY_SYSCALL', 0)."\n"'`
	echo "PERSONALITY='$PERSONALITY'" >> $BUILD_ROOT/.build/build.data
	echo "MYHOSTNAME='`hostname`'" >> $BUILD_ROOT/.build/build.data
	umount $BUILD_ROOT
	XMROOT=file:$XENIMAGE
	XMROOT=${XMROOT/#file:\/dev/phy:}
	XMROOT="disk=$XMROOT,hda1,w"
	XMSWAP=
	if test -n "$XENSWAP" ; then
	    XMSWAP=file:$XENSWAP
	    XMSWAP=${XMSWAP/#file:\/dev/phy:}
	    XMSWAP="disk=$XMSWAP,hda2,w"
	fi
	XENID="${XENIMAGE%/root}"
	XENID="${XENID##*/}"
	if test "$PERSONALITY" != 0 ; then
	    # have to switch back to PER_LINUX to make xm work
	    perl -e 'syscall('$PERSONALITY_SYSCALL', 0); exec(@ARGV) || die("$ARGV[0]: $!\n")' xm create -c $BUILD_DIR/xen.conf name="build:$XENID" $XENMEMORY $XMROOT $XMSWAP extra="init=/.build/build panic=1 console=ttyS0"
	else
	    xm create -c $BUILD_DIR/xen.conf name="build:$XENID" $XENMEMORY $XMROOT $XMSWAP extra="init=/.build/build panic=1 console=ttyS0"
	fi
	if test -n "$XENSWAP" ; then
	    BUILDSTATUS=`dd if="$XENSWAP" bs=12 count=1 2>/dev/null`
	    case $BUILDSTATUS in
	      BUILDSTATUS[0-9])
		exit ${BUILDSTATUS#BUILDSTATUS}
		;;
	    esac
	    exit 1
	fi
	exit 0
    fi

    if test "$DO_INIT" = true ; then
        echo init_buildsystem $USEUSEDFORBUILD $RPMLIST $SPECFILE $ADDITIONAL_PACKS ...
        $BUILD_INIT_BUILDSYSTEM $USEUSEDFORBUILD $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS || cleanup_and_exit 1
    fi

    if test -z "$BUILD_DIST" -a -e "$BUILD_ROOT/.guessed_dist" ; then
	BUILD_DIST=`cat $BUILD_ROOT/.guessed_dist`
    fi

    if test "$SPECFILE" = "${SPECFILE%.dsc}" ; then
	TOPDIR=`chroot $BUILD_ROOT rpm --eval '%_topdir'`
    else
	TOPDIR=/usr/src/packages
	mkdir -p $BUILD_ROOT$TOPDIR
    fi

    rm -f $BUILD_ROOT/.build.packages
    ln -s ${TOPDIR#/} $BUILD_ROOT/.build.packages

    #
    # fix rpmrc if we are compiling for i686
    #
    test -f $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 && mv $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 $BUILD_ROOT/usr/lib/rpm/rpmrc
    if test -e $BUILD_ROOT/usr/lib/rpm/rpmrc -a "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
	mv $BUILD_ROOT/usr/lib/rpm/rpmrc $BUILD_ROOT/usr/lib/rpm/rpmrc_i586
	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
    fi

    #
    # check if we want to build with the abuild user
    #
    BUILD_USER=root
    if test "$BUILD_USER" = abuild ; then
	egrep '^#[       ]*needsrootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=root
    else
	egrep '^#[       ]*norootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=abuild
    fi
    test -n "$NOROOTFORBUILD" && BUILD_USER=abuild
    if test $BUILD_USER = abuild ; then
        if ! egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
            echo 'abuild::99:99:Autobuild:/home/abuild:/bin/bash' >>$BUILD_ROOT/etc/passwd
            echo 'abuild::99:' >>$BUILD_ROOT/etc/group
            mkdir -p $BUILD_ROOT/home/abuild
            chown 99:99 $BUILD_ROOT/home/abuild
        fi
	if test -f $BUILD_ROOT/etc/shadow ; then
	    sed -e "s@^root::@root:*:@" < $BUILD_ROOT/etc/shadow > $BUILD_ROOT/etc/shadow.t && mv $BUILD_ROOT/etc/shadow.t $BUILD_ROOT/etc/shadow
	fi
    else
        if egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
            egrep -v '^abuild:' <$BUILD_ROOT/etc/passwd >$BUILD_ROOT/etc/passwd.new
            mv $BUILD_ROOT/etc/passwd.new $BUILD_ROOT/etc/passwd
            egrep -v '^abuild:' <$BUILD_ROOT/etc/group >$BUILD_ROOT/etc/group.new
            mv $BUILD_ROOT/etc/group.new $BUILD_ROOT/etc/group
            rm -rf $BUILD_ROOT/home/abuild
        fi
    fi

    mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
    mount -n -tdevpts none $BUILD_ROOT/dev/pts 2> /dev/null

    #
    # now clean up RPM building directories
    #
    rm -rf $BUILD_ROOT$TOPDIR
    for i in BUILD RPMS/`arch` RPMS/i386 RPMS/noarch SOURCES SPECS SRPMS ; do
        mkdir -p $BUILD_ROOT$TOPDIR/$i
	test BUILD_USER = abuild && chown 99:99 $BUILD_ROOT$TOPDIR/$i
    done
    test -e $BUILD_ROOT/exit && cleanup_and_exit
    mkdir -p $BUILD_ROOT$TOPDIR/SOURCES
    cp -p $MYSRCDIR/* $BUILD_ROOT$TOPDIR/SOURCES/
    test $MYSRCDIR = $BUILD_ROOT/.build-srcdir && rm -rf $MYSRCDIR

    CHANGELOGARGS=
    test -n "$CHANGELOG" -a -f "$BUILD_ROOT/.build-changelog" && CHANGELOGARGS="--changelog $BUILD_ROOT/.build-changelog"

    if test "$SPECFILE" = "${SPECFILE%.dsc}" ; then
	# do buildrequires/release substitution
	substitutedeps $RELEASE --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" $CHANGELOGARGS "$BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE" "$BUILD_ROOT/.spec.new" || cleanup_and_exit 1
	# extract macros from configuration
	getmacros --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" > $BUILD_ROOT/root/.rpmmacros
	test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmmacros $BUILD_ROOT/home/abuild/.rpmmacros
	# extract optflags from configuration
	getoptflags --dist "$BUILD_DIST" --configdir "$BUILD_DIR/configs" --archpath "$BUILD_ARCH" > $BUILD_ROOT/root/.rpmrc
	test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmrc $BUILD_ROOT/home/abuild/.rpmrc
    fi
    if test -f $BUILD_ROOT/.spec.new ; then
	if ! cmp -s $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT/.spec.new ; then
	    echo -----------------------------------------------------------------
	    echo I have the following modifications for $SPECFILE:
	    diff $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT/.spec.new
	    mv $BUILD_ROOT/.spec.new $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE
	else
	    rm -f $BUILD_ROOT/.spec.new
	fi
    fi

    if test "$SPECFILE" != "${SPECFILE%.dsc}" ; then
	rm -rf $BUILD_ROOT$TOPDIR/BUILD
	test $BUILD_USER = abuild && chown 99:99 $BUILD_ROOT$TOPDIR
	DEB_TRANSFORM=
	for f in $BUILD_ROOT$TOPDIR/SOURCES/debian.* ; do
	    test -f $f && DEB_TRANSFORM=true
	done
	if test -n $DEB_TRANSFORM ; then
	    mkdir -p $BUILD_ROOT$TOPDIR/SOURCES.DEB
            debtransform $CHANGELOGARGS $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT$TOPDIR/SOURCES.DEB
	    chroot $BUILD_ROOT su -c "dpkg-source -x $TOPDIR/SOURCES.DEB/*.dsc $TOPDIR/BUILD" - $BUILD_USER
        else
	    chroot $BUILD_ROOT su -c "dpkg-source -x $TOPDIR/SOURCES/$SPECFILE $TOPDIR/BUILD" - $BUILD_USER
        fi
    fi
    if test $BUILD_USER = abuild ; then
        chown -R 99:99 $BUILD_ROOT$TOPDIR/*
    else
        chown -R root:root $BUILD_ROOT$TOPDIR/*
    fi
    cd $BUILD_ROOT$TOPDIR/SOURCES || cleanup_and_exit 1

    echo -----------------------------------------------------------------
    if test "$BUILD_USER" = root ; then
        echo ----- building $SPECFILE
    else
        echo ----- building $SPECFILE "(user $BUILD_USER)"
    fi
    echo -----------------------------------------------------------------
    echo -----------------------------------------------------------------
    test -n "$RUNNING_IN_XEN" && ifconfig lo 127.0.0.1 up
    test -n "$RUNNING_IN_XEN" -a -n "$MYHOSTNAME" && hostname "$MYHOSTNAME"

    BUILD_SUCCEDED=false

    if test "$SPECFILE" = "${SPECFILE%.dsc}" ; then
	test -z "$BUILD_RPM_BUILD_STAGE" && BUILD_RPM_BUILD_STAGE=-ba

	BUILD_PARAMETERS="$BUILD_RPM_BUILD_STAGE"
	test -n "$ABUILD_TARGET_ARCH" && BUILD_PARAMETERS="$BUILD_PARAMETERS --target=\"$ABUILD_TARGET_ARCH\""
	test -n "$BUILD_JOBS" && BUILD_PARAMETERS="$BUILD_PARAMETERS --eval \"%define jobs \\\"$BUILD_JOBS\\\"\""
	test root != "$BUILD_USER" && BUILD_PARAMETERS="$BUILD_PARAMETERS --eval \"%define _srcdefattr (-,root,root)\""
	RPMBUILD=rpmbuild
	test -x $BUILD_ROOT/usr/lib/rpm/rpmi || RPMBUILD=rpm
	chroot $BUILD_ROOT su -c "$RPMBUILD $BUILD_PARAMETERS $TOPDIR/SOURCES/$SPECFILE" - $BUILD_USER < /dev/null && BUILD_SUCCEDED=true
    else
	chroot $BUILD_ROOT su -c "cd $TOPDIR/BUILD && dpkg-buildpackage -us -uc -rfakeroot" - $BUILD_USER < /dev/null && BUILD_SUCCEDED=true
	mkdir -p $BUILD_ROOT/$TOPDIR/DEBS
	for DEB in $BUILD_ROOT/$TOPDIR/*.deb ; do
	    test -e "$DEB" && mv "$DEB" "$BUILD_ROOT/$TOPDIR/DEBS"
	done
    fi

    umount -n $BUILD_ROOT/proc 2> /dev/null
    umount -n $BUILD_ROOT/mnt 2> /dev/null
    umount -n $BUILD_ROOT/dev/pts 2> /dev/null
    test "$BUILD_SUCCEDED" = true || cleanup_and_exit 1
    test -d "$SRCDIR" && cd "$SRCDIR"
done

if test -n "$CREATE_BASELIBS" ; then
    mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
    create_baselibs
    umount -n $BUILD_ROOT/proc 2> /dev/null
fi

cleanup_and_exit 0