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