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