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