resolve merge conflicts of 598e05b to master.

Change-Id: I1e393281f09fed776cf4d856f4858638287dd600
diff --git a/Makefile.android b/Makefile.android
index e701904..1694e66 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -8,6 +8,28 @@
     QEMU_HOST_TAG := windows
 endif
 
+# This defines EMULATOR_BUILD_64BITS to indicate that 64-bit binaries
+# must be generated by the build system. For now, only do it for
+# Linux and Darwin, since we the sources do not compile with Mingw-w64
+# yet due to differing procedure call ABI conventions.
+EMULATOR_BUILD_64BITS := $(strip $(filter linux darwin,$(HOST_OS)))
+
+# Disable 64-bit build for Darwin platform builds.
+ifeq ($(HOST_OS),darwin)
+ifneq (true,$(BUILD_STANDALONE_EMULATOR))
+EMULATOR_BUILD_64BITS := $(strip $(empty))
+endif # BUILD_STANDALONE_EMULATOR != true
+endif # HOST_OS == darwin
+
+# A function that includes a file only if 32-bit binaries are necessary.
+# This is always the case, but later used with include-if-bitness-64.
+# $1: Build file to include.
+include-if-bitness-32 = $(eval include $1)
+
+# A function that includes a file only of EMULATOR_BUILD_64BITS is not empty.
+# $1: Build file to include.
+include-if-bitness-64 = $(if $(strip $(EMULATOR_BUILD_64BITS)),$(eval include $1))
+
 # determine the location of platform-specific directories
 #
 CONFIG_DIRS     := \
@@ -24,18 +46,30 @@
 MY_LD  := $(HOST_LD)
 MY_AR  := $(HOST_AR)
 
-MY_CFLAGS := $(CONFIG_INCLUDES) -O2 -g -fno-PIC -falign-functions=0 -fomit-frame-pointer
-
-# Overwrite configuration for debug builds.
-#
+MY_CFLAGS := $(CONFIG_INCLUDES) -g -falign-functions=0
 ifeq ($(BUILD_DEBUG_EMULATOR),true)
-    MY_CFLAGS := $(CONFIG_INCLUDES)
-    MY_CFLAGS += -O0 -g
-    MY_CFLAGS += -fno-PIC -falign-functions=0
+    MY_CFLAGS += -O0
+else
+    MY_CFLAGS += -O2
 endif
 
+# Generate position-independent binaries. Don't add -fPIC when targetting
+# Windows, because newer toolchain complain loudly about it, since all
+# Windows code is position-independent.
+ifneq (windows,$(HOST_OS))
+  MY_CFLAGS += -fPIC
+endif
+
+MY_CFLAGS32 :=
+MY_CFLAGS64 :=
+
 MY_LDLIBS :=
+MY_LDLIBS32 :=
+MY_LDLIBS64 :=
+
 MY_LDFLAGS :=
+MY_LDFLAGS32 :=
+MY_LDFLAGS64 :=
 
 ifeq ($(HOST_OS),freebsd)
   MY_CFLAGS += -I /usr/local/include
@@ -46,7 +80,7 @@
   MY_CFLAGS += -DWINVER=0x501
   MY_CFLAGS += -D_WIN32
   # LARGEADDRESSAWARE gives more address space to 32-bit process
-  MY_LDLIBS += -Xlinker --large-address-aware
+  MY_LDFLAGS32 += -Xlinker --large-address-aware
   ifneq ($(HOST_IS_64_BIT),)
     # Microsoft 64-bit compiler define both _WIN32 and _WIN64
     MY_CFLAGS += -D_WIN64
@@ -65,7 +99,6 @@
 ifeq ($(HOST_OS),darwin)
     MY_CFLAGS += -D_DARWIN_C_SOURCE=1
     ifneq ($(host_toolchain_header),)
-        MY_CFLAGS += -mdynamic-no-pic
         MY_CFLAGS += -isystem $(host_toolchain_header)
     else
         ifneq (,$(mac_sdk_root))
@@ -202,12 +235,17 @@
     $(eval include $(CLEAR_VARS)) \
     $(eval LOCAL_NO_DEFAULT_COMPILER_FLAGS := true) \
     $(eval LOCAL_MODULE := $1) \
-    $(eval LOCAL_MODULE_CLASS := STATIC_LIBRARIES)
+    $(eval LOCAL_MODULE_CLASS := STATIC_LIBRARIES) \
+    $(eval LOCAL_MODULE_BITS := 32)
+
+start-emulator64-library = \
+    $(call start-emulator-library, $1) \
+    $(eval LOCAL_MODULE_BITS := 64)
 
 # Used with start-emulator-library
 end-emulator-library = \
     $(eval $(end-emulator-module-ev)) \
-    $(eval include $(BUILD_HOST_STATIC_LIBRARY))
+    $(call include-if-bitness-$(LOCAL_MODULE_BITS), $(BUILD_HOST_STATIC_LIBRARY))
 
 # A variant of start-emulator-library to start the definition of a host
 # program instead. Use with end-emulator-program
@@ -215,20 +253,46 @@
     $(call start-emulator-library,$1) \
     $(eval LOCAL_MODULE_CLASS := EXECUTABLES)
 
+start-emulator64-program = \
+    $(call start-emulator-program, $1) \
+    $(eval LOCAL_MODULE_BITS := 64)
+
 # A varient of end-emulator-library for host programs instead
 end-emulator-program = \
     $(eval LOCAL_LDLIBS += $(QEMU_SYSTEM_LDLIBS)) \
     $(eval $(end-emulator-module-ev)) \
-    $(eval include $(BUILD_HOST_EXECUTABLE))
+    $(call include-if-bitness-$(LOCAL_MODULE_BITS), $(BUILD_HOST_EXECUTABLE))
 
 define end-emulator-module-ev
 LOCAL_CC := $$(call my-host-tool,CC)
 LOCAL_CXX := $$(call my-host-tool,CXX)
 LOCAL_AR := $$(call my-host-tool,AR)
 LOCAL_LD := $$(call my-host-tool,LD)
-LOCAL_CFLAGS := $$(call my-host-tool,CFLAGS) $$(LOCAL_CFLAGS)
-LOCAL_LDFLAGS := $$(call my-host-tool,LDFLAGS) $$(LOCAL_LDFLAGS)
-LOCAL_LDLIBS := $$(LOCAL_LDLIBS) $$(call my-host-tool,LDLIBS)
+
+LOCAL_CFLAGS := \
+    $$(call my-host-tool,CFLAGS$$(LOCAL_MODULE_BITS)) \
+    $$(call my-host-tool,CFLAGS) \
+    $$(LOCAL_CFLAGS)
+
+LOCAL_LDFLAGS := \
+    $$(call my-host-tool,LDFLAGS$$(LOCAL_MODULE_BITS)) \
+    $$(call my-host-tool,LDFLAGS) \
+    $$(LOCAL_LDFLAGS)
+
+LOCAL_LDLIBS := \
+    $$(LOCAL_LDLIBS) \
+    $$(call my-host-tool,LDLIBS) \
+    $$(call my-host-tool,LDLIBS$$(LOCAL_MODULE_BITS))
+
+# Ensure only one of -m32 or -m64 is being used and place it first.
+LOCAL_CFLAGS := \
+    -m$$(LOCAL_MODULE_BITS) \
+    $$(filter-out -m32 -m64, $$(LOCAL_CFLAGS))
+
+LOCAL_LDFLAGS := \
+    -m$$(LOCAL_MODULE_BITS) \
+    $$(filter-out -m32 -m64, $$(LOCAL_LDFLAGS))
+
 endef
 
 # The common libraries
@@ -257,6 +321,10 @@
 ifeq ($(HOST_OS),darwin)
   QEMU_SYSTEM_LDLIBS += -Wl,-framework,Cocoa,-framework,QTKit,-framework,CoreVideo
 
+  # Required to avoid compilation errors when targetting i386 with newer
+  # XCode toolchain.
+  MY_LDFLAGS32 += -Wl,-read_only_relocs,suppress
+
   # SDK 10.6+ doesn't have __dyld_func_lookup anymore. Dynamic library lookup symbols
   # are instead resolved at runtime
   OSX_VERSION_MAJOR := $(shell echo $(mac_sdk_version) | cut -d . -f 2)
@@ -266,19 +334,6 @@
   endif
 endif
 
-# This defines EMULATOR_BUILD_64BITS to indicate that 64-bit binaries
-# must be generated by the build system. For now, only do it for
-# Linux and Darwin, since we the sources do not compile with Mingw-w64
-# yet due to differing procedure call ABI conventions.
-EMULATOR_BUILD_64BITS := $(strip $(filter linux darwin,$(HOST_OS)))
-
-# Disable 64-bit build for Darwin platform builds.
-ifeq ($(HOST_OS),darwin)
-ifneq (true,$(BUILD_STANDALONE_EMULATOR))
-EMULATOR_BUILD_64BITS := $(strip $(empty))
-endif # BUILD_STANDALONE_EMULATOR != true
-endif # HOST_OS == darwin
-
 include $(LOCAL_PATH)/Makefile.common
 
 ifeq ($(HOST_OS),windows)
diff --git a/Makefile.common b/Makefile.common
index 786a7f1..12fe3bd 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -162,13 +162,11 @@
 $(call gen-hw-config-defs)
 $(call end-emulator-library)
 
-ifdef EMULATOR_BUILD_64BITS
-  $(call start-emulator-library, emulator64-common)
-  LOCAL_CFLAGS += $(common_LOCAL_CFLAGS) -m64
-  LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
-  $(call gen-hw-config-defs)
-  $(call end-emulator-library)
-endif # EMULATOR_BUILD_64BITS
+$(call start-emulator64-library, emulator64-common)
+LOCAL_CFLAGS += $(common_LOCAL_CFLAGS)
+LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
+$(call gen-hw-config-defs)
+$(call end-emulator-library)
 
 ##############################################################################
 ##############################################################################
@@ -299,13 +297,11 @@
 $(call end-emulator-library)
 
 
-ifdef EMULATOR_BUILD_64BITS
-  $(call start-emulator-library, emulator64-libui)
-  LOCAL_CFLAGS += $(common_LOCAL_CFLAGS) -m64
-  LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
-  $(call gen-hw-config-defs)
-  $(call end-emulator-library)
-endif # HOST_OS == linux || darwin
+$(call start-emulator64-library, emulator64-libui)
+LOCAL_CFLAGS += $(common_LOCAL_CFLAGS)
+LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
+$(call gen-hw-config-defs)
+$(call end-emulator-library)
 
 
 ##############################################################################
@@ -604,25 +600,23 @@
 
 
 ## another for 64-bit, see note in emulator64-common
-ifdef EMULATOR_BUILD_64BITS
-  $(call start-emulator-library, emulator64-libqemu)
-  # 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)
-  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
+$(call start-emulator64-library, emulator64-libqemu)
+# 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)
+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)
-  LOCAL_CFLAGS += $(common_LOCAL_CFLAGS) -I$(intermediates) -m64
-  LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
-  $(call gen-hw-config-defs)
-  $(call end-emulator-library)
-endif # EMULATOR_BUILD_64BITS
+LOCAL_GENERATED_SOURCES += $(QEMU_GDBSTUB_XML_C)
+LOCAL_CFLAGS += $(common_LOCAL_CFLAGS) -I$(intermediates)
+LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
+$(call gen-hw-config-defs)
+$(call end-emulator-library)
 
 
 # Block sources, we must compile them with each executable because they
@@ -682,12 +676,10 @@
 
 
 ## another for 64-bit, see note in emulator64-common
-ifdef EMULATOR_BUILD_64BITS
-  $(call start-emulator-library, emulator64-libjpeg)
-  LOCAL_CFLAGS += $(common_LOCAL_CFLAGS) -m64
-  LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
-  $(call end-emulator-library)
-endif # EMULATOR_BUILD_64BITS
+$(call start-emulator64-library, emulator64-libjpeg)
+LOCAL_CFLAGS += $(common_LOCAL_CFLAGS)
+LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
+$(call end-emulator-library)
 
 
 ##############################################################################
@@ -729,13 +721,11 @@
 $(call end-emulator-library)
 
 
-ifdef EMULATOR_BUILD_64BITS
-  $(call start-emulator-library, emulator64-libelff)
-  LOCAL_CPP_EXTENSION := .cc
-  LOCAL_CFLAGS += $(common_LOCAL_CFLAGS) -m64
-  LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
-  $(call end-emulator-library)
-endif # EMULATOR_BUILD_64BITS
+$(call start-emulator64-library, emulator64-libelff)
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_CFLAGS += $(common_LOCAL_CFLAGS)
+LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
+$(call end-emulator-library)
 
 
 ##############################################################################
diff --git a/Makefile.target b/Makefile.target
index cce444d..0feb1e7 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -275,14 +275,12 @@
 $(call gen-hx-header,qemu-options.hx,qemu-options.def,os-posix.c os-win32.c)
 $(call end-emulator-library)
 
-ifdef EMULATOR_BUILD_64BITS
-  $(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 # EMULATOR_BUILD_64BITS
+$(call start-emulator64-library, emulator64-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)
 
 ##############################################################################
 ##############################################################################
@@ -392,21 +390,18 @@
 $(call end-emulator-program)
 
 
-ifdef EMULATOR_BUILD_64BITS
-  $(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-options.hx,qemu-options.def,vl-android.c qemu-options.h)
-  $(call gen-hw-config-defs)
-  $(call end-emulator-program)
-endif # EMULATOR_BUILD_64BITS
-
+$(call start-emulator64-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)
+LOCAL_CFLAGS += $(common_LOCAL_CFLAGS)
+LOCAL_SRC_FILES += $(common_LOCAL_SRC_FILES)
+$(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)
diff --git a/Makefile.tests b/Makefile.tests
index 611fbd9..a122684 100644
--- a/Makefile.tests
+++ b/Makefile.tests
@@ -39,13 +39,10 @@
 $(call end-emulator-program)
 
 
-ifdef EMULATOR_BUILD_64BITS
-  $(call start-emulator-program, emulator64_unittests)
-  LOCAL_C_INCLUDES += $(EMULATOR_GTEST_INCLUDES)
-  LOCAL_LDLIBS += $(EMULATOR_GTEST_LDLIBS)
-  LOCAL_SRC_FILES := $(EMULATOR_UNITTESTS_SOURCES)
-  LOCAL_CFLAGS += -O0 -m64
-  LOCAL_LDLIBS += -m64
-  LOCAL_STATIC_LIBRARIES += emulator64-common emulator64-libgtest
-  $(call end-emulator-program)
-endif
+$(call start-emulator64-program, emulator64_unittests)
+LOCAL_C_INCLUDES += $(EMULATOR_GTEST_INCLUDES)
+LOCAL_LDLIBS += $(EMULATOR_GTEST_LDLIBS)
+LOCAL_SRC_FILES := $(EMULATOR_UNITTESTS_SOURCES)
+LOCAL_CFLAGS += -O0
+LOCAL_STATIC_LIBRARIES += emulator64-common emulator64-libgtest
+$(call end-emulator-program)
diff --git a/android/build/binary.make b/android/build/binary.make
index a3a4437..45279e0 100644
--- a/android/build/binary.make
+++ b/android/build/binary.make
@@ -31,14 +31,8 @@
 
 # HACK ATTACK: For the Darwin x86 build, we need to add
 # '-read_only_relocs suppress' to the linker command to avoid errors.
-# The only way to detect if we're building 32-bit or 64-bit binaries
-# is to look at -m32 and -m64 statements in the final link line, and
-# only keep the last one.
-ifeq ($(HOST_OS),darwin)
-  my_bitness := $(lastword $(filter -m32 -m64,$(LOCAL_LDFLAGS) $(LOCAL_LDLIBS) $(LOCAL_CFLAGS)))
-  ifeq (-m32,$(my_bitness))
-    LOCAL_LDLIBS += -Wl,-read_only_relocs,suppress
-  endif
+ifeq ($(HOST_OS)-$(LOCAL_MODULE_BITS),darwin-32)
+  LOCAL_LDLIBS += -Wl,-read_only_relocs,suppress
 endif
 
 $(foreach src,$(LOCAL_C_SOURCES), \
diff --git a/android/build/common.sh b/android/build/common.sh
index 965a462..992b769 100644
--- a/android/build/common.sh
+++ b/android/build/common.sh
@@ -291,16 +291,9 @@
     fi
     link
     if [ $? != 0 ] ; then
-        OLD_LD=$LD
-        LD=gcc
-        compile
-        link
-        if [ $? != 0 ] ; then
-            LD=$OLD_LD
-            echo "your linker doesn't seem to work:"
-            cat $TMPL
-            clean_exit
-        fi
+        echo "your linker doesn't seem to work:"
+        cat $TMPL
+        clean_exit
     fi
     log "LD         : linker check ok ($LD)"
 
diff --git a/android/build/definitions.make b/android/build/definitions.make
index 664b28c..b64e13e 100644
--- a/android/build/definitions.make
+++ b/android/build/definitions.make
@@ -84,7 +84,7 @@
 OBJ:=$$(LOCAL_OBJS_DIR)/$$(SRC:%.c=%.o)
 LOCAL_OBJECTS += $$(OBJ)
 DEPENDENCY_DIRS += $$(dir $$(OBJ))
-$$(OBJ): PRIVATE_CFLAGS := $$(call local-host-tool,CFLAGS) $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
+$$(OBJ): PRIVATE_CFLAGS := $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
 $$(OBJ): PRIVATE_CC     := $$(LOCAL_CC)
 $$(OBJ): PRIVATE_OBJ    := $$(OBJ)
 $$(OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE)
@@ -104,7 +104,7 @@
 OBJ:=$$(LOCAL_OBJS_DIR)/$$(SRC:%$(LOCAL_CPP_EXTENSION)=%.o)
 LOCAL_OBJECTS += $$(OBJ)
 DEPENDENCY_DIRS += $$(dir $$(OBJ))
-$$(OBJ): PRIVATE_CFLAGS := $$(call local-host-tool,CFLAGS) $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
+$$(OBJ): PRIVATE_CFLAGS := $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
 $$(OBJ): PRIVATE_CXX    := $$(LOCAL_CC)
 $$(OBJ): PRIVATE_OBJ    := $$(OBJ)
 $$(OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE)
@@ -124,7 +124,7 @@
 OBJ:=$$(LOCAL_OBJS_DIR)/$$(notdir $$(SRC:%.m=%.o))
 LOCAL_OBJECTS += $$(OBJ)
 DEPENDENCY_DIRS += $$(dir $$(OBJ))
-$$(OBJ): PRIVATE_CFLAGS := $$(call local-host-tool,CFLAGS) $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
+$$(OBJ): PRIVATE_CFLAGS := $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
 $$(OBJ): PRIVATE_CC     := $$(LOCAL_CC)
 $$(OBJ): PRIVATE_OBJ    := $$(OBJ)
 $$(OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE)
@@ -144,7 +144,7 @@
 OBJ:=$$(LOCAL_OBJS_DIR)/$$(notdir $$(SRC:%.c=%.o))
 LOCAL_OBJECTS += $$(OBJ)
 DEPENDENCY_DIRS += $$(dir $$(OBJ))
-$$(OBJ): PRIVATE_CFLAGS := $$(call local-host-tool,CFLAGS) $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
+$$(OBJ): PRIVATE_CFLAGS := $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
 $$(OBJ): PRIVATE_CC     := $$(LOCAL_CC)
 $$(OBJ): PRIVATE_OBJ    := $$(OBJ)
 $$(OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE)
@@ -162,7 +162,7 @@
 OBJ:=$$(LOCAL_OBJS_DIR)/$$(notdir $$(SRC:%$(LOCAL_CPP_EXTENSION)=%.o))
 LOCAL_OBJECTS += $$(OBJ)
 DEPENDENCY_DIRS += $$(dir $$(OBJ))
-$$(OBJ): PRIVATE_CFLAGS := $$(call local-host-tool,CFLAGS) $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
+$$(OBJ): PRIVATE_CFLAGS := $$(LOCAL_CFLAGS) -I$$(LOCAL_PATH) -I$$(LOCAL_OBJS_DIR)
 $$(OBJ): PRIVATE_CXX    := $$(LOCAL_CC)
 $$(OBJ): PRIVATE_OBJ    := $$(OBJ)
 $$(OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE)
diff --git a/distrib/googletest/Android.mk b/distrib/googletest/Android.mk
index 1d0f7b6..8b5a59d 100644
--- a/distrib/googletest/Android.mk
+++ b/distrib/googletest/Android.mk
@@ -30,14 +30,12 @@
 LOCAL_SRC_FILES := $(EMULATOR_GTEST_SOURCES)
 $(call end-emulator-library)
 
-ifdef EMULATOR_BUILD_64BITS
-    $(call start-emulator-library, emulator64-libgtest)
-    LOCAL_C_INCLUDES += $(EMULATOR_GTEST_INCLUDES)
-    LOCAL_CPP_EXTENSION := .cc
-    LOCAL_CFLAGS += -O0 -m64
-    LOCAL_SRC_FILES := $(EMULATOR_GTEST_SOURCES)
-    $(call end-emulator-library)
-endif
+$(call start-emulator64-library, emulator64-libgtest)
+LOCAL_C_INCLUDES += $(EMULATOR_GTEST_INCLUDES)
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_CFLAGS += -O0
+LOCAL_SRC_FILES := $(EMULATOR_GTEST_SOURCES)
+$(call end-emulator-library)
 
 LOCAL_PATH := $(old_LOCAL_PATH)
 
diff --git a/distrib/mini-glib/sources.make b/distrib/mini-glib/sources.make
index d442f52..1f65a1e 100644
--- a/distrib/mini-glib/sources.make
+++ b/distrib/mini-glib/sources.make
@@ -4,4 +4,9 @@
 
 GLIB_SOURCES := \
     $(GLIB_DIR)/src/glib-mini.c \
-    $(GLIB_DIR)/src/glib-mini-win32.c
+
+ifeq ($(HOST_OS),windows)
+GLIB_SOURCES += \
+    $(GLIB_DIR)/src/glib-mini-win32.c \
+
+endif
diff --git a/distrib/sdl-1.2.15/sources.make b/distrib/sdl-1.2.15/sources.make
index ea0a5e9..0c988ce 100644
--- a/distrib/sdl-1.2.15/sources.make
+++ b/distrib/sdl-1.2.15/sources.make
@@ -236,13 +236,10 @@
 LOCAL_SRC_FILES := $(SDL_SOURCES)
 $(call end-emulator-library)
 
-ifdef EMULATOR_BUILD_64BITS
-$(call start-emulator-library,emulator_lib64SDL)
+$(call start-emulator64-library,emulator_lib64SDL)
 LOCAL_SRC_FILES := $(SDL_SOURCES)
 LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
-LOCAL_CFLAGS += -m64 -fPIC
 $(call end-emulator-library)
-endif  # EMULATOR_BUILD_64BITS
 
 ## Build libSDLmain
 ##
@@ -269,13 +266,10 @@
 LOCAL_SRC_FILES := $(SDLMAIN_SOURCES)
 $(call end-emulator-library)
 
-ifdef EMULATOR_BUILD_64BITS
-  $(call start-emulator-library,emulator_lib64SDLmain)
-  LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
-  LOCAL_SRC_FILES := $(SDLMAIN_SOURCES)
-  LOCAL_CFLAGS += -m64
-  $(call end-emulator-library)
-endif  # EMULATOR_BUILD_64BITS
+$(call start-emulator64-library,emulator_lib64SDLmain)
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
+LOCAL_SRC_FILES := $(SDLMAIN_SOURCES)
+$(call end-emulator-library)
 
 # Restore LOCAL_PATH
 LOCAL_PATH := $(SDL_OLD_LOCAL_PATH)