this commit should fix Ticket #122 (proper log files for all builds)
[coreboot.git] / util / abuild / abuild
1 #!/bin/bash
2 #
3 #  coreboot autobuild
4 #
5 #  This script builds coreboot images for all available targets.
6 #
7 #  (C) 2004 by Stefan Reinauer <stepan@openbios.org>
8 #  (C) 2006-2008 by coresystems GmbH <info@coresystems.de>
9 #
10 #  This file is subject to the terms and conditions of the GNU General
11 #  Public License. See the file COPYING in the main directory of this
12 #  archive for more details.
13 #     
14
15 #set -x # Turn echo on....
16
17 ABUILD_DATE="May 27th, 2008"
18 ABUILD_VERSION="0.7"
19
20 # Where shall we place all the build trees?
21 TARGET=$( pwd )/coreboot-builds
22 XMLFILE=$( pwd )/abuild.xml
23
24 # path to payload. Should be more generic
25 PAYLOAD=/dev/null
26
27 # Lines of error context to be printed in FAILURE case
28 CONTEXT=5
29
30 TESTSUBMISSION="http://qa.coreboot.org/deployment/send.php"
31
32 # Number of CPUs to compile on per default
33 cpus=1
34
35 # Configure-only mode
36 configureonly=0
37
38 # One might want to adjust these in case of cross compiling
39 MAKE="make"
40 PYTHON=python
41
42 # this can be changed to xml by -x
43 mode=text
44
45 # silent mode.. no compiler calls, only warnings in the log files.
46 # this is disabled per default but can be enabled with -s
47 silent=
48
49 # clang mode enabled by -sb option.
50 scanbuild=false
51
52 # stackprotect mode enabled by -ns option.
53 stackprotect=false
54
55 ARCH=`uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \
56         -e s/i86pc/i386/ \
57         -e s/arm.*/arm/ -e s/sa110/arm/ -e s/x86_64/amd64/ \
58         -e "s/Power Macintosh/ppc/"`
59
60 trap interrupt INT
61
62 function interrupt
63 {
64         printf "\n$0: execution interrupted manually.\n"
65         if [ "$mode" == "xml" ]; then
66                 printf "$0: deleting incomplete xml output file.\n"
67         fi
68         exit 1
69 }
70
71 function debug
72 {
73         test "$verbose" == "true" && printf "$*\n"
74 }
75
76 function xml
77 {
78         test "$mode" == "xml" && printf "$*\n" >> $XMLFILE
79 }
80
81 function xmlfile
82 {
83         test "$mode" == "xml" && { 
84                 printf '<![CDATA[\n'
85                 cat $1
86                 printf ']]>\n' 
87         } >> $XMLFILE
88 }
89
90
91
92 function vendors
93 {
94         # make this a function so we can easily select
95         # without breaking readability
96         ls -1 "$LBROOT/src/mainboard" | grep -v CVS
97 }
98
99 function mainboards
100 {
101         # make this a function so we can easily select
102         # without breaking readability
103         
104         VENDOR=$1
105         
106         ls -1 $LBROOT/src/mainboard/$VENDOR | grep -v CVS 
107 }
108
109 function architecture
110 {
111         VENDOR=$1
112         MAINBOARD=$2
113         cat $LBROOT/src/mainboard/$VENDOR/$MAINBOARD/Config.lb | \
114                 grep ^arch | cut -f 2 -d\ 
115 }
116
117 function create_config
118 {
119         VENDOR=$1
120         MAINBOARD=$2
121         TARCH=$( architecture $VENDOR $MAINBOARD )
122         TARGCONFIG=$LBROOT/targets/$VENDOR/$MAINBOARD/Config-abuild.lb
123
124         # get a working payload for the board if we have one.
125         # the --payload option expects a directory containing 
126         # an executable shell script payload.sh
127         #   Usage: payload.sh [VENDOR] [DEVICE]
128         # the script returns an absolute path to the payload binary.
129
130         if [ -x $payloads/payload.sh ]; then
131                 PAYLOAD=`$payloads/payload.sh $VENDOR $MAINBOARD`
132                 printf "Using payload $PAYLOAD\n"
133         fi
134         
135         mkdir -p $TARGET
136
137         if [ -f $TARGCONFIG ]; then
138                 cp $TARGCONFIG $TARGET/Config-${VENDOR}_${MAINBOARD}.lb
139                 printf "Using existing test target $TARGCONFIG"
140                 xml "  <config>$TARGCONFIG</config>"
141         else
142
143                 printf "  Creating config file..."
144                 xml "  <config>autogenerated</config>"
145                 ( cat << EOF
146 # This will make a target directory of ./VENDOR_MAINBOARD
147
148 target VENDOR_MAINBOARD
149 mainboard VENDOR/MAINBOARD
150
151 option CC="CROSSCC"
152 option CROSS_COMPILE="CROSS_PREFIX"
153 option HOSTCC="CROSS_HOSTCC"
154
155 __COMPRESSION__
156
157 EOF
158                 if [ "$TARCH" == i386 ] ; then
159                         cat <<EOF
160 romimage "normal"
161         option USE_FALLBACK_IMAGE=0
162         option ROM_IMAGE_SIZE=0x17000
163         option COREBOOT_EXTRA_VERSION=".0-normal"
164         payload __PAYLOAD__
165 end
166
167 romimage "fallback" 
168         option USE_FALLBACK_IMAGE=1
169         option ROM_IMAGE_SIZE=0x17000
170         option COREBOOT_EXTRA_VERSION=".0-fallback"
171         payload __PAYLOAD__
172 end
173 buildrom ./coreboot.rom ROM_SIZE "normal" "fallback"
174 EOF
175                 else
176                         cat <<EOF
177 romimage "only"
178         option COREBOOT_EXTRA_VERSION=".0"
179         payload __PAYLOAD__
180 end
181 buildrom ./coreboot.rom ROM_SIZE "only"
182 EOF
183                 fi 
184                 ) > $TARGET/Config-${VENDOR}_${MAINBOARD}.lb
185         fi
186
187         if [ "`which lzma`" != "" -a "$PAYLOAD" != /dev/null ]; then
188                 COMPRESSION="option CONFIG_COMPRESSED_PAYLOAD_LZMA=1"
189         else
190                 COMPRESSION="# no compression"
191         fi
192
193         cp $TARGET/Config-${VENDOR}_${MAINBOARD}.lb $TARGET/Config-${VENDOR}_${MAINBOARD}.lb.pre
194         sed -e s:VENDOR:$VENDOR:g \
195                 -e s:MAINBOARD:$MAINBOARD:g \
196                 -e s:payload\ __PAYLOAD__:payload\ $PAYLOAD:g \
197                 -e s:CROSSCC:"$CC":g \
198                 -e s:CROSS_PREFIX:"$CROSS_COMPILE":g \
199                 -e s:CROSS_HOSTCC:"$HOSTCC":g \
200                 -e s:__COMPRESSION__:"$COMPRESSION":g \
201                 $TARGET/Config-${VENDOR}_${MAINBOARD}.lb.pre > $TARGET/Config-${VENDOR}_${MAINBOARD}.lb
202         printf " ok\n"
203 }
204
205 function create_builddir
206 {       
207         VENDOR=$1
208         MAINBOARD=$2
209         
210         printf "  Creating builddir..."
211
212         target_dir=$TARGET
213         config_dir=$LBROOT/util/newconfig
214         yapps2_py=$config_dir/yapps2.py
215         config_g=$config_dir/config.g
216         config_lb=Config-${VENDOR}_${MAINBOARD}.lb
217
218         cd $target_dir
219
220         build_dir=${VENDOR}_${MAINBOARD}
221         config_py=$build_dir/config.py
222
223         if [ ! -d $build_dir ] ; then
224                 mkdir -p $build_dir
225         fi
226         if [ ! -f $config_py ]; then
227                 $PYTHON $yapps2_py $config_g $config_py &> $build_dir/py.log
228         fi
229
230         # make sure config.py is up-to-date
231
232         export PYTHONPATH=$config_dir
233         $PYTHON $config_py $config_lb $LBROOT &> $build_dir/config.log
234         if [ $? -eq 0 ]; then
235                 printf "ok\n"
236                 xml "  <builddir>ok</builddir>"
237                 xml "  <log>"
238                 xmlfile $build_dir/config.log
239                 xml "  </log>"
240                 xml ""
241                 return 0
242         else
243                 printf "FAILED! Log excerpt:\n"
244                 xml "  <builddir>failed</builddir>"
245                 xml "  <log>"
246                 xmlfile $build_dir/config.log
247                 xml "  </log>"
248                 xml ""
249                 tail -n $CONTEXT $build_dir/config.log
250                 return 1
251         fi
252 }
253
254 function create_buildenv
255 {
256         VENDOR=$1
257         MAINBOARD=$2
258         create_config $VENDOR $MAINBOARD
259         create_builddir $VENDOR $MAINBOARD
260 }
261
262 function compile_target
263 {       
264         VENDOR=$1
265         MAINBOARD=$2
266
267         printf "  Compiling image "
268         test "$cpus" == "" && printf "in parallel .. "
269         test "$cpus" == "1" && printf "on 1 cpu .. "
270         test 0$cpus -gt 1 && printf "on %d cpus in parallel .. " $cpus
271
272         CURR=$( pwd )
273         cd $TARGET/${VENDOR}_${MAINBOARD}
274         stime=`perl -e 'print time();'`
275         eval $MAKE $silent -j $cpus &> make.log
276         ret=$?
277         etime=`perl -e 'print time();'`
278         duration=$(( $etime - $stime ))
279         if [ $ret -eq 0 ]; then
280                 xml "  <compile>ok</compile>"
281                 xml "  <compiletime>${duration}s</compiletime>"
282                 xml "  <log>"
283                 xmlfile make.log
284                 xml "  </log>"
285                 printf "ok\n" > compile.status
286                 printf "ok. (took ${duration}s)\n"
287                 cd $CURR
288                 return 0
289         else
290                 xml "  <compile>failed</compile>"
291                 xml "  <compiletime>${duration}s</compiletime>"
292                 xml "  <log>"
293                 xmlfile make.log
294                 xml "  </log>"
295
296                 printf "FAILED after ${duration}s! Log excerpt:\n"
297                 tail -n $CONTEXT make.log
298                 cd $CURR
299                 return 1
300         fi
301 }
302
303 function built_successfully
304 {
305         CURR=`pwd`
306         status="fail"
307         if [ -d "$TARGET/${VENDOR}_${MAINBOARD}" ]; then
308                 cd $TARGET/${VENDOR}_${MAINBOARD}
309                 if [ -r compile.status ] ; then
310                         status=`cat compile.status`
311                 fi
312                 cd $CURR
313         fi
314         [ "$buildall" != "true" -a "$status" == "ok" ]
315 }
316
317 function build_broken
318 {
319         CURR=`pwd`
320         status="yes"
321         [ -r "$LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/BROKEN" ] && status="no"
322         [ "$buildbroken" == "true" -o "$status" == "yes" ]
323 }
324
325 function build_target
326 {
327         VENDOR=$1
328         MAINBOARD=$2
329         TARCH=$( architecture $VENDOR $MAINBOARD )
330
331         # default setting
332
333         CC="${CROSS_COMPILE}gcc"
334         CROSS_COMPILE=""
335         found_crosscompiler=false
336         if which $TARCH-elf-gcc 2>/dev/null >/dev/null; then
337                 # i386-elf target needs --divide, for i386-linux, that's the default
338                 CC="$TARCH-elf-gcc"
339                 if [ "$TARCH" = "i386" ]; then
340                         CC="$CC -Wa,--divide"
341                 fi
342                 echo using $CC
343                 CROSS_COMPILE="$TARCH-elf-"
344                 found_crosscompiler=true
345         fi
346
347         HOSTCC='gcc'
348
349         printf "Processing mainboard/$VENDOR/$MAINBOARD"
350
351         xml "<mainboard>"
352         xml ""
353         xml "  <vendor>$VENDOR</vendor>"
354         xml "  <device>$MAINBOARD</device>"
355         xml ""
356         xml "  <architecture>$TARCH</architecture>"
357         xml ""
358         
359         [ -r "$LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/abuild.info" ] && \
360                 source $LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/abuild.info
361         
362         if [ "$ARCH" = "$TARCH" -o $found_crosscompiler = true ]; then
363                 printf " ($TARCH: ok)\n"
364         else
365                 found_crosscompiler=false
366                 if [ "$ARCH" == amd64 -a "$TARCH" == i386 ]; then
367                         CC="gcc -m32"
368                         found_crosscompiler=true
369                 fi
370                 if [ "$ARCH" == ppc64 -a "$TARCH" == ppc ]; then
371                         CC="gcc -m32"
372                         found_crosscompiler=true
373                 fi
374                 if [ "$found_crosscompiler" == "false" -a "$TARCH" == ppc ];then
375                         for prefix in powerpc-eabi- powerpc-linux- ppc_74xx- \
376                             powerpc-7450-linux-gnu-; do
377                                 if ${prefix}gcc --version > /dev/null 2> /dev/null ; then
378                                         found_crosscompiler=true
379                                         CROSS_COMPILE=$prefix
380                                 fi
381                         done
382                 fi
383
384         
385                 # TBD: look for suitable cross compiler suite
386                 # cross-$TARCH-gcc and cross-$TARCH-ld
387                 
388                 # Check result:
389                 if [ $found_crosscompiler == "false" ]; then
390                         printf " ($TARCH: skipped, we're $ARCH)\n\n"
391                         xml "  <status>notbuilt</status>"
392                         xml ""
393                         xml "</mainboard>"
394                 
395                         return 0
396                 else
397                         printf " ($TARCH: ok, we're $ARCH)\n"
398                         xml "  <compiler>"
399                         xml "    <path>`which ${CROSS_COMPILE}gcc`</path>"
400                         xml "    <version>`${CROSS_COMPILE}gcc --version | head -1`</version>"
401                         xml "  </compiler>"
402                         xml ""
403                 fi
404         fi
405
406         if  [ "$stackprotect" = "true" ]; then
407                 CC="$CC -fno-stack-protector"
408         fi
409
410         if  [ "$scanbuild" = "true" ]; then
411                 ccwrap=`mktemp`
412                 mkdir -p $TARGET/${VENDOR}_${MAINBOARD}
413                 mkdir -p $TARGET/scan-build-results-tmp
414                 mv $ccwrap $TARGET/${VENDOR}_${MAINBOARD}
415                 ccwrap=$TARGET/${VENDOR}_${MAINBOARD}/`basename $ccwrap`
416                 echo '#!/bin/sh' > $ccwrap
417                 echo $CC' "$@"' >> $ccwrap
418                 chmod +x $ccwrap
419                 origMAKE=$MAKE
420                 MAKE="scan-build --use-cc=$ccwrap -o $TARGET/scan-build-results-tmp -analyze-headers $MAKE GCC=$ccwrap"
421                 CC="\$(CC)"
422                 HOSTCC="CCC_CC=$HOSTCC \$(CC)"
423         fi
424
425         built_successfully $VENDOR $MAINBOARD && \
426         {
427                 printf " ( mainboard/$VENDOR/$MAINBOARD previously ok )\n\n"
428                 xml "  <status>previouslyok</status>"
429                 xml ""
430                 xml "</mainboard>"
431                 return 0
432         }
433
434         build_broken $VENDOR $MAINBOARD || \
435         {
436                 printf " ( broken mainboard/$VENDOR/$MAINBOARD skipped )\n\n"
437                 xml "  <status>knownbroken</status>"
438                 xml ""
439                 xml "</mainboard>"
440                 return 0
441         }
442         
443         create_buildenv $VENDOR $MAINBOARD
444         if [ $? -eq 0  -a  $configureonly -eq 0 ]; then
445                 compile_target $VENDOR $MAINBOARD && 
446                         xml "  <status>ok</status>" ||
447                         xml "<status>broken</status>"
448                 if [ "$scanbuild" = "true" ]; then
449                         mv `dirname $TARGET/scan-build-results-tmp/*/index.html` $TARGET/${VENDOR}_${MAINBOARD}-scanbuild
450                         MAKE=$origMAKE
451                 fi
452         fi
453
454         xml ""
455         xml "</mainboard>"
456
457         printf "\n"
458 }
459
460 function test_target
461 {
462         VENDOR=$1
463         MAINBOARD=$2
464
465         if [ "$hwtest" != "true" ]; then
466                 return 0
467         fi
468
469         # image does not exist. we silently skip the patch.
470         if [ ! -r "$TARGET/${VENDOR}_${MAINBOARD}/coreboot.rom" ]; then
471                 return 0
472         fi
473
474         which curl &> /dev/null
475         if [ $? != 0 ]; then
476                 printf "curl is not installed but required for test submission.  skipping test.\n\n"
477                 return 0
478         fi
479
480         CURR=`pwd`
481         if [ -r "$TARGET/${VENDOR}_${MAINBOARD}/tested" ]; then
482                 printf "Testing image for board $VENDOR $MAINBOARD skipped (previously submitted).\n\n"
483                 return 0
484         fi
485         # touch $TARGET/${VENDOR}_${MAINBOARD}/tested
486
487         printf "Submitting image for board $VENDOR $MAINBOARD to test system...\n"
488
489         curl -f -F "romfile=@$TARGET/${VENDOR}_${MAINBOARD}/coreboot.rom" \
490                 -F "mode=abuild" -F "mainboard=${VENDOR}_${MAINBOARD}" -F "submit=Upload" \
491                 "http://qa.coreboot.org/deployment/send.php"
492
493         printf "\n"
494         return 0
495 }
496
497 function myhelp
498 {
499         printf "Usage: $0 [-v] [-a] [-b] [-t <vendor/board>] [-p <dir>] [lbroot]\n"
500         printf "       $0 [-V|--version]\n"
501         printf "       $0 [-h|--help]\n\n"
502
503         printf "Options:\n"
504         printf "    [-v|--verbose]                print more messages\n"
505         printf "    [-a|--all]                    build previously succeeded ports as well\n"
506         printf "    [-b|--broken]                 attempt to build ports that are known broken\n"
507         printf "    [-t|--target <vendor/board>]  attempt to build target vendor/board only\n"
508         printf "    [-p|--payloads <dir>]         use payloads in <dir> to build images\n"
509         printf "    [-V|--version]                print version number and exit\n"
510         printf "    [-h|--help]                   print this help and exit\n"
511         printf "    [-x|--xml]                    write xml log file \n"
512         printf "                                  (defaults to $XMLFILE)\n"
513         printf "    [-T|--test]                   submit image(s) to automated test system\n"
514         printf "    [-c|--cpus <numcpus>]         build on <numcpus> at the same time\n"
515         printf "    [-s|--silent]                 omit compiler calls in logs\n"
516         printf "    [-ns|--nostackprotect]        use gcc -fno-stack-protector option\n"
517         printf "    [-sb|--scan-build]            use clang's static analyzer\n"
518         printf "    [-C|--config]                 configure-only mode\n"
519         printf "    [lbroot]                      absolute path to coreboot sources\n"
520         printf "                                  (defaults to $LBROOT)\n\n"
521 }
522
523 function myversion 
524 {
525         cat << EOF
526
527 coreboot autobuild v$ABUILD_VERSION ($ABUILD_DATE)
528
529 Copyright (C) 2004 by Stefan Reinauer <stepan@openbios.org>
530 Copyright (C) 2006-2008 by coresystems GmbH <info@coresystems.de>
531
532 This program is free software; you may redistribute it under the terms
533 of the GNU General Public License. This program has absolutely no
534 warranty.
535
536 EOF
537 }
538
539 # default options
540 target=""
541 buildall=false
542 LBROOT=$( cd ../..; pwd )
543 verbose=false
544
545 # parse parameters.. try to find out whether we're running GNU getopt
546 getoptbrand="`getopt -V`"
547 if [ "${getoptbrand:0:6}" == "getopt" ]; then
548         # Detected GNU getopt that supports long options.
549         args=`getopt -l version,verbose,help,all,target:,broken,payloads:,test,cpus:,silent,xml,config Vvhat:bp:Tc:sxC -- "$@"`
550         eval set "$args"
551 else
552         # Detected non-GNU getopt
553         args=`getopt Vvhat:bp:Tc:sxC $*`
554         set -- $args
555 fi
556
557 if [ $? != 0 ]; then
558         myhelp
559         exit 1
560 fi
561
562 while true ; do
563         case "$1" in
564                 -x|--xml)       shift; mode=xml; rm -f $XMLFILE ;;
565                 -t|--target)    shift; target="$1"; shift;;
566                 -a|--all)       shift; buildall=true;;
567                 -b|--broken)    shift; buildbroken=true;;
568                 -v|--verbose)   shift; verbose=true;;
569                 -V|--version)   shift; myversion; exit 0;;
570                 -h|--help)      shift; myversion; myhelp; exit 0;;
571                 -p|--payloads)  shift; payloads="$1"; shift;;
572                 -T|--test)      shift; hwtest=true;;
573                 -c|--cpus)      shift; cpus="$1"; test "$cpus" == "max" && cpus=""; shift;;
574                 -s|--silent)    shift; silent="-s";;
575                 -ns|--nostackprotect) shift; stackprotect=true;;
576                 -sb|--scan-build) shift; scanbuild=true;;
577                 -C|--config)    shift; configureonly=1;;
578                 --)             shift; break;;
579                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
580                 *)              break;;
581         esac
582 done
583
584 # /path/to/freebios2/
585 test -z "$1" || LBROOT=$1
586
587 debug "LBROOT=$LBROOT"
588
589 xml '<?xml version="1.0" encoding="utf-8"?>'
590 xml '<abuild>'
591
592 if [ "$target" != "" ]; then
593         # build a single board
594         VENDOR=`printf $target|cut -f1 -d/`
595         MAINBOARD=`printf $target|cut -f2 -d/`
596         build_target $VENDOR $MAINBOARD
597         test_target $VENDOR $MAINBOARD
598 else
599         # build all boards per default
600         for VENDOR in $( vendors ); do
601                 for MAINBOARD in $( mainboards $VENDOR ); do
602                         build_target $VENDOR $MAINBOARD
603                         test_target $VENDOR $MAINBOARD
604                 done
605         done
606 fi
607 xml '</abuild>'
608