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