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