Add tdb2 build
[amitay/build-farm.git] / build_test.fns
1 #!/bin/sh -*- mode: shell-script; -*-
2
3 # build_farm -- distributed build/test architecture for samba, rsync, etc
4
5 # Copyright (C) 2001 by Andrew Tridgell <tridge@samba.org>
6 # Copyright (C) 2001 by Andrew Bartlett <abartlet@samba.org>
7 # Copyright (C) 2001, 2003 by Martin Pool <mbp@samba.org>
8
9 # default maximum runtime for any command
10 MAXTIME=25200 # 7 hours
11 SMBD_MAXTIME=18000 # 5 hours for a samba process ..
12 # default maximum memory size (100M) for any command
13 MAXMEM=100000
14 RUN_FROM_BUILD_FARM=yes
15 export RUN_FROM_BUILD_FARM
16 export MAXTIME SMBD_MAXTIME
17
18 deptrees="";
19
20 build_test_fns_id='$Id$'
21
22 copy_dir() {
23         Tsrc=$1
24         Tdst=$2
25         pwd
26         echo rsync -a --delete $Tsrc/ $Tdst
27         rsync -a --delete $Tsrc/ $Tdst || return 1
28         return 0
29 }
30
31 #############################
32 # build a signature of a tree, used to see if we
33 # need to rebuild 
34 #############################
35
36 sum_tree() {
37         sum_tree_test_root=$1
38         sum_tree_tree=$2
39         sum_tree_sum=$3
40         sum_tree_scm=$4
41         find $sum_tree_test_root/$sum_tree_tree -type f -print | grep -v version.h | sort | xargs sum > $sum_tree_sum
42         sum build_test build_test.fns >> $sum_tree_sum
43
44         if [ -f "$host.fns" ]; then
45                 sum $host.fns >> $sum_tree_sum
46         else
47                 sum generic.fns >> $sum_tree_sum
48         fi
49
50         if [ -f "$test_root/$tree.$scm" ]; then
51                 sum "$test_root/$tree.$scm" >> $sum_tree_sum
52         fi
53
54         for d in $deptrees; do
55                 dscm=`choose_scm "$d"`
56                 if [ -f "$test_root/$d.$dscm" ]; then
57                 sum "$test_root/$d.$dscm" >> $sum_tree_sum
58                 fi
59         done
60 }
61
62 #############################
63 # send the logs to the master site
64 #############################
65
66 send_logs() {
67         if [ "$nologreturn" = "yes" ]; then
68                 echo "skipping log transfer"
69         else
70                 log="$1"
71                 err="$2"
72                 shift
73                 shift
74                 chmod 0644 "$log" "$err"
75
76                 # xargs -i is implemented differently or not at all.
77                 # GNU xargs did not implement "-I" until 4.2.9:
78                 xargs --version 2>&1 | grep "^GNU xargs" > /dev/null
79                 status=$?
80                 if [ x"$status" = x"0" ]; then
81                         XARGS_IS_GNU=yes
82                 fi
83
84                 if [ x"$XARGS_IS_GNU" = x"yes" ]; then
85                         XARGS_I="xargs -i"
86                 else
87                         XARGS_I="xargs -I '{}'"
88                 fi
89
90                 find $log -size +40000 | $XARGS_I sh -c 'dd if={} bs=1024 count=20000 of={}.tmp && mv {}.tmp {} &&  echo "\n***LOG TRUNCATED***" >> {}'
91                 find $err -size +40000 | $XARGS_I sh -c 'dd if={} bs=1024 count=20000 of={}.tmp && mv {}.tmp {} &&  echo "\n***LOG TRUNCATED***" >> {}'
92
93                 rsync $* -c -q --password-file=.password -z --timeout=200 \
94                         "$log" "$err" $host@build.samba.org::build_farm_data/
95         fi
96 }
97
98 #############################
99 # send the logs when they haven't changed
100 # the aim is to just update the servers timestamp.
101 # sending with a very large rsync block size does this
102 # with minimal network traffic
103 #############################
104
105 send_logs_skip() {
106         touch "$1" "$2"
107         send_logs "$1" "$2" -B 10000000
108 }
109
110 ############################
111 # fetch the latest copy of the tree
112 ############################
113
114 fetch_tree() {
115         if [ "$norsync" = "yes" ]; then
116                 echo "skipping tree transfer"
117         else
118                 fetchtree=$1
119                 if rsync --exclude=autom4te.cache/ --exclude=.svn/ --exclude=.git/ \
120                         --delete-excluded -q --partial --timeout=200 -ctrlpz --delete --ignore-errors \
121                         samba.org::ftp/unpacked/$fetchtree/ $test_root/$fetchtree; then
122                         echo "transferred $fetchtree OK"
123                 else
124                         echo "transfer of $fetchtree failed code $?"
125                         return 1
126                 fi
127         fi
128         return 0
129 }
130
131 ############################
132 # fetch the latest copy of the rev meta info
133 ############################
134
135 fetch_revinfo() {
136         tree=$1
137         scm=$2
138
139         test -z "$scm" && return 1
140         test x"$scm" = x"unknown" && return 1
141         test x"$scm" = x"cvs" && return 1
142
143         if [ "$norsync" = "yes" ]; then
144                 echo "skipping .revinfo.$scm transfer"
145         else
146                 if [ -r $test_root/$tree.$scm ]; then
147                         [ -f $test_root/$tree.$scm.old ] && rm -f $test_root/$tree.$scm.old
148                         [ -f $test_root/$tree.$scm ] && mv $test_root/$tree.$scm $test_root/$tree.$scm.old
149                 fi
150                 rsync -q --timeout=200 -clz --ignore-errors \
151                         samba.org::ftp/unpacked/$tree/.revinfo.$scm $test_root/$tree.$scm
152         fi
153         if [ -r $test_root/$tree.$scm ]; then
154                 return 0;
155         fi
156         return 1
157 }
158
159 ############################
160 # choose the scm that is used for the given project
161 ############################
162
163 choose_scm() {
164         tree=$1
165
166         case "$tree" in
167                         samba* | rsync | libreplace | talloc | tdb* | ldb | pidl | ccache* | waf*)
168                         echo "git"
169                                 return 0
170                         ;;
171         esac
172
173         echo "svn"
174         return 0
175 }
176
177 locknesting=0
178
179 ############################
180 # grab a lock file. Not atomic, but close :)
181 # tries to cope with NFS
182 ############################
183
184 lock_file() {
185                 if [ -z "$lock_root" ]; then
186                         lock_root=`pwd`;
187                 fi
188
189                 lckf="$lock_root/$1"
190                 machine=`cat "$lckf" 2> /dev/null | cut -d: -f1`
191                 pid=`cat "$lckf" 2> /dev/null | cut -d: -f2`
192
193                 if [ "$pid" = "$$" ]; then
194                         locknesting=`expr $locknesting + 1`
195                         echo "lock nesting now $locknesting"
196                         return 0
197                 fi
198
199                 if test -f "$lckf"; then
200                         test x$machine = x$host || {
201                                 echo "lock file $lckf is valid for other machine $machine"
202                                 return 1
203                         }
204
205                         kill -0 $pid && {
206                                 echo "lock file $lckf is valid for process $pid"
207                                 return 1
208                         }
209
210                         echo "stale lock file $lckf for $machine:$pid"
211                         cat "$lckf"
212                         /bin/rm -f "$lckf"
213                 fi
214                 echo "$host:$$" > "$lckf"
215                 return 0
216 }
217
218 ############################
219 # unlock a lock file
220 ############################
221
222 unlock_file() {
223         if [ -z "$lock_root" ]; then
224                 lock_root=`pwd`;
225         fi
226         if [ "$locknesting" != "0" ]; then
227                 locknesting=`expr $locknesting - 1`
228                 echo "lock nesting now $locknesting"
229         else 
230                 lckf="$lock_root/$1"
231                 /bin/rm -f "$lckf"
232         fi
233 }
234
235 ############################
236 # run make, and print trace
237 ############################
238
239 do_make() {
240         # work out correct make command
241         case "$tree" in
242             waf*)
243                 MAKECOMMAND="./waf"
244                 ;;
245             *)
246                 MAKECOMMAND="$MAKE"
247                 if [ x"$MAKECOMMAND" = x ]; then
248                     MAKECOMMAND=make
249                 fi
250                 ;;
251         esac
252
253         MMTIME=$MAXTIME
254         # some trees don't need as much time
255         case "$tree" in
256                 rsync | tdb* | talloc | libreplace | ccache* | waf*)
257                         if [ "$compiler" != "checker" ]; then
258                                 MMTIME=`expr $MMTIME / 5`
259                         fi
260                 ;;
261         esac
262
263         # special build for some trees
264         case "$tree" in
265             waf*)
266                 ./waf distclean && ./waf configure build
267                 ;;
268         esac
269
270
271         for t in $*; do
272                 if [ x"$BUILD_FARM_NUM_JOBS" = x ]; then
273                         echo "$MAKECOMMAND $t"
274                         $builddir/timelimit $MMTIME "$MAKECOMMAND" "$t"
275                         status=$?
276                 else
277                         # we can parallelize everything and all targets
278                         if [ x"$t" = xeverything ] || [ x"$t" = xall]; then
279                                 echo "$MAKECOMMAND" "-j$BUILD_FARM_NUM_JOBS"  "$t"
280                                 $builddir/timelimit $MMTIME "$MAKECOMMAND" "-j$BUILD_FARM_NUM_JOBS"  "$t"
281                                 status=$?
282                         else
283                                 echo "$MAKECOMMAND $t"
284                                 $builddir/timelimit $MMTIME "$MAKECOMMAND" "$t"
285                                 status=$?
286                         fi
287                 fi
288
289                 if [ $status != 0 ]; then
290                         case "$t" in
291                                 test|check|installcheck)
292                                         ;;
293                                 *)
294                                         #run again with V=1, so we see failed commands
295                                         $builddir/timelimit $MMTIME "$MAKECOMMAND" "$t" V=1
296                                         status=$?
297                                         ;;
298                         esac
299                 fi
300
301                 if [ $status != 0 ]; then
302                         return $status;
303                 fi
304
305         done
306
307         return 0
308 }
309
310
311 ############################
312 # do the coverage report
313 ############################
314
315 action_lcovreport() {
316         if [ "$LCOV_REPORT" = "yes" ]; then
317                 case "$tree" in
318                 lorikeet-heimdal*)
319                         lcov --directory $builddir --capture --output-file $builddir/$tree.lcov.info
320                         ;;
321                 samba_3_master*)
322                         lcov --base-directory $builddir --directory $builddir/.. --capture --output-file $builddir/$tree.lcov.info
323                         ;;
324                 samba_4*|tdb*|talloc|ldb|libreplace)
325                         lcov --base-directory $builddir/bin --directory $builddir/bin --capture --output-file $builddir/$tree.lcov.info
326                         ;;
327                 waf)
328                         lcov --base-directory $builddir/demos --directory $builddir/demos --capture --output-file $builddir/$tree.lcov.info
329                         ;;
330                 *)
331                         lcov --base-directory $builddir --directory $builddir --capture --output-file $builddir/$tree.lcov.info
332                         ;;
333                 esac
334                 genhtml -o $builddir/coverage $builddir/$tree.lcov.info
335                 rc=$?
336                 echo "return code: $rc"
337         else
338                 echo "LCOV_REPORT not set and lcovreport asked"
339                 echo "Most probably an error please fix !"
340                 return 1
341         fi
342 }
343
344 action_callcatcherreport() {
345         if [ "$CALLCATCHER_REPORT" = "yes" ]; then
346                 case "$tree" in
347                 tdb*|talloc|ldb)
348                         callanalyse `find $builddir/bin -name \*.so*` $builddir/bin/* > $builddir/coverage/unused-fns.txt
349                         ;;
350                 samba_3_master|samba_4*)
351                         callanalyse `find $builddir/bin -name \*.so*` $builddir/bin/* > $builddir/coverage/all-unused-fns.txt 
352                         grep -v -f $srcdir/callcatcher-exceptions.grep $builddir/coverage/all-unused-fns.txt > $builddir/coverage/unused-fns.txt
353                         ;;
354                 esac
355                 rc=$?
356                 echo "return code: $rc"
357         else
358                 echo "CALLCATCHER_REPORT not set and callcatcher asked"
359                 echo "Most probably an error please fix !"
360                 return 1
361         fi
362 }
363
364
365 ############################
366 # configure the tree
367 ############################
368
369 action_configure() {
370         # special handling for some trees
371         case "$tree" in
372             waf*)
373                 $builddir/timelimit $MAXTIME ./waf configure
374                 cstatus=$?
375                 echo "CONFIGURE STATUS: $cstatus"
376                 return $cstatus
377                 ;;
378         esac
379
380         if [ ! -x $srcdir/configure -a -r $srcdir/Makefile.PL ]; then
381                 perl $srcdir/Makefile.PL PREFIX="$prefix"
382                 cstatus=$?
383                 echo "CONFIGURE STATUS: $cstatus"
384                 return $cstatus;
385         fi
386
387         if [ ! -x $srcdir/configure ]; then
388                 ls -l $srcdir/configure
389                 echo "$srcdir/configure is missing"
390                 cstatus=255
391                 echo "CONFIGURE STATUS: $cstatus"
392                 return $cstatus;
393         fi
394
395         echo "CFLAGS=$CFLAGS"
396         echo configure options: $config_and_prefix
397         echo CC="$CCACHE $compiler" $srcdir/configure $config_and_prefix
398
399         CC="$CCACHE $compiler"
400         export CC
401         $builddir/timelimit $MAXTIME $srcdir/configure $config_and_prefix
402         cstatus=$?
403
404         if [ x"$cstatus" != x"0" ]; then
405                 if [ -f config.log ]; then
406                         echo "contents of config.log:"
407                         cat config.log
408                 fi
409
410                 # Waf style
411                 if [ -f bin/config.log ]; then
412                         echo "contents of config.log:"
413                         cat bin/config.log
414                 fi
415         fi
416         echo "CONFIGURE STATUS: $cstatus"
417         return $cstatus
418 }
419
420 ############################
421 # show the configure log
422 ############################
423
424 action_config_log() {
425
426         log_files="config.log bin/config.log"
427         for f in $log_files; do
428                 if [ -f $f ]; then
429                         echo "contents of config.log:"
430                         cat $f
431                         return 0
432                 fi
433         done
434         return 0
435 }
436
437 ############################
438 # show the config.h
439 ############################
440
441 action_config_header() {
442         hdr_files="config.h include/config.h include/autoconf/config.h bin/default/config.h bin/default/include/config.h bin/default/source3/include/config.h"
443         for h in $hdr_files; do
444                 if [ -f $h ]; then
445                         echo "contents of $h:"
446                         cat $h
447                         return 0
448                 fi
449         done
450
451         return 0
452 }
453
454
455
456 ############################
457 # build the tree
458 ############################
459 action_build() {
460         case "$tree" in
461         samba_4*)
462                 do_make everything
463                 bstatus=$?
464                 ;;
465         samba_3*)
466                 do_make everything torture
467                 bstatus=$?
468                 ;;
469         waf*)
470                 do_make build
471                 bstatus=$?
472                 ;;
473         *)
474                 do_make all
475                 bstatus=$?
476                 ;;
477         esac
478
479         echo "BUILD STATUS: $bstatus"
480
481         return $bstatus
482 }
483
484 ############################
485 # show static analysis results
486 ############################
487
488 action_cc_checker() {
489
490         # default to passing the cc_checker
491         cccstatus=0
492
493         if [ -f ibm_checker.out ]; then
494                 cat ibm_checker.out
495                 cccstatus=`cat ibm_checker.out | grep '^\-\- ' | wc -l`
496         fi
497
498         echo "CC_CHECKER STATUS: $cccstatus"
499         return $cccstatus;      
500 }
501
502 ############################
503 # install the tree
504 ############################
505
506 action_install() {
507         if [ -d $prefix ]; then
508                 if [ "$noclean" != "yes" ]; then
509                         rm -rf $prefix
510                 fi
511         fi
512
513         do_make install
514         istatus=$?
515         echo "INSTALL STATUS: $istatus"
516         return $istatus;
517 }
518
519 ############################
520 # test the tree
521 action_test_samba() {
522         do_make test
523         totalstatus=$?
524
525         # if we produced a test summary then show it
526         [ -f st/summary ] && {
527                 echo "TEST SUMMARY"
528                 cat st/summary
529         }
530
531         return "$totalstatus"
532 }
533
534 action_test_generic() {
535         CC="$compiler"
536         export CC
537         do_make installcheck
538         totalstatus=$?
539         echo "TEST STATUS: $totalstatus"
540         return "$totalstatus"
541 }
542
543 action_test_lorikeet_heimdal() {
544         CC="$compiler"
545         export CC
546         SOCKET_WRAPPER_DIR=`pwd`/sw
547         mkdir $SOCKET_WRAPPER_DIR
548         export SOCKET_WRAPPER_DIR
549         do_make check
550         totalstatus=$?
551         SOCKET_WRAPPER_DIR=
552         export SOCKET_WRAPPER_DIR
553         echo "TEST STATUS: $totalstatus"
554         return "$totalstatus"
555 }
556
557
558 #############################
559 # attempt some basic tests of functionaility
560 # starting as basic as possible, and getting incresingly complex
561 #############################
562
563 action_test() {
564         # Samba needs crufty code of its own for backward
565         # compatiblity.  I think a better way to do this in the future
566         # is to just call 'make installcheck'.
567         case "$tree" in
568         samba*|smb-build|pidl)
569                 action_test_samba
570                 ;;
571         lorikeet-heimdal*)
572                 action_test_lorikeet_heimdal
573                 ;;
574         *)
575                 action_test_generic
576                 ;;
577         esac
578 }
579
580 ###########################
581 # do a test build of a particular tree
582 # This is the master function called by generic.fns or
583 # host.fns
584 ###########################
585
586 test_tree() {
587         tree=$1
588         source=$2
589         compiler="$3"
590         shift
591         shift
592         shift
593         echo "Starting to deal with tree $tree with compiler $compiler"
594         if [ "$compiler" = "gcc" ] && [ "$tree" != "ccache" ] && [ "$tree" != "ccache-maint" ] && ccache -V > /dev/null 2>/dev/null; then
595                 CCACHE="ccache"
596                 export CCACHE
597         else
598                 CCACHE=""
599         fi
600
601         # limit our resource usage
602         ulimit -t $MAXTIME 2> /dev/null
603
604         # max mem size 100M
605         ulimit -m $MAXMEM 2> /dev/null
606
607         # max file size 100M
608         # darn, this affects sparse files too! disable it
609         # ulimit -f 100000 2> /dev/null
610
611         # try and limit the number of open files to 250. That means we'll discover
612         # fd leaks faster
613         ulimit -n 250 2> /dev/null
614
615         # Keep stuff private
616         umask 077
617
618         if [ -z "$test_root" ]; then
619                 test_root=`pwd`
620         fi
621
622         log="build.$tree.$host.$compiler.log"
623         err="build.$tree.$host.$compiler.err"
624         sum="build.$tree.$host.$compiler.sum"
625         lck="build.$tree.lck"
626                 srcdir="$test_root/$tree/$source"
627
628         lock_file "$lck" || {
629                 return
630         }
631
632         # work out what other trees this package depends on
633         deptrees=""
634         case "$tree" in
635                 samba-gtk)
636                 deptrees="samba_4_0_test"
637                 ;;
638         esac
639
640         scm=`choose_scm "$tree"`
641
642         # pull the entries, if any
643         # Remove old .svn or .git files
644         # Move the current .svn org .git to .svn.old or
645         # .git.old then fetch the new from rsync
646         if fetch_revinfo "$tree" "$scm"; then
647                 for d in $deptrees; do
648                         # If there is dependency substree(s) we add info
649                         # from the dependency tree so that we
650                         # can rebuild in case one of them has changed
651                         dscm=`choose_scm "$d"`
652                         if [ -f "$test_root/$d.$dscm" ]; then
653                                 if [ "$d" != "$tree" ]; then
654                                         cat "$test_root/$d.$dscm" >> $test_root/$tree.$scm
655                                 fi
656                         fi
657                 done
658                 [ -f $test_root/$tree.$compiler.$scm.old ] && rm -f $test_root/$tree.$compiler.$scm.old
659                 [ -f $test_root/$tree.$compiler.$scm ] && mv $test_root/$tree.$compiler.$scm $test_root/$tree.$compiler.$scm.old
660                 [ -f $test_root/$tree.$scm ] && cp $test_root/$tree.$scm $test_root/$tree.$compiler.$scm
661
662                 if [ -f $test_root/$tree.$compiler.$scm.old ] && \
663                                 cmp $test_root/$tree.$compiler.$scm $test_root/$tree.$compiler.$scm.old > /dev/null; then
664
665                         echo "skip: $tree.$compiler nothing changed in $scm"
666                         cd $test_root
667                         send_logs_skip "$log" "$err"
668                         unlock_file "$lck"
669                         return
670                 fi
671         fi
672
673         # pull the tree
674         fetch_tree "$tree" || {
675                 cd $test_root
676                 unlock_file "$lck"
677                 return
678         }
679
680         # check for essential files
681         case "$tree" in
682             pidl)
683                 # no generated files
684                 ;;
685             waf*)
686                 if [ ! -x $srcdir/waf ]; then
687                     echo "skip: $tree.$compiler waf not present, try again next time!"
688                     cd $test_root
689                     unlock_file "$lck"
690                     return
691                 fi
692                 ;;
693             *)
694                 if [ ! -x $srcdir/configure ]; then
695                     echo "skip: $tree.$compiler configure not present, try again next time!"
696                     cd $test_root
697                     unlock_file "$lck"
698                     return
699                 fi
700                 ;;
701         esac
702
703         echo "Starting build of $tree.$compiler in process $$ at `date`"
704
705
706         # Parameters for the build depending on the tree
707         case "$tree" in
708                 *)
709                         builddir=$srcdir
710                         export builddir
711                         ;;
712         esac
713
714         #Fix the user
715         if [ ! x$USER = x"" ]; then
716                 whoami=$USER
717         else 
718                 if [ ! x$LOGNAME = x"" ]; then
719                         whoami=$LOGNAME
720                 else
721                         whoami=build
722                 fi
723         fi
724
725         # build the timelimit utility
726         echo "Building timelimit"
727         mkdir -p $builddir
728         echo $compiler $TIMELIMIT_FLAGS -o $builddir/timelimit $test_root/timelimit.c
729         $compiler $TIMELIMIT_FLAGS -o $builddir/timelimit $test_root/timelimit.c || exit 1
730
731         # build the killbysubdir utility
732         echo "Building killbysubdir"
733         echo $compiler -o $builddir/killbysubdir $test_root/killbysubdir.c
734         $compiler -o $builddir/killbysubdir $test_root/killbysubdir.c
735
736         prefix="$test_root/prefix/$tree.$compiler"
737         mkdir -p "$prefix"
738
739         # This can be defined in <host>.fns files
740         sw_config=$config
741
742         case "$tree" in
743         lorikeet-heimdal)
744                 sw_config="$config --enable-socket-wrapper"
745                 ;;
746         samba_4*)
747                 sw_config="$config --enable-socket-wrapper"
748                 sw_config="$sw_config --enable-nss-wrapper"
749                 sw_config="$sw_config --enable-uid-wrapper"
750                 ;;
751         samba_3*)
752                 sw_config="$config --enable-socket-wrapper"
753                 sw_config="$sw_config --enable-nss-wrapper"
754                 ;;
755         samba-gtk)
756                 PKG_CONFIG_PATH="$test_root/prefix/samba_4_0_test.$compiler/lib/pkgconfig"
757                 export PKG_CONFIG_PATH
758                 ;;
759         *)
760                 testsuite=testsuite
761                 ;;
762         esac
763
764         if [ "$LCOV_REPORT" = "yes" ]; then
765                 PRE_GCOV_CFLAGS=$CFLAGS
766                 PRE_GCOV_LDFLAGS=$LDFLAGS
767                 GCOV_FLAGS="--coverage"
768                 CFLAGS="$CFLAGS $GCOV_FLAGS" 
769                 LDFLAGS="$LDFLAGS $GCOV_FLAGS" 
770                 export CFLAGS LDFLAGS
771         fi
772
773         config_and_prefix="$sw_config --prefix=$prefix"
774
775         # see if we need to rebuild
776         sum_tree $test_root $tree $sum $scm
777         echo "CFLAGS=$CFLAGS $config_and_prefix" >> $sum
778
779         if [ -f "$sum.old" ] && cmp "$sum" "$sum.old" > /dev/null; then
780                 echo "skip: $tree.$compiler nothing changed"
781                 cd $test_root
782                 send_logs_skip "$log" "$err"
783                 unlock_file "$lck"
784                 echo "Ending build of $tree.$compiler in process $$ at `date`"
785                 if [ "$LCOV_REPORT" = "yes" ]; then
786                     CFLAGS=$PRE_GCOV_CFLAGS
787                     LDFLAGS=$PRE_GCOV_LDFLAGS
788                     export CFLAGS LDFLAGS
789                 fi
790                 return
791         fi
792
793         # we do need to rebuild - save the old sum
794         [ -f $sum.old ] && /bin/rm -f $sum.old
795         mv $sum $sum.old
796
797         #Action == what to do ie. configure config_log ...
798         actions="$*"
799
800         if [ "$actions" = "" ]; then
801                 actions="configure config_log config_header build install test $EXTRA_ACTIONS"
802         fi
803
804         # start the build
805         (
806         {
807                 # we all want to be able to read the output...
808                 LANG=C
809                 export LANG
810
811                 uname -a
812
813                 echo ""
814                 echo "build_test                  : $build_test_id"
815                 echo "build_test.fns      : $build_test_fns_id"
816                 echo "local settings file : $build_test_settings_local_file"
817                 echo "local functions file: $build_test_fns_local_file"
818                 echo "used .fns file      : $build_test_used_fns_file"
819                 echo ""
820
821                 # we need to be able to see if a build farm machine is accumulating
822                 # stuck processes. We do this in two ways, as we don't know what style
823                 # of ps it will have
824                 ps xfuw 2> /dev/null
825                 ps -fu $USER 2> /dev/null
826
827                 echo "building $tree with CC=$compiler on $host at "`date`
828                 echo "builddir=$builddir"
829                 echo "prefix=$prefix"
830
831                 echo "Showing limits"
832                 ulimit -a 2> /dev/null
833
834                 # the following is for non-samba builds only
835                 if [ "$scm" = "svn" -a -r $test_root/$tree.svn ]; then
836                         h_rev=`grep 'Revision: ' $test_root/$tree.svn | cut -d ':' -f2 | cut -d ' ' -f2 | sed 1q`
837                         if [ -n "$h_rev" ]; then
838                                 echo "HIGHEST SVN REVISION: $h_rev"
839                         fi
840                         rev=`grep 'Last Changed Rev: ' $test_root/$tree.svn | cut -d ':' -f2 | cut -d ' ' -f2 | sed 1q`
841                         if [ -n "$rev" ]; then
842                         echo "BUILD REVISION: $rev"
843                         fi
844                 elif [ "$scm" = "git" -a -r $test_root/$tree.git ]; then
845                         csha1=`cat $test_root/$tree.git |head -3 | tail -1`
846                         if [ -n "$csha1" ]; then
847                                 echo "BUILD COMMIT REVISION: $csha1"
848                         fi
849                         cdate=`cat $test_root/$tree.git |head -4 | tail -1`
850                         if [ -n "$cdate" ]; then
851                                 echo "BUILD COMMIT DATE: $cdate"
852                         fi
853                         ctime=`cat $test_root/$tree.git |head -2 | tail -1`
854                         if [ -n "$ctime" ]; then
855                                 echo "BUILD COMMIT TIME: $ctime"
856                   fi
857                 fi
858
859                 if [ -x $builddir/killbysubdir ]; then
860                         echo "$builddir/killbysubdir $builddir in `pwd`"
861                         $builddir/killbysubdir $builddir
862                 fi
863
864                 for action in $actions; do
865
866                         echo Running action $action
867
868                         date
869
870                         cd $builddir || exit 1
871                         export srcdir
872                         df .
873                         mount
874                         vmstat
875
876                         if [ "x$PREHOOKS" != "x" ]; then
877                                 for hooks in $PREHOOKS; do
878                                         if [ "x$hooks" = "x$action" ]; then
879                                                 ( prehook_$action )
880                                         fi
881                                 done
882                         fi
883
884                         ( action_$action )
885                         action_status=$?
886
887                         if [ "x$POSTHOOKS" != "x" ]; then
888                                 for hooks in $POSTHOOKS; do
889                                         if [ "x$hooks" = "x$action" ]; then
890                                                 ( posthook_$action )
891                                         fi
892                                 done
893                         fi
894
895                         df .
896
897                         if [ $action_status != 0 ]; then
898                                 echo "ACTION FAILED: $action";
899                                 echo " return code $action_status $action";
900                         else
901                                 echo "ACTION PASSED: $action";
902                         fi
903
904                         if [ $action_status != 0 ]; then 
905                                 break;
906                         fi
907                 done
908
909
910                 if [ "$noclean" = "yes" ]; then
911                         echo cleanup skipped!
912                 else
913                         echo cleaning up
914                         do_make clean
915                 fi
916                 date
917         } 3>&2 2>&1 1>&3 | tee "$err"
918         ) > "$log" 2>&1
919         # be aware the above channel swap may sometimes result in unordered
920         # stdout/stderr merge
921
922         if [ "$LCOV_REPORT" = "yes" ]; then
923                 chmod u=rwX,g=rX,o=rX -R $builddir/coverage
924                 rsync -rct -q --password-file=.password -z --timeout=200 \
925                         $builddir/coverage/ $host@build.samba.org::lcov_data/$host/$tree/
926                 CFLAGS=$PRE_GCOV_CFLAGS
927                 LDFLAGS=$PRE_GCOV_LDFLAGS
928                 export CFLAGS LDFLAGS
929         fi
930
931         cd $test_root
932
933         /bin/rm -rf $prefix
934         # send the logs to the master site
935         send_logs "$log" "$err"
936
937         # cleanup
938         echo "Ending build of $tree.$compiler in process $$ at `date`"
939         unlock_file "$lck"
940 }
941
942 #########################################################
943 # if you want to build only one project at a time
944 # add 'global_lock' after 'per_run_hook' and
945 # 'global_unlock' to the end of the file
946 #########################################################
947
948 global_lock() {
949         lock_file "global.lck" || {
950                 exit 0
951         }
952 }
953
954 global_unlock() {
955         unlock_file "global.lck"
956 }
957
958 delete_old_tree() {
959         otree=$1
960         test -z "$otree" && return 0;
961
962         rm -rf $otree
963         rm -rf $otree.svn
964         rm -rf $otree.*.svn
965         rm -rf $otree.git
966         rm -rf $otree.*.git
967         rm -rf build.$otree.*
968 }
969
970 # this is a special fn that allows us to add a "special" hook to the build
971 # farm that we want to do to the build farm. never leave it empty. instead,
972 # use ":" as the fn body.
973 per_run_hook() {
974         # kill old processes on systems with a known problem
975         case $host in
976         nohost)
977                 echo "just a placeholder";
978                 ;;
979         deckchair)
980                 rm -f deckchair.fns
981                 ;;
982         esac
983
984         # trim the log if too large
985         if [ "`wc -c < build.log`" -gt 2000000 ]; then
986         rm -f build.log
987         fi
988
989         old_trees="web popt distcc samba-gtk smb-build lorikeet-heimdal samba_3_2"
990         old_trees="$old_tree samba_3_2_test samba4 samba_4_0_waf samba_4_0_waf.metze"
991         old_trees="$old_tree samba_3_X_test samba_3_X_devel samba_3_X_devel samba_3_waf"
992         for d in $old_trees; do
993                 delete_old_tree $d
994         done
995 }
996
997
998 ######################################################
999 # main code that is run on each call to the build code
1000 ######################################################
1001 rsync --timeout=200 -q -az build.samba.org::build_farm/*.c .
1002
1003
1004 # build.log can grow to an excessive size, trim it beyond 50M
1005 if [ -f build.log ]; then
1006         find build.log -size +100000 -exec /bin/rm '{}' \;
1007 fi