# This file is included several times to build target-specific
# modules for the Android emulator. It will be called several times
# for arm, x86 and mips
#

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: target-specific emulation code.
###
###  Used by both the core and standalone programs.
###

# 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 ($(HOST_ARCH),x86)
  TCG_TARGET := i386
endif
ifeq ($(HOST_ARCH),x86_64)
  TCG_TARGET := i386
endif

EMULATOR_TARGET_CFLAGS += \
    -I$(LOCAL_PATH)/tcg \
    -I$(LOCAL_PATH)/tcg/$(TCG_TARGET) \
    -DTARGET_ARCH=\"$(EMULATOR_TARGET_ARCH)\"


common_LOCAL_CFLAGS =
common_LOCAL_SRC_FILES =


# The following is to ensure that "config.h" will map to a target-specific
# configuration file header.
common_LOCAL_CFLAGS += $(EMULATOR_TARGET_CFLAGS)

common_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_audio.c \
    goldfish_device.c \
    goldfish_events_device.c \
    goldfish_fb.c \
    goldfish_battery.c \
    goldfish_mmc.c   \
    goldfish_memlog.c \
    goldfish_nand.c \
    goldfish_pipe.c \
    goldfish_tty.c \
    goldfish_vmem.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_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)

common_LOCAL_SRC_FILES += arm-dis.c

# smc91c111.c requires <zlib.h>
common_LOCAL_CFLAGS += $(ZLIB_CFLAGS)
endif

# required to ensure we properly initialize virtual audio hardware
common_LOCAL_CFLAGS += -DHAS_AUDIO

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

ifeq ($(EMULATOR_TARGET_ARCH),mips)
HW_SOURCES += \
    android_mips.c \
    mips_pic.c \
    goldfish_interrupt.c \
    goldfish_switch.c \
    goldfish_timer.c \
    goldfish_trace.c \
    mips_timer.c \
    mips_int.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)

common_LOCAL_SRC_FILES += mips-dis.c

# smc91c111.c requires <zlib.h>
LOCAL_CFLAGS += $(ZLIB_CFLAGS)
ifeq ($(ARCH_HAS_BIGENDIAN),true)
  LOCAL_CFLAGS += -DTARGET_WORDS_BIGENDIAN
endif

endif
common_LOCAL_SRC_FILES += $(HW_SOURCES:%=hw/%)

common_LOCAL_SRC_FILES += \
    cpu-exec.c  \
    exec.c \
    translate-all.c \
    trace.c \
    varint.c \
    softmmu_outside_jit.c

##############################################################################
# CPU-specific emulation.
#
common_LOCAL_CFLAGS += -fno-PIC -fomit-frame-pointer -Wno-sign-compare

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

ifeq ($(EMULATOR_TARGET_ARCH),arm)
common_LOCAL_SRC_FILES += \
    target-arm/op_helper.c \
    target-arm/iwmmxt_helper.c \
    target-arm/neon_helper.c \
    target-arm/helper.c \
    target-arm/helper-android.c \
    target-arm/translate.c \
    target-arm/machine.c \
    hw/armv7m.c \
    hw/armv7m_nvic.c \
    arm-semi.c

common_LOCAL_SRC_FILES += fpu/softfloat.c
endif

ifeq ($(EMULATOR_TARGET_ARCH), x86)
common_LOCAL_SRC_FILES += \
    target-i386/op_helper.c \
    target-i386/helper.c \
    target-i386/translate.c \
    target-i386/machine.c \

ifeq ($(HOST_OS),darwin)
common_LOCAL_SRC_FILES += \
      target-i386/hax-all.c       \
      target-i386/hax-darwin.c
endif

ifeq ($(HOST_OS),windows)
common_LOCAL_SRC_FILES += \
      target-i386/hax-all.c       \
      target-i386/hax-windows.c
endif

common_LOCAL_SRC_FILES += fpu/softfloat-native.c
endif

ifeq ($(EMULATOR_TARGET_ARCH), mips)
common_LOCAL_SRC_FILES += \
    target-mips/op_helper.c \
    target-mips/helper.c \
    target-mips/translate.c \
    target-mips/machine.c

common_LOCAL_SRC_FILES += fpu/softfloat.c
endif

# compile KVM only if target is x86 on x86 Linux
QEMU_KVM_TAG := $(QEMU_HOST_TAG)-$(EMULATOR_TARGET_ARCH)
QEMU_DO_KVM := $(if $(filter linux-x86-x86 linux-x86_64-x86,$(QEMU_KVM_TAG)),true,false)
ifeq ($(QEMU_DO_KVM),true)
    common_LOCAL_SRC_FILES += \
        target-i386/kvm.c \
        target-i386/kvm-gs-restore.c \
        kvm-all.c \
        kvm-android.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.
#
common_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

common_LOCAL_SRC_FILES += $(MCHK_SOURCES:%=memcheck/%)

common_LOCAL_SRC_FILES += \
    cpus.c \
    arch_init.c

# What a mess, os-posix.c depends on the exact values of options
# which are target specific.
ifeq ($(HOST_OS),windows)
    common_LOCAL_SRC_FILES += os-win32.c oslib-win32.c
else
    common_LOCAL_SRC_FILES += os-posix.c oslib-posix.c
endif


## one for 32-bit
$(call start-emulator-library, emulator-target-$(EMULATOR_TARGET_CPU))
LOCAL_CFLAGS += $(common_LOCAL_CFLAGS)
LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
$(call gen-hw-config-defs)
$(call gen-hx-header,qemu-options.hx,qemu-options.def,os-posix.c os-win32.c)
$(call end-emulator-library)

## another for 64-bit, see note in file Makefile.common emulator64-common
ifneq ($(filter linux darwin,$(HOST_OS)),)
  ifneq ($(BUILD_STANDALONE_EMULATOR),true)
    $(call start-emulator-library, emulator64-target-$(EMULATOR_TARGET_CPU))
    LOCAL_CFLAGS += $(common_LOCAL_CFLAGS) -m64
    LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
    $(call gen-hw-config-defs)
    $(call gen-hx-header,qemu-options.hx,qemu-options.def,os-posix.c os-win32.c)
    $(call end-emulator-library)
  endif # BUILD_STANDALONE_EMULATOR == nil
endif # HOST_OS == linux || darwin



##############################################################################
##############################################################################
###
###  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

ifneq ($(QEMU_OPENGLES_INCLUDE),)
    LOCAL_CFLAGS += -I$(QEMU_OPENGLES_INCLUDE)
endif

LOCAL_CFLAGS += $(GCC_W_NO_MISSING_FIELD_INITIALIZERS)

LOCAL_STATIC_LIBRARIES := \
    emulator-libqemu \
    emulator-target-$(EMULATOR_TARGET_CPU) \
    emulator-libjpeg \
    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 \
    qemu-timer-common.c \
    user-events-qemu.c \
    vl-android.c \
    android/console.c \
    android/opengles.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 \
    android/protocol/core-commands-qemu.c

$(call gen-hx-header,qemu-monitor.hx,qemu-monitor.h,monitor.c)
$(call gen-hx-header,qemu-options.hx,qemu-options.def,vl-android.c qemu-options.h)
$(call gen-hw-config-defs)

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.
#
ifneq ($(strip $(CONFIG_STATIC_EXECUTABLE)$(BUILD_HOST_static)),)
    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)

$(call end-emulator-program)

##############################################################################
##############################################################################
###
###  emulator-$ARCH: Standalone emulator program
###
###

common_LOCAL_LDLIBS =
common_LOCAL_CFLAGS =
common_LOCAL_SRC_FILES =


common_LOCAL_STATIC_LIBRARIES := \
    emulator-libui \
    emulator-libqemu \
    emulator-target-$(EMULATOR_TARGET_CPU) \
    emulator-libelff \
    emulator-common

common_LOCAL_LDLIBS += \
    $(EMULATOR_COMMON_LDLIBS) \
    $(EMULATOR_LIBQEMU_LDLIBS) \
    $(EMULATOR_LIBUI_LDLIBS) \
    $(ELFF_LDLIBS)

common_LOCAL_CFLAGS += \
    $(EMULATOR_TARGET_CFLAGS) \
    $(EMULATOR_COMMON_CFLAGS) \
    $(EMULATOR_LIBQEMU_CFLAGS) \
    $(EMULATOR_LIBUI_CFLAGS)

common_LOCAL_SRC_FILES := \
    audio/audio.c \
    disas.c \
    dma-helpers.c \
    gdbstub.c \
    keymaps.c \
    loader.c \
    monitor.c \
    qemu-timer.c \
    qemu-timer-common.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/opengles.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
common_LOCAL_SRC_FILES += $(HW_OBJ_SOURCES)
common_LOCAL_CFLAGS    += $(HW_OBJ_CFLAGS)

common_LOCAL_SRC_FILES += $(BLOCK_SOURCES)
common_LOCAL_CFLAGS    += $(BLOCK_CFLAGS)

common_LOCAL_SRC_FILES += $(SDLMAIN_SOURCES)

# Generate a completely static executable if needed.
# Note that this means no sound and graphics on Linux.
#
ifneq ($(strip $(CONFIG_STATIC_EXECUTABLE)$(BUILD_HOST_static)),)
    common_LOCAL_SRC_FILES += dynlink-static.c
    common_LOCAL_LDLIBS    += -static
endif

## one for 32-bit
$(call start-emulator-program, emulator-$(EMULATOR_TARGET_ARCH))
LOCAL_STATIC_LIBRARIES += \
    emulator-libui \
    emulator-libqemu \
    emulator-target-$(EMULATOR_TARGET_CPU) \
    emulator-libjpeg \
    emulator-libelff \
    emulator-common \
    $(SDL_STATIC_LIBRARIES)
LOCAL_LDLIBS += $(common_LOCAL_LDLIBS)
LOCAL_CFLAGS += $(common_LOCAL_CFLAGS)
LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
$(call gen-hx-header,qemu-monitor.hx,qemu-monitor.h,monitor.c)
$(call gen-hx-header,qemu-options.hx,qemu-options.def,vl-android.c qemu-options.h)
$(call gen-hw-config-defs)

ifeq ($(HOST_OS),windows)
$(eval $(call insert-windows-icon))
endif

$(call end-emulator-program)


## another for 64-bit, see note in file Makefile.common emulator64-common
ifneq ($(filter linux darwin,$(HOST_OS)),)
  ifneq ($(BUILD_STANDALONE_EMULATOR),true)
    $(call start-emulator-program, emulator64-$(EMULATOR_TARGET_ARCH))
    LOCAL_STATIC_LIBRARIES += \
        emulator64-libui \
        emulator64-libqemu \
        emulator64-target-$(EMULATOR_TARGET_CPU) \
        emulator64-libjpeg \
        emulator64-libelff \
        emulator64-common \
        $(SDL_STATIC_LIBRARIES_64)
    LOCAL_LDLIBS += $(common_LOCAL_LDLIBS) -m64
    LOCAL_CFLAGS += $(common_LOCAL_CFLAGS) -m64
    LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
    $(call gen-hx-header,qemu-monitor.hx,qemu-monitor.h,monitor.c)
    $(call gen-hx-header,qemu-options.hx,qemu-options.def,vl-android.c qemu-options.h)
    $(call gen-hw-config-defs)
    $(call end-emulator-program)
  endif # BUILD_STANDALONE_EMULATOR == nil
endif # HOST_OS == linux || darwin