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