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