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