blob: c5e60ef78a7fa24a33729d129f7f21b945c0d56e [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001#!/bin/bash
2#
3# this script is used to rebuild the Android emulator from sources
4# in the current directory. It also contains logic to speed up the
5# rebuild if it detects that you're using the Android build system
6#
7# in this case, it will use prebuilt binaries for the compiler,
8# the audio library and the SDL library. You can disable this
9# by using the --no-prebuilt-libs and --cc=<compiler> options
10#
11#
12# here's the list of environment variables you can define before
13# calling this script to control it (besides options):
14#
15#
16
17# first, let's see which system we're running this on
18cd `dirname $0`
19PROGNAME=`basename $0`
20
21# this function will be used to execute commands and eventually
22# dump them if VERBOSE is 'yes'
23VERBOSE=yes
24VERBOSE2=no
25
26function log()
27{
28 if [ "$VERBOSE" = "yes" ] ; then
29 echo "$1"
30 fi
31}
32
33function log2()
34{
35 if [ "$VERBOSE2" = "yes" ] ; then
36 echo "$1"
37 fi
38}
39
40function execute()
41{
42 log2 "Running: $*"
43 $*
44}
45
46function compile()
47{
48 log2 "Object : $CC -o $TMPO -c $CFLAGS $TMPC"
49 $CC -o $TMPO -c $CFLAGS $TMPC 2> $TMPL
50}
51
52function link()
53{
54 log2 "Link : $LD $LDFLAGS -o $TMPE $TMPO"
55 $LD $LDFLAGS -o $TMPE $TMPO 2> $TMPL
56}
57
58function compile-exec-run()
59{
60 log2 "RunExec : $CC -o $TMPE $CFLAGS $TMPC"
61 compile
62 if [ $? != 0 ] ; then
63 echo "Failure to compile test program"
64 cat $TMPL
65 exit 1
66 fi
67 link
68 if [ $? != 0 ] ; then
69 echo "Failure to link test program"
70 cat $TMPL
71 exit 1
72 fi
73 $TMPE
74}
75
76OS=`uname -s`
77CPU=`uname -m`
78EXE=""
79case "$CPU" in
80 i?86) CPU=x86
81 ;;
82esac
83
84case "$OS" in
85 Darwin)
86 OS=darwin-$CPU
87 ;;
88 Linux)
89 # note that building on x86_64 is handled later
90 OS=linux-$CPU
91 ;;
92 *_NT-*)
93 OS=windows
94 EXE=.exe
95 ;;
96esac
97
98# Are we running in the Android build system ?
99unset TOP
100if [ -n "$ANDROID_PRODUCT_OUT" ] ; then
101 TOP=`cd $ANDROID_PRODUCT_OUT/../../../.. && pwd`
102 log "TOP found at $TOP"
103 # $TOP/config/envsetup.make is for the old tree layout
104 # $TOP/build/envsetup.sh is for the new one
105 ANDROID_CONFIG_MK=$TOP/config/envsetup.make
106 if [ ! -f $ANDROID_CONFIG_MK ] ; then
107 ANDROID_CONFIG_MK=$TOP/build/core/config.mk
108 fi
109 if [ ! -f $ANDROID_CONFIG_MK ] ; then
110 echo "Cannot find build system root (TOP)"
111 echo "defaulting to non-Android build"
112 unset TOP
113 fi
114fi
115
116# normalize the TOP variable, we don't want any trailing /
117IN_ANDROID_BUILD=no
118if [ -n "$TOP" ] ; then
119 TOPDIR=`dirname $TOP`
120 if [ "$TOPDIR" != "." ] ; then
121 TOP=$TOPDIR/`basename $TOP`
122 fi
123 IN_ANDROID_BUILD=yes
124 log "In Android Build"
125fi
126
127# Parse options
128OPTION_TARGETS=""
129OPTION_DEBUG=no
130OPTION_IGNORE_AUDIO=no
131OPTION_NO_PREBUILTS=no
132OPTION_TRY_64=no
133OPTION_HELP=no
134
135if [ -z "$CC" ] ; then
136 CC=gcc
137fi
138
139for opt do
140 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
141 case "$opt" in
142 --help|-h|-\?) OPTION_HELP=yes
143 ;;
144 --verbose)
145 if [ "$VERBOSE" = "yes" ] ; then
146 VERBOSE2=yes
147 else
148 VERBOSE=yes
149 fi
150 ;;
151 --install=*) OPTION_TARGETS="$TARGETS $optarg";
152 ;;
153 --sdl-config=*) SDL_CONFIG=$optarg
154 ;;
155 --cc=*) CC="$optarg" ; HOSTCC=$CC
156 ;;
157 --no-strip) OPTION_NO_STRIP=yes
158 ;;
159 --debug) OPTION_DEBUG=yes
160 ;;
161 --ignore-audio) OPTION_IGNORE_AUDIO=yes
162 ;;
163 --no-prebuilts) OPTION_NO_PREBUILTS=yes
164 ;;
165 --try-64) OPTION_TRY_64=yes
166 ;;
167 *)
168 echo "unknown option '$opt', use --help"
169 exit 1
170 esac
171done
172
173# Print the help message
174#
175if [ "$OPTION_HELP" = "yes" ] ; then
176 cat << EOF
177
178Usage: rebuild.sh [options]
179Options: [defaults in brackets after descriptions]
180EOF
181 echo "Standard options:"
182 echo " --help print this message"
183 echo " --install=FILEPATH copy emulator executable to FILEPATH [$TARGETS]"
184 echo " --cc=PATH specify C compiler [$CC]"
185 echo " --sdl-config=FILE use specific sdl-config script [$SDL_CONFIG]"
186 echo " --no-strip do not strip emulator executable"
187 echo " --debug enable debug (-O0 -g) build"
188 echo " --ignore-audio ignore audio messages (may build sound-less emulator)"
189 echo " --no-prebuilts do not use prebuilt libraries and compiler"
190 echo " --try-64 try to build a 64-bit executable (may crash)"
191 echo " --verbose verbose configuration"
192 echo ""
193 exit 1
194fi
195
196# Various probes are going to need to run a small C program
197TMPC=/tmp/android-qemu-$$.c
198TMPO=/tmp/android-qemu-$$.o
199TMPE=/tmp/android-qemu-$$$EXE
200TMPL=/tmp/android-qemu-$$.log
201
202function clean-exit ()
203{
204 rm -f $TMPC $TMPO $TMPL $TMPE
205 exit 1
206}
207
208# Adjust a few things when we're building within the Android build
209# system:
210# - locate prebuilt directory
211# - locate and use prebuilt libraries
212# - copy the new binary to the correct location
213#
214if [ "$OPTION_NO_PREBUILTS" = "yes" ] ; then
215 IN_ANDROID_BUILD=no
216fi
217
218if [ "$IN_ANDROID_BUILD" = "yes" ] ; then
219
220 # Get the value of a build variable as an absolute path.
221 function get_abs_build_var()
222 {
223 (cd $TOP && CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core make -f $ANDROID_CONFIG_MK dumpvar-abs-$1)
224 }
225
226 # locate prebuilt directory
227 PREBUILT_HOST_TAG=$OS
228 case $OS in
229 linux-*)
230 # Linux is a special case because in the old tree layout
231 # we simply used 'Linux' as the prebuilt host tag, but
232 # are now using "linux-x86" in the new layout
233 # check which one should be used
234 #
235 if [ -d $TOP/prebuilt/Linux ] ; then
236 PREBUILT_HOST_TAG=Linux
237 fi
238 ;;
239 esac
240 PREBUILT=$TOP/prebuilt/$PREBUILT_HOST_TAG
241 if [ ! -d $PREBUILT ] ; then
242 # this can happen when building on x86_64
243 case $OS in
244 linux-x86_64)
245 PREBUILT_HOST_TAG=linux-x86
246 PREBUILT=$TOP/prebuilt/$PREBUILT_HOST_TAG
247 log "Forcing usage of 32-bit prebuilts"
248 ;;
249 *)
250 esac
251 if [ ! -d $PREBUILT ] ; then
252 echo "Can't find the prebuilt directory $PREBUILT in Android build"
253 exit 1
254 fi
255 fi
256 log "Prebuilt : PREBUILT=$PREBUILT"
257
258 # use ccache if USE_CCACHE is defined and the corresponding
259 # binary is available.
260 #
261 # note: located in PREBUILT/ccache/ccache in the new tree layout
262 # located in PREBUILT/ccache in the old one
263 #
264 if [ -n "$USE_CCACHE" ] ; then
265 CCACHE="$PREBUILT/ccache/ccache$EXE"
266 if [ ! -f $CCACHE ] ; then
267 CCACHE="$PREBUILT/ccache$EXE"
268 fi
269 if [ -f $CCACHE ] ; then
270 CC="$CCACHE $CC"
271 fi
272 log "Prebuilt : CCACHE=$CCACHE"
273 fi
274
275 # if the user didn't specify a sdl-config script, get the prebuilt one
276 if [ -z "$SDL_CONFIG" -a "$OPTION_NO_PREBUILTS" = "no" ] ; then
277 # always use our own static libSDL by default
278 SDL_CONFIG=$PREBUILT/sdl/bin/sdl-config
279 log "Prebuilt : SDL_CONFIG=$SDL_CONFIG"
280 fi
281
282 # finally ensure that our new binary is copied to the 'out'
283 # subdirectory as 'emulator'
284 HOST_BIN=$(get_abs_build_var HOST_OUT_EXECUTABLES)
285 if [ -n "$HOST_BIN" ] ; then
286 TARGETS="$TARGETS $HOST_BIN/emulator$EXE"
287 log "Targets : TARGETS=$TARGETS"
288 fi
289fi # IN_ANDROID_BUILD = no
290
291
292####
293#### Compiler checks
294####
295####
296if [ -z "$CC" ] ; then
297 CC=gcc
298 if [ $CPU = "powerpc" ] ; then
299 CC=gcc-3.3
300 fi
301fi
302
303cat > $TMPC <<EOF
304int main(void) {}
305EOF
306
307if [ -z "$LD" ] ; then
308 LD=$CC
309fi
310
311# we only support generating 32-bit binaris on 64-bit systems.
312# And we may need to add a -Wa,--32 to CFLAGS to let the assembler
313# generate 32-bit binaries on Linux x86_64.
314#
315if [ "$OPTION_TRY_64" != "yes" ] ; then
316 if [ "$CPU" = "x86_64" -o "$CPU" = "amd64" ] ; then
317 log "Check32Bits: Forcing generation of 32-bit binaries (--try-64 to disable)"
318 CPU="i386"
319 case $OS in
320 linux-*)
321 OS=linux-x86
322 ;;
323 darwin-*)
324 OS=darwin-x86
325 ;;
326 esac
327 CFLAGS="$CFLAGS -m32"
328 LDFLAGS="$LDFLAGS -m32"
329 compile
330 if [ $? != 0 ] ; then
331 CFLAGS="$CFLAGS -Wa,--32"
332 fi
333 # check that the compiler can link 32-bit executables
334 # if not, try the host linker
335 link
336 if [ $? != 0 ] ; then
337 OLD_LD=$LD
338 LD=gcc
339 compile
340 link
341 if [ $? != 0 ] ; then
342 log "not using gcc for LD"
343 LD=$OLD_LD
344 fi
345 fi
346 fi
347fi
348
349compile
350if [ $? != 0 ] ; then
351 echo "C compiler doesn't seem to work:"
352 cat $TMPL
353 clean-exit
354fi
355log "CC : compiler check ok ($CC)"
356
357# on 64-bit systems, some of our prebuilt compilers are not
358# capable of linking 32-bit executables properly
359#
360link
361if [ $? != 0 ] ; then
362 echo "Linker doesn't seem to work:"
363 cat $TMPL
364 clean-exit
365fi
366log "LD : linker check ok ($LD)"
367
368###
369### SDL Probe
370###
371
372# For now, we require an external libSDL library, if SDL_CONFIG is not
373# defined, try to grab it from the environment
374#
375if [ -z "$SDL_CONFIG" ] ; then
376 SDL_CONFIG=`which sdl-config`
377 if [ $? != 0 ] ; then
378 echo "Please ensure that you have the emulator's patched libSDL"
379 echo "built somewhere and point to its sdl-config script either"
380 echo "with the SDL_CONFIG env. variable, or the --sdl-config=<script>"
381 echo "option."
382 clean-exit
383 fi
384fi
385
386# check that we can link statically with the library.
387#
388SDL_CFLAGS=`$SDL_CONFIG --cflags`
389SDL_LIBS=`$SDL_CONFIG --static-libs`
390
391# quick hack, remove the -D_GNU_SOURCE=1 of some SDL Cflags
392# since they break recent Mingw releases
393SDL_CFLAGS=`echo $SDL_CFLAGS | sed -e s/-D_GNU_SOURCE=1//g`
394
395log "SDL-probe : SDL_CFLAGS = $SDL_CFLAGS"
396log "SDL-probe : SDL_LIBS = $SDL_LIBS"
397
398OLD_CFLAGS=$CFLAGS
399OLD_LDFLAGS=$LDFLAGS
400
401CFLAGS="$CFLAGS $SDL_CFLAGS"
402LDFLAGS="$LDFLAGS $SDL_LIBS"
403
404cat > $TMPC << EOF
405#include <SDL.h>
406#undef main
407int main( void ) {
408 return SDL_Init (SDL_INIT_VIDEO);
409}
410EOF
411compile
412if [ $? != 0 ] ; then
413 echo "You provided an explicit sdl-config script, but the corresponding library"
414 echo "cannot be statically linked with the Android emulator directly."
415 echo "Error message:"
416 cat $TMPL
417 clean-exit
418fi
419log "SDL-probe : static linking ok"
420
421# now, let's check that the SDL library has the special functions
422# we added to our own sources
423#
424cat > $TMPC << EOF
425#include <SDL.h>
426#undef main
427int main( void ) {
428 int x, y;
429 SDL_WM_GetPos(&x, &y);
430 SDL_WM_SetPos(x, y);
431 return SDL_Init (SDL_INIT_VIDEO);
432}
433EOF
434compile
435if [ $? != 0 ] ; then
436 echo "You provided an explicit sdl-config script in SDL_CONFIG, but the"
437 echo "corresponding library doesn't have the patches required to link"
438 echo "with the Android emulator. Unsetting SDL_CONFIG will use the"
439 echo "sources bundled with the emulator instead"
440 echo "Error:"
441 cat $TMPL
442 clean-exit
443fi
444
445log "SDL-probe : extra features ok"
446rm -f $TMPL $TMPC $TMPE
447
448CFLAGS=$OLD_CFLAGS
449LDFLAGS=$OLD_LDFLAGS
450
451
452###
453### Audio subsystems probes
454###
455PROBE_COREAUDIO=no
456PROBE_ALSA=no
457PROBE_OSS=no
458PROBE_ESD=no
459PROBE_WINAUDIO=no
460
461case "$OS" in
462 darwin*) PROBE_COREAUDIO=yes;
463 ;;
464 linux-*) PROBE_ALSA=yes; PROBE_OSS=yes; PROBE_ESD=yes;
465 ;;
466 windows) PROBE_WINAUDIO=yes
467 ;;
468esac
469
470ORG_CFLAGS=$CFLAGS
471ORG_LDFLAGS=$LDFLAGS
472
473if [ "$PROBE_ESD" = yes ] ; then
474 CFLAGS="$ORG_CFLAGS"
475 LDFLAGS="$ORG_LDFLAGS -ldl"
476 cp -f android/config/check-esd.c $TMPC
477 compile && link && $TMPE
478 if [ $? = 0 ] ; then
479 log "AudioProbe : ESD seems to be usable on this system"
480 else
481 if [ "$OPTION_IGNORE_AUDIO" = no ] ; then
482 echo "the EsounD development files do not seem to be installed on this system"
483 echo "Are you missing the libesd-dev package ?"
484 echo "Correct the errors below and try again:"
485 cat $TMPL
486 clean-exit
487 fi
488 PROBE_ESD=no
489 log "AudioProbe : ESD seems to be UNUSABLE on this system !!"
490 fi
491fi
492
493if [ "$PROBE_ALSA" = yes ] ; then
494 CFLAGS="$ORG_CFLAGS"
495 LDFLAGS="$ORG_CFLAGS -ldl"
496 cp -f android/config/check-alsa.c $TMPC
497 compile && link && $TMPE
498 if [ $? = 0 ] ; then
499 log "AudioProbe : ALSA seems to be usable on this system"
500 else
501 if [ "$OPTION_IGNORE_AUDIO" = no ] ; then
502 echo "the ALSA development files do not seem to be installed on this system"
503 echo "Are you missing the libasound-dev package ?"
504 echo "Correct the erros below and try again"
505 cat $TMPL
506 clean-exit
507 fi
508 PROBE_ALSA=no
509 log "AudioProbe : ALSA seems to be UNUSABLE on this system !!"
510 fi
511fi
512
513CFLAGS=$ORG_CFLAGS
514LDFLAGS=$ORG_LDFLAGS
515
516# create the objs directory that is going to contain all generated files
517# including the configuration ones
518#
519mkdir -p objs
520
521###
522### Compiler probe
523###
524
525####
526#### Host system probe
527####
528
529# because the previous version could be read-only
530rm -f $TMPC
531
532# check host endianess
533#
534HOST_BIGENDIAN=no
535cat > $TMPC << EOF
536#include <inttypes.h>
537int main(int argc, char ** argv){
538 volatile uint32_t i=0x01234567;
539 return (*((uint8_t*)(&i))) == 0x67;
540}
541EOF
542compile-exec-run && HOST_BIGENDIAN=yes
543log "Host : HOST_BIGENDIAN=$HOST_BIGENDIAN"
544
545# check size of host long bits
546HOST_LONGBITS=32
547cat > $TMPC << EOF
548int main(void) {
549 return sizeof(void*)*8;
550}
551EOF
552compile-exec-run
553HOST_LONGBITS=$?
554log "Host : HOST_LONGBITS=$HOST_LONGBITS"
555
556# check whether we have <byteswap.h>
557#
558HAVE_BYTESWAP_H=yes
559cat > $TMPC << EOF
560#include <byteswap.h>
561EOF
562compile
563if [ $? != 0 ] ; then
564 HAVE_BYTESWAP_H=no
565fi
566log "Host : HAVE_BYTESWAP_H=$HAVE_BYTESWAP_H"
567
568# Build the config.make file
569#
570rm -rf objs
571mkdir -p objs
572config_mk=objs/config.make
573echo "# This file was autogenerated by $PROGNAME" > $config_mk
574echo "TARGET_ARCH := arm" >> $config_mk
575case $OS in
576 linux-*) HOST_OS=linux
577 ;;
578 darwin-*) HOST_OS=darwin
579 ;;
580 *) HOST_OS=$OS
581esac
582echo "OS := $OS" >> $config_mk
583echo "HOST_OS := $HOST_OS" >> $config_mk
584case $CPU in
585 i?86) HOST_ARCH=x86
586 ;;
587 amd64) HOST_ARCH=x86_64
588 ;;
589 powerpc) HOST_ARCH=ppc
590 ;;
591 *) HOST_ARCH=$CPU
592esac
593echo "HOST_ARCH := $HOST_ARCH" >> $config_mk
594PWD=`pwd`
595echo "SRC_PATH := $PWD" >> $config_mk
596echo "CC := $CC" >> $config_mk
597echo "HOST_CC := $CC" >> $config_mk
598echo "LD := $LD" >> $config_mk
599echo "NO_PREBUILT := $OPTION_NO_PREBUILTS" >> $config_mk
600echo "HOST_PREBUILT_TAG := $PREBUILT_HOST_TAG" >> $config_mk
601echo "PREBUILT := $PREBUILT" >> $config_mk
602echo "CFLAGS := $CFLAGS" >> $config_mk
603echo "LDFLAGS := $LDFLAGS" >> $config_mk
604echo "SDL_CONFIG := $SDL_CONFIG" >> $config_mk
605echo "CONFIG_COREAUDIO := $PROBE_COREAUDIO" >> $config_mk
606echo "CONFIG_WINAUDIO := $PROBE_WINAUDIO" >> $config_mk
607echo "CONFIG_ESD := $PROBE_ESD" >> $config_mk
608echo "CONFIG_ALSA := $PROBE_ALSA" >> $config_mk
609echo "CONFIG_OSS := $PROBE_OSS" >> $config_mk
610echo "" >> $config_mk
611echo "BUILD_STANDALONE_EMULATOR := true" >> $config_mk
612echo "HOST_PREBUILT_TAG := $PREBUILT_HOST_TAG" >> $config_mk
613
614log "Generate : $config_mk"
615
616# Build the config-host.h file
617#
618config_h=objs/config-host.h
619echo "/* This file was autogenerated by '$PROGNAME' */" > $config_h
620echo "#define CONFIG_QEMU_SHAREDIR \"/usr/local/share/qemu\"" >> $config_h
621echo "#define HOST_LONG_BITS $HOST_LONGBITS" >> $config_h
622if [ "$HAVE_BYTESWAP_H" = "yes" ] ; then
623 echo "#define HAVE_BYTESWAP_H 1" >> $config_h
624fi
625echo "#define CONFIG_GDBSTUB 1" >> $config_h
626echo "#define CONFIG_SLIRP 1" >> $config_h
627echo "#define CONFIG_SKINS 1" >> $config_h
628# the -nand-limits options can only work on non-windows systems
629if [ "$OS" != "windows" ] ; then
630 echo "#define CONFIG_NAND_LIMITS 1" >> $config_h
631fi
632echo "#define QEMU_VERSION \"0.8.2\"" >> $config_h
633case "$CPU" in
634 i386) HOST_CPU=I386
635 ;;
636 powerpc) HOST_CPU=PPC
637 ;;
638 x86_64|amd64) HOST_CPU=X86_64
639 ;;
640 *) HOST_CPU=$CPU
641 ;;
642esac
643echo "#define HOST_$HOST_CPU 1" >> $config_h
644log "Generate : $config_h"
645
646echo "Ready to go. Type 'make' to build emulator"