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