20090310-3-scanbuild:
[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 ""
238                 return 0
239         else
240                 printf "FAILED! Log excerpt:\n"
241                 xml "  <builddir>failed</builddir>"
242                 xml "  <log>"
243                 xmlfile $build_dir/config.log
244                 xml "  </log>"
245                 xml ""
246                 tail -n $CONTEXT $build_dir/config.log
247                 return 1
248         fi
249 }
250
251 function create_buildenv
252 {
253         VENDOR=$1
254         MAINBOARD=$2
255         create_config $VENDOR $MAINBOARD
256         create_builddir $VENDOR $MAINBOARD
257 }
258
259 function compile_target
260 {       
261         VENDOR=$1
262         MAINBOARD=$2
263
264         printf "  Compiling image "
265         test "$cpus" == "" && printf "in parallel .. "
266         test "$cpus" == "1" && printf "on 1 cpu .. "
267         test 0$cpus -gt 1 && printf "on %d cpus in parallel .. " $cpus
268
269         CURR=$( pwd )
270         cd $TARGET/${VENDOR}_${MAINBOARD}
271         stime=`perl -e 'print time();'`
272         eval $MAKE $silent -j $cpus &> make.log
273         ret=$?
274         etime=`perl -e 'print time();'`
275         duration=$(( $etime - $stime ))
276         if [ $ret -eq 0 ]; then
277                 xml "  <compile>ok</compile>"
278                 xml "  <compiletime>${duration}s</compiletime>"
279                 printf "ok\n" > compile.status
280                 printf "ok. (took ${duration}s)\n"
281                 cd $CURR
282                 return 0
283         else
284                 xml "  <compile>failed</compile>"
285                 xml "  <compiletime>${duration}s</compiletime>"
286                 xml "  <log>"
287                 xmlfile make.log
288                 xml "  </log>"
289
290                 printf "FAILED after ${duration}s! Log excerpt:\n"
291                 tail -n $CONTEXT make.log
292                 cd $CURR
293                 return 1
294         fi
295 }
296
297 function built_successfully
298 {
299         CURR=`pwd`
300         status="fail"
301         if [ -d "$TARGET/${VENDOR}_${MAINBOARD}" ]; then
302                 cd $TARGET/${VENDOR}_${MAINBOARD}
303                 if [ -r compile.status ] ; then
304                         status=`cat compile.status`
305                 fi
306                 cd $CURR
307         fi
308         [ "$buildall" != "true" -a "$status" == "ok" ]
309 }
310
311 function build_broken
312 {
313         CURR=`pwd`
314         status="yes"
315         [ -r "$LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/BROKEN" ] && status="no"
316         [ "$buildbroken" == "true" -o "$status" == "yes" ]
317 }
318
319 function build_target
320 {
321         VENDOR=$1
322         MAINBOARD=$2
323         TARCH=$( architecture $VENDOR $MAINBOARD )
324
325         # default setting
326
327         CC="${CROSS_COMPILE}gcc"
328         CROSS_COMPILE=""
329         found_crosscompiler=false
330         if which $TARCH-elf-gcc 2>/dev/null >/dev/null; then
331                 # i386-elf target needs --divide, for i386-linux, that's the default
332                 CC="$TARCH-elf-gcc"
333                 if [ "$TARCH" = "i386" ]; then
334                         CC="$CC -Wa,--divide"
335                 fi
336                 echo using $CC
337                 CROSS_COMPILE="$TARCH-elf-"
338                 found_crosscompiler=true
339         fi
340
341         HOSTCC='gcc'
342
343         printf "Processing mainboard/$VENDOR/$MAINBOARD"
344
345         xml "<mainboard>"
346         xml ""
347         xml "  <vendor>$VENDOR</vendor>"
348         xml "  <device>$MAINBOARD</device>"
349         xml ""
350         xml "  <architecture>$TARCH</architecture>"
351         xml ""
352         
353         [ -r "$LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/abuild.info" ] && \
354                 source $LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/abuild.info
355         
356         if [ "$ARCH" = "$TARCH" -o $found_crosscompiler = true ]; then
357                 printf " ($TARCH: ok)\n"
358         else
359                 found_crosscompiler=false
360                 if [ "$ARCH" == amd64 -a "$TARCH" == i386 ]; then
361                         CC="gcc -m32"
362                         found_crosscompiler=true
363                 fi
364                 if [ "$ARCH" == ppc64 -a "$TARCH" == ppc ]; then
365                         CC="gcc -m32"
366                         found_crosscompiler=true
367                 fi
368                 if [ "$found_crosscompiler" == "false" -a "$TARCH" == ppc ];then
369                         for prefix in powerpc-eabi- powerpc-linux- ppc_74xx- \
370                             powerpc-7450-linux-gnu-; do
371                                 if ${prefix}gcc --version > /dev/null 2> /dev/null ; then
372                                         found_crosscompiler=true
373                                         CROSS_COMPILE=$prefix
374                                 fi
375                         done
376                 fi
377
378         
379                 # TBD: look for suitable cross compiler suite
380                 # cross-$TARCH-gcc and cross-$TARCH-ld
381                 
382                 # Check result:
383                 if [ $found_crosscompiler == "false" ]; then
384                         printf " ($TARCH: skipped, we're $ARCH)\n\n"
385                         xml "  <status>notbuilt</status>"
386                         xml ""
387                         xml "</mainboard>"
388                 
389                         return 0
390                 else
391                         printf " ($TARCH: ok, we're $ARCH)\n"
392                         xml "  <compiler>"
393                         xml "    <path>`which ${CROSS_COMPILE}gcc`</path>"
394                         xml "    <version>`${CROSS_COMPILE}gcc --version | head -1`</version>"
395                         xml "  </compiler>"
396                         xml ""
397                 fi
398         fi
399
400         if  [ "$stackprotect" = "true" ]; then
401                 CC="$CC -fno-stack-protector"
402         fi
403
404         if  [ "$scanbuild" = "true" ]; then
405                 ccwrap=`mktemp`
406                 mkdir -p $TARGET/${VENDOR}_${MAINBOARD}
407                 mkdir -p $TARGET/scan-build-results-tmp
408                 mv $ccwrap $TARGET/${VENDOR}_${MAINBOARD}
409                 ccwrap=$TARGET/${VENDOR}_${MAINBOARD}/`basename $ccwrap`
410                 echo '#!/bin/sh' > $ccwrap
411                 echo $CC' "$@"' >> $ccwrap
412                 chmod +x $ccwrap
413                 origMAKE=$MAKE
414                 MAKE="scan-build --use-cc=$ccwrap -o $TARGET/scan-build-results-tmp -analyze-headers $MAKE GCC=$ccwrap"
415                 CC="\$(CC)"
416                 HOSTCC="CCC_CC=$HOSTCC \$(CC)"
417         fi
418
419         built_successfully $VENDOR $MAINBOARD && \
420         {
421                 printf " ( mainboard/$VENDOR/$MAINBOARD previously ok )\n\n"
422                 xml "  <status>previouslyok</status>"
423                 xml ""
424                 xml "</mainboard>"
425                 return 0
426         }
427
428         build_broken $VENDOR $MAINBOARD || \
429         {
430                 printf " ( broken mainboard/$VENDOR/$MAINBOARD skipped )\n\n"
431                 xml "  <status>knownbroken</status>"
432                 xml ""
433                 xml "</mainboard>"
434                 return 0
435         }
436         
437         create_buildenv $VENDOR $MAINBOARD
438         if [ $? -eq 0  -a  $configureonly -eq 0 ]; then
439                 compile_target $VENDOR $MAINBOARD && 
440                         xml "  <status>ok</status>" ||
441                         xml "<status>broken</status>"
442                 if [ "$scanbuild" = "true" ]; then
443                         mv `dirname $TARGET/scan-build-results-tmp/*/index.html` $TARGET/${VENDOR}_${MAINBOARD}-scanbuild
444                         MAKE=$origMAKE
445                 fi
446         fi
447
448         xml ""
449         xml "</mainboard>"
450
451         printf "\n"
452 }
453
454 function test_target
455 {
456         VENDOR=$1
457         MAINBOARD=$2
458
459         if [ "$hwtest" != "true" ]; then
460                 return 0
461         fi
462
463         # image does not exist. we silently skip the patch.
464         if [ ! -r "$TARGET/${VENDOR}_${MAINBOARD}/coreboot.rom" ]; then
465                 return 0
466         fi
467
468         which curl &> /dev/null
469         if [ $? != 0 ]; then
470                 printf "curl is not installed but required for test submission.  skipping test.\n\n"
471                 return 0
472         fi
473
474         CURR=`pwd`
475         if [ -r "$TARGET/${VENDOR}_${MAINBOARD}/tested" ]; then
476                 printf "Testing image for board $VENDOR $MAINBOARD skipped (previously submitted).\n\n"
477                 return 0
478         fi
479         # touch $TARGET/${VENDOR}_${MAINBOARD}/tested
480
481         printf "Submitting image for board $VENDOR $MAINBOARD to test system...\n"
482
483         curl -f -F "romfile=@$TARGET/${VENDOR}_${MAINBOARD}/coreboot.rom" \
484                 -F "mode=abuild" -F "mainboard=${VENDOR}_${MAINBOARD}" -F "submit=Upload" \
485                 "http://qa.coreboot.org/deployment/send.php"
486
487         printf "\n"
488         return 0
489 }
490
491 function myhelp
492 {
493         printf "Usage: $0 [-v] [-a] [-b] [-t <vendor/board>] [-p <dir>] [lbroot]\n"
494         printf "       $0 [-V|--version]\n"
495         printf "       $0 [-h|--help]\n\n"
496
497         printf "Options:\n"
498         printf "    [-v|--verbose]                print more messages\n"
499         printf "    [-a|--all]                    build previously succeeded ports as well\n"
500         printf "    [-b|--broken]                 attempt to build ports that are known broken\n"
501         printf "    [-t|--target <vendor/board>]  attempt to build target vendor/board only\n"
502         printf "    [-p|--payloads <dir>]         use payloads in <dir> to build images\n"
503         printf "    [-V|--version]                print version number and exit\n"
504         printf "    [-h|--help]                   print this help and exit\n"
505         printf "    [-x|--xml]                    write xml log file \n"
506         printf "                                  (defaults to $XMLFILE)\n"
507         printf "    [-T|--test]                   submit image(s) to automated test system\n"
508         printf "    [-c|--cpus <numcpus>]         build on <numcpus> at the same time\n"
509         printf "    [-s|--silent]                 omit compiler calls in logs\n"
510         printf "    [-ns|--nostackprotect]        use gcc -fno-stack-protector option\n"
511         printf "    [-sb|--scan-build]            use clang's static analyzer\n"
512         printf "    [-C|--config]                 configure-only mode\n"
513         printf "    [lbroot]                      absolute path to coreboot sources\n"
514         printf "                                  (defaults to $LBROOT)\n\n"
515 }
516
517 function myversion 
518 {
519         cat << EOF
520
521 coreboot autobuild v$ABUILD_VERSION ($ABUILD_DATE)
522
523 Copyright (C) 2004 by Stefan Reinauer <stepan@openbios.org>
524 Copyright (C) 2006-2008 by coresystems GmbH <info@coresystems.de>
525
526 This program is free software; you may redistribute it under the terms
527 of the GNU General Public License. This program has absolutely no
528 warranty.
529
530 EOF
531 }
532
533 # default options
534 target=""
535 buildall=false
536 LBROOT=$( cd ../..; pwd )
537 verbose=false
538
539 # parse parameters.. try to find out whether we're running GNU getopt
540 getoptbrand="`getopt -V`"
541 if [ "${getoptbrand:0:6}" == "getopt" ]; then
542         # Detected GNU getopt that supports long options.
543         args=`getopt -l version,verbose,help,all,target:,broken,payloads:,test,cpus:,silent,xml,config Vvhat:bp:Tc:sxC -- "$@"`
544         eval set "$args"
545 else
546         # Detected non-GNU getopt
547         args=`getopt Vvhat:bp:Tc:sxC $*`
548         set -- $args
549 fi
550
551 if [ $? != 0 ]; then
552         myhelp
553         exit 1
554 fi
555
556 while true ; do
557         case "$1" in
558                 -x|--xml)       shift; mode=xml; rm -f $XMLFILE ;;
559                 -t|--target)    shift; target="$1"; shift;;
560                 -a|--all)       shift; buildall=true;;
561                 -b|--broken)    shift; buildbroken=true;;
562                 -v|--verbose)   shift; verbose=true;;
563                 -V|--version)   shift; myversion; exit 0;;
564                 -h|--help)      shift; myversion; myhelp; exit 0;;
565                 -p|--payloads)  shift; payloads="$1"; shift;;
566                 -T|--test)      shift; hwtest=true;;
567                 -c|--cpus)      shift; cpus="$1"; test "$cpus" == "max" && cpus=""; shift;;
568                 -s|--silent)    shift; silent="-s";;
569                 -ns|--nostackprotect) shift; stackprotect=true;;
570                 -sb|--scan-build) shift; scanbuild=true;;
571                 -C|--config)    shift; configureonly=1;;
572                 --)             shift; break;;
573                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
574                 *)              break;;
575         esac
576 done
577
578 # /path/to/freebios2/
579 test -z "$1" || LBROOT=$1
580
581 debug "LBROOT=$LBROOT"
582
583 xml '<?xml version="1.0" encoding="utf-8"?>'
584 xml '<abuild>'
585
586 if [ "$target" != "" ]; then
587         # build a single board
588         VENDOR=`printf $target|cut -f1 -d/`
589         MAINBOARD=`printf $target|cut -f2 -d/`
590         build_target $VENDOR $MAINBOARD
591         test_target $VENDOR $MAINBOARD
592 else
593         # build all boards per default
594         for VENDOR in $( vendors ); do
595                 for MAINBOARD in $( mainboards $VENDOR ); do
596                         build_target $VENDOR $MAINBOARD
597                         test_target $VENDOR $MAINBOARD
598                 done
599         done
600 fi
601 xml '</abuild>'
602