add full xml logging to abuild to work on the complete information
[coreboot.git] / util / abuild / abuild.sh
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 # Where shall we place all the build trees?
18 TARGET=$( pwd )/linuxbios-builds
19 XMLFILE=$( pwd )/abuild.xml
20
21 # path to payload. Should be more generic
22 PAYLOAD=/dev/null
23
24 # Lines of error context to be printed in FAILURE case
25 CONTEXT=5
26
27 # One might want to adjust these in case of cross compiling
28 MAKE="make"
29 PYTHON=python
30
31 # this can be changed to xml by -x
32 mode=text
33
34 ARCH=`uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \
35         -e s/arm.*/arm/ -e s/sa110/arm/ -e s/x86_64/amd64/ \
36         -e "s/Power Macintosh/ppc/"`
37
38 function debug
39 {
40         test "$verbose" == "true" && echo $*
41 }
42
43 function xml
44 {
45         test "$mode" == "xml" && echo "$*" >> $XMLFILE
46 }
47
48 function xmlfile
49 {
50         test "$mode" == "xml" && { echo '<![CDATA['; cat $1; echo ']]>'; } >> $XMLFILE
51 }
52
53
54
55 function vendors
56 {
57         # make this a function so we can easily select
58         # without breaking readability
59         ls -1 "$LBROOT/src/mainboard" | grep -v CVS
60 }
61
62 function mainboards
63 {
64         # make this a function so we can easily select
65         # without breaking readability
66         
67         VENDOR=$1
68         
69         ls -1 $LBROOT/src/mainboard/$VENDOR | grep -v CVS 
70 }
71
72 function architecture
73 {
74         VENDOR=$1
75         MAINBOARD=$2
76         cat $LBROOT/src/mainboard/$VENDOR/$MAINBOARD/Config.lb | \
77                 grep ^arch | cut -f 2 -d\ 
78 }
79
80 function create_config
81 {
82         VENDOR=$1
83         MAINBOARD=$2
84         TARCH=$( architecture $VENDOR $MAINBOARD )
85         TARGCONFIG=$LBROOT/targets/$VENDOR/$MAINBOARD/Config-abuild.lb
86
87         mkdir -p $TARGET
88
89         if [ -f $TARGCONFIG ]; then
90                 cp $TARGCONFIG $TARGET/Config-${VENDOR}_${MAINBOARD}.lb
91                 echo "Used existing test target $TARGCONFIG"
92                 xml "  <config>$TARGCONFIG</config>"
93                 return 
94         fi
95
96         echo -n "  Creating config file..."
97         xml "  <config>autogenerated</config>"
98         ( cat << EOF
99 # This will make a target directory of ./VENDOR_MAINBOARD
100
101 target VENDOR_MAINBOARD
102 mainboard VENDOR/MAINBOARD
103
104 option CC="CROSSCC"
105 option CROSS_COMPILE="CROSS_PREFIX"
106 option HOSTCC="CROSS_HOSTCC"
107
108 EOF
109         if [ "$TARCH" == i386 ] ; then
110                 cat <<EOF
111 romimage "normal"
112         option USE_FALLBACK_IMAGE=0
113         option ROM_IMAGE_SIZE=0x20000
114         option LINUXBIOS_EXTRA_VERSION=".0-normal"
115         payload PAYLOAD
116 end
117
118 romimage "fallback" 
119         option USE_FALLBACK_IMAGE=1
120         option ROM_IMAGE_SIZE=0x20000
121         option LINUXBIOS_EXTRA_VERSION=".0-fallback"
122         payload PAYLOAD
123 end
124 buildrom ./linuxbios.rom ROM_SIZE "normal" "fallback"
125 EOF
126         else
127                 cat <<EOF
128 romimage "only"
129         option LINUXBIOS_EXTRA_VERSION=".0"
130         payload PAYLOAD
131 end
132 buildrom ./linuxbios.rom ROM_SIZE "only"
133 EOF
134         fi
135         ) | sed -e s,VENDOR,$VENDOR,g \
136                 -e s,MAINBOARD,$MAINBOARD,g \
137                 -e s,PAYLOAD,$PAYLOAD,g \
138                 -e s,CROSSCC,"$CC",g \
139                 -e s,CROSS_PREFIX,"$CROSS_COMPILE",g \
140                 -e s,CROSS_HOSTCC,"$HOSTCC",g \
141                 > $TARGET/Config-${VENDOR}_${MAINBOARD}.lb
142         echo " ok"
143 }
144
145 function create_builddir
146 {       
147         VENDOR=$1
148         MAINBOARD=$2
149         
150         echo -n "  Creating builddir..."
151
152         target_dir=$TARGET
153         config_dir=$LBROOT/util/newconfig
154         yapps2_py=$config_dir/yapps2.py
155         config_g=$config_dir/config.g
156         config_lb=Config-${VENDOR}_${MAINBOARD}.lb
157
158         cd $target_dir
159
160         build_dir=${VENDOR}_${MAINBOARD}
161         config_py=$build_dir/config.py
162
163         if [ ! -d $build_dir ] ; then
164                 mkdir -p $build_dir
165         fi
166         if [ ! -f $config_py ]; then
167                 $PYTHON $yapps2_py $config_g $config_py &> $build_dir/py.log
168         fi
169
170         # make sure config.py is up-to-date
171
172         export PYTHONPATH=$config_dir
173         $PYTHON $config_py $config_lb $LBROOT &> $build_dir/config.log
174         if [ $? -eq 0 ]; then
175                 echo "ok"
176                 xml "  <builddir>ok</builddir>"
177                 xml ""
178                 return 0
179         else
180                 echo "FAILED! Log excerpt:"
181                 xml "  <builddir>failed</builddir>"
182                 xml "  <log>"
183                 xmlfile $build_dir/config.log
184                 xml "  </log>"
185                 xml ""
186                 tail -n $CONTEXT $build_dir/config.log
187                 return 1
188         fi
189 }
190
191 function create_buildenv
192 {
193         VENDOR=$1
194         MAINBOARD=$2
195         create_config $VENDOR $MAINBOARD
196         create_builddir $VENDOR $MAINBOARD
197 }
198
199 function compile_target
200 {       
201         VENDOR=$1
202         MAINBOARD=$2
203
204         echo -n "  Compiling image .."
205         CURR=$( pwd )
206         cd $TARGET/${VENDOR}_${MAINBOARD}
207         stime=`date +%s`
208         eval $MAKE &> make.log
209         ret=$?
210         etime=`date +%s`
211         duration=$(( $etime - $stime ))
212         if [ $ret -eq 0 ]; then
213                 xml "  <compile>ok</compile>"
214                 xml "  <compiletime>${duration}s</compiletime>"
215                 echo "ok" > compile.status
216                 echo "ok. (took ${duration}s)"
217                 cd $CURR
218                 return 0
219         else
220                 xml "  <compile>failed</compile>"
221                 xml "  <compiletime>${duration}s</compiletime>"
222                 xml "  <log>"
223                 xmlfile make.log
224                 xml "  </log>"
225
226                 echo "FAILED after ${duration}s! Log excerpt:"
227                 tail -n $CONTEXT make.log
228                 cd $CURR
229                 return 1
230         fi
231 }
232
233 function built_successfully
234 {
235         CURR=`pwd`
236         status="fail"
237         if [ -d "$TARGET/${VENDOR}_${MAINBOARD}" ]; then
238                 cd $TARGET/${VENDOR}_${MAINBOARD}
239                 if [ -r compile.status ] ; then
240                         status=`cat compile.status`
241                 fi
242                 cd $CURR
243         fi
244         [ "$buildall" != "true" -a "$status" == "ok" ]
245 }
246
247 function build_broken
248 {
249         CURR=`pwd`
250         status="yes"
251         [ -r "$LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/BROKEN" ] && status="no"
252         [ "$buildbroken" == "true" -o "$status" == "yes" ]
253 }
254
255 function build_target
256 {
257         VENDOR=$1
258         MAINBOARD=$2
259         TARCH=$( architecture $VENDOR $MAINBOARD )
260
261         # default setting
262         CC='$(CROSS_COMPILE)gcc'
263         HOSTCC='gcc'
264         CROSS_COMPILE=''
265
266         echo -n "Processing mainboard/$VENDOR/$MAINBOARD"
267
268         xml "<mainboard>"
269         xml ""
270         xml "  <vendor>$VENDOR</vendor>"
271         xml "  <device>$MAINBOARD</device>"
272         xml ""
273         xml "  <architecture>$TARCH</architecture>"
274         xml ""
275         
276         [ -r "$LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/abuild.info" ] && \
277                 source $LBROOT/src/mainboard/${VENDOR}/${MAINBOARD}/abuild.info
278         
279         if [ "$ARCH" == "$TARCH" ]; then
280                 echo " ($TARCH: ok)"
281         else
282                 found_crosscompiler=false
283                 if [ "$ARCH" == amd64 -a "$TARCH" == i386 ]; then
284                         CC="gcc -m32"
285                         found_crosscompiler=true
286                 fi
287                 if [ "$ARCH" == ppc64 -a "$TARCH" == ppc ]; then
288                         CC="gcc -m32"
289                         found_crosscompiler=true
290                 fi
291                 if [ "$found_crosscompiler" == "false" -a "$TARCH" == ppc ];then
292                         for prefix in powerpc-eabi- powerpc-linux- ppc_74xx- \
293                             powerpc-7450-linux-gnu-; do
294                                 if ${prefix}gcc --version > /dev/null 2> /dev/null ; then
295                                         found_crosscompiler=true
296                                         CROSS_COMPILE=$prefix
297                                 fi
298                         done
299                 fi
300
301                 xml "  <compiler>"
302                 xml "    <path>`which ${CROSS_COMPILE}gcc`</path>"
303                 xml "    <version>`${CROSS_COMPILE}gcc --version | head -1`</version>"
304                 xml "  </compiler>"
305                 xml ""
306                 
307                 # TBD: look for suitable cross compiler suite
308                 # cross-$TARCH-gcc and cross-$TARCH-ld
309                 
310                 # Check result:
311                 if [ $found_crosscompiler == "false" ]; then
312                         echo " ($TARCH: skipped, we're $ARCH)"
313                         echo
314                         return 0
315                 else
316                         echo " ($TARCH: ok, we're $ARCH)"
317                 fi
318         fi
319
320         built_successfully $VENDOR $MAINBOARD && \
321         {
322                 echo " ( mainboard/$VENDOR/$MAINBOARD previously ok )"
323                 echo
324                 xml "  <status>previouslyok</status>"
325                 xml ""
326                 xml "</mainboard>"
327                 return 0
328         }
329
330         build_broken $VENDOR $MAINBOARD || \
331         {
332                 echo " ( broken mainboard/$VENDOR/$MAINBOARD skipped )"
333                 echo
334                 xml "  <status>knownbroken</status>"
335                 xml ""
336                 xml "</mainboard>"
337                 return 0
338         }
339         
340         create_buildenv $VENDOR $MAINBOARD
341         if [ $? -eq 0 ]; then
342                 compile_target $VENDOR $MAINBOARD && 
343                         xml "<status>ok</status>" ||
344                         xml "<status>broken</status>"
345         fi
346
347         xml ""
348         xml "</mainboard>"
349
350         echo
351 }
352
353 function myhelp
354 {
355         echo "Usage: $0 [-v] [-a] [-b] [-t <vendor/board>] [lbroot]"
356         echo "       $0 [-V|--version]"
357         echo "       $0 [-h|--help]"
358         echo
359         echo "Options:"
360         echo "    [-v|--verbose]                  print more messages"
361         echo "    [-a|--all]                      build previously succeeded ports as well"
362         echo "    [-b|--broken]           attempt to build ports that are known broken"
363         echo "    [-t|--target <vendor/board>]  attempt to build target vendor/board only"
364         echo "    [-V|--version]                  print version number and exit"
365         echo "    [-h|--help]                     print this help and exit"
366         echo "    [-x|--xml]                      write xml log file $XMLFILE"
367         echo "    [lbroot]                        absolute path to LinuxBIOS sources"
368         echo "                            (defaults to $LBROOT)"
369         echo
370 }
371
372 function myversion 
373 {
374         cat << EOF
375
376 LinuxBIOS autobuild: V0.2.
377
378 Copyright (C) 2004 by Stefan Reinauer <stepan@openbios.org>
379 Copyright (C) 2006 by coresystems GmbH <info@coresystems.de>
380 This program is free software; you may redistribute it under the terms
381 of the GNU General Public License. This program has absolutely no
382 warranty.
383
384 EOF
385         myhelp
386 }
387
388 # default options
389 target=""
390 buildall=false
391 LBROOT=$( cd ../..; pwd )
392 verbose=false
393
394 # parse parameters
395 args=`getopt -l version,verbose,help,all,target:,broken Vvhat:b -- "$@"`
396
397 if [ $? != 0 ]; then
398         myhelp
399         exit 1
400 fi
401
402 eval set "$args"
403 while true ; do
404         case "$1" in
405                 -x|--xml)       shift; mode=xml; rm -f $XMLFILE ;;
406                 -t|--target)    shift; target="$1"; shift;;
407                 -a|--all)       shift; buildall=true;;
408                 -b|--broken)    shift; buildbroken=true;;
409                 -v|--verbose)   shift; verbose=true;;
410                 -V|--version)   shift; myversion; exit 0;;
411                 -h|--help)      shift; myhelp; exit 0;;
412                 --)             shift; break;;
413                 -*)             echo -e "Invalid option\n"; myhelp; exit 1;;
414                 *)              break;;
415         esac
416 done
417
418 # /path/to/freebios2/
419 test -z "$1" || LBROOT=$1
420
421 debug "LBROOT=$LBROOT"
422
423 xml '<?xml version="1.0" encoding="utf-8"?>'
424 xml '<abuild>'
425
426 if [ "$target" != "" ]; then
427         # build a single board
428         VENDOR=`echo $target|cut -f1 -d/`
429         MAINBOARD=`echo $target|cut -f2 -d/`
430         build_target $VENDOR $MAINBOARD
431 else
432         # build all boards per default
433         for VENDOR in $( vendors ); do
434                 for MAINBOARD in $( mainboards $VENDOR ); do
435                         build_target $VENDOR $MAINBOARD
436                 done
437         done
438 fi
439 xml '</abuild>'
440