ifeq ($(TARGET_ARCH),arm)
LOCAL_PATH:= $(call my-dir)

# determine the location of platform-specific directories
#
ifneq ($(NO_PREBUILT),yes)
  PREBUILT ?= prebuilt/$(HOST_PREBUILT_TAG)
endif

CONFIG_DIRS     := \
    $(LOCAL_PATH)/android/config \
    $(LOCAL_PATH)/android/config/$(HOST_PREBUILT_TAG)

CONFIG_INCLUDES := $(CONFIG_DIRS:%=-I%)

MY_CFLAGS := $(CONFIG_INCLUDES)
# this is needed to build the emulator on 64-bit Linux systems
ifeq ($(HOST_OS),linux)
  MY_CFLAGS += -Wa,--32
endif

ifeq ($(HOST_OS),windows)
  MY_CFLAGS += -D_WIN32 -mno-cygwin
  # we need Win32 features  that are available since Windows 2000 (NT 5.0)
  MY_CFLAGS += -DWINVER=0x500
endif

MY_CC := $(HOST_CC)

ifneq ($(NO_PREBUILT),yes)
# We're going to use a specific version of gcc (3.4.6) if it is available
# from the prebuilt directory.  This prevents many nasty emulator problems
# due to the way QEMU works
#

  GCCQEMU := $(PREBUILT)/gcc-qemu/bin/gcc
  ifneq ($(strip $(wildcard $(GCCQEMU))),)
    MY_CC := $(GCCQEMU)
  endif

  ifneq ($(USE_CCACHE),)
    MY_CC := $(PREBUILT)/ccache $(MY_CC)
  endif
endif

include $(CLEAR_VARS)

###############################################################################
# compile the 'dyngen' executable, it is used to generate the various
# runtime code generators used by the emulator. it does that by parsing a
# special .o file containing routines for each one of the pseudo-opcodes
# handled by the CPU-specific translator
#
LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
LOCAL_CC                        := $(MY_CC)
LOCAL_CFLAGS                    := $(MY_CFLAGS) $(LOCAL_CFLAGS)
LOCAL_SRC_FILES                 := dyngen.c

ifeq ($(HOST_OS),windows)
  LOCAL_LDLIBS += -mno-cygwin -mconsole
endif

LOCAL_MODULE := emulator-dyngen
include $(BUILD_HOST_EXECUTABLE)

DYNGEN := $(LOCAL_BUILT_MODULE)

###############################################################################
# build the normal dynamic translation code as a library, because it needs
# specific compilation flags. not that we are poking some object
# files directly through dyngen !
#

# determine the C flags used to compile op.c into op.h and others
#
OP_CFLAGS := -I$(LOCAL_PATH)/target-arm \
             -I$(LOCAL_PATH)/fpu \
             $(MY_CFLAGS)

OP_CFLAGS += -O2 -g -fomit-frame-pointer -fno-strict-aliasing -falign-functions=0 \
             -fno-gcse -fno-reorder-blocks -fno-optimize-sibling-calls \
             -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE \
             -fno-PIC

ifeq ($(HOST_ARCH),ppc)
    OP_CFLAGS += -D__powerpc__ -mdynamic-no-pic
endif

ifeq ($(HOST_ARCH),x86)
ifeq ($(HOST_OS),darwin)
    OP_CFLAGS += -mpreferred-stack-boundary=4 -mdynamic-no-pic
else
    OP_CFLAGS += -mpreferred-stack-boundary=2
endif
endif

# build the nomal dynamic translation code as the 'emulator-op' library
#
include $(CLEAR_VARS)

LOCAL_MODULE := emulator-op
OP_SUFFIX    :=

include $(LOCAL_PATH)/translate.make

include $(BUILD_HOST_STATIC_LIBRARY)


# build the tracing dynamic translation code as the 'emulator-op-trace' library
#
include $(CLEAR_VARS)

LOCAL_MODULE := emulator-op-trace
LOCAL_CFLAGS := -DGEN_TRACE=1
OP_SUFFIX    := -trace

include $(LOCAL_PATH)/translate.make

include $(BUILD_HOST_STATIC_LIBRARY)

# define to true to build the ARM-specific code generator as an independent
# static library. this allows you to apply different optimization strategy
BUILD_ARM_LIBRARY := true

ifeq ($(BUILD_ARM_LIBRARY),true)
##############################################################################
# build the ARM-specific target sources
#
include $(CLEAR_VARS)

LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
LOCAL_CC                        := $(MY_CC)
LOCAL_MODULE                    := emulator-arm

LOCAL_CFLAGS := -fno-PIC -fomit-frame-pointer -Wno-sign-compare
LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS)

ifeq ($(HOST_OS),darwin)
LOCAL_CFLAGS += -O2
endif
ifeq ($(HOST_OS),windows)
LOCAL_CFLAGS += -O2
endif

# generate the normal translator source files from emulator-op library
#
LOCAL_CFLAGS    += -I$(LOCAL_PATH)/target-arm \
                   -I$(LOCAL_PATH)/fpu \
                   -I$(INTERMEDIATE)

ifeq ($(HOST_ARCH),ppc)
    LOCAL_CFLAGS += -D__powerpc__
endif

ifeq ($(HOST_OS),darwin)
    LOCAL_CFLAGS += -mdynamic-no-pic
endif

# include CPU translation sources
#
LOCAL_SRC_FILES += exec.c cpu-exec.c  \
                   target-arm/op_helper.c \
                   target-arm/helper.c

# include ARM-specific soft-float implementation
#
NWFPE_SOURCES := fpa11.c fpa11_cpdo.c fpa11_cpdt.c fpa11_cprt.c fpopcode.c single_cpdo.c \
                 double_cpdo.c extended_cpdo.c

FPU_SOURCES := softfloat.c

LOCAL_SRC_FILES += $(XXNWFPE_SOURCES:%=target-arm/nwfpe/%) \
                   $(FPU_SOURCES:%=fpu/%)

LOCAL_CFLAGS    += -I$(LOCAL_PATH)/target-arm/nwfpe \
                   -I$(LOCAL_PATH)/linux-user \
                   -I$(LOCAL_PATH)/linux-user/arm

include $(BUILD_HOST_STATIC_LIBRARY)
endif # BUILD_ARM_LIBRARY

##############################################################################
# now build the emulator itself
#
include $(CLEAR_VARS)

LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
LOCAL_CC                        := $(MY_CC)
LOCAL_MODULE                    := emulator
LOCAL_STATIC_LIBRARIES          := $(EMULATOR_OP_LIBRARIES) emulator-arm

# don't remove the -fno-strict-aliasing, or you'll break things
# (e.g. slirp/network support)
#
LOCAL_CFLAGS := -fno-PIC -fomit-frame-pointer -Wno-sign-compare \
                -fno-strict-aliasing -g -W -Wall -Wno-unused-parameter

LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS)

ifeq ($(HOST_OS),darwin)
LOCAL_CFLAGS += -O2
endif
ifeq ($(HOST_OS),windows)
LOCAL_CFLAGS += -O2
endif
# add the build ID to the default macro definitions
LOCAL_CFLAGS += -DANDROID_BUILD_ID="$(strip $(BUILD_ID))-$(strip $(BUILD_NUMBER))"

ifeq ($(HOST_ARCH),ppc)
    LOCAL_CFLAGS += -D__powerpc__
endif

ifeq ($(HOST_OS),darwin)
    LOCAL_CFLAGS += -mdynamic-no-pic
endif

# include the Zlib sources
#
ZLIB_DIR := distrib/zlib-1.2.3
include $(LOCAL_PATH)/$(ZLIB_DIR)/sources.make
LOCAL_SRC_FILES += $(ZLIB_SOURCES)
LOCAL_CFLAGS    += $(ZLIB_CFLAGS) -I$(LOCAL_PATH)/$(ZLIB_DIR)

# include the Libpng sources
#
LIBPNG_DIR := distrib/libpng-1.2.19
include $(LOCAL_PATH)/$(LIBPNG_DIR)/sources.make
LOCAL_SRC_FILES += $(LIBPNG_SOURCES)
LOCAL_CFLAGS    += $(LIBPNG_CFLAGS) -I$(LOCAL_PATH)/$(LIBPNG_DIR)

LOCAL_CFLAGS    += -I$(LOCAL_PATH)/target-arm \
                   -I$(LOCAL_PATH)/fpu \
                   -I$(INTERMEDIATE)

ifneq ($(BUILD_ARM_LIBRARY),true)
# generate the normal translator source files from emulator-op library
#

# include CPU translation sources
#
LOCAL_SRC_FILES += exec.c cpu-exec.c  \
                   target-arm/op_helper.c \
		   target-arm/helper.c

# include ARM-specific soft-float implementation
#
NWFPE_SOURCES := fpa11.c fpa11_cpdo.c fpa11_cpdt.c fpa11_cprt.c fpopcode.c single_cpdo.c \
                 double_cpdo.c extended_cpdo.c

FPU_SOURCES := softfloat.c

LOCAL_SRC_FILES += $(XXNWFPE_SOURCES:%=target-arm/nwfpe/%) \
                   $(FPU_SOURCES:%=fpu/%)

LOCAL_CFLAGS    += -I$(LOCAL_PATH)/target-arm/nwfpe \
                   -I$(LOCAL_PATH)/linux-user \
                   -I$(LOCAL_PATH)/linux-user/arm
endif # !BUILD_ARM_LIBRARY

# include telephony stuff
#
TELEPHONY_SOURCES := android_modem.c modem_driver.c gsm.c sim_card.c sysdeps_qemu.c sms.c remote_call.c
LOCAL_SRC_FILES += $(TELEPHONY_SOURCES:%=telephony/%)
LOCAL_CFLAGS    += -I$(LOCAL_PATH)/telephony

# include sound support source files. we first try to see if we have a prebuilt audio
# library. if not, we build things the "hard" way.
#
# note that to generate the prebuilt audio library, you should do the following:
#
#   cd tools/qemu
#   ./android-rebuild.sh
#   distrib/update-audio.sh
#
QEMU_AUDIO_LIB := $(wildcard $(PREBUILT)/qemu/libqemu-audio.a)

ifeq ($(QEMU_AUDIO_LIB),)
  AUDIO_SOURCES := audio.c noaudio.c wavaudio.c sdlaudio.c wavcapture.c mixeng.c

  ifeq ($(HOST_OS),darwin)
    CONFIG_COREAUDIO ?= yes
  endif

  ifeq ($(HOST_OS),windows)
    CONFIG_WINAUDIO ?= yes
  endif

  ifeq ($(HOST_OS),linux)
    CONFIG_OSS  ?= yes
    CONFIG_ALSA ?= yes
    CONFIG_ESD  ?= yes
  endif

  ifeq ($(CONFIG_COREAUDIO),yes)
    AUDIO_SOURCES += coreaudio.c
    LOCAL_CFLAGS  += -DCONFIG_COREAUDIO
    LOCAL_LDLIBS  += -Wl,-framework,CoreAudio
  endif

  ifeq ($(CONFIG_WINAUDIO),yes)
    AUDIO_SOURCES += winaudio.c
    LOCAL_CFLAGS  += -DCONFIG_WINAUDIO
  endif

  ifeq ($(CONFIG_ALSA),yes)
    AUDIO_SOURCES += alsaaudio.c audio_pt_int.c
    LOCAL_CFLAGS  += -DCONFIG_ALSA
  endif

  ifeq ($(CONFIG_ESD),yes)
    AUDIO_SOURCES += esdaudio.c
    LOCAL_CFLAGS  += -DCONFIG_ESD
  endif

  ifeq ($(CONFIG_OSS),yes)
    AUDIO_SOURCES += ossaudio.c
    LOCAL_CFLAGS  += -DCONFIG_OSS
  endif

  LOCAL_SRC_FILES += $(AUDIO_SOURCES:%=audio/%)
endif  # !QEMU_AUDIO_LIB

LOCAL_CFLAGS += -I$(LOCAL_PATH)/audio
LOCAL_CFLAGS += -DHAS_AUDIO

# include emulated hardware source files
#
HW_SOURCES := android_arm.c  arm_boot.c  arm_pic.c  cdrom.c \
              goldfish_events_device.c  pci.c irq.c \
	      goldfish_nand.c  \
	      goldfish_audio.c goldfish_device.c goldfish_fb.c \
	      goldfish_interrupt.c goldfish_mmc.c goldfish_switch.c \
	      goldfish_memlog.c  goldfish_battery.c \
	      goldfish_timer.c  goldfish_tty.c scsi-disk.c \
	      smc91c111.c goldfish_trace.c usb-hid.c usb-hub.c usb-msd.c usb-ohci.c \
	      usb.c \
	      dma.c

LOCAL_SRC_FILES += $(HW_SOURCES:%=hw/%)
LOCAL_CFLAGS    += -I$(LOCAL_PATH)/hw

# include slirp code, i.e. the user-level networking stuff
#
SLIRP_SOURCES := bootp.c     cksum.c      debug.c  if.c     ip_icmp.c  ip_input.c   ip_output.c  \
                 mbuf.c      misc.c       sbuf.c   slirp.c  socket.c   tcp_input.c  tcp_output.c \
                 tcp_subr.c  tcp_timer.c  tftp.c   udp.c

LOCAL_SRC_FILES += $(SLIRP_SOURCES:%=slirp/%)
LOCAL_CFLAGS    += -I$(LOCAL_PATH)/slirp

# socket proxy support
#
PROXY_SOURCES := \
    proxy_common.c \
    proxy_http.c \
    proxy_http_connector.c \
    proxy_http_rewriter.c \

LOCAL_SRC_FILES += $(PROXY_SOURCES:%=proxy/%)
LOCAL_CFLAGS    += -I$(LOCAL_PATH)/proxy

# the linux-user sources, I doubt we really need these
#
#LINUX_SOURCES := main.c elfload.c mmap.c signal.c path.c syscall.c
#LOCAL_SRC_FILES += $(LINUX_SOURCES:%=linux-user/%)

# the skin support sources
#
SKIN_SOURCES := skin_rect.c \
                skin_region.c \
                skin_image.c \
                skin_trackball.c \
                skin_keyboard.c \
                skin_keyset.c \
                skin_file.c \
                skin_window.c \
                skin_scaler.c \
                skin_composer.c \
                skin_surface.c \

LOCAL_SRC_FILES += $(SKIN_SOURCES:%=skins/%)
LOCAL_CFLAGS    += -I$(LOCAL_PATH)/skins

ifeq ($(HOST_ARCH),x86)
# enable MMX code for our skin scaler
LOCAL_CFLAGS += -DUSE_MMX=1 -mmmx
endif

# include other sources
#
VL_SOURCES := vl.c osdep.c \
              block.c readline.c monitor.c console.c  loader.c sockets.c \
              block-qcow.c aes.c block-cloop.c block-dmg.c \
              cbuffer.c \
              gdbstub.c usb-linux.c \
              trace.c dcache.c varint.c vnc.c disas.c arm-dis.c \
              qemu_timers.c \
              shaper.c charpipe.c loadpng.c \
              framebuffer.c \
              android_debug.c \
              android_help.c \
              android_option.c \
              android_main.c \
              android_charmap.c \
              android_resource.c \
              android_utils.c \
              android_profile.c \
              android_console.c \
              android_events.c \
              android_timezone.c \
              android_config.c \
              android_gps.c \
              android_qemud.c \
              android_kmsg.c \
              android_hw_control.c \
              android/utils/ini.c \
              android/vm/hw-config.c \

ifeq ($(HOST_OS),linux)
    LOCAL_LDLIBS += -lX11
endif

ifeq ($(HOST_ARCH),x86)
    VL_SOURCES += i386-dis.c
endif
ifeq ($(HOST_ARCH),ppc)
    VL_SOURCES += ppc-dis.c
endif

ifeq ($(HOST_OS),windows)
  VL_SOURCES   += tap-win32.c
  LOCAL_LDLIBS += -mno-cygwin -mwindows -mconsole
endif

LOCAL_SRC_FILES += $(VL_SOURCES)

ifeq ($(HOST_OS),linux)
  LOCAL_LDLIBS += -lutil -lrt
endif

# add SDL-specific flags
#
SDL_CONFIG ?= $(PREBUILT)/sdl/bin/sdl-config
SDL_CFLAGS := $(shell $(SDL_CONFIG) --cflags)

# We need to filter out the _GNU_SOURCE variable because it breaks recent
# releases of Cygwin when using the -mno-cygwin option. Moreover, we don't
# need this macro at all to build the Android emulator.
SDL_CFLAGS := $(filter-out -D_GNU_SOURCE=1,$(SDL_CFLAGS))

LOCAL_CFLAGS += $(SDL_CFLAGS)
LOCAL_LDLIBS += $(filter-out %.a %.lib,$(shell $(SDL_CONFIG) --static-libs))
LOCAL_STATIC_LIBRARIES += libSDL libSDLmain

# on Windows, link the icon file as well into the executable
# unfortunately, our build system doesn't help us much, so we need
# to use some weird pathnames to make this work...
#
ifeq ($(HOST_OS),windows)
INTERMEDIATE     := $(call intermediates-dir-for,EXECUTABLES,$(LOCAL_MODULE),true)
ANDROID_ICON_OBJ := android_icon.o
ANDROID_ICON_PATH := $(LOCAL_PATH)/images
$(ANDROID_ICON_PATH)/$(ANDROID_ICON_OBJ): $(ANDROID_ICON_PATH)/android_icon.rc
	windres $< -I $(ANDROID_ICON_PATH) -o $@

# seems to be the only way to add an object file that was not generated from
# a C/C++/Java source file to our build system. and very unfortunately,
# $(TOPDIR)/$(LOCALPATH) will always be prepend to this value, which forces
# use to put the object file in the source directory...
#
LOCAL_PREBUILT_OBJ_FILES += images/$(ANDROID_ICON_OBJ)
endif

# other flags
LOCAL_CFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
LOCAL_LDLIBS += -lm -lpthread

ifeq ($(HOST_OS),windows)
    LOCAL_LDLIBS += -lwinmm -lws2_32 -liphlpapi
endif

LOCAL_LDLIBS += $(QEMU_AUDIO_LIB)

LOCAL_MODULE := emulator

include $(BUILD_HOST_EXECUTABLE)

endif  # TARGET_ARCH == arm
