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