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