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