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