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