Merge "Extend the CPU ABI specification mechanism."
diff --git a/core/64_bit_blacklist.mk b/core/64_bit_blacklist.mk
index 5ad7b51..75e1e33 100644
--- a/core/64_bit_blacklist.mk
+++ b/core/64_bit_blacklist.mk
@@ -16,10 +16,6 @@
 	device/generic/goldfish/opengl \
 	device/generic/goldfish/camera \
 
-# not needed yet, and too many directories to blacklist individually
-_64_bit_directory_blacklist += \
-	frameworks/av/media/libeffects \
-
 _64_bit_directory_blacklist_pattern := $(addsuffix %,$(_64_bit_directory_blacklist))
 
 define directory_is_64_bit_blacklisted
diff --git a/core/base_rules.mk b/core/base_rules.mk
index 32e6b73..43f351a 100644
--- a/core/base_rules.mk
+++ b/core/base_rules.mk
@@ -99,8 +99,21 @@
   $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS must contain exactly one word, not "$(LOCAL_MODULE_CLASS)")
 endif
 
+ifndef LOCAL_IS_HOST_MODULE
+my_32_64_bit_suffix := $(if $($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET_IS_64_BIT),64,32)
+endif
+
 ifneq (true,$(LOCAL_UNINSTALLABLE_MODULE))
+ifndef LOCAL_IS_HOST_MODULE
+my_multilib_module_path := $(strip $(LOCAL_MODULE_PATH_$(my_32_64_bit_suffix)))
+else
+my_multilib_module_path :=
+endif
+ifdef my_multilib_module_path
+my_module_path := $(my_multilib_module_path)
+else
 my_module_path := $(strip $(LOCAL_MODULE_PATH))
+endif
 my_module_relative_path := $(strip $(LOCAL_MODULE_RELATIVE_PATH))
 ifeq ($(my_module_path),)
   ifdef LOCAL_IS_HOST_MODULE
@@ -154,17 +167,7 @@
 ###########################################################
 # Pick a name for the intermediate and final targets
 ###########################################################
-ifndef LOCAL_MODULE_STEM
-  LOCAL_MODULE_STEM := $(LOCAL_MODULE)
-endif
-
-ifndef LOCAL_BUILT_MODULE_STEM
-  LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE_STEM)$(LOCAL_MODULE_SUFFIX)
-endif
-
-ifndef LOCAL_INSTALLED_MODULE_STEM
-  LOCAL_INSTALLED_MODULE_STEM := $(LOCAL_MODULE_STEM)$(LOCAL_MODULE_SUFFIX)
-endif
+include $(BUILD_SYSTEM)/configure_module_stem.mk
 
 # OVERRIDE_BUILT_MODULE_PATH is only allowed to be used by the
 # internal SHARED_LIBRARIES build files.
diff --git a/core/binary.mk b/core/binary.mk
index 5011129..cc8de46 100644
--- a/core/binary.mk
+++ b/core/binary.mk
@@ -106,8 +106,6 @@
 my_generated_sources := $(LOCAL_GENERATED_SOURCES)
 
 ifndef LOCAL_IS_HOST_MODULE
-my_32_64_bit_suffix := $(if $($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET_IS_64_BIT),64,32)
-
 my_src_files += $(LOCAL_SRC_FILES_$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_SRC_FILES_$(my_32_64_bit_suffix))
 my_shared_libraries += $(LOCAL_SHARED_LIBRARIES_$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_SHARED_LIBRARIES_$(my_32_64_bit_suffix))
 my_cflags += $(LOCAL_CFLAGS_$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_CFLAGS_$(my_32_64_bit_suffix))
diff --git a/core/clang/HOST_x86.mk b/core/clang/HOST_x86.mk
index f38f04e..ee61e5e 100644
--- a/core/clang/HOST_x86.mk
+++ b/core/clang/HOST_x86.mk
@@ -1,8 +1,13 @@
 
 include $(BUILD_SYSTEM)/clang/x86.mk
+include $(BUILD_SYSTEM)/clang/x86_common.mk
 
 ifeq ($(HOST_OS),linux)
 CLANG_CONFIG_x86_HOST_TRIPLE := i686-linux-gnu
+CLANG_CONFIG_x86_HOST_COMBO_EXTRA_ASFLAGS := $(CLANG_CONFIG_x86_LINUX_HOST_EXTRA_ASFLAGS)
+CLANG_CONFIG_x86_HOST_COMBO_EXTRA_CFLAGS := $(CLANG_CONFIG_x86_LINUX_HOST_EXTRA_CFLAGS)
+CLANG_CONFIG_x86_HOST_COMBO_EXTRA_CPPFLAGS := $(CLANG_CONFIG_x86_LINUX_HOST_EXTRA_CPPFLAGS)
+CLANG_CONFIG_x86_HOST_COMBO_EXTRA_LDFLAGS := $(CLANG_CONFIG_x86_LINUX_HOST_EXTRA_LDFLAGS)
 endif
 ifeq ($(HOST_OS),darwin)
 CLANG_CONFIG_x86_HOST_TRIPLE := i686-apple-darwin
@@ -15,25 +20,29 @@
   $(CLANG_CONFIG_EXTRA_ASFLAGS) \
   $(CLANG_CONFIG_HOST_EXTRA_ASFLAGS) \
   $(CLANG_CONFIG_x86_EXTRA_ASFLAGS) \
-  -target $(CLANG_CONFIG_x86_HOST_TRIPLE) \
+  $(CLANG_CONFIG_x86_HOST_COMBO_EXTRA_ASFLAGS) \
+  -target $(CLANG_CONFIG_x86_HOST_TRIPLE)
 
 CLANG_CONFIG_x86_HOST_EXTRA_CFLAGS := \
   $(CLANG_CONFIG_EXTRA_CFLAGS) \
   $(CLANG_CONFIG_HOST_EXTRA_CFLAGS) \
   $(CLANG_CONFIG_x86_EXTRA_CFLAGS) \
+  $(CLANG_CONFIG_x86_HOST_COMBO_EXTRA_CFLAGS) \
   $(CLANG_CONFIG_x86_HOST_EXTRA_ASFLAGS)
 
 CLANG_CONFIG_x86_HOST_EXTRA_CPPFLAGS := \
   $(CLANG_CONFIG_EXTRA_CPPFLAGS) \
   $(CLANG_CONFIG_HOST_EXTRA_CPPFLAGS) \
   $(CLANG_CONFIG_x86_EXTRA_CPPFLAGS) \
+  $(CLANG_CONFIG_x86_HOST_COMBO_EXTRA_CPPFLAGS) \
+  -target $(CLANG_CONFIG_x86_HOST_TRIPLE)
 
 CLANG_CONFIG_x86_HOST_EXTRA_LDFLAGS := \
   $(CLANG_CONFIG_EXTRA_LDFLAGS) \
   $(CLANG_CONFIG_HOST_EXTRA_LDFLAGS) \
   $(CLANG_CONFIG_x86_EXTRA_LDFLAGS) \
-  -target $(CLANG_CONFIG_x86_HOST_TRIPLE) \
-
+  $(CLANG_CONFIG_x86_HOST_COMBO_EXTRA_LDFLAGS) \
+  -target $(CLANG_CONFIG_x86_HOST_TRIPLE)
 
 define convert-to-host-clang-flags
   $(strip \
diff --git a/core/clang/HOST_x86_64.mk b/core/clang/HOST_x86_64.mk
index ea33a46..d60e15f 100644
--- a/core/clang/HOST_x86_64.mk
+++ b/core/clang/HOST_x86_64.mk
@@ -1,8 +1,13 @@
 
 include $(BUILD_SYSTEM)/clang/x86_64.mk
+include $(BUILD_SYSTEM)/clang/x86_common.mk
 
 ifeq ($(HOST_OS),linux)
 CLANG_CONFIG_x86_64_HOST_TRIPLE := x86_64-linux-gnu
+CLANG_CONFIG_x86_64_HOST_COMBO_EXTRA_ASFLAGS := $(CLANG_CONFIG_x86_LINUX_HOST_EXTRA_ASFLAGS)
+CLANG_CONFIG_x86_64_HOST_COMBO_EXTRA_CFLAGS := $(CLANG_CONFIG_x86_LINUX_HOST_EXTRA_CFLAGS)
+CLANG_CONFIG_x86_64_HOST_COMBO_EXTRA_CPPFLAGS := $(CLANG_CONFIG_x86_LINUX_HOST_EXTRA_CPPFLAGS)
+CLANG_CONFIG_x86_64_HOST_COMBO_EXTRA_LDFLAGS := $(CLANG_CONFIG_x86_LINUX_HOST_EXTRA_LDFLAGS)
 endif
 ifeq ($(HOST_OS),darwin)
 CLANG_CONFIG_x86_64_HOST_TRIPLE := x86_64-apple-darwin
@@ -15,25 +20,29 @@
   $(CLANG_CONFIG_EXTRA_ASFLAGS) \
   $(CLANG_CONFIG_HOST_EXTRA_ASFLAGS) \
   $(CLANG_CONFIG_x86_64_EXTRA_ASFLAGS) \
-  -target $(CLANG_CONFIG_x86_64_HOST_TRIPLE) \
+  $(CLANG_CONFIG_x86_64_HOST_COMBO_EXTRA_ASFLAGS) \
+  -target $(CLANG_CONFIG_x86_64_HOST_TRIPLE)
 
 CLANG_CONFIG_x86_64_HOST_EXTRA_CFLAGS := \
   $(CLANG_CONFIG_EXTRA_CFLAGS) \
   $(CLANG_CONFIG_HOST_EXTRA_CFLAGS) \
   $(CLANG_CONFIG_x86_64_EXTRA_CFLAGS) \
+  $(CLANG_CONFIG_x86_64_HOST_COMBO_EXTRA_CFLAGS) \
   $(CLANG_CONFIG_x86_64_HOST_EXTRA_ASFLAGS)
 
 CLANG_CONFIG_x86_64_HOST_EXTRA_CPPFLAGS := \
   $(CLANG_CONFIG_EXTRA_CPPFLAGS) \
   $(CLANG_CONFIG_HOST_EXTRA_CPPFLAGS) \
   $(CLANG_CONFIG_x86_64_EXTRA_CPPFLAGS) \
+  $(CLANG_CONFIG_x86_64_HOST_COMBO_EXTRA_CPPFLAGS) \
+  -target $(CLANG_CONFIG_x86_64_HOST_TRIPLE)
 
 CLANG_CONFIG_x86_64_HOST_EXTRA_LDFLAGS := \
   $(CLANG_CONFIG_EXTRA_LDFLAGS) \
   $(CLANG_CONFIG_HOST_EXTRA_LDFLAGS) \
   $(CLANG_CONFIG_x86_64_EXTRA_LDFLAGS) \
-  -target $(CLANG_CONFIG_x86_64_HOST_TRIPLE) \
-
+  $(CLANG_CONFIG_x86_64_HOST_COMBO_EXTRA_LDFLAGS) \
+  -target $(CLANG_CONFIG_x86_64_HOST_TRIPLE)
 
 define convert-to-host-clang-flags
   $(strip \
diff --git a/core/clang/config.mk b/core/clang/config.mk
index 96f860d..7a513c6 100644
--- a/core/clang/config.mk
+++ b/core/clang/config.mk
@@ -38,10 +38,10 @@
   -Wno-unused-but-set-parameter
 
 # Clang flags for all host rules
-CLANG_CONFIG_HOST_EXTRA_ASFLAGS :=
-CLANG_CONFIG_HOST_EXTRA_CFLAGS :=
-CLANG_CONFIG_HOST_EXTRA_CPPFLAGS :=
-CLANG_CONFIG_HOST_EXTRA_LDFLAGS :=
+CLANG_CONFIG_HOST_EXTRA_ASFLAGS := --gcc-toolchain=$(HOST_TOOLCHAIN_FOR_CLANG)
+CLANG_CONFIG_HOST_EXTRA_CFLAGS := --gcc-toolchain=$(HOST_TOOLCHAIN_FOR_CLANG)
+CLANG_CONFIG_HOST_EXTRA_CPPFLAGS := --gcc-toolchain=$(HOST_TOOLCHAIN_FOR_CLANG)
+CLANG_CONFIG_HOST_EXTRA_LDFLAGS := --gcc-toolchain=$(HOST_TOOLCHAIN_FOR_CLANG)
 
 # Clang flags for all target rules
 CLANG_CONFIG_TARGET_EXTRA_ASFLAGS :=
@@ -50,7 +50,11 @@
 CLANG_CONFIG_TARGET_EXTRA_LDFLAGS :=
 
 # HOST config
+ifneq ($(strip $(BUILD_HOST_64bit)),)
+include $(BUILD_SYSTEM)/clang/HOST_x86_64.mk
+else
 include $(BUILD_SYSTEM)/clang/HOST_$(HOST_ARCH).mk
+endif
 
 # TARGET config
 clang_2nd_arch_prefix :=
diff --git a/core/clang/x86_common.mk b/core/clang/x86_common.mk
new file mode 100644
index 0000000..d75d83f
--- /dev/null
+++ b/core/clang/x86_common.mk
@@ -0,0 +1,42 @@
+ifeq ($(HOST_OS),darwin)
+# nothing required here yet
+endif
+
+ifeq ($(HOST_OS),linux)
+
+ifneq ($(strip $(BUILD_HOST_64bit)),)
+# Needs to be updated along with gcc
+HOST_ARCH_DESCRIPTOR_FOR_CLANG := x86_64-linux
+else
+# Needs to be updated along with gcc
+HOST_ARCH_DESCRIPTOR_FOR_CLANG := i686-linux
+endif
+
+CLANG_CONFIG_x86_LINUX_HOST_EXTRA_ASFLAGS := \
+  --sysroot=$(HOST_TOOLCHAIN_FOR_CLANG)/sysroot
+
+CLANG_CONFIG_x86_LINUX_HOST_EXTRA_CFLAGS :=
+
+CLANG_CONFIG_x86_LINUX_HOST_EXTRA_CPPFLAGS :=   \
+  --sysroot=$(HOST_TOOLCHAIN_FOR_CLANG)/sysroot \
+  -isystem $(HOST_TOOLCHAIN_FOR_CLANG)/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG)/include/c++/4.6.x-google \
+  -isystem $(HOST_TOOLCHAIN_FOR_CLANG)/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG)/include/c++/4.6.x-google/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG) \
+  -isystem $(HOST_TOOLCHAIN_FOR_CLANG)/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG)/include/c++/4.6.x-google/backward \
+
+CLANG_CONFIG_x86_LINUX_HOST_EXTRA_LDFLAGS := \
+  --sysroot=$(HOST_TOOLCHAIN_FOR_CLANG)/sysroot \
+  -B$(HOST_TOOLCHAIN_FOR_CLANG)/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG)/bin \
+  -B$(HOST_TOOLCHAIN_FOR_CLANG)/lib/gcc/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG)/4.6.x-google \
+  -L$(HOST_TOOLCHAIN_FOR_CLANG)/lib/gcc/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG)/4.6.x-google
+
+ifneq ($(strip $(BUILD_HOST_64bit)),)
+# need to add lib64 if building 64-bit, otherwise lib
+CLANG_CONFIG_x86_LINUX_HOST_EXTRA_LDFLAGS += -L$(HOST_TOOLCHAIN_FOR_CLANG)/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG)/lib64/
+else
+CLANG_CONFIG_x86_LINUX_HOST_EXTRA_LDFLAGS += -L$(HOST_TOOLCHAIN_FOR_CLANG)/$(HOST_ARCH_DESCRIPTOR_FOR_CLANG)/lib/
+endif
+endif # linux
+
+ifeq ($(HOST_OS),windows)
+# nothing required here yet
+endif
\ No newline at end of file
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index 4c403b4..ed692b9 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -197,6 +197,11 @@
 LOCAL_ASFLAGS_64:=
 LOCAL_C_INCLUDES_32:=
 LOCAL_C_INCLUDES_64:=
+LOCAL_MODULE_PATH_32:=
+LOCAL_MODULE_PATH_64:=
+LOCAL_MODULE_STEM_32:=
+LOCAL_MODULE_STEM_64:=
+LOCAL_MULTILIB:=
 
 # Trim MAKEFILE_LIST so that $(call my-dir) doesn't need to
 # iterate over thousands of entries every time.
diff --git a/core/combo/HOST_darwin-x86.mk b/core/combo/HOST_darwin-x86.mk
index 037dee6..994196a 100644
--- a/core/combo/HOST_darwin-x86.mk
+++ b/core/combo/HOST_darwin-x86.mk
@@ -156,3 +156,7 @@
 define get-file-size
 stat -f "%z" $(1)
 endef
+
+# gcc location for clang; to be updated when clang is updated
+# HOST_TOOLCHAIN_ROOT is a Darwin-specific define
+HOST_TOOLCHAIN_FOR_CLANG := $(HOST_TOOLCHAIN_ROOT)
\ No newline at end of file
diff --git a/core/combo/HOST_linux-x86.mk b/core/combo/HOST_linux-x86.mk
index 129acb0..3c87264 100644
--- a/core/combo/HOST_linux-x86.mk
+++ b/core/combo/HOST_linux-x86.mk
@@ -41,10 +41,14 @@
 # which can benefit from 64-bit host arch.
 HOST_GLOBAL_CFLAGS += -m64 -Wa,--noexecstack
 HOST_GLOBAL_LDFLAGS += -m64 -Wl,-z,noexecstack
+# gcc location for clang; to be updated when clang is updated
+HOST_TOOLCHAIN_FOR_CLANG := prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/
 else
 # We expect SSE3 floating point math.
 HOST_GLOBAL_CFLAGS += -mstackrealign -msse3 -mfpmath=sse -m32 -Wa,--noexecstack
 HOST_GLOBAL_LDFLAGS += -m32 -Wl,-z,noexecstack
+# gcc location for clang; to be updated when clang is updated
+HOST_TOOLCHAIN_FOR_CLANG := prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6/
 endif # BUILD_HOST_64bit
 
 ifneq ($(strip $(BUILD_HOST_static)),)
diff --git a/core/combo/javac.mk b/core/combo/javac.mk
index cc2c872..8c24e4b 100644
--- a/core/combo/javac.mk
+++ b/core/combo/javac.mk
@@ -27,7 +27,7 @@
     COMMON_JAVAC := development/host/windows/prebuilt/javawrap.exe -J-Xmx256m \
         $(common_flags)
 else
-    COMMON_JAVAC := $(JAVACC) -J-Xmx512M $(common_flags)
+    COMMON_JAVAC := $(JAVACC) -J-Xmx768M $(common_flags)
 endif
 
 # Eclipse.
diff --git a/core/configure_module_stem.mk b/core/configure_module_stem.mk
new file mode 100644
index 0000000..2823e37
--- /dev/null
+++ b/core/configure_module_stem.mk
@@ -0,0 +1,16 @@
+my_multilib_stem := $(LOCAL_MODULE_STEM_$(if $($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET_IS_64_BIT),64,32))
+ifdef my_multilib_stem
+LOCAL_MODULE_STEM := $(my_multilib_stem)
+endif
+
+ifndef LOCAL_MODULE_STEM
+  LOCAL_MODULE_STEM := $(LOCAL_MODULE)
+endif
+
+ifndef LOCAL_BUILT_MODULE_STEM
+  LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE_STEM)$(LOCAL_MODULE_SUFFIX)
+endif
+
+ifndef LOCAL_INSTALLED_MODULE_STEM
+  LOCAL_INSTALLED_MODULE_STEM := $(LOCAL_MODULE_STEM)$(LOCAL_MODULE_SUFFIX)
+endif
diff --git a/core/dex_preopt_libart.mk b/core/dex_preopt_libart.mk
index 0318505..d5ec40c 100644
--- a/core/dex_preopt_libart.mk
+++ b/core/dex_preopt_libart.mk
@@ -43,7 +43,7 @@
 ifeq ($(TARGET_ARCH),mips)
 LIBART_IMG_TARGET_BASE_ADDRESS := 0x30000000
 else
-LIBART_IMG_TARGET_BASE_ADDRESS := 0x60000000
+LIBART_IMG_TARGET_BASE_ADDRESS := 0x70000000
 endif
 
 ########################################################################
diff --git a/core/dynamic_binary.mk b/core/dynamic_binary.mk
index 32f2c91..b3ca4fc 100644
--- a/core/dynamic_binary.mk
+++ b/core/dynamic_binary.mk
@@ -13,20 +13,9 @@
 endif
 
 # The name of the target file, without any path prepended.
-# TODO: This duplicates logic from base_rules.mk because we need to
-#       know its results before base_rules.mk is included.
-#       Consolidate the duplicates.
-ifndef LOCAL_MODULE_STEM
-  LOCAL_MODULE_STEM := $(LOCAL_MODULE)
-endif
-
-ifndef LOCAL_BUILT_MODULE_STEM
-  LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE_STEM)$(LOCAL_MODULE_SUFFIX)
-endif
-
-ifndef LOCAL_INSTALLED_MODULE_STEM
-  LOCAL_INSTALLED_MODULE_STEM := $(LOCAL_MODULE_STEM)$(LOCAL_MODULE_SUFFIX)
-endif
+# This duplicates logic from base_rules.mk because we need to
+# know its results before base_rules.mk is included.
+include $(BUILD_SYSTEM)/configure_module_stem.mk
 
 # base_rules.make defines $(intermediates), but we need its value
 # before we include base_rules.  Make a guess, and verify that
diff --git a/core/executable.mk b/core/executable.mk
index 899a02f..1e8d73a 100644
--- a/core/executable.mk
+++ b/core/executable.mk
@@ -1,31 +1,68 @@
 # We don't automatically set up rules to build executables for both
 # TARGET_ARCH and TARGET_2ND_ARCH.
 # By default, an executable is built for TARGET_ARCH.
-# To build it for TARGET_2ND_ARCH in a 64bit product, use "LOCAL_32_BIT_ONLY := true".
+# To build it for TARGET_2ND_ARCH in a 64bit product, use "LOCAL_MULTILIB := 32"
+# To build it for both set LOCAL_MULTILIB := both and specify
+# LOCAL_MODULE_PATH_32 and LOCAL_MODULE_PATH_64 or LOCAL_MODULE_STEM_32 and
+# LOCAL_MODULE_STEM_64
+
+include $(BUILD_SYSTEM)/multilib.mk
 
 ifeq ($(TARGET_PREFER_32_BIT),true)
-ifneq ($(LOCAL_NO_2ND_ARCH),true)
-LOCAL_32_BIT_ONLY := true
+ifeq (,$(filter $(my_module_multilib),first both)
+# if TARGET_PREFER_32_BIT is not explicitly set to "first" or "both"
+# build only for secondary
+my_module_multilib := 32
 endif
 endif
 
+ifndef my_module_multilib
+# executables default to building for the first architecture
+my_module_multilib := first
+endif
+
+ifeq ($(my_module_multilib),both)
+ifeq ($(LOCAL_MODULE_PATH_32)$(LOCAL_MODULE_STEM_32),)
+$(error $(LOCAL_PATH): LOCAL_MODULE_STEM_32 or LOCAL_MODULE_PATH_32 is required for LOCAL_MULTILIB := both for module $(LOCAL_MODULE))
+endif
+ifeq ($(LOCAL_MODULE_PATH_64)$(LOCAL_MODULE_STEM_64),)
+$(error $(LOCAL_PATH): LOCAL_MODULE_STEM_64 or LOCAL_MODULE_PATH_64 is required for LOCAL_MULTILIB := both for module $(LOCAL_MODULE))
+endif
+else #!LOCAL_MULTILIB == both
 LOCAL_NO_2ND_ARCH_MODULE_SUFFIX := true
+endif
 
-# check if primary arch is supported
+my_skip_secondary_arch :=
+
+# check if first arch is supported
 include $(BUILD_SYSTEM)/module_arch_supported.mk
 ifeq ($(my_module_arch_supported),true)
-# primary arch is supported
+# first arch is supported
 include $(BUILD_SYSTEM)/executable_internal.mk
-else ifneq (,$(TARGET_2ND_ARCH))
+ifneq ($(my_module_multilib),both)
+my_skip_secondary_arch := true
+endif
+endif
+
+# check if first arch was not supported or asked to build both
+ifndef my_skip_secondary_arch
+ifdef TARGET_2ND_ARCH
 # check if secondary arch is supported
 LOCAL_2ND_ARCH_VAR_PREFIX := $(TARGET_2ND_ARCH_VAR_PREFIX)
 include $(BUILD_SYSTEM)/module_arch_supported.mk
 ifeq ($(my_module_arch_supported),true)
 # secondary arch is supported
+OVERRIDE_BUILT_MODULE_PATH :=
+LOCAL_BUILT_MODULE :=
+LOCAL_INSTALLED_MODULE :=
+LOCAL_MODULE_STEM :=
+LOCAL_BUILT_MODULE_STEM :=
+LOCAL_INSTALLED_MODULE_STEM :=
+LOCAL_INTERMEDIATE_TARGETS :=
 include $(BUILD_SYSTEM)/executable_internal.mk
 endif
 endif # TARGET_2ND_ARCH
-
+endif # !my_skip_secondary_arch || LOCAL_MULTILIB
 LOCAL_2ND_ARCH_VAR_PREFIX :=
 LOCAL_NO_2ND_ARCH_MODULE_SUFFIX :=
 
diff --git a/core/module_arch_supported.mk b/core/module_arch_supported.mk
index ddfd67d..1e03914 100644
--- a/core/module_arch_supported.mk
+++ b/core/module_arch_supported.mk
@@ -2,8 +2,7 @@
 ## Determine if a module can be built for an arch
 ##
 ## Inputs from module makefile:
-## LOCAL_32_BIT_ONLY
-## LOCAL_NO_2ND_ARCH
+## my_module_multilib
 ## LOCAL_MODULE_TARGET_ARCH
 ## LOCAL_MODULE_TARGET_ARCH_WARN
 ## LOCAL_MODULE_UNSUPPORTED_TARGET_ARCH
@@ -19,17 +18,21 @@
 
 my_module_arch_supported := true
 
+ifeq ($(my_module_multilib),none)
+my_module_arch_supported := false
+endif
+
 ifeq ($(LOCAL_2ND_ARCH_VAR_PREFIX),)
-ifeq ($(TARGET_IS_64_BIT)|$(LOCAL_32_BIT_ONLY),true|true)
+ifeq ($(TARGET_IS_64_BIT)|$(my_module_multilib),true|32)
 my_module_arch_supported := false
 else ifeq ($(call directory_is_64_bit_blacklisted,$(LOCAL_PATH)),true)
 my_module_arch_supported := false
 endif
 else # LOCAL_2ND_ARCH_VAR_PREFIX
-ifeq ($(LOCAL_NO_2ND_ARCH),true)
+ifeq ($(my_module_multilib),first)
 my_module_arch_supported := false
 endif
-endif # !LOCAL_2ND_ARCH_VAR_PREFIX
+endif # LOCAL_2ND_ARCH_VAR_PREFIX
 
 ifneq (,$(LOCAL_MODULE_TARGET_ARCH))
 ifeq (,$(filter $(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH),$(LOCAL_MODULE_TARGET_ARCH)))
diff --git a/core/multilib.mk b/core/multilib.mk
new file mode 100644
index 0000000..fa5397a
--- /dev/null
+++ b/core/multilib.mk
@@ -0,0 +1,26 @@
+# Translate LOCAL_32_BIT_ONLY and LOCAL_NO_2ND_ARCH to LOCAL_MULTILIB,
+# and check LOCAL_MULTILIB is a valid value.  Returns module's multilib
+# setting in my_module_multilib, or empty if not set.
+
+my_module_multilib := $(strip $(LOCAL_MULTILIB))
+ifndef my_module_multilib
+ifeq ($(LOCAL_32_BIT_ONLY)|$(LOCAL_NO_2ND_ARCH),true|true)
+ifdef TARGET_2ND_ARCH
+# Both LOCAL_32_BIT_ONLY and LOCAL_NO_2ND_ARCH specified on 64-bit target
+# skip the module completely
+my_module_multilib := none
+else
+# Both LOCAL_32_BIT_ONLY and LOCAL_NO_2ND_ARCH specified on 32-bit target
+# build for 32-bit
+my_module_multilib := 32
+endif
+else ifeq ($(LOCAL_32_BIT_ONLY),true)
+my_module_multilib := 32
+else ifeq ($(LOCAL_NO_2ND_ARCH),true)
+my_module_multilib := first
+endif
+else # my_module_multilib defined
+ifeq (,$(filter 32 first both none,$(my_module_multilib)))
+$(error $(LOCAL_PATH): Invalid LOCAL_MULTILIB specified for module $(LOCAL_MODULE))
+endif
+endif # my_module_multilib defined
diff --git a/core/package.mk b/core/package.mk
index b40ac37..337d3d0 100644
--- a/core/package.mk
+++ b/core/package.mk
@@ -1,15 +1,23 @@
 # We don't automatically set up rules to build packages for both
 # TARGET_ARCH and TARGET_2ND_ARCH.
 # By default, an package is built for TARGET_ARCH.
-# To build it for TARGET_2ND_ARCH in a 64bit product, use "LOCAL_32_BIT_ONLY := true".
+# To build it for TARGET_2ND_ARCH in a 64bit product, use "LOCAL_MULTILIB := 32".
+
+include $(BUILD_SYSTEM)/multilib.mk
+
+ifndef my_module_multilib
+# packages default to building for either architecture,
+# the first if its supported, otherwise the second.
+my_module_multilib := both
+endif
 
 LOCAL_NO_2ND_ARCH_MODULE_SUFFIX := true
 
-# check if primary arch is supported
+# check if first arch is supported
 LOCAL_2ND_ARCH_VAR_PREFIX :=
 include $(BUILD_SYSTEM)/module_arch_supported.mk
 ifeq ($(my_module_arch_supported),true)
-# primary arch is supported
+# first arch is supported
 include $(BUILD_SYSTEM)/package_internal.mk
 else ifneq (,$(TARGET_2ND_ARCH))
 # check if secondary arch is supported
diff --git a/core/prebuilt.mk b/core/prebuilt.mk
index 027c80a..5f91110 100644
--- a/core/prebuilt.mk
+++ b/core/prebuilt.mk
@@ -9,10 +9,19 @@
 ifdef LOCAL_IS_HOST_MODULE
 include $(BUILD_SYSTEM)/prebuilt_internal.mk
 else #!LOCAL_IS_HOST_MODULE
-# check if primary arch is supported
+
+include $(BUILD_SYSTEM)/multilib.mk
+
+ifndef my_module_multilib
+# prebuilts default to building for either architecture,
+# the first if its supported, otherwise the second.
+my_module_multilib := both
+endif
+
+# check if first arch is supported
 include $(BUILD_SYSTEM)/module_arch_supported.mk
 ifeq ($(my_module_arch_supported),true)
-# primary arch is supported
+# first arch is supported
 include $(BUILD_SYSTEM)/prebuilt_internal.mk
 else ifneq (,$(TARGET_2ND_ARCH))
 # check if secondary arch is supported
diff --git a/core/shared_library.mk b/core/shared_library.mk
index 87b6068..95a8d7b 100644
--- a/core/shared_library.mk
+++ b/core/shared_library.mk
@@ -10,6 +10,13 @@
 endif
 endif
 
+include $(BUILD_SYSTEM)/multilib.mk
+
+ifndef my_module_multilib
+# libraries default to building for both architecturess
+my_module_multilib := both
+endif
+
 LOCAL_2ND_ARCH_VAR_PREFIX :=
 include $(BUILD_SYSTEM)/module_arch_supported.mk
 
diff --git a/core/shared_library_internal.mk b/core/shared_library_internal.mk
index b5ac32a..3090d36 100644
--- a/core/shared_library_internal.mk
+++ b/core/shared_library_internal.mk
@@ -16,7 +16,7 @@
 ifneq ($(strip $(OVERRIDE_BUILT_MODULE_PATH)),)
 $(error $(LOCAL_PATH): Illegal use of OVERRIDE_BUILT_MODULE_PATH)
 endif
-ifneq ($(strip $(LOCAL_MODULE_STEM)$(LOCAL_BUILT_MODULE_STEM)),)
+ifneq ($(strip $(LOCAL_MODULE_STEM)$(LOCAL_BUILT_MODULE_STEM)$(LOCAL_MODULE_STEM_32)$(LOCAL_MODULE_STEM_64)),)
 $(error $(LOCAL_PATH): Cannot set module stem for a library)
 endif
 
diff --git a/core/static_library.mk b/core/static_library.mk
index d6e8d51..bedefd0 100644
--- a/core/static_library.mk
+++ b/core/static_library.mk
@@ -1,3 +1,10 @@
+include $(BUILD_SYSTEM)/multilib.mk
+
+ifndef my_module_multilib
+# libraries default to building for both architecturess
+my_module_multilib := both
+endif
+
 LOCAL_2ND_ARCH_VAR_PREFIX :=
 include $(BUILD_SYSTEM)/module_arch_supported.mk