Merge "Build arm and x86 binaries at the same time."
diff --git a/Makefile b/Makefile
index 7b23968..4f9bfec 100644
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,16 @@
 # limitations under the License.
 #
 
+# disable implicit rules
+.SUFFIXES:
+%:: %,v
+%:: RCS/%
+%:: RCS/%,v
+%:: s.%
+%:: SCCS/s.%
+%.c: %.w %.ch
+
+
 # this is a set of definitions that allow the usage of Makefile.android
 # even if we're not using the Android build system.
 #
@@ -23,7 +33,7 @@
 CONFIG_H     := $(OBJS_DIR)/config-host.h
 
 ifeq ($(wildcard $(CONFIG_MAKE)),)
-    $(error "The configuration file '$(CONFIG_MAKE)' doesnt' exist, please run the "rebuilt.sh" script)
+    $(error "The configuration file '$(CONFIG_MAKE)' doesnt' exist, please run the "android-configure.sh" script)
 endif
 
 include $(CONFIG_MAKE)
diff --git a/Makefile.android b/Makefile.android
index 1b44140..c70f537 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -17,7 +17,6 @@
 # determine the location of platform-specific directories
 #
 CONFIG_DIRS     := \
-    $(LOCAL_PATH)/android/config \
     $(LOCAL_PATH)/android/config/$(QEMU_HOST_TAG)
 
 ifeq ($(BUILD_STANDALONE_EMULATOR),true)
@@ -26,6 +25,9 @@
 
 CONFIG_INCLUDES := $(CONFIG_DIRS:%=-I%)
 
+ifneq ($(BUILD_STANDALONE_EMULATOR),true)
+endif # BUILD_STANDALONE_EMULATOR != true
+
 MY_CC := $(HOST_CC)
 
 MY_OPTIM := -O2 -g -fno-PIC -falign-functions=0 -fomit-frame-pointer
@@ -42,12 +44,10 @@
                  -fno-PIC -falign-functions=0
 endif
 
-MY_CFLAGS += -DCONFIG_MEMCHECK
-
 # prepare config.h for x86
-ifeq ($(TARGET_ARCH),x86)
-MY_CFLAGS += -DARCH_FLAGS_x86
-endif
+#ifeq ($(TARGET_ARCH),x86)
+#MY_CFLAGS += -DARCH_FLAGS_x86
+#endif
 
 MY_LDLIBS :=
 
@@ -96,6 +96,17 @@
 # adequate values for HOST_CC
 #
 ifneq ($(BUILD_STANDALONE_EMULATOR),true)
+  # On Linux, use our custom 32-bit host toolchain, which contains the
+  # relevant headers and 32-bit libraries for audio (The host 64-bit Lucid
+  # doesn't provide these anymore, only their 64-bit versions).
+  ifeq ($(HOST_OS),linux)
+    HOST_SDK_TOOLCHAIN_PREFIX := prebuilt/linux-x86/toolchain/i686-linux-glibc2.7-4.4.3/bin/i686-linux
+    # Don't do anything if the toolchain is not there
+    ifneq (,$(strip $(wildcard $(HOST_SDK_TOOLCHAIN_PREFIX)-gcc)))
+      MY_CC  := $(HOST_SDK_TOOLCHAIN_PREFIX)-gcc
+      MY_CXX := $(HOST_SDK_TOOLCHAIN_PREFIX)-g++
+    endif # $(HOST_SDK_TOOLCHAIN_PREFIX)-gcc exists
+  endif # HOST_OS == linux
 
   ifneq ($(USE_CCACHE),)
     ccache := prebuilt/$(HOST_PREBUILT_TAG)/ccache/ccache
@@ -119,751 +130,42 @@
 #
 MY_CFLAGS += -Wall -Wno-missing-field-initializers
 
-include $(CLEAR_VARS)
+# Needed to build fpu/softfloat-native.h properly
+MY_CFLAGS += -D_GNU_SOURCE=1
 
-###########################################################
-# Zlib configuration
+# A useful function that can be used to start the declaration of a host
+# module. Avoids repeating the same stuff again and again.
+# Usage:
 #
-ZLIB_DIR := distrib/zlib-1.2.3
-include $(LOCAL_PATH)/$(ZLIB_DIR)/sources.make
-
-###########################################################
-# Libpng configuration
+#  $(call start-emulator-library, <module-name>)
 #
-LIBPNG_DIR := distrib/libpng-1.2.19
-include $(LOCAL_PATH)/$(LIBPNG_DIR)/sources.make
-
-###############################################################################
-# build the TCG code generator
+#  ... declarations
 #
-include $(CLEAR_VARS)
-
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_CFLAGS                    := $(MY_CFLAGS) $(LOCAL_CFLAGS)
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-LOCAL_MODULE                    := emulator-tcg
-
-TCG_TARGET := $(HOST_ARCH)
-ifeq ($(TCG_TARGET),x86)
-  TCG_TARGET := i386
-endif
-
-TCG_CFLAGS := -I$(LOCAL_PATH)/tcg -I$(LOCAL_PATH)/tcg/$(TCG_TARGET)
-
-LOCAL_CFLAGS += $(TCG_CFLAGS) -DNEED_CPU_H \
-                -I$(LOCAL_PATH)/$(EMULATOR_TARGET_CPU) \
-                -I$(LOCAL_PATH)/fpu \
-
-LOCAL_SRC_FILES := \
-    tcg/tcg.c \
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-##############################################################################
-# build the HW emulation support
+#  $(call end-emulator-library)
 #
-include $(CLEAR_VARS)
+start-emulator-library = \
+    $(eval include $(CLEAR_VARS)) \
+    $(eval LOCAL_NO_DEFAULT_COMPILER_FLAGS := true) \
+    $(eval LOCAL_CC := $(MY_CC)) \
+    $(eval LOCAL_CXX := $(MY_CXX)) \
+    $(eval LOCAL_CFLAGS := $(MY_CFLAGS)) \
+    $(eval LOCAL_LDLIBS := $(MY_LDLIBS)) \
+    $(eval LOCAL_MODULE_TAGS := debug) \
+    $(eval LOCAL_MODULE := $1)
 
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-LOCAL_MODULE                    := emulator-hw
+# Used with start-emulator-library
+end-emulator-library = \
+    $(eval include $(BUILD_HOST_STATIC_LIBRARY))
 
-HW_CFLAGS    := -I$(LOCAL_PATH)/hw
+# A variant of start-emulator-library to start the definition of a host
+# program instead. Use with end-emulator-program
+start-emulator-program = $(call start-emulator-library,$1)
 
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) -DNEED_CPU_H
-LOCAL_CFLAGS += -I$(LOCAL_PATH)/$(EMULATOR_TARGET_CPU) -I$(LOCAL_PATH)/fpu $(HW_CFLAGS)
-LOCAL_CFLAGS += $(ZLIB_CFLAGS) -I$(LOCAL_PATH)/$(ZLIB_DIR)
-
-HW_SOURCES := \
-    bt.c \
-    bt-hci.c \
-    bt-hid.c \
-    bt-l2cap.c \
-    bt-sdp.c \
-    cdrom.c \
-    dma.c \
-    irq.c \
-    goldfish_device.c \
-    goldfish_events_device.c \
-    goldfish_fb.c \
-    goldfish_battery.c \
-    goldfish_mmc.c   \
-    goldfish_memlog.c \
-    goldfish_nand.c \
-    goldfish_tty.c \
-    msmouse.c \
-    pci.c \
-    qdev.c \
-    scsi-disk.c \
-    sysbus.c \
-    usb-hid.c \
-    usb-hub.c \
-    usb-msd.c \
-    usb-ohci.c \
-    usb.c \
-    watchdog.c \
-
-ifeq ($(TARGET_ARCH),arm)
-HW_SOURCES += android_arm.c \
-    arm_pic.c \
-    smc91c111.c \
-    goldfish_audio.c \
-    goldfish_interrupt.c \
-    goldfish_switch.c \
-    goldfish_timer.c \
-    goldfish_trace.c \
-
-endif
-
-ifeq ($(TARGET_ARCH),x86)
-HW_SOURCES += pc.c \
-    apic.c \
-    i8259.c \
-    mc146818rtc.c \
-    piix_pci.c \
-    i8254.c \
-    ne2000.c \
-    pckbd.c \
-    ioapic.c \
-    ps2.c \
-    smbios.c \
-    fw_cfg.c \
-
-endif
-
-LOCAL_SRC_FILES += $(HW_SOURCES:%=hw/%)
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-##############################################################################
-# build the ELF/DWARF stuff
-# This library is used by emulator's memory checker to extract debug information
-# from the symbol files when reporting memory allocation violations. In
-# particular, this library is used to extract routine name and source file
-# location for the code address where violation has been detected.
-#
-include $(CLEAR_VARS)
-
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-LOCAL_MODULE                    := emulator-elff
-LOCAL_CPP_EXTENSION             := .cc
-
-ELFF_CFLAGS    := -I$(LOCAL_PATH)/elff
-
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) -fno-exceptions
-LOCAL_CFLAGS += -I$(LOCAL_PATH)/$(EMULATOR_TARGET_CPU) -I$(LOCAL_PATH)/fpu $(ELFF_CFLAGS)
-LOCAL_CFLAGS += $(ZLIB_CFLAGS) -I$(LOCAL_PATH)/$(ZLIB_DIR)
-
-ELFF_SOURCES := \
-    dwarf_cu.cc \
-    dwarf_die.cc \
-    dwarf_utils.cc \
-    elf_alloc.cc \
-    elf_file.cc \
-    elf_mapped_section.cc \
-    elff_api.cc \
-
-LOCAL_SRC_FILES += $(ELFF_SOURCES:%=elff/%)
-ELFF_LDLIBS := -lstdc++
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-##############################################################################
-# build the memory access checking support
-# Memory access checker uses information collected by instrumented code in
-# libc.so in order to keep track of memory blocks allocated from heap. Memory
-# checker then uses this information to make sure that every access to allocated
-# memory is within allocated block. This information also allows detecting
-# memory leaks and attempts to free/realloc invalid pointers.
-#
-include $(CLEAR_VARS)
-
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-LOCAL_MODULE                    := emulator-memcheck
-
-MCHK_CFLAGS    := -I$(LOCAL_PATH)/memcheck -I$(LOCAL_PATH)/elff
-
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) -DNEED_CPU_H
-LOCAL_CFLAGS += -I$(LOCAL_PATH)/$(EMULATOR_TARGET_CPU) -I$(LOCAL_PATH)/fpu $(MCHK_CFLAGS)
-LOCAL_CFLAGS += $(ZLIB_CFLAGS) -I$(LOCAL_PATH)/$(ZLIB_DIR)
-
-MCHK_SOURCES := \
-    memcheck.c \
-    memcheck_proc_management.c \
-    memcheck_malloc_map.c \
-    memcheck_mmrange_map.c \
-    memcheck_util.c \
-
-LOCAL_SRC_FILES += $(MCHK_SOURCES:%=memcheck/%)
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-##############################################################################
-# build the CPU-specific emulation engine sources
-#
-include $(CLEAR_VARS)
-
-ifeq ($(TARGET_ARCH),arm)
-LOCAL_MODULE			:= emulator-arm
-endif
-ifeq ($(TARGET_ARCH),x86)
-LOCAL_MODULE			:= emulator-i386
-LOCAL_MODULE_TAGS		:= optional
-endif
-
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_MODULE                    := $(LOCAL_MODULE)
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-LOCAL_CFLAGS                    := $(MY_CFLAGS) $(LOCAL_CFLAGS)
-LOCAL_STATIC_LIBRARIES          := emulator-hw
-
-LOCAL_CFLAGS := -fno-PIC -fomit-frame-pointer -Wno-sign-compare
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS)
-
-LOCAL_CFLAGS += -I$(LOCAL_PATH) \
-                -I$(LOCAL_PATH)/$(EMULATOR_TARGET_CPU) \
-                -I$(LOCAL_PATH)/fpu \
-                $(TCG_CFLAGS) \
-                $(HW_CFLAGS) \
-                -DNEED_CPU_H \
-
-ifeq ($(HOST_ARCH),ppc)
-    LOCAL_CFLAGS += -D__powerpc__
-endif
-
-ifeq ($(TARGET_ARCH),arm)
-LOCAL_SRC_FILES += exec.c cpu-exec.c  \
-                   $(EMULATOR_TARGET_CPU)/op_helper.c \
-                   $(EMULATOR_TARGET_CPU)/iwmmxt_helper.c \
-                   $(EMULATOR_TARGET_CPU)/neon_helper.c \
-                   $(EMULATOR_TARGET_CPU)/helper.c \
-                   $(EMULATOR_TARGET_CPU)/translate.c \
-                   $(EMULATOR_TARGET_CPU)/machine.c \
-                   translate-all.c \
-                   hw/armv7m.c \
-                   hw/armv7m_nvic.c \
-                   arm-semi.c \
-                   trace.c \
-                   varint.c \
-                   dcache.c \
-                   softmmu_outside_jit.c \
-
-LOCAL_SRC_FILES += fpu/softfloat.c
-endif
-
-ifeq ($(TARGET_ARCH), x86)
-LOCAL_SRC_FILES += exec.c cpu-exec.c  \
-                   $(EMULATOR_TARGET_CPU)/op_helper.c \
-                   $(EMULATOR_TARGET_CPU)/helper.c \
-                   $(EMULATOR_TARGET_CPU)/translate.c \
-                   $(EMULATOR_TARGET_CPU)/machine.c \
-                   translate-all.c \
-                   trace.c \
-                   varint.c \
-                   dcache.c \
-                   softmmu_outside_jit.c \
-
-LOCAL_SRC_FILES += fpu/softfloat-native.c
-endif
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-##############################################################################
-# SDL-related definitions
-#
-
-# Build SDL from sources except on linux-x86, to avoid installing all
-# the X11 development packages on our build servers.
-#
-BUILD_SDL_FROM_SOURCES := true
-ifeq ($(QEMU_HOST_TAG),linux-x86)
-    BUILD_SDL_FROM_SOURCES := false
-endif
-ifeq ($(QEMU_HOST_TAG),darwin-x86)
-    BUILD_SDL_FROM_SOURCES := false
-endif
-ifeq ($(BUILD_STANDALONE_EMULATOR),true)
-    BUILD_SDL_FROM_SOURCES := true
-endif
-
-ifneq ($(SDL_CONFIG),)
-    BUILD_SDL_FROM_SOURCES := false
-endif
-
-ifneq ($(BUILD_SDL_FROM_SOURCES),true)
-
-SDL_CONFIG ?= prebuilt/$(QEMU_HOST_TAG)/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))
-SDL_LDLIBS := $(filter-out %.a %.lib,$(shell $(SDL_CONFIG) --static-libs))
-
-# Circular dependencies between libSDL and libSDLmain;
-# We repeat the libraries in the final link to work around it.
-SDL_STATIC_LIBRARIES := libSDL libSDLmain libSDL libSDLmain
-
-else   # !BUILD_STANDALONE_EMULATOR
-
-SDL_DIR := distrib/sdl-1.2.12
-include $(LOCAL_PATH)/$(SDL_DIR)/sources.make
-
-endif  # !BUILD_STANDALONE_EMULATOR
-
-##############################################################################
-# determine audio sources, build the prebuilt audio-library if needed
-#
-
-# determine AUDIO sources based on current configuration
-#
-AUDIO_SOURCES := audio.c noaudio.c wavaudio.c wavcapture.c mixeng.c
-AUDIO_CFLAGS  := -I$(LOCAL_PATH)/audio -DHAS_AUDIO
-AUDIO_LDLIBS  :=
-
-ifeq ($(HOST_OS),darwin)
-  CONFIG_COREAUDIO ?= yes
-  AUDIO_CFLAGS += -DHOST_BSD=1
-endif
-
-ifeq ($(HOST_OS),windows)
-  CONFIG_WINAUDIO ?= yes
-endif
-
-ifeq ($(HOST_OS),linux)
-  CONFIG_OSS  ?= yes
-  CONFIG_ALSA ?= yes
-  CONFIG_PULSEAUDIO ?= yes
-  CONFIG_ESD  ?= yes
-endif
-
-ifeq ($(HOST_OS),freebsd)
-  CONFIG_OSS ?= yes
-endif
-
-ifeq ($(CONFIG_COREAUDIO),yes)
-  AUDIO_SOURCES += coreaudio.c
-  AUDIO_CFLAGS  += -DCONFIG_COREAUDIO
-  AUDIO_LDLIBS  += -Wl,-framework,CoreAudio
-endif
-
-ifeq ($(CONFIG_WINAUDIO),yes)
-  AUDIO_SOURCES += winaudio.c
-  AUDIO_CFLAGS  += -DCONFIG_WINAUDIO
-endif
-
-ifeq ($(CONFIG_PULSEAUDIO),yes)
-  AUDIO_SOURCES += paaudio.c audio_pt_int.c
-  AUDIO_CFLAGS  += -DCONFIG_PULSEAUDIO
-endif
-
-ifeq ($(CONFIG_ALSA),yes)
-  AUDIO_SOURCES += alsaaudio.c audio_pt_int.c
-  AUDIO_CFLAGS  += -DCONFIG_ALSA
-endif
-
-ifeq ($(CONFIG_ESD),yes)
-  AUDIO_SOURCES += esdaudio.c
-  AUDIO_CFLAGS  += -DCONFIG_ESD
-endif
-
-ifeq ($(CONFIG_OSS),yes)
-  AUDIO_SOURCES += ossaudio.c
-  AUDIO_CFLAGS  += -DCONFIG_OSS
-endif
-
-AUDIO_SOURCES := $(call sort,$(AUDIO_SOURCES:%=audio/%))
-
-# determine whether we're going to use the prebuilt
-# audio library (this is useful on Linux to avoid requiring
-# all sound-related development packages to be installed on
-# the build and developer machines).
-#
-# note that you can define BUILD_QEMU_AUDIO_LIB to true
-# in your environment to force recompilation.
-#
-QEMU_AUDIO_LIB :=
-
-ifneq ($(BUILD_STANDALONE_EMULATOR),true)
-  QEMU_AUDIO_LIB := $(wildcard \
-	prebuilt/$(QEMU_HOST_TAG)/emulator/libqemu-audio.a)
-endif
-
-ifeq ($(BUILD_QEMU_AUDIO_LIB),true)
-  include $(CLEAR_VARS)
-  LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-  LOCAL_CC                        := $(MY_CC)
-  LOCAL_MODULE                    := libqemu-audio
-  LOCAL_LDLIBS                    := $(MY_LDLIBS)
-
-  LOCAL_CFLAGS := -Wno-sign-compare \
-                  -fno-strict-aliasing -W -Wall -Wno-unused-parameter \
-                  -I$(LOCAL_PATH) \
-                  -I$(LOCAL_PATH)/$(EMULATOR_TARGET_CPU) \
-                  -I$(LOCAL_PATH)/fpu \
-
-  # this is very important, otherwise the generated binaries may
-  # not link properly on our build servers
-  ifeq ($(HOST_OS),linux)
-    LOCAL_CFLAGS += -fno-stack-protector
-  endif
-
-  LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) $(AUDIO_CFLAGS)
-
-  LOCAL_CFLAGS += $(SDL_CFLAGS)
-
-  LOCAL_SRC_FILES += $(AUDIO_SOURCES)
-
-  include $(BUILD_HOST_STATIC_LIBRARY)
-  QEMU_AUDIO_LIB := $(LOCAL_BUILT_MODULE)
-
-endif  # !QEMU_AUDIO_LIB
-
-##############################################################################
-# Common CFLAGS for UI and Core builds
-
-# add the build ID to the default macro definitions
-UI_AND_CORE_CFLAGS := -DANDROID_BUILD_ID="$(strip $(BUILD_ID))-$(strip $(BUILD_NUMBER))"
-
-# For non-standalone builds, extract the major version number from the Android SDK
-# tools revision number.
-ifneq ($(BUILD_STANDALONE_EMULATOR),true)
-    ANDROID_SDK_TOOLS_REVISION := $(shell awk -F= '/Pkg.Revision/ { print $$2; }' sdk/files/tools_source.properties)
-endif
-
-ANDROID_SDK_TOOLS_REVISION := $(strip $(ANDROID_SDK_TOOLS_REVISION))
-ifdef ANDROID_SDK_TOOLS_REVISION
-    UI_AND_CORE_CFLAGS += -DANDROID_SDK_TOOLS_REVISION=$(ANDROID_SDK_TOOLS_REVISION)
-endif
-
-UI_AND_CORE_CFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
-
-
-##############################################################################
-# Build emulator core library.
-# This library contains "pure" emulation code separated from the intricacies
-# of the UI.
-#
-include $(CLEAR_VARS)
-
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-LOCAL_MODULE                    := emulator-core
-
-# don't remove the -fno-strict-aliasing, or you'll break things
-# (e.g. slirp-android/network support)
-#
-EMULATOR_CORE_CFLAGS := -fno-PIC -Wno-sign-compare \
-                -fno-strict-aliasing -g -W -Wall -Wno-unused-parameter
-
-# Needed by the upstream code
-EMULATOR_CORE_CFLAGS += -DNEED_CPU_H
-
-# Common includes for the emulator
-EMULATOR_CORE_CFLAGS += -I$(LOCAL_PATH)/ \
-                        -I$(LOCAL_PATH)/$(EMULATOR_TARGET_CPU) \
-                        -I$(LOCAL_PATH)/fpu \
-                        $(TCG_CFLAGS) \
-                        $(HW_CFLAGS) \
-
-# Required by block.c, default value is empty in upstream
-EMULATOR_CORE_CFLAGS += -DCONFIG_BDRV_WHITELIST=""
-
-# Required
-EMULATOR_CORE_CFLAGS += -D_XOPEN_SOURCE=600 -D_BSD_SOURCE=1
-# include slirp-android 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-android/%)
-EMULATOR_CORE_CFLAGS += -I$(LOCAL_PATH)/slirp-android
-
-# 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/%)
-EMULATOR_CORE_CFLAGS += -I$(LOCAL_PATH)/proxy
-
-# 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/%)
-EMULATOR_CORE_CFLAGS += -I$(LOCAL_PATH)/telephony
-
-# include android related stuff
-#
-ANDROID_SOURCES := qemu-setup.c
-LOCAL_SRC_FILES += $(ANDROID_SOURCES:%=android/%)
-
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) $(EMULATOR_CORE_CFLAGS)
-LOCAL_CFLAGS += $(UI_AND_CORE_CFLAGS)
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-##############################################################################
-# Build emulator UI library.
-# This library contains some emulator related UI components.
-#
-include $(CLEAR_VARS)
-
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-LOCAL_MODULE                    := emulator-uilib
-
-EMULATOR_UI_CFLAGS :=
-
-# include the SDL sources
-#
-
-# IMPORTANT: Normally, we should add SDLMAIN_SOURCES here, however this breaks
-#            the Linux mingw32 build. Apparently, the i586-mingw32-ld wants the
-#            implementation of _WinMain@16 to be in an object file on the final
-#            link command used to generate the executable, and will not search
-#            in the static libraries that are used to build it.
-#
-LOCAL_SRC_FILES += $(SDL_SOURCES) #$(SDLMAIN_SOURCES)
-EMULATOR_UI_CFLAGS += $(SDL_CFLAGS) -I$(LOCAL_PATH)/$(SDL_DIR)/include
-
-# the skin support sources
-#
-SKIN_SOURCES := rect.c \
-                region.c \
-                image.c \
-                trackball.c \
-                keyboard.c \
-                keyset.c \
-                file.c \
-                window.c \
-                scaler.c \
-                composer.c \
-                surface.c \
-
-LOCAL_SRC_FILES += $(SKIN_SOURCES:%=android/skin/%)
-EMULATOR_UI_CFLAGS += -I$(LOCAL_PATH)/skin
-
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) $(EMULATOR_CORE_CFLAGS) $(EMULATOR_UI_CFLAGS)
-
-LOCAL_MODULE_TAGS := debug
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-##############################################################################
-# lists of source files used to build the emulator core
-#
-
-# block sources
-#
-CORE_BLOCK_SOURCES = block.c \
-                     blockdev.c \
-                     block/qcow.c \
-                     block/qcow2.c \
-                     block/qcow2-refcount.c \
-                     block/qcow2-snapshot.c \
-                     block/qcow2-cluster.c \
-                     block/cloop.c \
-                     block/dmg.c \
-                     block/vvfat.c \
-                     block/raw.c
-ifeq ($(HOST_OS),windows)
-  CORE_BLOCK_SOURCES += block/raw-win32.c
-else
-  CORE_BLOCK_SOURCES += block/raw-posix.c
-endif
-
-# hw sources
-#
-ifeq ($(TARGET_ARCH),arm)
-CORE_HW_SOURCES = hw/arm_boot.c \
-                  hw/android_arm.c
-endif
-
-ifeq ($(TARGET_ARCH),x86)
-CORE_HW_SOURCES = hw/pc.c
-endif
-
-# migration sources
-#
-CORE_MIGRATION_SOURCES = iolooper-select.c
-ifeq ($(HOST_OS),windows)
-  CORE_MIGRATION_SOURCES += migration-dummy-android.c
-else
-  CORE_MIGRATION_SOURCES += migration.c \
-                            migration-exec.c \
-                            migration-tcp-android.c
-endif
-
-# misc. sources
-#
-CORE_MISC_SOURCES = vl-android.c \
-                    async.c \
-                    console.c \
-                    qemu-malloc.c \
-                    cutils.c \
-                    osdep.c \
-                    loader.c \
-                    monitor.c \
-                    readline.c \
-                    qemu-char-android.c \
-                    outputchannel.c \
-                    qemu-error.c \
-                    qerror.c \
-                    disas.c \
-                    aes.c \
-                    d3des.c \
-                    vnc-android.c \
-                    acl.c \
-                    keymaps.c \
-                    buffered_file.c \
-                    cbuffer.c \
-                    gdbstub.c \
-                    input.c \
-                    ioport.c \
-                    notify.c \
-                    path.c \
-                    shaper.c \
-                    charpipe.c \
-                    tcpdump.c \
-                    qemu-option.c \
-                    savevm.c \
-                    net-android.c \
-                    aio-android.c \
-                    dma-helpers.c \
-                    qemu-config.c \
-                    qemu-sockets-android.c \
-                    bt-host.c \
-                    bt-vhci.c \
-                    module.c \
-                    qemu-timer.c \
-                    android/boot-properties.c \
-                    android/hw-kmsg.c \
-                    android/hw-lcd.c \
-                    android/gps.c \
-                    android/hw-events.c \
-                    android/hw-control.c \
-                    android/console.c \
-                    android/hw-sensors.c \
-                    android/hw-qemud.c \
-                    android/core-init-utils.c   \
-                    android/config.c \
-                    android/snapshot.c \
-                    android/utils/timezone.c \
-
-ifeq ($(TARGET_ARCH),arm)
-    CORE_MISC_SOURCES += arm-dis.c
-endif
-
-ifeq ($(HOST_ARCH),x86)
-    CORE_MISC_SOURCES += i386-dis.c
-endif
-ifeq ($(HOST_ARCH),x86_64)
-    CORE_MISC_SOURCES += i386-dis.c
-endif
-ifeq ($(HOST_ARCH),ppc)
-    CORE_MISC_SOURCES += ppc-dis.c \
-                         cache-utils.c
-endif
-
-ifeq ($(HOST_OS),linux)
-    CORE_MISC_SOURCES += usb-linux.c \
-                         qemu-thread.c
-else
-    CORE_MISC_SOURCES += usb-dummy-android.c
-endif
-
-ifeq ($(HOST_OS),windows)
-  CORE_MISC_SOURCES   += tap-win32.c
-else
-  CORE_MISC_SOURCES   += posix-aio-compat.c
-endif
-
-# sources inherited from upstream, but not fully
-# integrated into android emulator
-#
-CORE_UPSTREAM_SOURCES = json-lexer.c \
-                        json-parser.c \
-                        json-streamer.c \
-                        qjson.c \
-                        qbool.c \
-                        qdict.c \
-                        qfloat.c \
-                        qint.c \
-                        qlist.c \
-                        qstring.c \
-
-
-CORE_SOURCES = $(CORE_BLOCK_SOURCES) $(CORE_HW_SOURCES)
-CORE_SOURCES += $(CORE_MIGRATION_SOURCES) $(CORE_MISC_SOURCES)
-CORE_SOURCES += $(CORE_UPSTREAM_SOURCES)
-
-CORE_SOURCES += android/audio-test.c
-
-##############################################################################
-# lists of source files used to build the emulator UI
-#
-
-UI_SOURCES = loadpng.c \
-             android/user-config.c \
-             android/resource.c \
-             android/qemulator.c \
-             android/keycode.c \
-             android/help.c \
-             android/avd/info.c \
-
-##############################################################################
-# lists of source files used by both, emulator UI and emulator core
-#
-
-UI_AND_CORE_SOURCES = \
-                      sockets.c \
-                      android/keycode-array.c \
-                      android/charmap.c \
-                      android/async-utils.c \
-                      android/sync-utils.c \
-                      android/async-console.c \
-                      android/utils/assert.c \
-                      android/utils/bufprint.c \
-                      android/utils/debug.c \
-                      android/utils/path.c \
-                      android/utils/dirscanner.c \
-                      android/utils/filelock.c \
-                      android/utils/mapfile.c \
-                      android/utils/misc.c \
-                      android/utils/panic.c \
-                      android/utils/reflist.c \
-                      android/utils/refset.c \
-                      android/utils/stralloc.c \
-                      android/utils/system.c \
-                      android/utils/tempfile.c \
-                      android/utils/vector.c \
-                      android/avd/hw-config.c \
-                      android/utils/ini.c \
-
-# include the Zlib sources
-#
-UI_AND_CORE_SOURCES += $(ZLIB_SOURCES)
-UI_AND_CORE_CFLAGS  += $(ZLIB_CFLAGS) -I$(LOCAL_PATH)/$(ZLIB_DIR)
+# A varient of end-emulator-library for host programs instead
+end-emulator-program = \
+    $(eval LOCAL_LDLIBS += $(QEMU_SYSTEM_LDLIBS)) \
+    $(eval include $(BUILD_HOST_EXECUTABLE))
 
-# include the Libpng sources
-#
-UI_AND_CORE_SOURCES += $(LIBPNG_SOURCES)
-UI_AND_CORE_CFLAGS  += $(LIBPNG_CFLAGS) -I$(LOCAL_PATH)/$(LIBPNG_DIR)
 
 # The common libraries
 #
@@ -890,471 +192,28 @@
   QEMU_SYSTEM_LDLIBS += -Wl,-framework,Cocoa
 endif
 
+include $(LOCAL_PATH)/Makefile.common
 
-##############################################################################
-# now build the emulator itself
-#
-include $(CLEAR_VARS)
+# corresponding to the product's target architecture.
 
-LOCAL_GENERATED_SOURCES :=
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_MODULE                    := emulator
-ifeq ($(TARGET_ARCH),arm)
-LOCAL_STATIC_LIBRARIES          := emulator-memcheck emulator-hw emulator-arm emulator-tcg
-endif
-ifeq ($(TARGET_ARCH),x86)
-LOCAL_STATIC_LIBRARIES          := emulator-memcheck emulator-hw emulator-i386 emulator-tcg
-endif
-LOCAL_STATIC_LIBRARIES          += emulator-elff
-LOCAL_STATIC_LIBRARIES          += emulator-core emulator-uilib
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) $(EMULATOR_CORE_CFLAGS) $(EMULATOR_UI_CFLAGS)
-
-# add the build ID to the default macro definitions
-LOCAL_CFLAGS += $(UI_AND_CORE_CFLAGS)
-
-# 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
-#
-ifeq ($(QEMU_AUDIO_LIB),)
-  LOCAL_SRC_FILES += $(AUDIO_SOURCES)
-endif  # !QEMU_AUDIO_LIB
-
-LOCAL_CFLAGS  += $(AUDIO_CFLAGS)
-LOCAL_LDLIBS  += $(AUDIO_LDLIBS)
-
-# 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/%)
-
-ifeq ($(HOST_ARCH),x86)
-# enable MMX code for our skin scaler
-LOCAL_CFLAGS += -DUSE_MMX=1 -mmmx -DNEED_CPU_H
-endif
-
-# include other sources
-#
-VL_SOURCES := android/framebuffer.c \
-              user-events-qemu.c \
-              android/cmdline-option.c \
-              android/display.c \
-              android/looper-qemu.c \
-              android/protocol/ui-commands-qemu.c \
-              android/protocol/core-commands-qemu.c \
-              android/main-common.c \
-              android/main.c \
-
-# Add common system libraries
-#
-LOCAL_LDLIBS += $(QEMU_SYSTEM_LDLIBS)
-
-LOCAL_SRC_FILES += $(VL_SOURCES) $(CORE_SOURCES) $(UI_SOURCES) $(UI_AND_CORE_SOURCES)
-
-# add SDL-specific flags
-#
-LOCAL_CFLAGS += $(SDL_CFLAGS)
-LOCAL_LDLIBS += $(SDL_LDLIBS)
-LOCAL_STATIC_LIBRARIES += $(SDL_STATIC_LIBRARIES)
-
-# add ELFF-specific flags
-#
-LOCAL_LDLIBS += $(ELFF_LDLIBS)
-
-# 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)
-
-# Locate windres executable
-WINDRES := windres
-ifneq ($(USE_MINGW),)
-  # When building the Windows emulator under Linux, use the MinGW one
-  WINDRES := i586-mingw32msvc-windres
-endif
-
-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 prepended to this value, which forces
-# us to put the object file in the source directory...
-#
-LOCAL_PREBUILT_OBJ_FILES += images/$(ANDROID_ICON_OBJ)
-endif
-
-# qemu-options.h is generated from qemu-options.hx with the "hxtool" shell script
-#
-intermediates := $(call intermediates-dir-for,EXECUTABLES,$(LOCAL_MODULE),true)
-
-QEMU_OPTIONS_H := $(intermediates)/qemu-options.h
-$(QEMU_OPTIONS_H): PRIVATE_PATH := $(LOCAL_PATH)
-$(QEMU_OPTIONS_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/hxtool -h < $< > $@
-$(QEMU_OPTIONS_H): $(LOCAL_PATH)/qemu-options.hx $(LOCAL_PATH)/hxtool
-	$(transform-generated-source)
-
-$(intermediates)/vl-android.o: $(QEMU_OPTIONS_H)
-
-LOCAL_GENERATED_SOURCES += $(QEMU_OPTIONS_H)
-
-# qemu-monitor.h is generated from qemu-monitor.hx with the "hxtool" shell script
-#
-intermediates := $(call intermediates-dir-for,EXECUTABLES,$(LOCAL_MODULE),true)
-
-QEMU_MONITOR_H := $(intermediates)/qemu-monitor.h
-$(QEMU_MONITOR_H): PRIVATE_PATH := $(LOCAL_PATH)
-$(QEMU_MONITOR_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/hxtool -h < $< > $@
-$(QEMU_MONITOR_H): $(LOCAL_PATH)/qemu-monitor.hx $(LOCAL_PATH)/hxtool
-	$(transform-generated-source)
-
-$(intermediates)/vl-android.o: $(QEMU_MONITOR_H)
-
-LOCAL_GENERATED_SOURCES += $(QEMU_MONITOR_H)
-
-
-# gdbstub-xml.c contains C-compilable arrays corresponding to the content
-# of $(LOCAL_PATH)/gdb-xml/, and is generated with the 'feature_to_c.sh' script.
-#
-ifeq ($(QEMU_TARGET_XML_SOURCES),)
-    QEMU_TARGET_XML_SOURCES := arm-core arm-neon arm-vfp arm-vfp3
-    QEMU_TARGET_XML_SOURCES := $(QEMU_TARGET_XML_SOURCES:%=$(LOCAL_PATH)/gdb-xml/%.xml)
-endif
-
-QEMU_GDBSTUB_XML_C := $(intermediates)/gdbstub-xml.c
-$(QEMU_GDBSTUB_XML_C): PRIVATE_PATH := $(LOCAL_PATH)
-$(QEMU_GDBSTUB_XML_C): PRIVATE_SOURCES := $(TARGET_XML_SOURCES)
-$(QEMU_GDBSTUB_XML_C): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/feature_to_c.sh $@ $(QEMU_TARGET_XML_SOURCES)
-$(QEMU_GDBSTUB_XML_C): $(QEMU_TARGET_XML_SOURCES) $(LOCAL_PATH)/feature_to_c.sh
-	$(hide) rm -f $@
-	$(transform-generated-source)
-
-$(intermediates)/vl-android.o: $(QEMU_GDBSTUB_XML_C)
-
-LOCAL_GENERATED_SOURCES += $(QEMU_GDBSTUB_XML_C)
-
-# hw-config-defs.h is generated from android/avd/hardware-properties.ini
-#
-QEMU_HARDWARE_PROPERTIES_INI := $(LOCAL_PATH)/android/avd/hardware-properties.ini
-QEMU_HW_CONFIG_DEFS_H := $(LOCAL_PATH)/android/avd/hw-config-defs.h
-$(QEMU_HW_CONFIG_DEFS_H): PRIVATE_PATH := $(LOCAL_PATH)
-$(QEMU_HW_CONFIG_DEFS_H): PRIVATE_SOURCES := $(QEMU_HARDWARE_PROPERTIES_INI)
-$(QEMU_HW_CONFIG_DEFS_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/android/tools/gen-hw-config.py $(QEMU_HARDWARE_PROPERTIES_INI) $@
-$(QEMU_HW_CONFIG_DEFS_H): $(QEMU_HARDWARE_PROPERTIES_INI) $(LOCAL_PATH)/android/tools/gen-hw-config.py
-	$(hide) rm -f $@
-	$(transform-generated-source)
-
-$(LOCAL_PATH)/android/avd/hw-config.h: $(QEMU_HW_CONFIG_DEFS_H)
-
-LOCAL_GENERATED_SOURCES += $(QEMU_HW_CONFIG_DEFS_H)
-
-# this is already done by the Android build system, but is done for the
-# benefit of the stand-alone one.
-#
 ifeq ($(BUILD_STANDALONE_EMULATOR),true)
-  LOCAL_CFLAGS += -I$(intermediates)
-endif
+    # For standalone builds, we want to build all variants of the emulator
+    # binaries. This makes it easier to catch target-specific regressions
+    # during emulator development.
+    EMULATOR_TARGET_ARCH := arm
+    include $(LOCAL_PATH)/Makefile.target
 
-LOCAL_LDLIBS += $(QEMU_AUDIO_LIB)
+    EMULATOR_TARGET_ARCH := x86
+    include $(LOCAL_PATH)/Makefile.target
 
-# Generate a completely static executable if needed.
-# Note that this means no sound and graphics on Linux.
-#
-ifeq ($(CONFIG_STATIC_EXECUTABLE),true)
-    LOCAL_SRC_FILES += dynlink-static.c
-    LOCAL_LDLIBS    += -static
-endif
+else # !BUILD_STANDALONE_EMULATOR
 
-LOCAL_MODULE := emulator
+    # On the other hand, for platform builds, only build the binaries
+    # corresponding to the build product's target architecture.
+    #
+    EMULATOR_TARGET_ARCH := $(TARGET_ARCH)
+    include $(LOCAL_PATH)/Makefile.target
 
-# See comment about SDLMAIN_SOURCES in the 'emulator-uilib' module declarations.
-LOCAL_SRC_FILES += $(SDLMAIN_SOURCES)
-
-include $(BUILD_HOST_EXECUTABLE)
-
-##############################################################################
-# Build standalone emulator core.
-#
-include $(CLEAR_VARS)
-
-LOCAL_GENERATED_SOURCES         :=
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_MODULE                    := qemu-android-$(TARGET_ARCH)
-ifeq ($(TARGET_ARCH),arm)
-LOCAL_STATIC_LIBRARIES          := emulator-memcheck emulator-hw emulator-arm emulator-tcg
-endif
-ifeq ($(TARGET_ARCH),x86)
-LOCAL_STATIC_LIBRARIES          := emulator-memcheck emulator-hw emulator-i386 emulator-tcg
-endif
-
-LOCAL_STATIC_LIBRARIES          += emulator-elff
-LOCAL_STATIC_LIBRARIES          += emulator-core
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) $(EMULATOR_CORE_CFLAGS)
-LOCAL_CFLAGS += $(UI_AND_CORE_CFLAGS) -DCONFIG_STANDALONE_CORE
-
-# 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
-#
-ifeq ($(QEMU_AUDIO_LIB),)
-  LOCAL_SRC_FILES += $(AUDIO_SOURCES)
-endif  # !QEMU_AUDIO_LIB
-
-LOCAL_CFLAGS  += $(AUDIO_CFLAGS)
-LOCAL_LDLIBS  += $(AUDIO_LDLIBS)
-
-# 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/%)
-
-# include other sources
-#
-VL_SOURCES := android/framebuffer.c \
-              user-events-qemu.c \
-              android/looper-qemu.c \
-              android/looper-generic.c \
-              android/display-core.c \
-              android/protocol/attach-ui-proxy.c \
-              android/protocol/fb-updates-proxy.c \
-              android/protocol/user-events-impl.c \
-              android/protocol/ui-commands-proxy.c \
-              android/protocol/core-commands-impl.c \
-
-# Add common system libraries
-#
-LOCAL_LDLIBS += $(QEMU_SYSTEM_LDLIBS)
-
-LOCAL_SRC_FILES += $(VL_SOURCES) $(CORE_SOURCES) $(UI_AND_CORE_SOURCES)
-
-# add ELFF-specific flags
-#
-LOCAL_LDLIBS += $(ELFF_LDLIBS)
-
-# 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)
-
-# Locate windres executable
-WINDRES := windres
-ifneq ($(USE_MINGW),)
-  # When building the Windows emulator under Linux, use the MinGW one
-  WINDRES := i586-mingw32msvc-windres
-endif
-
-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 prepended to this value, which forces
-# us to put the object file in the source directory...
-#
-LOCAL_PREBUILT_OBJ_FILES += images/$(ANDROID_ICON_OBJ)
-endif
-
-# qemu-options.h is generated from qemu-options.hx with the "hxtool" shell script
-#
-intermediates := $(call intermediates-dir-for,EXECUTABLES,$(LOCAL_MODULE),true)
-
-QEMU_OPTIONS_H := $(intermediates)/qemu-options.h
-$(QEMU_OPTIONS_H): PRIVATE_PATH := $(LOCAL_PATH)
-$(QEMU_OPTIONS_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/hxtool -h < $< > $@
-$(QEMU_OPTIONS_H): $(LOCAL_PATH)/qemu-options.hx $(LOCAL_PATH)/hxtool
-	$(transform-generated-source)
-
-$(intermediates)/vl-android.o: $(QEMU_OPTIONS_H)
-
-LOCAL_GENERATED_SOURCES += $(QEMU_OPTIONS_H)
-
-# qemu-monitor.h is generated from qemu-monitor.hx with the "hxtool" shell script
-#
-intermediates := $(call intermediates-dir-for,EXECUTABLES,$(LOCAL_MODULE),true)
-
-QEMU_MONITOR_H := $(intermediates)/qemu-monitor.h
-$(QEMU_MONITOR_H): PRIVATE_PATH := $(LOCAL_PATH)
-$(QEMU_MONITOR_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/hxtool -h < $< > $@
-$(QEMU_MONITOR_H): $(LOCAL_PATH)/qemu-monitor.hx $(LOCAL_PATH)/hxtool
-	$(transform-generated-source)
-
-$(intermediates)/vl-android.o: $(QEMU_MONITOR_H)
-
-LOCAL_GENERATED_SOURCES += $(QEMU_MONITOR_H)
-
-
-# gdbstub-xml.c contains C-compilable arrays corresponding to the content
-# of $(LOCAL_PATH)/gdb-xml/, and is generated with the 'feature_to_c.sh' script.
-#
-ifeq ($(QEMU_TARGET_XML_SOURCES),)
-    QEMU_TARGET_XML_SOURCES := arm-core arm-neon arm-vfp arm-vfp3
-    QEMU_TARGET_XML_SOURCES := $(QEMU_TARGET_XML_SOURCES:%=$(LOCAL_PATH)/gdb-xml/%.xml)
-endif
-
-QEMU_GDBSTUB_XML_C := $(intermediates)/gdbstub-xml.c
-$(QEMU_GDBSTUB_XML_C): PRIVATE_PATH := $(LOCAL_PATH)
-$(QEMU_GDBSTUB_XML_C): PRIVATE_SOURCES := $(TARGET_XML_SOURCES)
-$(QEMU_GDBSTUB_XML_C): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/feature_to_c.sh $@ $(QEMU_TARGET_XML_SOURCES)
-$(QEMU_GDBSTUB_XML_C): $(QEMU_TARGET_XML_SOURCES) $(LOCAL_PATH)/feature_to_c.sh
-	$(hide) rm -f $@
-	$(transform-generated-source)
-
-$(intermediates)/vl-android.o: $(QEMU_GDBSTUB_XML_C)
-
-LOCAL_GENERATED_SOURCES += $(QEMU_GDBSTUB_XML_C)
-
-# this is already done by the Android build system, but is done for the
-# benefit of the stand-alone one.
-#
-ifeq ($(BUILD_STANDALONE_EMULATOR),true)
-  LOCAL_CFLAGS += -I$(intermediates)
-endif
-
-# other flags
-ifneq ($(HOST_OS),windows)
-    LOCAL_LDLIBS += -ldl
-endif
-
-LOCAL_LDLIBS += $(QEMU_AUDIO_LIB)
-
-ifeq ($(HOST_OS),darwin)
-    FRAMEWORKS := OpenGL Cocoa QuickTime ApplicationServices Carbon IOKit
-    LOCAL_LDLIBS += $(FRAMEWORKS:%=-Wl,-framework,%)
-endif
-
-# Generate a completely static executable if needed.
-# Note that this means no sound and graphics on Linux.
-#
-ifeq ($(CONFIG_STATIC_EXECUTABLE),true)
-    LOCAL_SRC_FILES += dynlink-static.c
-    LOCAL_LDLIBS    += -static
-endif
-
-LOCAL_MODULE_TAGS := debug
-
-include $(BUILD_HOST_EXECUTABLE)
-
-##############################################################################
-# now build the emulator UI
-#
-include $(CLEAR_VARS)
-
-LOCAL_GENERATED_SOURCES :=
-LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
-LOCAL_CC                        := $(MY_CC)
-LOCAL_MODULE                    := emulator-ui
-LOCAL_STATIC_LIBRARIES          := emulator-uilib
-LOCAL_LDLIBS                    := $(MY_LDLIBS)
-
-LOCAL_CFLAGS := $(MY_CFLAGS) $(LOCAL_CFLAGS) $(EMULATOR_UI_CFLAGS) $(EMULATOR_CORE_CFLAGS)
-
-# add the build ID to the default macro definitions
-LOCAL_CFLAGS += $(UI_AND_CORE_CFLAGS) -DCONFIG_STANDALONE_UI
-
-ifeq ($(HOST_ARCH),x86)
-# enable MMX code for our skin scaler
-LOCAL_CFLAGS += -DUSE_MMX=1 -mmmx
-endif
-
-# include other sources
-#
-VL_SOURCES := android/framebuffer.c \
-              android/cmdline-option.c \
-              android/config.c \
-              android/looper-generic.c \
-              android/snapshot.c \
-              android/main-common.c \
-              android/main-ui.c \
-              vl-android-ui.c \
-              iolooper-select.c \
-              android/protocol/core-connection.c \
-              android/protocol/attach-ui-impl.c \
-              android/protocol/fb-updates-impl.c \
-              android/protocol/ui-commands-impl.c \
-              android/protocol/core-commands-proxy.c \
-              android/protocol/user-events-proxy.c \
-
-# Add common system libraries
-#
-LOCAL_LDLIBS += $(QEMU_SYSTEM_LDLIBS)
-
-LOCAL_SRC_FILES += $(VL_SOURCES) $(UI_SOURCES) $(UI_AND_CORE_SOURCES)
-
-# add SDL-specific flags
-#
-LOCAL_CFLAGS += $(SDL_CFLAGS)
-LOCAL_LDLIBS += $(SDL_LDLIBS)
-LOCAL_STATIC_LIBRARIES += $(SDL_STATIC_LIBRARIES)
-
-# 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)
-
-# Locate windres executable
-WINDRES := windres
-ifneq ($(USE_MINGW),)
-  # When building the Windows emulator under Linux, use the MinGW one
-  WINDRES := i586-mingw32msvc-windres
-endif
-
-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 prepended to this value, which forces
-# us to put the object file in the source directory...
-#
-LOCAL_PREBUILT_OBJ_FILES += images/$(ANDROID_ICON_OBJ)
-endif
-
-# this is already done by the Android build system, but is done for the
-# benefit of the stand-alone one.
-#
-ifeq ($(BUILD_STANDALONE_EMULATOR),true)
-  LOCAL_CFLAGS += -I$(intermediates)
-endif
-
-# Generate a completely static executable if needed.
-# Note that this means no sound and graphics on Linux.
-#
-ifeq ($(CONFIG_STATIC_EXECUTABLE),true)
-    LOCAL_SRC_FILES += dynlink-static.c
-    LOCAL_LDLIBS    += -static
-endif
-
-# See comment about SDLMAIN_SOURCES in the 'emulator-uilib' module declarations.
-LOCAL_SRC_FILES += $(SDLMAIN_SOURCES)
-
-include $(BUILD_HOST_EXECUTABLE)
+endif # !BUILD_STANDALONE_EMULATOR
 
 endif  # TARGET_ARCH == arm || TARGET_ARCH == x86
diff --git a/Makefile.common b/Makefile.common
new file mode 100644
index 0000000..72e2824
--- /dev/null
+++ b/Makefile.common
@@ -0,0 +1,592 @@
+# When building this project, we actually generate several components which
+# are the following:
+#
+#  - the emulator-ui program (which is target-agnostic)
+#  - the target-specific qemu-android-$ARCH programs (headless emulation engines)
+#  - the "standalone" emulator programs (embed both UI and engine in a single
+#    binary and process), i.e. "emulator" for ARM and "emulator-x86" for x86.
+#
+# This file defines static host libraries that will be used by at least two
+# of these components.
+#
+
+##############################################################################
+##############################################################################
+###
+###  emulator-common: LIBRARY OF COMMON FUNCTIONS
+###
+###  THESE ARE POTENTIALLY USED BY ALL COMPONENTS
+###
+
+$(call start-emulator-library, emulator-common)
+
+EMULATOR_COMMON_CFLAGS :=
+
+# Needed by everything about the host
+EMULATOR_COMMON_CFLAGS += \
+    -I$(LOCAL_PATH)/android/config/$(HOST_PREBUILT_TAG)
+
+# add the build ID to the default macro definitions
+ifeq ($(BUILD_STANDALONE_EMULATOR),)
+EMULATOR_COMMON_CFLAGS += -DANDROID_BUILD_ID="$(strip $(BUILD_ID))-$(strip $(BUILD_NUMBER))"
+endif
+
+# For non-standalone builds, extract the major version number from the Android SDK
+# tools revision number.
+ifneq ($(BUILD_STANDALONE_EMULATOR),true)
+    ANDROID_SDK_TOOLS_REVISION := $(shell awk -F= '/Pkg.Revision/ { print $$2; }' sdk/files/tools_source.properties)
+endif
+
+ANDROID_SDK_TOOLS_REVISION := $(strip $(ANDROID_SDK_TOOLS_REVISION))
+ifdef ANDROID_SDK_TOOLS_REVISION
+    EMULATOR_COMMON_CFLAGS += -DANDROID_SDK_TOOLS_REVISION=$(ANDROID_SDK_TOOLS_REVISION)
+endif
+
+# Enable large-file support (i.e. make off_t a 64-bit value)
+ifeq ($(HOST_OS),linux)
+EMULATOR_COMMON_CFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
+endif
+
+###########################################################
+# Zlib sources
+#
+ZLIB_DIR := distrib/zlib-1.2.3
+include $(LOCAL_PATH)/$(ZLIB_DIR)/sources.make
+EMULATOR_COMMON_CFLAGS += -I$(LOCAL_PATH)/$(ZLIB_DIR)
+
+LOCAL_SRC_FILES += $(ZLIB_SOURCES)
+
+###########################################################
+# Android utility functions
+#
+LOCAL_SRC_FILES += \
+	sockets.c \
+	iolooper-select.c \
+	android/async-console.c \
+	android/async-utils.c \
+	android/charmap.c \
+	android/framebuffer.c \
+	android/keycode-array.c \
+	android/avd/hw-config.c \
+	android/avd/info.c \
+	android/sync-utils.c \
+	android/utils/assert.c \
+	android/utils/bufprint.c \
+	android/utils/debug.c \
+	android/utils/dirscanner.c \
+	android/utils/filelock.c \
+	android/utils/ini.c \
+	android/utils/mapfile.c \
+	android/utils/misc.c \
+	android/utils/panic.c \
+	android/utils/path.c \
+	android/utils/reflist.c \
+	android/utils/refset.c \
+	android/utils/stralloc.c \
+	android/utils/system.c \
+	android/utils/tempfile.c \
+	android/utils/vector.c \
+
+# qemu-options.h is generated from qemu-options.hx with the "hxtool" shell script
+#
+intermediates := $(call intermediates-dir-for,STATIC_LIBRARIES,$(LOCAL_MODULE),true)
+
+QEMU_OPTIONS_H := $(intermediates)/qemu-options.h
+$(QEMU_OPTIONS_H): PRIVATE_PATH := $(LOCAL_PATH)
+$(QEMU_OPTIONS_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/hxtool -h < $< > $@
+$(QEMU_OPTIONS_H): $(LOCAL_PATH)/qemu-options.hx $(LOCAL_PATH)/hxtool
+	$(transform-generated-source)
+
+LOCAL_GENERATED_SOURCES += $(QEMU_OPTIONS_H)
+
+LOCAL_CFLAGS += $(EMULATOR_COMMON_CFLAGS)
+
+$(call end-emulator-library)
+
+##############################################################################
+##############################################################################
+###
+###  emulator-libui: LIBRARY OF UI-RELATED FUNCTIONS
+###
+###  THESE ARE USED BY 'emulator-ui' AND THE STANDALONE PROGRAMS
+###
+
+$(call start-emulator-library, emulator-libui)
+
+EMULATOR_LIBUI_CFLAGS :=
+
+LOCAL_CFLAGS += $(EMULATOR_COMMON_CFLAGS)
+
+###########################################################
+# Libpng configuration
+#
+LIBPNG_DIR := distrib/libpng-1.2.19
+include $(LOCAL_PATH)/$(LIBPNG_DIR)/sources.make
+
+EMULATOR_LIBUI_CFLAGS += \
+    $(LIBPNG_CFLAGS) \
+    -I$(LOCAL_PATH)/$(LIBPNG_DIR)
+
+LOCAL_SRC_FILES += $(LIBPNG_SOURCES) loadpng.c
+
+##############################################################################
+# SDL-related definitions
+#
+
+SDL_DIR := distrib/sdl-1.2.12
+include $(LOCAL_PATH)/$(SDL_DIR)/sources.make
+LOCAL_SRC_FILES += $(SDL_SOURCES)
+EMULATOR_LIBUI_CFLAGS += \
+    $(SDL_CFLAGS) \
+    -I$(LOCAL_PATH)/android/config/$(HOST_PREBUILT_TAG) \
+    -I$(LOCAL_PATH)/$(SDL_DIR)/include
+
+EMULATOR_LIBUI_LDLIBS += $(SDL_LDLIBS)
+
+# The following is needed by SDL_LoadObject
+ifneq ($(HOST_OS),windows)
+    EMULATOR_LIBUI_LDLIBS += -ldl
+endif
+
+# the skin support sources
+#
+SKIN_SOURCES := rect.c \
+                region.c \
+                image.c \
+                trackball.c \
+                keyboard.c \
+                keyset.c \
+                file.c \
+                window.c \
+                scaler.c \
+                composer.c \
+                surface.c \
+
+LOCAL_SRC_FILES += $(SKIN_SOURCES:%=android/skin/%)
+
+LOCAL_SRC_FILES += \
+             android/user-config.c \
+             android/resource.c \
+             android/qemulator.c \
+             android/keycode.c \
+
+# enable MMX code for our skin scaler
+ifeq ($(HOST_ARCH),x86)
+LOCAL_CFLAGS += -DUSE_MMX=1 -mmmx
+endif
+
+LOCAL_CFLAGS += $(EMULATOR_LIBUI_CFLAGS)
+
+$(call end-emulator-library)
+
+##############################################################################
+##############################################################################
+###
+###  emulator-libqemu: TARGET-INDEPENDENT QEMU FUNCTIONS
+###
+###  THESE ARE USED BY EVERYTHING EXCEPT 'emulator-ui'
+###
+
+$(call start-emulator-library, emulator-libqemu)
+
+EMULATOR_LIBQEMU_CFLAGS :=
+
+LOCAL_CFLAGS += $(EMULATOR_COMMON_CFLAGS)
+
+AUDIO_SOURCES := noaudio.c wavaudio.c wavcapture.c mixeng.c
+AUDIO_CFLAGS  := -I$(LOCAL_PATH)/audio -DHAS_AUDIO
+AUDIO_LDLIBS  :=
+
+LOCAL_CFLAGS += -Wall -Wno-missing-field-initializers
+
+ifeq ($(HOST_OS),darwin)
+  CONFIG_COREAUDIO ?= yes
+  AUDIO_CFLAGS += -DHOST_BSD=1
+endif
+
+ifeq ($(HOST_OS),windows)
+  CONFIG_WINAUDIO ?= yes
+endif
+
+ifeq ($(HOST_OS),linux)
+  CONFIG_OSS  ?= yes
+  CONFIG_ALSA ?= yes
+  CONFIG_PULSEAUDIO ?= yes
+  CONFIG_ESD  ?= yes
+endif
+
+ifeq ($(HOST_OS),freebsd)
+  CONFIG_OSS ?= yes
+endif
+
+ifeq ($(CONFIG_COREAUDIO),yes)
+  AUDIO_SOURCES += coreaudio.c
+  AUDIO_CFLAGS  += -DCONFIG_COREAUDIO
+  AUDIO_LDLIBS  += -Wl,-framework,CoreAudio
+endif
+
+ifeq ($(CONFIG_WINAUDIO),yes)
+  AUDIO_SOURCES += winaudio.c
+  AUDIO_CFLAGS  += -DCONFIG_WINAUDIO
+endif
+
+ifeq ($(CONFIG_PULSEAUDIO),yes)
+  AUDIO_SOURCES += paaudio.c audio_pt_int.c
+  AUDIO_CFLAGS  += -DCONFIG_PULSEAUDIO
+endif
+
+ifeq ($(CONFIG_ALSA),yes)
+  AUDIO_SOURCES += alsaaudio.c audio_pt_int.c
+  AUDIO_CFLAGS  += -DCONFIG_ALSA
+endif
+
+ifeq ($(CONFIG_ESD),yes)
+  AUDIO_SOURCES += esdaudio.c
+  AUDIO_CFLAGS  += -DCONFIG_ESD
+endif
+
+ifeq ($(CONFIG_OSS),yes)
+  AUDIO_SOURCES += ossaudio.c
+  AUDIO_CFLAGS  += -DCONFIG_OSS
+endif
+
+AUDIO_SOURCES := $(call sort,$(AUDIO_SOURCES:%=audio/%))
+
+LOCAL_CFLAGS += -Wno-sign-compare \
+                -fno-strict-aliasing -W -Wall -Wno-unused-parameter \
+
+# this is very important, otherwise the generated binaries may
+# not link properly on our build servers
+ifeq ($(HOST_OS),linux)
+LOCAL_CFLAGS += -fno-stack-protector
+endif
+
+LOCAL_SRC_FILES += $(AUDIO_SOURCES)
+LOCAL_SRC_FILES += \
+    android/audio-test.c
+
+# other flags
+ifneq ($(HOST_OS),windows)
+    AUDIO_LDLIBS += -ldl
+else
+endif
+
+
+EMULATOR_LIBQEMU_CFLAGS += $(AUDIO_CFLAGS)
+EMULATOR_LIBQEMU_LDLIBS += $(AUDIO_LDLIBS)
+
+LOCAL_CFLAGS += -Wno-missing-field-initializers
+
+# migration sources
+#
+ifeq ($(HOST_OS),windows)
+  LOCAL_SRC_FILES += migration-dummy-android.c
+else
+  LOCAL_SRC_FILES += migration.c \
+                     migration-exec.c \
+                     migration-tcp-android.c
+endif
+
+# misc. sources
+#
+CORE_MISC_SOURCES = \
+    acl.c \
+    aes.c \
+    aio-android.c \
+    async.c \
+    bt-host.c \
+    bt-vhci.c \
+    buffered_file.c \
+    cbuffer.c \
+    charpipe.c \
+    console.c \
+    cutils.c \
+    d3des.c \
+    input.c \
+    ioport.c \
+    module.c \
+    net-android.c \
+    notify.c \
+    osdep.c \
+    outputchannel.c \
+    path.c \
+    qemu-char-android.c \
+    qemu-config.c \
+    qemu-error.c \
+    qemu-malloc.c \
+    qemu-option.c \
+    qemu-sockets-android.c \
+    qerror.c \
+    readline.c \
+    savevm.c \
+    shaper.c \
+    tcpdump.c \
+    vnc-android.c \
+    android/boot-properties.c \
+    android/config.c \
+    android/core-init-utils.c   \
+    android/gps.c \
+    android/hw-kmsg.c \
+    android/hw-lcd.c \
+    android/hw-events.c \
+    android/hw-control.c \
+    android/hw-sensors.c \
+    android/hw-qemud.c \
+    android/looper-qemu.c \
+    android/qemu-setup.c \
+    android/snapshot.c \
+    android/utils/timezone.c \
+
+ifeq ($(HOST_ARCH),x86)
+    CORE_MISC_SOURCES += i386-dis.c
+endif
+ifeq ($(HOST_ARCH),x86_64)
+    CORE_MISC_SOURCES += i386-dis.c
+endif
+ifeq ($(HOST_ARCH),ppc)
+    CORE_MISC_SOURCES += ppc-dis.c \
+                         cache-utils.c
+endif
+
+ifeq ($(HOST_OS),linux)
+    CORE_MISC_SOURCES += usb-linux.c \
+                         qemu-thread.c
+else
+    CORE_MISC_SOURCES += usb-dummy-android.c
+endif
+
+ifeq ($(HOST_OS),windows)
+  CORE_MISC_SOURCES   += tap-win32.c
+else
+  CORE_MISC_SOURCES   += posix-aio-compat.c
+endif
+
+LOCAL_SRC_FILES += $(CORE_MISC_SOURCES)
+
+# Required
+LOCAL_CFLAGS += -D_XOPEN_SOURCE=600 -D_BSD_SOURCE=1
+
+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-android/%)
+EMULATOR_LIBQEMU_CFLAGS += -I$(LOCAL_PATH)/slirp-android
+
+# 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/%)
+EMULATOR_LIBQEMU_CFLAGS += -I$(LOCAL_PATH)/proxy
+
+# 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/%)
+EMULATOR_LIBQEMU_CFLAGS += -I$(LOCAL_PATH)/telephony
+
+# sources inherited from upstream, but not fully
+# integrated into android emulator
+#
+LOCAL_SRC_FILES += \
+    json-lexer.c \
+    json-parser.c \
+    json-streamer.c \
+    qjson.c \
+    qbool.c \
+    qdict.c \
+    qfloat.c \
+    qint.c \
+    qlist.c \
+    qstring.c \
+
+# qemu-options.h is generated from qemu-options.hx with the "hxtool" shell script
+#
+intermediates := $(call intermediates-dir-for,STATIC_LIBRARIES,$(LOCAL_MODULE),true)
+
+QEMU_OPTIONS_H := $(intermediates)/qemu-options.h
+$(QEMU_OPTIONS_H): PRIVATE_PATH := $(LOCAL_PATH)
+$(QEMU_OPTIONS_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/hxtool -h < $< > $@
+$(QEMU_OPTIONS_H): $(LOCAL_PATH)/qemu-options.hx $(LOCAL_PATH)/hxtool
+	$(transform-generated-source)
+
+# qemu-monitor.h is generated from qemu-monitor.hx with the "hxtool" shell script
+#
+intermediates := $(call intermediates-dir-for,STATIC_LIBRARIES,$(LOCAL_MODULE),true)
+
+QEMU_MONITOR_H := $(intermediates)/qemu-monitor.h
+$(QEMU_MONITOR_H): PRIVATE_PATH := $(LOCAL_PATH)
+$(QEMU_MONITOR_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/hxtool -h < $< > $@
+$(QEMU_MONITOR_H): $(LOCAL_PATH)/qemu-monitor.hx $(LOCAL_PATH)/hxtool
+	$(transform-generated-source)
+
+# hw-config-defs.h is generated from android/avd/hardware-properties.ini
+#
+QEMU_HARDWARE_PROPERTIES_INI := $(LOCAL_PATH)/android/avd/hardware-properties.ini
+QEMU_HW_CONFIG_DEFS_H := $(LOCAL_PATH)/android/avd/hw-config-defs.h
+$(QEMU_HW_CONFIG_DEFS_H): PRIVATE_PATH := $(LOCAL_PATH)
+$(QEMU_HW_CONFIG_DEFS_H): PRIVATE_SOURCES := $(QEMU_HARDWARE_PROPERTIES_INI)
+$(QEMU_HW_CONFIG_DEFS_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/android/tools/gen-hw-config.py $(QEMU_HARDWARE_PROPERTIES_INI) $@
+$(QEMU_HW_CONFIG_DEFS_H): $(QEMU_HARDWARE_PROPERTIES_INI) $(LOCAL_PATH)/android/tools/gen-hw-config.py
+	$(hide) rm -f $@
+	$(transform-generated-source)
+
+$(LOCAL_PATH)/android/avd/hw-config.h: $(QEMU_HW_CONFIG_DEFS_H)
+
+# gdbstub-xml.c contains C-compilable arrays corresponding to the content
+# of $(LOCAL_PATH)/gdb-xml/, and is generated with the 'feature_to_c.sh' script.
+#
+intermediates := $(call intermediates-dir-for,STATIC_LIBRARIES,$(LOCAL_MODULE),true)
+
+ifeq ($(QEMU_TARGET_XML_SOURCES),)
+    QEMU_TARGET_XML_SOURCES := arm-core arm-neon arm-vfp arm-vfp3
+    QEMU_TARGET_XML_SOURCES := $(QEMU_TARGET_XML_SOURCES:%=$(LOCAL_PATH)/gdb-xml/%.xml)
+endif
+
+QEMU_GDBSTUB_XML_C := $(intermediates)/gdbstub-xml.c
+$(QEMU_GDBSTUB_XML_C): PRIVATE_PATH := $(LOCAL_PATH)
+$(QEMU_GDBSTUB_XML_C): PRIVATE_SOURCES := $(TARGET_XML_SOURCES)
+$(QEMU_GDBSTUB_XML_C): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/feature_to_c.sh $@ $(QEMU_TARGET_XML_SOURCES)
+$(QEMU_GDBSTUB_XML_C): $(QEMU_TARGET_XML_SOURCES) $(LOCAL_PATH)/feature_to_c.sh
+	$(hide) rm -f $@
+	$(transform-generated-source)
+
+LOCAL_GENERATED_SOURCES += $(QEMU_GDBSTUB_XML_C)
+
+EMULATOR_LIBQEMU_CFLAGS += -I$(intermediates)
+
+LOCAL_CFLAGS += $(EMULATOR_LIBQEMU_CFLAGS)
+
+$(call end-emulator-library)
+
+# Block sources, we must compile them with each executable because they
+# are only referenced by the rest of the code using constructor functions.
+# If their object files are put in a static library, these are never compiled
+# into the final linked executable that uses them.
+#
+# Normally, one would solve thus using LOCAL_WHOLE_STATIC_LIBRARIES, but
+# the Darwin linker doesn't support -Wl,--whole-archive or equivalent :-(
+#
+BLOCK_SOURCES += \
+    block.c \
+    blockdev.c \
+    block/qcow.c \
+    block/qcow2.c \
+    block/qcow2-refcount.c \
+    block/qcow2-snapshot.c \
+    block/qcow2-cluster.c \
+    block/cloop.c \
+    block/dmg.c \
+    block/vvfat.c \
+    block/raw.c
+
+ifeq ($(HOST_OS),windows)
+    BLOCK_SOURCES += block/raw-win32.c
+else
+    BLOCK_SOURCES += block/raw-posix.c
+endif
+
+BLOCK_CFLAGS += $(EMULATOR_COMMON_CFLAGS)
+BLOCK_CFLAGS += -DCONFIG_BDRV_WHITELIST=""
+
+
+##############################################################################
+##############################################################################
+###
+###  emulator-libelff: TARGET-INDEPENDENT ELF/DWARD PARSER
+###
+###  THESE ARE USED BY EVERYTHING EXCEPT 'emulator-ui', BUT WE CANNOT PUT
+###  THEM IN emulator-libqemu SINCE THE SOURCES ARE C++
+###
+
+$(call start-emulator-library, emulator-libelff)
+
+LOCAL_CPP_EXTENSION := .cc
+
+ELFF_CFLAGS := -I$(LOCAL_PATH)/elff
+ELFF_LDLIBS := -lstdc++
+
+ELFF_SOURCES := \
+    dwarf_cu.cc \
+    dwarf_die.cc \
+    dwarf_utils.cc \
+    elf_alloc.cc \
+    elf_file.cc \
+    elf_mapped_section.cc \
+    elff_api.cc \
+
+LOCAL_SRC_FILES += $(ELFF_SOURCES:%=elff/%)
+
+LOCAL_CFLAGS += \
+    -fno-exceptions \
+    $(ELFF_CFLAGS) \
+
+$(call end-emulator-library)
+
+##############################################################################
+##############################################################################
+###
+###  emulator-ui: UI FRONT-END PROGRAM
+###
+###
+$(call start-emulator-program, emulator-ui)
+
+LOCAL_STATIC_LIBRARIES := \
+    emulator-common \
+    emulator-libui \
+    emulator-common \
+
+
+LOCAL_CFLAGS += -DCONFIG_STANDALONE_UI=1
+
+LOCAL_CFLAGS += $(EMULATOR_COMMON_CFLAGS) $(EMULATOR_LIBUI_CFLAGS)
+LOCAL_LDLIBS += $(EMULATOR_COMMON_LDLIBS) $(EMULATOR_LIBUI_LDLIBS)
+
+LOCAL_SRC_FILES := \
+    android/cmdline-option.c \
+    android/config.c \
+    android/help.c \
+    android/looper-generic.c \
+    android/snapshot.c \
+    android/main-common.c \
+    android/main-ui.c \
+    vl-android-ui.c \
+    android/protocol/core-connection.c \
+    android/protocol/attach-ui-impl.c \
+    android/protocol/fb-updates-impl.c \
+    android/protocol/ui-commands-impl.c \
+    android/protocol/core-commands-proxy.c \
+    android/protocol/user-events-proxy.c \
+
+LOCAL_SRC_FILES += $(SDLMAIN_SOURCES)
+
+$(call end-emulator-program)
diff --git a/Makefile.target b/Makefile.target
new file mode 100644
index 0000000..57533c3
--- /dev/null
+++ b/Makefile.target
@@ -0,0 +1,352 @@
+# This file is included several times to build target-specific
+# modules for the Android emulator. It will be called several times
+# (e.g. once for the arm target, and once for the x86 target).
+#
+
+ifndef EMULATOR_TARGET_ARCH
+$(error EMULATOR_TARGET_ARCH is not defined!)
+endif
+
+EMULATOR_TARGET_CPU := $(EMULATOR_TARGET_ARCH)
+ifeq ($(EMULATOR_TARGET_CPU),x86)
+  EMULATOR_TARGET_CPU := i386
+endif
+
+##############################################################################
+##############################################################################
+###
+###  emulator-target-$CPU: Code generator depends on the target CPU
+###
+###
+
+# Common compiler flags for all target-dependent libraries
+EMULATOR_TARGET_CFLAGS := \
+    -I$(LOCAL_PATH)/android/config/$(HOST_PREBUILT_TAG) \
+    -I$(LOCAL_PATH)/android/config/target-$(EMULATOR_TARGET_ARCH) \
+    -I$(LOCAL_PATH)/target-$(EMULATOR_TARGET_CPU) \
+    -I$(LOCAL_PATH)/fpu \
+    -DNEED_CPU_H \
+
+TCG_TARGET := $(HOST_ARCH)
+ifeq ($(TCG_TARGET),x86)
+  TCG_TARGET := i386
+endif
+
+EMULATOR_TARGET_CFLAGS += \
+    -I$(LOCAL_PATH)/tcg \
+    -I$(LOCAL_PATH)/tcg/$(TCG_TARGET)
+
+
+$(call start-emulator-library, emulator-target-$(EMULATOR_TARGET_CPU))
+
+# The following is to ensure that "config.h" will map to a target-specific
+# configuration file header.
+LOCAL_CFLAGS += $(EMULATOR_TARGET_CFLAGS)
+
+LOCAL_SRC_FILES += \
+    tcg/tcg.c \
+
+##############################################################################
+# Emulated hardware devices.
+#
+
+HW_SOURCES := \
+    bt.c \
+    bt-hci.c \
+    bt-hid.c \
+    bt-l2cap.c \
+    bt-sdp.c \
+    cdrom.c \
+    dma.c \
+    irq.c \
+    goldfish_device.c \
+    goldfish_events_device.c \
+    goldfish_fb.c \
+    goldfish_battery.c \
+    goldfish_mmc.c   \
+    goldfish_memlog.c \
+    goldfish_nand.c \
+    goldfish_tty.c \
+    msmouse.c \
+    pci.c \
+    qdev.c \
+    scsi-disk.c \
+    sysbus.c \
+    usb-hid.c \
+    usb-hub.c \
+    usb-msd.c \
+    usb-ohci.c \
+    usb.c \
+    watchdog.c
+
+ifeq ($(EMULATOR_TARGET_ARCH),arm)
+HW_SOURCES += android_arm.c \
+    arm_pic.c \
+    goldfish_audio.c \
+    goldfish_interrupt.c \
+    goldfish_switch.c \
+    goldfish_timer.c \
+    goldfish_trace.c \
+    arm_boot.c \
+
+# The following sources must be compiled with the final executables
+# because they contain device_init() or machine_init() statements.
+HW_OBJ_SOURCES := hw/smc91c111.c
+HW_OBJ_CFLAGS  := $(EMULATOR_TARGET_CFLAGS)
+
+LOCAL_SRC_FILES += arm-dis.c
+
+# smc91c111.c requires <zlib.h>
+LOCAL_CFLAGS += $(ZLIB_CFLAGS)
+endif
+
+ifeq ($(EMULATOR_TARGET_ARCH),x86)
+HW_SOURCES += \
+    apic.c \
+    i8259.c \
+    mc146818rtc.c \
+    piix_pci.c \
+    i8254.c \
+    pckbd.c \
+    ioapic.c \
+    ps2.c \
+    smbios.c \
+    fw_cfg.c
+
+# The following sources must be compiled with the final executables
+# because they contain device_init() or machine_init() statements.
+HW_OBJ_SOURCES := \
+    hw/ne2000.c \
+    hw/pc.c
+
+HW_OBJ_CFLAGS  := $(EMULATOR_TARGET_CFLAGS)
+
+endif
+
+LOCAL_SRC_FILES += $(HW_SOURCES:%=hw/%)
+
+LOCAL_SRC_FILES += \
+    exec.c \
+    cpu-exec.c  \
+    translate-all.c \
+    trace.c \
+    varint.c \
+    dcache.c \
+    softmmu_outside_jit.c \
+
+##############################################################################
+# CPU-specific emulation.
+#
+LOCAL_CFLAGS += -fno-PIC -fomit-frame-pointer -Wno-sign-compare
+
+ifeq ($(HOST_ARCH),ppc)
+    LOCAL_CFLAGS += -D__powerpc__
+endif
+
+ifeq ($(EMULATOR_TARGET_ARCH),arm)
+LOCAL_SRC_FILES += \
+    target-arm/op_helper.c \
+    target-arm/iwmmxt_helper.c \
+    target-arm/neon_helper.c \
+    target-arm/helper.c \
+    target-arm/translate.c \
+    target-arm/machine.c \
+    hw/armv7m.c \
+    hw/armv7m_nvic.c \
+    arm-semi.c \
+
+LOCAL_SRC_FILES += fpu/softfloat.c
+endif
+
+ifeq ($(EMULATOR_TARGET_ARCH), x86)
+LOCAL_SRC_FILES += \
+    target-i386/op_helper.c \
+    target-i386/helper.c \
+    target-i386/translate.c \
+    target-i386/machine.c \
+
+LOCAL_SRC_FILES += fpu/softfloat-native.c
+endif
+
+##############################################################################
+# Memory-access checking support.
+# Memory access checker uses information collected by instrumented code in
+# libc.so in order to keep track of memory blocks allocated from heap. Memory
+# checker then uses this information to make sure that every access to allocated
+# memory is within allocated block. This information also allows detecting
+# memory leaks and attempts to free/realloc invalid pointers.
+#
+LOCAL_CFLAGS += \
+    -I$(LOCAL_PATH)/memcheck \
+    -I$(LOCAL_PATH)/elff
+
+MCHK_SOURCES := \
+    memcheck.c \
+    memcheck_proc_management.c \
+    memcheck_malloc_map.c \
+    memcheck_mmrange_map.c \
+    memcheck_util.c \
+
+LOCAL_SRC_FILES += $(MCHK_SOURCES:%=memcheck/%)
+
+
+$(call end-emulator-library)
+
+##############################################################################
+##############################################################################
+###
+###  qemu-android-$CPU: headless emulator core program
+###
+###
+$(call start-emulator-program, qemu-android-$(EMULATOR_TARGET_ARCH))
+
+LOCAL_CFLAGS += \
+    $(EMULATOR_COMMON_CFLAGS) \
+    $(ELFF_CFLAGS) \
+    $(EMULATOR_LIBQEMU_CFLAGS) \
+    $(EMULATOR_TARGET_CFLAGS) \
+    -DCONFIG_STANDALONE_CORE \
+    -DTARGET_ARCH=\"$(EMULATOR_TARGET_ARCH)\"
+
+LOCAL_CFLAGS += -Wno-missing-field-initializers
+
+
+LOCAL_STATIC_LIBRARIES := \
+    emulator-libqemu \
+    emulator-target-$(EMULATOR_TARGET_CPU) \
+    emulator-libelff \
+    emulator-common \
+
+
+LOCAL_LDLIBS += \
+    $(EMULATOR_COMMON_LDLIBS) \
+    $(EMULATOR_LIBQEMU_LDLIBS) \
+    $(ELFF_LDLIBS) \
+
+LOCAL_SRC_FILES := \
+    audio/audio.c \
+    disas.c \
+    dma-helpers.c \
+    gdbstub.c \
+    keymaps.c \
+    loader.c \
+    monitor.c \
+    qemu-timer.c \
+    user-events-qemu.c \
+    vl-android.c \
+    android/cmdline-option.c \
+    android/console.c \
+    android/display-core.c \
+    android/help.c \
+    android/protocol/attach-ui-proxy.c \
+    android/protocol/fb-updates-proxy.c \
+    android/protocol/user-events-impl.c \
+    android/protocol/ui-commands-proxy.c \
+    android/protocol/core-commands-impl.c \
+    android/protocol/core-commands-qemu.c \
+
+intermediates := $(call intermediates-dir-for,EXECUTABLES,$(LOCAL_MODULE),true)
+$(intermediates)/vl-android.o: $(QEMU_OPTIONS_H)
+$(intermediates)/monitor.o: $(QEMU_MONITOR_H)
+
+ifeq ($(HOST_OS),darwin)
+    FRAMEWORKS := OpenGL Cocoa QuickTime ApplicationServices Carbon IOKit
+    LOCAL_LDLIBS += $(FRAMEWORKS:%=-Wl,-framework,%)
+endif
+
+# Generate a completely static executable if needed.
+# Note that this means no sound and graphics on Linux.
+#
+ifeq ($(CONFIG_STATIC_EXECUTABLE),true)
+    LOCAL_SRC_FILES += dynlink-static.c
+    LOCAL_LDLIBS    += -static
+endif
+
+# The following files cannot be in static libraries because they contain
+# constructor functions that are otherwise stripped by the final linker
+LOCAL_SRC_FILES += $(HW_OBJ_SOURCES)
+LOCAL_CFLAGS    += $(HW_OBJ_CFLAGS)
+
+LOCAL_SRC_FILES += $(BLOCK_SOURCES)
+LOCAL_CFLAGS    += $(BLOCK_CFLAGS)
+
+LOCAL_MODULE_TAGS := debug
+
+$(call end-emulator-program)
+
+##############################################################################
+##############################################################################
+###
+###  emulator-$ARCH: Standalone emulator program
+###
+###
+
+# Special case, "emulator-arm" is named "emulator" for now.
+ifeq ($(EMULATOR_TARGET_ARCH),arm)
+$(call start-emulator-program, emulator)
+else
+$(call start-emulator-program, emulator-$(EMULATOR_TARGET_ARCH))
+endif
+
+LOCAL_STATIC_LIBRARIES := \
+    emulator-libui \
+    emulator-libqemu \
+    emulator-target-$(EMULATOR_TARGET_CPU) \
+    emulator-libelff \
+    emulator-common \
+
+LOCAL_LDLIBS += \
+    $(EMULATOR_COMMON_LDLIBS) \
+    $(EMULATOR_LIBQEMU_LDLIBS) \
+    $(EMULATOR_LIBUI_LDLIBS) \
+    $(ELFF_LDLIBS) \
+
+LOCAL_CFLAGS += \
+    $(EMULATOR_TARGET_CFLAGS) \
+    $(EMULATOR_COMMON_CFLAGS) \
+    $(EMULATOR_LIBQEMU_CFLAGS) \
+    $(EMULATOR_LIBUI_CFLAGS)
+
+LOCAL_CFLAGS += -DTARGET_ARCH=\"$(EMULATOR_TARGET_ARCH)\"
+
+LOCAL_SRC_FILES := \
+    audio/audio.c \
+    disas.c \
+    dma-helpers.c \
+    gdbstub.c \
+    keymaps.c \
+    loader.c \
+    monitor.c \
+    qemu-timer.c \
+    user-events-qemu.c \
+    vl-android.c \
+    android/cmdline-option.c \
+    android/console.c \
+    android/display.c \
+    android/display-core.c \
+    android/help.c \
+    android/main-common.c \
+    android/main.c \
+    android/protocol/core-commands-qemu.c \
+    android/protocol/ui-commands-qemu.c \
+    android/
+
+# The following files cannot be in static libraries because they contain
+# constructor functions that are otherwise stripped by the final linker
+LOCAL_SRC_FILES += $(HW_OBJ_SOURCES)
+LOCAL_CFLAGS    += $(HW_OBJ_CFLAGS)
+
+LOCAL_SRC_FILES += $(BLOCK_SOURCES)
+LOCAL_CFLAGS    += $(BLOCK_CFLAGS)
+
+LOCAL_SRC_FILES += $(SDLMAIN_SOURCES)
+
+# Generate a completely static executable if needed.
+# Note that this means no sound and graphics on Linux.
+#
+ifeq ($(CONFIG_STATIC_EXECUTABLE),true)
+    LOCAL_SRC_FILES += dynlink-static.c
+    LOCAL_LDLIBS    += -static
+endif
+
+$(call end-emulator-program)
diff --git a/android/avd/info.c b/android/avd/info.c
index 3d91a30..dc53849 100644
--- a/android/avd/info.c
+++ b/android/avd/info.c
@@ -124,6 +124,7 @@
     char      inAndroidBuild;
     char*     androidOut;
     char*     androidBuildRoot;
+    char*     targetArch;
 
     /* for the normal virtual device case */
     char*     deviceName;
@@ -558,7 +559,7 @@
 }
 
 
-/* copy image file from a given source 
+/* copy image file from a given source
  * assumes locking is needed.
  */
 static void
@@ -606,7 +607,7 @@
 
     /* set image state */
     l->pState[0] = (flags & IMAGE_DONT_LOCK) == 0
-                 ? IMAGE_STATE_MUSTLOCK 
+                 ? IMAGE_STATE_MUSTLOCK
                  : IMAGE_STATE_READONLY;
 
     /* check user-provided path */
@@ -651,7 +652,7 @@
         if (flags & IMAGE_REQUIRED) {
             AvdInfo*  i = l->info;
 
-            derror("could not find required %s image (%s).", 
+            derror("could not find required %s image (%s).",
                    l->imageText, l->imageFile);
 
             if (i->inAndroidBuild) {
@@ -1190,14 +1191,8 @@
     if ( !imageLoader_load( l, IMAGE_OPTIONAL |
                                IMAGE_DONT_LOCK ) )
     {
-#ifdef TARGET_ARM
-#define  PREBUILT_KERNEL_PATH   "prebuilt/android-arm/kernel/kernel-qemu"
-#endif
-#ifdef TARGET_I386
-#define  PREBUILT_KERNEL_PATH   "prebuilt/android-x86/kernel/kernel-qemu"
-#endif
-        p = bufprint(temp, end, "%s/%s", i->androidBuildRoot,
-                        PREBUILT_KERNEL_PATH);
+        p = bufprint(temp, end, "%s/prebuilt/android-%s/kernel/kernel-qemu",
+                     i->androidBuildRoot, i->targetArch);
         if (p >= end || !path_exists(temp)) {
             derror("bad workspace: cannot find prebuilt kernel in: %s", temp);
             exit(1);
@@ -1280,7 +1275,7 @@
 
         /* if the user provided one cache image, lock & use it */
         if ( params->forcePaths[l->id] != NULL ) {
-            imageLoader_load(l, IMAGE_REQUIRED | 
+            imageLoader_load(l, IMAGE_REQUIRED |
                                 IMAGE_IGNORE_IF_LOCKED);
         }
     }
@@ -1398,6 +1393,7 @@
 AvdInfo*
 avdInfo_newForAndroidBuild( const char*     androidBuildRoot,
                             const char*     androidOut,
+                            const char*     targetArch,
                             AvdInfoParams*  params )
 {
     AvdInfo*  i;
@@ -1408,6 +1404,7 @@
     i->androidBuildRoot = ASTRDUP(androidBuildRoot);
     i->androidOut       = ASTRDUP(androidOut);
     i->contentPath      = ASTRDUP(androidOut);
+    i->targetArch       = ASTRDUP(targetArch);
 
     /* TODO: find a way to provide better information from the build files */
     i->deviceName = ASTRDUP("<build>");
diff --git a/android/avd/info.h b/android/avd/info.h
index 2b2899f..1a65355 100644
--- a/android/avd/info.h
+++ b/android/avd/info.h
@@ -126,6 +126,7 @@
  */
 AvdInfo*  avdInfo_newForAndroidBuild( const char*     androidBuildRoot,
                                       const char*     androidOut,
+                                      const char*     targetArch,
                                       AvdInfoParams*  params );
 
 /* Frees an AvdInfo object and the corresponding strings that may be
diff --git a/android/cmdline-options.h b/android/cmdline-options.h
index 273d151..70e5f6c 100644
--- a/android/cmdline-options.h
+++ b/android/cmdline-options.h
@@ -131,7 +131,9 @@
 
 OPT_PARAM( report_console, "<socket>", "report console port to remote socket" )
 OPT_PARAM( gps, "<device>", "redirect NMEA GPS to character device" )
+#ifndef CONFIG_STANDALONE_CORE
 OPT_PARAM( keyset, "<name>", "specify keyset file name" )
+#endif
 OPT_PARAM( shell_serial, "<device>", "specific character device for root shell" )
 OPT_FLAG ( old_system, "support old (pre 1.4) system images" )
 OPT_PARAM( tcpdump, "<file>", "capture network packets to file" )
diff --git a/android/config/config.h b/android/config/config.h
index fdfbe31..e07cc2c 100644
--- a/android/config/config.h
+++ b/android/config/config.h
@@ -1,4 +1,9 @@
-/* Automatically generated by configure - do not modify */
+/* This file is included by target-specific files under
+ * android/config/target-$ARCH/config.h, but contains all config
+ * definitions that are independent of the target CPU.
+ *
+ * Do not include directly.
+ */
 #include "config-host.h"
 
 #define TARGET_PHYS_ADDR_BITS  32
@@ -11,13 +16,4 @@
 #define CONFIG_NAND_LIMITS 1
 #endif
 #define CONFIG_ANDROID_SNAPSHOTS 1
-
-#ifdef ARCH_FLAGS_x86
-#define TARGET_ARCH "x86"
-#define TARGET_I386 1
-#else
-#define TARGET_ARCH "arm"
-#define TARGET_ARM 1
-#define CONFIG_SOFTFLOAT 1
-#endif
-
+#define CONFIG_MEMCHECK 1
diff --git a/android/config/target-arm/config.h b/android/config/target-arm/config.h
new file mode 100644
index 0000000..d6da04f
--- /dev/null
+++ b/android/config/target-arm/config.h
@@ -0,0 +1,5 @@
+/* ARM-specific configuration */
+#include "android/config/config.h"
+
+#define TARGET_ARM 1
+#define CONFIG_SOFTFLOAT 1
diff --git a/android/config/target-x86/config.h b/android/config/target-x86/config.h
new file mode 100644
index 0000000..ea31c39
--- /dev/null
+++ b/android/config/target-x86/config.h
@@ -0,0 +1,3 @@
+/* x86-specific configuration */
+#include "android/config/config.h"
+#define TARGET_I386 1
diff --git a/android/help.c b/android/help.c
index 7df6704..a2180ef 100644
--- a/android/help.c
+++ b/android/help.c
@@ -213,6 +213,7 @@
     datadir );
 }
 
+#ifndef CONFIG_STANDALONE_CORE
 static void
 help_keys(stralloc_t*  out)
 {
@@ -253,7 +254,7 @@
     PRINTF( "\n" );
     PRINTF( "  note that NumLock must be deactivated for keypad keys to work\n\n" );
 }
-
+#endif /* !CONFIG_STANDALONE_CORE */
 
 static void
 help_environment(stralloc_t*  out)
@@ -285,7 +286,7 @@
     );
 }
 
-
+#ifndef CONFIG_STANDALONE_CORE
 static void
 help_keyset_file(stralloc_t*  out)
 {
@@ -355,7 +356,7 @@
     "\n"
     );
 }
-
+#endif /* !CONFIG_STANDALONE_CORE */
 
 static void
 help_debug_tags(stralloc_t*  out)
@@ -1304,6 +1305,7 @@
 }
 
 
+#ifndef CONFIG_STANDALONE_CORE
 static void
 help_keyset(stralloc_t*  out)
 {
@@ -1343,6 +1345,7 @@
     "\n"
     );
 }
+#endif /* !CONFIG_STANDALONE_CORE */
 
 static void
 help_old_system(stralloc_t*  out)
@@ -1494,11 +1497,15 @@
 
 static const TopicHelp    topic_help[] = {
     { "disk-images",  "about disk images",      help_disk_images },
+#ifndef CONFIG_STANDALONE_CORE
     { "keys",         "supported key bindings", help_keys },
+#endif
     { "debug-tags",   "debug tags for -debug <tags>", help_debug_tags },
     { "char-devices", "character <device> specification", help_char_devices },
     { "environment",  "environment variables",  help_environment },
+#ifndef CONFIG_STANDALONE_CORE
     { "keyset-file",  "key bindings configuration file", help_keyset_file },
+#endif
     { "virtual-device", "virtual device management", help_virtual_device },
     { "sdk-images",   "about disk images when using the SDK", help_sdk_images },
     { "build-images", "about disk images when building Android", help_build_images },
diff --git a/android/main-common.c b/android/main-common.c
index bb07943..3c8b320 100644
--- a/android/main-common.c
+++ b/android/main-common.c
@@ -19,8 +19,16 @@
 #include <process.h>
 #endif
 
+#ifndef CONFIG_STANDALONE_CORE
 #include <SDL.h>
 #include <SDL_syswm.h>
+#include "android/qemulator.h"
+#include "android/skin/image.h"
+#include "android/skin/trackball.h"
+#include "android/skin/keyboard.h"
+#include "android/skin/file.h"
+#include "android/skin/window.h"
+#endif
 
 #include "console.h"
 
@@ -31,13 +39,7 @@
 #include "android/globals.h"
 #include "android/resource.h"
 #include "android/user-config.h"
-#include "android/qemulator.h"
 #include "android/display.h"
-#include "android/skin/image.h"
-#include "android/skin/trackball.h"
-#include "android/skin/keyboard.h"
-#include "android/skin/file.h"
-#include "android/skin/window.h"
 
 
 
@@ -54,6 +56,20 @@
 /***  CONFIGURATION
  ***/
 
+#ifdef CONFIG_STANDALONE_CORE
+
+void
+user_config_init( void )
+{
+}
+
+/* only call this function on normal exits, so that ^C doesn't save the configuration */
+void
+user_config_done( void )
+{
+}
+
+#else /* !CONFIG_STANDALONE_CORE */
 static AUserConfig*  userConfig;
 
 void
@@ -86,6 +102,7 @@
     if (userConfig)
         auserConfig_getWindowPos(userConfig, window_x, window_y);
 }
+#endif /* !CONFIG_STANDALONE_CORE */
 
 unsigned convertBytesToMB( uint64_t  size )
 {
@@ -105,6 +122,7 @@
 }
 
 
+#ifndef CONFIG_STANDALONE_CORE
 /***********************************************************************/
 /***********************************************************************/
 /*****                                                             *****/
@@ -199,7 +217,7 @@
     }
 }
 
-
+#endif /* !CONFIG_STANDALONE_CORE */
 
 /***********************************************************************/
 /***********************************************************************/
@@ -209,6 +227,8 @@
 /***********************************************************************/
 /***********************************************************************/
 
+#ifndef CONFIG_STANDALONE_CORE
+
 void *readpng(const unsigned char*  base, size_t  size, unsigned *_width, unsigned *_height);
 
 #ifdef CONFIG_DARWIN
@@ -269,15 +289,19 @@
             }
         }
 
+#ifndef CONFIG_STANDALONE_CORE
         SDL_Surface* icon = sdl_surface_from_argb32( icon_pixels, icon_w, icon_h );
         if (icon != NULL) {
             SDL_WM_SetIcon(icon, NULL);
             SDL_FreeSurface(icon);
             free( icon_pixels );
         }
+#endif
+
 #endif  /* !_WIN32 */
     }
 }
+#endif /* !CONFIG_STANDALONE_CORE */
 
 /***********************************************************************/
 /***********************************************************************/
@@ -290,7 +314,7 @@
 const char*  skin_network_speed = NULL;
 const char*  skin_network_delay = NULL;
 
-
+#ifndef CONFIG_STANDALONE_CORE
 static void sdl_at_exit(void)
 {
     user_config_done();
@@ -319,6 +343,7 @@
     android_display_init(ds, qframebuffer_fifo_get());
 #endif
 }
+#endif
 
 /* list of skin aliases */
 static const struct {
@@ -462,6 +487,26 @@
 }
 
 
+#ifdef CONFIG_STANDALONE_CORE
+void
+init_sdl_ui(AConfig*         skinConfig,
+            const char*      skinPath,
+            AndroidOptions*  opts)
+{
+    signal(SIGINT, SIG_DFL);
+#ifndef _WIN32
+    signal(SIGQUIT, SIG_DFL);
+   /* prevent SIGTTIN and SIGTTOUT from stopping us. this is necessary to be
+    * able to run the emulator in the background (e.g. "emulator &").
+    * despite the fact that the emulator should not grab input or try to
+    * write to the output in normal cases, we're stopped on some systems
+    * (e.g. OS X)
+    */
+    signal(SIGTTIN, SIG_IGN);
+    signal(SIGTTOU, SIG_IGN);
+#endif
+}
+#else /* !CONFIG_STANDALONE_CORE */
 void
 init_sdl_ui(AConfig*         skinConfig,
             const char*      skinPath,
@@ -536,6 +581,7 @@
         qemulator_get()->onion_rotation = rotate;
     }
 }
+#endif /* !CONFIG_STANDALONE_CORE */
 
 int64_t  get_screen_pixels(AConfig*  skinConfig)
 {
diff --git a/android/main-ui.c b/android/main-ui.c
index ca83a9a..c31cfef 100644
--- a/android/main-ui.c
+++ b/android/main-ui.c
@@ -812,6 +812,7 @@
         android_avdInfo = avdInfo_newForAndroidBuild(
                             android_build_root,
                             android_build_out,
+                            "arm",
                             android_avdParams );
 
         if(android_avdInfo == NULL) {
diff --git a/android/main.c b/android/main.c
index 919810b..15adaf4 100644
--- a/android/main.c
+++ b/android/main.c
@@ -26,8 +26,11 @@
 #include "console.h"
 #include "user-events.h"
 
+#ifndef CONFIG_STANDALONE_CORE
 #include <SDL.h>
 #include <SDL_syswm.h>
+#include "android/qemulator.h"
+#endif
 
 #include "math.h"
 
@@ -48,7 +51,6 @@
 
 #include "android/globals.h"
 
-#include "android/qemulator.h"
 #include "android/display.h"
 
 #include "android/snapshot.h"
@@ -260,7 +262,7 @@
     if (imageMB > defaultMB) {
         snprintf(temp, sizeof temp, "(%d MB > %d MB)", imageMB, defaultMB);
     } else {
-        snprintf(temp, sizeof temp, "(%lld bytes > %lld bytes)", imageBytes, defaultBytes);
+        snprintf(temp, sizeof temp, "(%" PRUd64" bytes > %" PRUd64" bytes)", imageBytes, defaultBytes);
     }
 
     if (inAndroidBuild) {
@@ -603,6 +605,7 @@
         android_avdInfo = avdInfo_newForAndroidBuild(
                             android_build_root,
                             android_build_out,
+                            TARGET_ARCH,
                             android_avdParams );
 
         if(android_avdInfo == NULL) {
@@ -631,6 +634,7 @@
         exit(1);
     }
 
+#ifndef CONFIG_STANDALONE_CORE
     if (opts->keyset) {
         parse_keyset(opts->keyset, opts);
         if (!android_keyset) {
@@ -653,6 +657,7 @@
                 write_default_keyset();
         }
     }
+#endif /* !CONFIG_STANDALONE_CORE */
 
     if (opts->shared_net_id) {
         char*  end;
@@ -1090,7 +1095,7 @@
     }
 
     /* Pass LCD density value to the core. */
-    snprintf(lcd_density, sizeof(lcd_density), "%d", get_device_dpi(opts));
+    snprintf(lcd_density, sizeof(lcd_density), "%d", hw->hw_lcd_density);
     args[n++] = "-lcd-density";
     args[n++] = lcd_density;
 
diff --git a/android/utils/system.h b/android/utils/system.h
index c8163c6..464957d 100644
--- a/android/utils/system.h
+++ b/android/utils/system.h
@@ -173,6 +173,12 @@
 #ifndef PRIx64
 #  define PRIx64  "llx"
 #endif
+#ifndef PRUd64
+#  define PRUd64  "llu"
+#endif
+#ifndef PRUx64
+#  define PRUx64  "llx"
+#endif
 
 /* */
 
diff --git a/hw/hw.h b/hw/hw.h
index 4f9b650..efcbe1e 100644
--- a/hw/hw.h
+++ b/hw/hw.h
@@ -4,7 +4,10 @@
 
 #include "qemu-common.h"
 #include "irq.h"
+
+#if defined(TARGET_PHYS_ADDRESS_SPACE_BITS) && !defined(NEED_CPU_H)
 #include "cpu.h"
+#endif
 
 /* VM Load/Save */
 
diff --git a/memcheck/memcheck.c b/memcheck/memcheck.c
index 3c8194a..3ccb196 100644
--- a/memcheck/memcheck.c
+++ b/memcheck/memcheck.c
@@ -14,12 +14,6 @@
  * Contains implementation of memory checking framework in the emulator.
  */
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "qemu-queue.h"
 #include "qemu_file.h"
 #include "elff_api.h"
diff --git a/memcheck/memcheck.h b/memcheck/memcheck.h
index a9a6422..a7761a9 100644
--- a/memcheck/memcheck.h
+++ b/memcheck/memcheck.h
@@ -18,12 +18,6 @@
 #ifndef QEMU_MEMCHECK_MEMCHECK_H
 #define QEMU_MEMCHECK_MEMCHECK_H
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "memcheck_common.h"
 
 #ifdef __cplusplus
diff --git a/memcheck/memcheck_common.h b/memcheck/memcheck_common.h
index e6d12db..e753094 100644
--- a/memcheck/memcheck_common.h
+++ b/memcheck/memcheck_common.h
@@ -18,12 +18,6 @@
 #ifndef QEMU_MEMCHECK_MEMCHECK_COMMON_H
 #define QEMU_MEMCHECK_MEMCHECK_COMMON_H
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "qemu-common.h"
 #include "cpu.h"
 
diff --git a/memcheck/memcheck_logging.h b/memcheck/memcheck_logging.h
index c2ae6e9..94a3c09 100644
--- a/memcheck/memcheck_logging.h
+++ b/memcheck/memcheck_logging.h
@@ -17,12 +17,6 @@
 #ifndef QEMU_MEMCHECK_MEMCHECK_LOGGING_H
 #define QEMU_MEMCHECK_MEMCHECK_LOGGING_H
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "qemu-common.h"
 #include "android/utils/debug.h"
 
diff --git a/memcheck/memcheck_malloc_map.c b/memcheck/memcheck_malloc_map.c
index 07ae889..d7e8032 100644
--- a/memcheck/memcheck_malloc_map.c
+++ b/memcheck/memcheck_malloc_map.c
@@ -15,12 +15,6 @@
  * memory blocks allocated by the guest system.
  */
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "memcheck_malloc_map.h"
 #include "memcheck_util.h"
 #include "memcheck_logging.h"
diff --git a/memcheck/memcheck_malloc_map.h b/memcheck/memcheck_malloc_map.h
index b356180..1c25738 100644
--- a/memcheck/memcheck_malloc_map.h
+++ b/memcheck/memcheck_malloc_map.h
@@ -24,12 +24,6 @@
 #ifndef QEMU_MEMCHECK_MEMCHECK_MALLOC_MAP_H
 #define QEMU_MEMCHECK_MEMCHECK_MALLOC_MAP_H
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "sys-tree.h"
 #include "memcheck_common.h"
 
diff --git a/memcheck/memcheck_mmrange_map.c b/memcheck/memcheck_mmrange_map.c
index f2609df..d9a1211 100644
--- a/memcheck/memcheck_mmrange_map.c
+++ b/memcheck/memcheck_mmrange_map.c
@@ -15,12 +15,6 @@
  * memory mappings in the guest system.
  */
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "memcheck_mmrange_map.h"
 #include "memcheck_logging.h"
 
diff --git a/memcheck/memcheck_mmrange_map.h b/memcheck/memcheck_mmrange_map.h
index f2c9701..f291c95 100644
--- a/memcheck/memcheck_mmrange_map.h
+++ b/memcheck/memcheck_mmrange_map.h
@@ -22,12 +22,6 @@
 #ifndef QEMU_MEMCHECK_MEMCHECK_MMRANGE_MAP_H
 #define QEMU_MEMCHECK_MEMCHECK_MMRANGE_MAP_H
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "sys-tree.h"
 #include "memcheck_common.h"
 
diff --git a/memcheck/memcheck_proc_management.c b/memcheck/memcheck_proc_management.c
index 593ba32..45cf141 100644
--- a/memcheck/memcheck_proc_management.c
+++ b/memcheck/memcheck_proc_management.c
@@ -15,12 +15,6 @@
  * memchecker framework.
  */
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "elff/elff_api.h"
 #include "memcheck.h"
 #include "memcheck_proc_management.h"
diff --git a/memcheck/memcheck_proc_management.h b/memcheck/memcheck_proc_management.h
index 68b6181..7f489cf 100644
--- a/memcheck/memcheck_proc_management.h
+++ b/memcheck/memcheck_proc_management.h
@@ -18,12 +18,6 @@
 #ifndef QEMU_MEMCHECK_MEMCHECK_PROC_MANAGEMENT_H
 #define QEMU_MEMCHECK_MEMCHECK_PROC_MANAGEMENT_H
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "qemu-queue.h"
 #include "memcheck_common.h"
 #include "memcheck_malloc_map.h"
diff --git a/memcheck/memcheck_util.c b/memcheck/memcheck_util.c
index 5449488..8a59914 100644
--- a/memcheck/memcheck_util.c
+++ b/memcheck/memcheck_util.c
@@ -14,12 +14,6 @@
  * Contains implementation of utility routines for memchecker framework.
  */
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "stdio.h"
 #include "qemu-common.h"
 #include "android/utils/path.h"
diff --git a/memcheck/memcheck_util.h b/memcheck/memcheck_util.h
index b75ee53..2a9907e 100644
--- a/memcheck/memcheck_util.h
+++ b/memcheck/memcheck_util.h
@@ -17,12 +17,6 @@
 #ifndef QEMU_MEMCHECK_MEMCHECK_UTIL_H
 #define QEMU_MEMCHECK_MEMCHECK_UTIL_H
 
-/* This file should compile iff qemu is built with memory checking
- * configuration turned on. */
-#ifndef CONFIG_MEMCHECK
-#error CONFIG_MEMCHECK is not defined.
-#endif  // CONFIG_MEMCHECK
-
 #include "memcheck_common.h"
 #include "elff/elff_api.h"
 
diff --git a/net-android.c b/net-android.c
index 234235b..6459eff 100644
--- a/net-android.c
+++ b/net-android.c
@@ -126,7 +126,7 @@
 #include "libslirp.h"
 #endif
 
-#if defined(CONFIG_SHAPER)
+#if defined(CONFIG_ANDROID)
 #include "shaper.h"
 #endif
 
@@ -730,7 +730,7 @@
     return ( data[12] == 10 && data[16] == 10);
 }
 
-#ifdef CONFIG_SHAPER
+#ifdef CONFIG_ANDROID
 
 NetShaper  slirp_shaper_in;
 NetShaper  slirp_shaper_out;
@@ -773,12 +773,12 @@
     netshaper_set_rate( slirp_shaper_in,  qemu_net_upload_speed  );
 }
 
-#endif /* CONFIG_SHAPER */
+#endif /* CONFIG_ANDROID */
 
 
 int slirp_can_output(void)
 {
-#ifdef CONFIG_SHAPER
+#ifdef CONFIG_ANDROID
     return !slirp_vc ||
            ( netshaper_can_send(slirp_shaper_out) &&
              qemu_can_send_packet(slirp_vc) );
@@ -799,7 +799,7 @@
     if (!slirp_vc)
         return;
 
-#ifdef CONFIG_SHAPER
+#ifdef CONFIG_ANDROID
     netshaper_send(slirp_shaper_out, (void*)pkt, pkt_len);
 #else
     qemu_send_packet(slirp_vc, pkt, pkt_len);
@@ -820,7 +820,7 @@
     if (qemu_tcpdump_active)
         qemu_tcpdump_packet(buf, size);
 
-#ifdef CONFIG_SHAPER
+#ifdef CONFIG_ANDROID
     netshaper_send(slirp_shaper_in, (char*)buf, size);
 #else
     slirp_input(buf, size);
diff --git a/target-arm/helper.c b/target-arm/helper.c
index 258e2cb..f595b2c 100644
--- a/target-arm/helper.c
+++ b/target-arm/helper.c
@@ -1214,7 +1214,7 @@
 int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
                               int access_type, int mmu_idx, int is_softmmu)
 {
-    uint32_t phys_addr;
+    uint32_t phys_addr = 0;
     int prot;
     int ret, is_user;
 
@@ -1244,7 +1244,7 @@
 
 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
 {
-    uint32_t phys_addr;
+    uint32_t phys_addr = 0;
     int prot;
     int ret;
 
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
index 4e7eb58..88547cb 100644
--- a/target-i386/cpu.h
+++ b/target-i386/cpu.h
@@ -181,7 +181,7 @@
 #define HF2_VINTR_SHIFT      3 /* value of V_INTR_MASKING bit */
 
 #define HF2_GIF_MASK          (1 << HF2_GIF_SHIFT)
-#define HF2_HIF_MASK          (1 << HF2_HIF_SHIFT) 
+#define HF2_HIF_MASK          (1 << HF2_HIF_SHIFT)
 #define HF2_NMI_MASK          (1 << HF2_NMI_SHIFT)
 #define HF2_VINTR_MASK        (1 << HF2_VINTR_SHIFT)
 
@@ -409,7 +409,7 @@
 #define CPUID_VENDOR_INTEL_3 0x6c65746e /* "ntel" */
 
 #define CPUID_VENDOR_AMD_1   0x68747541 /* "Auth" */
-#define CPUID_VENDOR_AMD_2   0x69746e65 /* "enti" */ 
+#define CPUID_VENDOR_AMD_2   0x69746e65 /* "enti" */
 #define CPUID_VENDOR_AMD_3   0x444d4163 /* "cAMD" */
 
 #define CPUID_MWAIT_IBE     (1 << 1) /* Interrupts can exit capability */