android-configure.sh revision 51e8424b90a592619034f13b162cb5f6d0fee3d0
1#!/bin/sh
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`
19
20# source common functions definitions
21. android/build/common.sh
22
23# Parse options
24OPTION_TARGETS=""
25OPTION_DEBUG=no
26OPTION_IGNORE_AUDIO=no
27OPTION_NO_PREBUILTS=no
28OPTION_TRY_64=no
29OPTION_HELP=no
30OPTION_DEBUG=no
31
32if [ -z "$CC" ] ; then
33  CC=gcc
34fi
35
36for opt do
37  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
38  case "$opt" in
39  --help|-h|-\?) OPTION_HELP=yes
40  ;;
41  --verbose)
42    if [ "$VERBOSE" = "yes" ] ; then
43        VERBOSE2=yes
44    else
45        VERBOSE=yes
46    fi
47  ;;
48  --debug) OPTION_DEBUG=yes
49  ;;
50  --install=*) OPTION_TARGETS="$OPTION_TARGETS $optarg";
51  ;;
52  --sdl-config=*) SDL_CONFIG=$optarg
53  ;;
54  --cc=*) CC="$optarg" ; HOSTCC=$CC
55  ;;
56  --no-strip) OPTION_NO_STRIP=yes
57  ;;
58  --debug) OPTION_DEBUG=yes
59  ;;
60  --ignore-audio) OPTION_IGNORE_AUDIO=yes
61  ;;
62  --no-prebuilts) OPTION_NO_PREBUILTS=yes
63  ;;
64  --try-64) OPTION_TRY_64=yes
65  ;;
66  *)
67    echo "unknown option '$opt', use --help"
68    exit 1
69  esac
70done
71
72# Print the help message
73#
74if [ "$OPTION_HELP" = "yes" ] ; then
75    cat << EOF
76
77Usage: rebuild.sh [options]
78Options: [defaults in brackets after descriptions]
79EOF
80    echo "Standard options:"
81    echo "  --help                   print this message"
82    echo "  --install=FILEPATH       copy emulator executable to FILEPATH [$TARGETS]"
83    echo "  --cc=PATH                specify C compiler [$CC]"
84    echo "  --sdl-config=FILE        use specific sdl-config script [$SDL_CONFIG]"
85    echo "  --no-strip               do not strip emulator executable"
86    echo "  --debug                  enable debug (-O0 -g) build"
87    echo "  --ignore-audio           ignore audio messages (may build sound-less emulator)"
88    echo "  --no-prebuilts           do not use prebuilt libraries and compiler"
89    echo "  --try-64                 try to build a 64-bit executable (may crash)"
90    echo "  --verbose                verbose configuration"
91    echo "  --debug                  build debug version of the emulator"
92    echo ""
93    exit 1
94fi
95
96# we only support generating 32-bit binaris on 64-bit systems.
97# And we may need to add a -Wa,--32 to CFLAGS to let the assembler
98# generate 32-bit binaries on Linux x86_64.
99#
100if [ "$OPTION_TRY_64" != "yes" ] ; then
101    force_32bit_binaries
102fi
103
104# Are we running in the Android build system ?
105check_android_build
106
107
108# Adjust a few things when we're building within the Android build
109# system:
110#    - locate prebuilt directory
111#    - locate and use prebuilt libraries
112#    - copy the new binary to the correct location
113#
114if [ "$OPTION_NO_PREBUILTS" = "yes" ] ; then
115    IN_ANDROID_BUILD=no
116fi
117
118if [ "$IN_ANDROID_BUILD" = "yes" ] ; then
119    locate_android_prebuilt
120
121    # use ccache if USE_CCACHE is defined and the corresponding
122    # binary is available.
123    #
124    # note: located in PREBUILT/ccache/ccache in the new tree layout
125    #       located in PREBUILT/ccache in the old one
126    #
127    if [ -n "$USE_CCACHE" ] ; then
128        CCACHE="$ANDROID_PREBUILT/ccache/ccache$EXE"
129        if [ ! -f $CCACHE ] ; then
130            CCACHE="$ANDROID_PREBUILT/ccache$EXE"
131        fi
132        if [ -f $CCACHE ] ; then
133            CC="$CCACHE $CC"
134        fi
135        log "Prebuilt   : CCACHE=$CCACHE"
136    fi
137
138    # if the user didn't specify an sdl-config script, get the prebuilt one
139    if [ -z "$SDL_CONFIG" -a "$OPTION_NO_PREBUILTS" = "no" ] ; then
140        # always use our own static libSDL by default
141        SDL_CONFIG=$ANDROID_PREBUILT/sdl/bin/sdl-config
142        log "Prebuilt   : SDL_CONFIG=$SDL_CONFIG"
143    fi
144
145    # finally ensure that our new binary is copied to the 'out'
146    # subdirectory as 'emulator'
147    HOST_BIN=$(get_android_abs_build_var HOST_OUT_EXECUTABLES)
148    if [ -n "$HOST_BIN" ] ; then
149        OPTION_TARGETS="$OPTION_TARGETS $HOST_BIN/emulator$EXE"
150        log "Targets    : TARGETS=$OPTION_TARGETS"
151    fi
152
153    # find the Android SDK Tools revision number
154    TOOLS_PROPS=$ANDROID_TOP/sdk/files/tools_source.properties
155    if [ -f $TOOLS_PROPS ] ; then
156        ANDROID_SDK_TOOLS_REVISION=`awk -F= '/Pkg.Revision/ { print $2; }' $TOOLS_PROPS 2> /dev/null`
157        log "Tools      : Found tools revision number $ANDROID_SDK_TOOLS_REVISION"
158    else
159        log "Tools      : Could not locate $TOOLS_PROPS !?"
160    fi
161fi  # IN_ANDROID_BUILD = no
162
163
164# we can build the emulator with Cygwin, so enable it
165enable_cygwin
166
167setup_toolchain
168
169###
170###  SDL Probe
171###
172
173# if the user didn't specify a sdl-config script, get the prebuilt one
174if [ -z "$SDL_CONFIG" -a "$OPTION_NO_PREBUILTS" = "no" ] ; then
175    #  try to find one from our git repository
176    SDL_CONFIG=../sdl/out/$OS/bin/sdl-config
177    if [ -f $SDL_CONFIG ] ; then
178        log "Prebuilt   : SDL_CONFIG=$SDL_CONFIG"
179    else
180        echo "WARNING: YOU SHOULD USE THE --sdl-config OPTION"
181        SDL_CONFIG=
182    fi
183fi
184
185# For now, we require an external libSDL library, if SDL_CONFIG is not
186# defined, try to grab it from the environment
187#
188if [ -z "$SDL_CONFIG" ] ; then
189    SDL_CONFIG=`which sdl-config`
190    if [ $? != 0 ] ; then
191        echo "Please ensure that you have the emulator's patched libSDL"
192        echo "built somewhere and point to its sdl-config script either"
193        echo "with the SDL_CONFIG env. variable, or the --sdl-config=<script>"
194        echo "option."
195        clean_exit
196    fi
197fi
198
199# check that we can link statically with the library.
200#
201SDL_CFLAGS=`$SDL_CONFIG --cflags`
202SDL_LIBS=`$SDL_CONFIG --static-libs`
203
204# quick hack, remove the -D_GNU_SOURCE=1 of some SDL Cflags
205# since they break recent Mingw releases
206SDL_CFLAGS=`echo $SDL_CFLAGS | sed -e s/-D_GNU_SOURCE=1//g`
207
208log "SDL-probe  : SDL_CFLAGS = $SDL_CFLAGS"
209log "SDL-probe  : SDL_LIBS   = $SDL_LIBS"
210
211
212EXTRA_CFLAGS="$SDL_CFLAGS"
213EXTRA_LDFLAGS="$SDL_LIBS"
214
215case "$OS" in
216    freebsd-*)
217    EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm -lpthread"
218    ;;
219esac
220
221cat > $TMPC << EOF
222#include <SDL.h>
223#undef main
224int main( int argc, char** argv ) {
225   return SDL_Init (SDL_INIT_VIDEO); 
226}
227EOF
228feature_check_link  SDL_LINKING
229
230if [ $SDL_LINKING != "yes" ] ; then
231    echo "You provided an explicit sdl-config script, but the corresponding library"
232    echo "cannot be statically linked with the Android emulator directly."
233    echo "Error message:"
234    cat $TMPL
235    clean_exit
236fi
237log "SDL-probe  : static linking ok"
238
239# now, let's check that the SDL library has the special functions
240# we added to our own sources
241#
242cat > $TMPC << EOF
243#include <SDL.h>
244#undef main
245int main( int argc, char** argv ) {
246    int  x, y;
247    SDL_Rect  r;
248    SDL_WM_GetPos(&x, &y);
249    SDL_WM_SetPos(x, y);
250    SDL_WM_GetMonitorDPI(&x, &y);
251    SDL_WM_GetMonitorRect(&r);
252    return SDL_Init (SDL_INIT_VIDEO); 
253}
254EOF
255feature_check_link  SDL_LINKING
256
257if [ $SDL_LINKING != "yes" ] ; then
258    echo "You provided an explicit sdl-config script in SDL_CONFIG, but the"
259    echo "corresponding library doesn't have the patches required to link"
260    echo "with the Android emulator. Unsetting SDL_CONFIG will use the"
261    echo "sources bundled with the emulator instead"
262    echo "Error:"
263    cat $TMPL
264    clean_exit
265fi
266
267log "SDL-probe  : extra features ok"
268clean_temp
269
270EXTRA_CFLAGS=
271EXTRA_LDFLAGS=
272
273###
274###  Audio subsystems probes
275###
276PROBE_COREAUDIO=no
277PROBE_ALSA=no
278PROBE_OSS=no
279PROBE_ESD=no
280PROBE_WINAUDIO=no
281
282case "$OS" in
283    darwin*) PROBE_COREAUDIO=yes;
284    ;;
285    linux-*) PROBE_ALSA=yes; PROBE_OSS=yes; PROBE_ESD=yes;
286    ;;
287    freebsd-*) PROBE_OSS=yes;
288    ;;
289    windows) PROBE_WINAUDIO=yes
290    ;;
291esac
292
293ORG_CFLAGS=$CFLAGS
294ORG_LDFLAGS=$LDFLAGS
295
296if [ "$PROBE_ESD" = yes ] ; then
297    CFLAGS="$ORG_CFLAGS"
298    LDFLAGS="$ORG_LDFLAGS -ldl"
299    cp -f android/config/check-esd.c $TMPC
300    compile && link && $TMPE
301    if [ $? = 0 ] ; then
302        log "AudioProbe : ESD seems to be usable on this system"
303    else
304        if [ "$OPTION_IGNORE_AUDIO" = no ] ; then
305            echo "the EsounD development files do not seem to be installed on this system"
306            echo "Are you missing the libesd-dev package ?"
307            echo "Correct the errors below and try again:"
308            cat $TMPL
309            clean_exit
310        fi
311        PROBE_ESD=no
312        log "AudioProbe : ESD seems to be UNUSABLE on this system !!"
313    fi
314fi
315
316if [ "$PROBE_ALSA" = yes ] ; then
317    CFLAGS="$ORG_CFLAGS"
318    LDFLAGS="$ORG_CFLAGS -ldl"
319    cp -f android/config/check-alsa.c $TMPC
320    compile && link && $TMPE
321    if [ $? = 0 ] ; then
322        log "AudioProbe : ALSA seems to be usable on this system"
323    else
324        if [ "$OPTION_IGNORE_AUDIO" = no ] ; then
325            echo "the ALSA development files do not seem to be installed on this system"
326            echo "Are you missing the libasound-dev package ?"
327            echo "Correct the erros below and try again"
328            cat $TMPL
329            clean_exit
330        fi
331        PROBE_ALSA=no
332        log "AudioProbe : ALSA seems to be UNUSABLE on this system !!"
333    fi
334fi
335
336CFLAGS=$ORG_CFLAGS
337LDFLAGS=$ORG_LDFLAGS
338
339# create the objs directory that is going to contain all generated files
340# including the configuration ones
341#
342mkdir -p objs
343
344###
345###  Compiler probe
346###
347
348####
349####  Host system probe
350####
351
352# because the previous version could be read-only
353rm -f $TMPC
354
355# check host endianess
356#
357HOST_BIGENDIAN=no
358cat > $TMPC << EOF
359#include <inttypes.h>
360int main(int argc, char ** argv){
361        volatile uint32_t i=0x01234567;
362        return (*((uint8_t*)(&i))) == 0x67;
363}
364EOF
365feature_run_exec HOST_BIGENDIAN
366
367# check size of host long bits
368cat > $TMPC << EOF
369int main(void) {
370        return sizeof(void*)*8;
371}
372EOF
373feature_run_exec HOST_LONGBITS
374
375# check whether we have <byteswap.h>
376#
377feature_check_header HAVE_BYTESWAP_H "<byteswap.h>"
378
379# Build the config.make file
380#
381
382create_config_mk
383
384PWD=`pwd`
385echo "TARGET_ARCH := arm" >> $config_mk
386echo "SRC_PATH          := $PWD" >> $config_mk
387echo "SDL_CONFIG         := $SDL_CONFIG" >> $config_mk
388echo "CONFIG_COREAUDIO  := $PROBE_COREAUDIO" >> $config_mk
389echo "CONFIG_WINAUDIO   := $PROBE_WINAUDIO" >> $config_mk
390echo "CONFIG_ESD        := $PROBE_ESD" >> $config_mk
391echo "CONFIG_ALSA       := $PROBE_ALSA" >> $config_mk
392echo "CONFIG_OSS        := $PROBE_OSS" >> $config_mk
393echo "BUILD_STANDALONE_EMULATOR := true" >> $config_mk
394if [ $OPTION_DEBUG = yes ] ; then
395    echo "BUILD_DEBUG_EMULATOR := true" >> $config_mk
396fi
397if [ -n "$ANDROID_SDK_TOOLS_REVISION" ] ; then
398    echo "ANDROID_SDK_TOOLS_REVISION := $ANDROID_SDK_TOOLS_REVISION" >> $config_mk
399fi
400
401# Build the config-host.h file
402#
403config_h=objs/config-host.h
404echo "/* This file was autogenerated by '$PROGNAME' */" > $config_h
405echo "#define CONFIG_QEMU_SHAREDIR   \"/usr/local/share/qemu\"" >> $config_h
406echo "#define HOST_LONG_BITS  $HOST_LONGBITS" >> $config_h
407if [ "$HAVE_BYTESWAP_H" = "yes" ] ; then
408  echo "#define HAVE_BYTESWAP_H 1" >> $config_h
409fi
410echo "#define CONFIG_GDBSTUB  1" >> $config_h
411echo "#define CONFIG_SLIRP    1" >> $config_h
412echo "#define CONFIG_SKINS    1" >> $config_h
413echo "#define CONFIG_TRACE    1" >> $config_h
414# the -nand-limits options can only work on non-windows systems
415if [ "$OS" != "windows" ] ; then
416    echo "#define CONFIG_NAND_LIMITS  1" >> $config_h
417fi
418echo "#define QEMU_VERSION    \"0.10.50\"" >> $config_h
419echo "#define QEMU_PKGVERSION \"Android\"" >> $config_h
420case "$CPU" in
421    x86) CONFIG_CPU=I386
422    ;;
423    ppc) CONFIG_CPU=PPC
424    ;;
425    x86_64) CONFIG_CPU=X86_64
426    ;;
427    *) CONFIG_CPU=$CPU
428    ;;
429esac
430echo "#define HOST_$CONFIG_CPU    1" >> $config_h
431BSD=0
432case "$OS" in
433    linux-*) CONFIG_OS=LINUX
434    ;;
435    darwin-*) CONFIG_OS=DARWIN
436              BSD=1
437    ;;
438    freebsd-*) CONFIG_OS=FREEBSD
439              BSD=1
440    ;;
441    windows*) CONFIG_OS=WIN32
442    ;;
443    *) CONFIG_OS=$OS
444esac
445
446case $OS in
447    linux-*|darwin-*)
448        echo "#define HAVE_IOVEC 1" >> $config_h
449        ;;
450esac
451
452echo "#define CONFIG_$CONFIG_OS   1" >> $config_h
453if [ $BSD = 1 ] ; then
454    echo "#define _BSD             1" >> $config_h
455    echo "#define O_LARGEFILE      0" >> $config_h
456    echo "#define MAP_ANONYMOUS    MAP_ANON" >> $config_h
457fi
458
459log "Generate   : $config_h"
460
461echo "Ready to go. Type 'make' to build emulator"
462