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