Fix bug in 'sed' invocation in abuild, which causes build errors in
[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
109         if [ -x $payloads/payload.sh ]; then
110                 PAYLOAD=`$payloads/payload.sh $VENDOR $MAINBOARD`
111                 printf "Using custom payload $PAYLOAD\n"
112         fi
113         
114         mkdir -p $TARGET
115
116         if [ -f $TARGCONFIG ]; then
117                 cp $TARGCONFIG $TARGET/Config-${VENDOR}_${MAINBOARD}.lb
118                 printf "Using existing test target $TARGCONFIG"
119                 xml "  <config>$TARGCONFIG</config>"
120         else
121
122                 printf "  Creating config file..."
123                 xml "  <config>autogenerated</config>"
124                 ( cat << EOF
125 # This will make a target directory of ./VENDOR_MAINBOARD
126
127 target VENDOR_MAINBOARD
128 mainboard VENDOR/MAINBOARD
129
130 option CC="CROSSCC"
131 option CROSS_COMPILE="CROSS_PREFIX"
132 option HOSTCC="CROSS_HOSTCC"
133
134 EOF
135                 if [ "$TARCH" == i386 ] ; then
136                         cat <<EOF
137 romimage "normal"
138         option USE_FALLBACK_IMAGE=0
139         option ROM_IMAGE_SIZE=0x20000
140         option LINUXBIOS_EXTRA_VERSION=".0-normal"
141         payload PAYLOAD
142 end
143
144 romimage "fallback" 
145         option USE_FALLBACK_IMAGE=1
146         option ROM_IMAGE_SIZE=0x20000
147         option LINUXBIOS_EXTRA_VERSION=".0-fallback"
148         payload PAYLOAD
149 end
150 buildrom ./linuxbios.rom ROM_SIZE "normal" "fallback"
151 EOF
152                 else
153                         cat <<EOF
154 romimage "only"
155         option LINUXBIOS_EXTRA_VERSION=".0"
156         payload PAYLOAD
157 end
158 buildrom ./linuxbios.rom ROM_SIZE "only"
159 EOF
160                 fi 
161                 ) > $TARGET/Config-${VENDOR}_${MAINBOARD}.lb
162         fi
163
164         sed -i.pre -e s,VENDOR,$VENDOR,g \
165                 -e s,MAINBOARD,$MAINBOARD,g \
166                 -e s,payload\ PAYLOAD,payload\ $PAYLOAD,g \
167                 -e s,CROSSCC,"$CC",g \
168                 -e s,CROSS_PREFIX,"$CROSS_COMPILE",g \
169                 -e s,CROSS_HOSTCC,"$HOSTCC",g \
170                 $TARGET/Config-${VENDOR}_${MAINBOARD}.lb
171                 
172         printf " ok\n"
173 }
174
175 function create_builddir
176 {       
177         VENDOR=$1
178         MAINBOARD=$2
179         
180         printf "  Creating builddir..."
181
182         target_dir=$TARGET
183         config_dir=$LBROOT/util/newconfig
184         yapps2_py=$config_dir/yapps2.py
185         config_g=$config_dir/config.g
186         config_lb=Config-${VENDOR}_${MAINBOARD}.lb
187
188         cd $target_dir
189
190         build_dir=${VENDOR}_${MAINBOARD}
191         config_py=$build_dir/config.py
192
193         if [ ! -d $build_dir ] ; then
194                 mkdir -p $build_dir
195         fi
196         if [ ! -f $config_py ]; then
197                 $PYTHON $yapps2_py $config_g $config_py &> $build_dir/py.log
198         fi
199
200         # make sure config.py is up-to-date
201
202         export PYTHONPATH=$config_dir
203         $PYTHON $config_py $config_lb $LBROOT &> $build_dir/config.log
204         if [ $? -eq 0 ]; then
205                 printf "ok\n"
206                 xml "  <builddir>ok</builddir>"
207                 xml ""
208                 return 0
209         else
210                 printf "FAILED! Log excerpt:\n"
211                 xml "  <builddir>failed</builddir>"
212                 xml "  <log>"
213                 xmlfile $build_dir/config.log
214                 xml "  </log>"
215                 xml ""
216                 tail -n $CONTEXT $build_dir/config.log
217                 return 1
218         fi
219 }
220
221 function create_buildenv
222 {
223         VENDOR=$1
224         MAINBOARD=$2
225         create_config $VENDOR $MAINBOARD
226         create_builddir $VENDOR $MAINBOARD
227 }
228
229 function compile_target
230 {       
231         VENDOR=$1
232         MAINBOARD=$2
233
234         printf "  Compiling image .."
235         CURR=$( pwd )
236         cd $TARGET/${VENDOR}_${MAINBOARD}
237         stime=`date +%s`
238         eval $MAKE &> make.log
239         ret=$?
240         etime=`date +%s`
241         duration=$(( $etime - $stime ))
242         if [ $ret -eq 0 ]; then
243                 xml "  <compile>ok</compile>"
244                 xml "  <compiletime>${duration}s</compiletime>"
245                 printf "ok\n" > compile.status
246                 printf "ok. (took ${duration}s)\n"
247                 cd $CURR
248                 return 0
249         else
250                 xml "  <compile>failed</compile>"
251                 xml "  <compiletime>${duration}s</compiletime>"
252                 xml "  <log>"
253                 xmlfile make.log
254                 xml "  </log>"
255
256                 printf "FAILED after ${duration}s! Log excerpt:\n"
257                 tail -n $CONTEXT make.log
258                 cd $CURR
259                 return 1
260         fi
261 }
262
263 function built_successfully
264 {
265         CURR=`pwd`
266         status="fail"
267         if [ -d "$TARGET/${VENDOR}_${MAINBOARD}" ]; then
268                 cd $TARGET/${VENDOR}_${MAINBOARD}
269                 if [ -r compile.status ] ; then
270                         status=`cat compile.status`
271                 fi
272                 cd $CURR
273         fi
274         [ "$buildall" != "true" -a "$status" == "ok" ]
275 }
276
277 function build_broken
278 {
279         CURR=`pwd`
280         status="yes"
281         [ -r "$LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/BROKEN" ] && status="no"
282         [ "$buildbroken" == "true" -o "$status" == "yes" ]
283 }
284
285 function build_target
286 {
287         VENDOR=$1
288         MAINBOARD=$2
289         TARCH=$( architecture $VENDOR $MAINBOARD )
290
291         # default setting
292         CC='$(CROSS_COMPILE)gcc'
293         HOSTCC='gcc'
294         CROSS_COMPILE=''
295
296         printf "Processing mainboard/$VENDOR/$MAINBOARD"
297
298         xml "<mainboard>"
299         xml ""
300         xml "  <vendor>$VENDOR</vendor>"
301         xml "  <device>$MAINBOARD</device>"
302         xml ""
303         xml "  <architecture>$TARCH</architecture>"
304         xml ""
305         
306         [ -r "$LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/abuild.info" ] && \
307                 source $LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/abuild.info
308         
309         if [ "$ARCH" == "$TARCH" ]; then
310                 printf " ($TARCH: ok)\n"
311         else
312                 found_crosscompiler=false
313                 if [ "$ARCH" == amd64 -a "$TARCH" == i386 ]; then
314                         CC="gcc -m32"
315                         found_crosscompiler=true
316                 fi
317                 if [ "$ARCH" == ppc64 -a "$TARCH" == ppc ]; then
318                         CC="gcc -m32"
319                         found_crosscompiler=true
320                 fi
321                 if [ "$found_crosscompiler" == "false" -a "$TARCH" == ppc ];then
322                         for prefix in powerpc-eabi- powerpc-linux- ppc_74xx- \
323                             powerpc-7450-linux-gnu-; do
324                                 if ${prefix}gcc --version > /dev/null 2> /dev/null ; then
325                                         found_crosscompiler=true
326                                         CROSS_COMPILE=$prefix
327                                 fi
328                         done
329                 fi
330
331         
332                 # TBD: look for suitable cross compiler suite
333                 # cross-$TARCH-gcc and cross-$TARCH-ld
334                 
335                 # Check result:
336                 if [ $found_crosscompiler == "false" ]; then
337                         printf " ($TARCH: skipped, we're $ARCH)\n\n"
338                         xml "  <status>notbuilt</status>"
339                         xml ""
340                         xml "</mainboard>"
341                 
342                         return 0
343                 else
344                         printf " ($TARCH: ok, we're $ARCH)\n"
345                         xml "  <compiler>"
346                         xml "    <path>`which ${CROSS_COMPILE}gcc`</path>"
347                         xml "    <version>`${CROSS_COMPILE}gcc --version | head -1`</version>"
348                         xml "  </compiler>"
349                         xml ""
350                 fi
351         fi
352
353         built_successfully $VENDOR $MAINBOARD && \
354         {
355                 printf " ( mainboard/$VENDOR/$MAINBOARD previously ok )\n\n"
356                 xml "  <status>previouslyok</status>"
357                 xml ""
358                 xml "</mainboard>"
359                 return 0
360         }
361
362         build_broken $VENDOR $MAINBOARD || \
363         {
364                 printf " ( broken mainboard/$VENDOR/$MAINBOARD skipped )\n\n"
365                 xml "  <status>knownbroken</status>"
366                 xml ""
367                 xml "</mainboard>"
368                 return 0
369         }
370         
371         create_buildenv $VENDOR $MAINBOARD
372         if [ $? -eq 0 ]; then
373                 compile_target $VENDOR $MAINBOARD && 
374                         xml "  <status>ok</status>" ||
375                         xml "<status>broken</status>"
376         fi
377
378         xml ""
379         xml "</mainboard>"
380
381         printf "\n"
382 }
383
384 function test_target
385 {
386         VENDOR=$1
387         MAINBOARD=$2
388
389         if [ "$hwtest" != "true" ]; then
390                 return 0
391         fi
392
393         # image does not exist. we silently skip the patch.
394         if [ ! -r "$TARGET/${VENDOR}_${MAINBOARD}/linuxbios.rom" ]; then
395                 return 0
396         fi
397
398         which curl &> /dev/null
399         if [ $? != 0 ]; then
400                 printf "curl is not installed but required for test submission.  skipping test.\n\n"
401                 return 0
402         fi
403
404         CURR=`pwd`
405         if [ -r "$TARGET/${VENDOR}_${MAINBOARD}/tested" ]; then
406                 printf "Testing image for board $VENDOR $MAINBOARD skipped (previously submitted).\n\n"
407                 return 0
408         fi
409         # touch $TARGET/${VENDOR}_${MAINBOARD}/tested
410
411         printf "Submitting image for board $VENDOR $MAINBOARD to test system...\n"
412
413         curl -f -F "romfile=@$TARGET/${VENDOR}_${MAINBOARD}/linuxbios.rom" \
414                 -F "mode=abuild" -F "mainboard=${VENDOR}_${MAINBOARD}" -F "submit=Upload" \
415                 "http://qa.linuxbios.org/deployment/send.php"
416
417         printf "\n"
418         return 0
419 }
420
421 function myhelp
422 {
423         printf "Usage: $0 [-v] [-a] [-b] [-t <vendor/board>] [-p <dir>] [lbroot]\n"
424         printf "       $0 [-V|--version]\n"
425         printf "       $0 [-h|--help]\n"
426         printf
427         printf "Options:\n"
428         printf "    [-v|--verbose]                print more messages\n"
429         printf "    [-a|--all]                    build previously succeeded ports as well\n"
430         printf "    [-b|--broken]                 attempt to build ports that are known broken\n"
431         printf "    [-t|--target <vendor/board>]  attempt to build target vendor/board only\n"
432         printf "    [-p|--payloads <dir>]         use payloads in <dir> to build images\n"
433         printf "    [-V|--version]                print version number and exit\n"
434         printf "    [-h|--help]                   print this help and exit\n"
435         printf "    [-x|--xml]                    write xml log file \n"
436         printf "                                  (defaults to $XMLFILE)\n"
437         printf "    [-T|--test]                   submit image(s) to automated test system\n"
438         printf "    [lbroot]                      absolute path to LinuxBIOS sources\n"
439         printf "                                  (defaults to $LBROOT)\n\n"
440 }
441
442 function myversion 
443 {
444         cat << EOF
445
446 LinuxBIOS autobuild v$ABUILD_VERSION ($ABUILD_DATE)
447
448 Copyright (C) 2004 by Stefan Reinauer <stepan@openbios.org>
449 Copyright (C) 2006 by coresystems GmbH <info@coresystems.de>
450
451 This program is free software; you may redistribute it under the terms
452 of the GNU General Public License. This program has absolutely no
453 warranty.
454
455 EOF
456 }
457
458 # default options
459 target=""
460 buildall=false
461 LBROOT=$( cd ../..; pwd )
462 verbose=false
463
464 # parse parameters
465 args=`getopt -l version,verbose,help,all,target:,broken,payloads:,test Vvhat:bp:T -- "$@"`
466
467 if [ $? != 0 ]; then
468         myhelp
469         exit 1
470 fi
471
472 eval set "$args"
473 while true ; do
474         case "$1" in
475                 -x|--xml)       shift; mode=xml; rm -f $XMLFILE ;;
476                 -t|--target)    shift; target="$1"; shift;;
477                 -a|--all)       shift; buildall=true;;
478                 -b|--broken)    shift; buildbroken=true;;
479                 -v|--verbose)   shift; verbose=true;;
480                 -V|--version)   shift; myversion; exit 0;;
481                 -h|--help)      shift; myversion; myhelp; exit 0;;
482                 -p|--payloads)  shift; payloads="$1"; shift;;
483                 -T|--test)      shift; hwtest=true;;
484                 --)             shift; break;;
485                 -*)             printf "Invalid option\n\n"; myhelp; exit 1;;
486                 *)              break;;
487         esac
488 done
489
490 # /path/to/freebios2/
491 test -z "$1" || LBROOT=$1
492
493 debug "LBROOT=$LBROOT"
494
495 xml '<?xml version="1.0" encoding="utf-8"?>'
496 xml '<abuild>'
497
498 if [ "$target" != "" ]; then
499         # build a single board
500         VENDOR=`printf $target|cut -f1 -d/`
501         MAINBOARD=`printf $target|cut -f2 -d/`
502         build_target $VENDOR $MAINBOARD
503         test_target $VENDOR $MAINBOARD
504 else
505         # build all boards per default
506         for VENDOR in $( vendors ); do
507                 for MAINBOARD in $( mainboards $VENDOR ); do
508                         build_target $VENDOR $MAINBOARD
509                         test_target $VENDOR $MAINBOARD
510                 done
511         done
512 fi
513 xml '</abuild>'
514