Merge "A build property's value is returned empty in runtime when its length exceeds 92 symbols (91 valid symbols + \0). It is better to catch that issue on earlier stage, i.e. fail the build with an appropriate message."
diff --git a/core/base_rules.mk b/core/base_rules.mk
index a6af2bc..32e6b73 100644
--- a/core/base_rules.mk
+++ b/core/base_rules.mk
@@ -597,7 +597,8 @@
 ALL_MODULES.$(my_register_name).INSTALLED := \
     $(strip $(ALL_MODULES.$(my_register_name).INSTALLED) $(LOCAL_INSTALLED_MODULE))
 ALL_MODULES.$(my_register_name).REQUIRED := \
-    $(ALL_MODULES.$(my_register_name).REQUIRED) $(LOCAL_REQUIRED_MODULES) $(LOCAL_REQUIRED_MODULES_$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH))
+    $(strip $(ALL_MODULES.$(my_register_name).REQUIRED) $(LOCAL_REQUIRED_MODULES) \
+      $(LOCAL_REQUIRED_MODULES_$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)))
 ALL_MODULES.$(my_register_name).EVENT_LOG_TAGS := \
     $(ALL_MODULES.$(my_register_name).EVENT_LOG_TAGS) $(event_log_tags)
 ALL_MODULES.$(my_register_name).INTERMEDIATE_SOURCE_DIR := \
@@ -608,6 +609,9 @@
 ALL_MODULES.$(my_register_name).OWNER := \
     $(sort $(ALL_MODULES.$(my_register_name).OWNER) $(LOCAL_MODULE_OWNER))
 endif
+ifdef LOCAL_2ND_ARCH_VAR_PREFIX
+ALL_MODULES.$(my_register_name).FOR_2ND_ARCH := true
+endif
 
 INSTALLABLE_FILES.$(LOCAL_INSTALLED_MODULE).MODULE := $(my_register_name)
 
diff --git a/core/clang/TARGET_arm.mk b/core/clang/TARGET_arm.mk
index 373f276..f32bbfb 100644
--- a/core/clang/TARGET_arm.mk
+++ b/core/clang/TARGET_arm.mk
@@ -3,7 +3,7 @@
 
 CLANG_CONFIG_arm_TARGET_TRIPLE := arm-linux-androideabi
 CLANG_CONFIG_arm_TARGET_TOOLCHAIN_PREFIX := \
-  $(TARGET_TOOLCHAIN_ROOT)/$(CLANG_CONFIG_arm_TARGET_TRIPLE)/bin
+  $($(clang_2nd_arch_prefix)TARGET_TOOLCHAIN_ROOT)/$(CLANG_CONFIG_arm_TARGET_TRIPLE)/bin
 
 CLANG_CONFIG_arm_TARGET_EXTRA_ASFLAGS := \
   $(CLANG_CONFIG_EXTRA_ASFLAGS) \
diff --git a/core/clang/TARGET_mips.mk b/core/clang/TARGET_mips.mk
index 215a8ae..e70e254 100644
--- a/core/clang/TARGET_mips.mk
+++ b/core/clang/TARGET_mips.mk
@@ -3,7 +3,7 @@
 
 CLANG_CONFIG_mips_TARGET_TRIPLE := mipsel-linux-android
 CLANG_CONFIG_mips_TARGET_TOOLCHAIN_PREFIX := \
-  $(TARGET_TOOLCHAIN_ROOT)/$(CLANG_CONFIG_mips_TARGET_TRIPLE)/bin
+  $($(clang_2nd_arch_prefix)TARGET_TOOLCHAIN_ROOT)/$(CLANG_CONFIG_mips_TARGET_TRIPLE)/bin
 
 CLANG_CONFIG_mips_TARGET_EXTRA_ASFLAGS := \
   $(CLANG_CONFIG_EXTRA_ASFLAGS) \
diff --git a/core/clang/TARGET_x86.mk b/core/clang/TARGET_x86.mk
index 0178093..0f530b5 100644
--- a/core/clang/TARGET_x86.mk
+++ b/core/clang/TARGET_x86.mk
@@ -3,7 +3,7 @@
 
 CLANG_CONFIG_x86_TARGET_TRIPLE := i686-linux-android
 CLANG_CONFIG_x86_TARGET_TOOLCHAIN_PREFIX := \
-  $(TARGET_TOOLCHAIN_ROOT)/x86_64-linux-android/bin
+  $($(clang_2nd_arch_prefix)TARGET_TOOLCHAIN_ROOT)/x86_64-linux-android/bin
 
 CLANG_CONFIG_x86_TARGET_EXTRA_ASFLAGS := \
   $(CLANG_CONFIG_EXTRA_ASFLAGS) \
diff --git a/core/definitions.mk b/core/definitions.mk
index b34e274..e0e9608 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -275,6 +275,19 @@
 endef
 
 ###########################################################
+## Find all of the S files under the named directories.
+## Meant to be used like:
+##    SRC_FILES := $(call all-c-files-under,src tests)
+###########################################################
+
+define all-S-files-under
+$(patsubst ./%,%, \
+  $(shell cd $(LOCAL_PATH) ; \
+          find -L $(1) -name "*.S" -and -not -name ".*") \
+ )
+endef
+
+###########################################################
 ## Find all of the html files under the named directories.
 ## Meant to be used like:
 ##    SRC_FILES := $(call all-html-files-under,src tests)
diff --git a/core/executable.mk b/core/executable.mk
index 6d8734e..65319ef 100644
--- a/core/executable.mk
+++ b/core/executable.mk
@@ -3,6 +3,12 @@
 # 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".
 
+ifeq ($(TARGET_PREFER_32_BIT),true)
+ifneq ($(LOCAL_NO_2ND_ARCH),true)
+LOCAL_32_BIT_ONLY := true
+endif
+endif
+
 ifeq ($(TARGET_IS_64_BIT)|$(LOCAL_32_BIT_ONLY),true|true)
 LOCAL_2ND_ARCH_VAR_PREFIX := $(TARGET_2ND_ARCH_VAR_PREFIX)
 else
diff --git a/core/main.mk b/core/main.mk
index b593acd..871f51b 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -571,9 +571,49 @@
 # APPS:Quake or HOST:SHARED_LIBRARIES:libutils.
 # BUG: the system image won't know to depend on modules that are
 # brought in as requirements of other modules.
+#
+# Resolve the required module name to 32-bit or 64-bit variant.
+ifeq ($(TARGET_IS_64_BIT),true)
+# Get a list of corresponding 32-bit module names, if one exists.
+define get-32-bit-modules
+$(strip $(foreach m,$(1),\
+  $(if $(ALL_MODULES.$(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX).CLASS),\
+    $(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX))))
+endef
+# Get a list of corresponding 32-bit module names, if one exists;
+# otherwise return the original module name
+define get-32-bit-modules-if-we-can
+$(strip $(foreach m,$(1),\
+  $(if $(ALL_MODULES.$(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX).CLASS),\
+    $(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX),
+    $(m))))
+endef
+
+# If a module is built for 32-bit, the required modules must be 32-bit too;
+# Otherwise if the module is an exectuable or shared library,
+#   the required modules must be 64-bit;
+#   otherwise we require both 64-bit and 32-bit variant, if one exists.
+$(foreach m,$(ALL_MODULES),\
+  $(eval r := $(ALL_MODULES.$(m).REQUIRED))\
+  $(if $(r),\
+    $(if $(ALL_MODULES.$(m).FOR_2ND_ARCH),\
+      $(eval r_r := $(call get-32-bit-modules-if-we-can,$(r))),\
+      $(if $(filter EXECUTABLES SHARED_LIBRARIES,$(ALL_MODULES.$(m).CLASS)),\
+        $(eval r_r := $(r)),\
+        $(eval r_r := $(r) $(call get-32-bit-modules,$(r)))\
+       )\
+     )\
+     $(eval ALL_MODULES.$(m).REQUIRED := $(r_r))\
+  )\
+)
+r_r :=
+endif
+
+
 define add-required-deps
 $(1): | $(2)
 endef
+
 $(foreach m,$(ALL_MODULES), \
   $(eval r := $(ALL_MODULES.$(m).REQUIRED)) \
   $(if $(r), \
@@ -628,11 +668,26 @@
 ifdef FULL_BUILD
   # The base list of modules to build for this product is specified
   # by the appropriate product definition file, which was included
-  # by product_config.make.
+  # by product_config.mk.
   product_MODULES := $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES)
   # Filter out the overridden packages before doing expansion
   product_MODULES := $(filter-out $(foreach p, $(product_MODULES), \
       $(PACKAGES.$(p).OVERRIDES)), $(product_MODULES))
+
+  # Resolve the :32 :64 module name
+  modules_32 := $(patsubst %:32,%,$(filter %:32, $(product_MODULES)))
+  modules_64 := $(patsubst %:64,%,$(filter %:64, $(product_MODULES)))
+  modules_rest := $(filter-out %:32 %:64,$(product_MODULES))
+  ifeq ($(TARGET_IS_64_BIT),true)
+    product_MODULES := $(addsuffix $(TARGET_2ND_ARCH_MODULE_SUFFIX),$(modules_32))
+    product_MODULES += $(modules_64)
+    # For the rest we add both
+    product_MODULES += $(call get-32-bit-modules, $(modules_rest))
+    product_MODULES += $(modules_rest)
+  else
+    product_MODULES := $(modules_32) $(modules_64) $(modules_rest)
+  endif
+
   $(call expand-required-modules,product_MODULES,$(product_MODULES))
   product_FILES := $(call module-installed-files, $(product_MODULES))
   ifeq (0,1)
@@ -698,9 +753,13 @@
 
   # Ensure every module listed in PRODUCT_PACKAGES* gets something installed
   # TODO: Should we do this for all builds and not just the sdk?
+  dangling_modules :=
   $(foreach m, $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES), \
     $(if $(strip $(ALL_MODULES.$(m).INSTALLED)),,\
-      $(error $(ALL_MODULES.$(m).MAKEFILE): Module '$(m)' in PRODUCT_PACKAGES has nothing to install!)))
+      $(eval dangling_modules += $(m))))
+  ifneq ($(dangling_modules),)
+    $(error Module names '$(dangling_modules)' in PRODUCT_PACKAGES has nothing to install!)
+  endif
   $(foreach m, $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES_DEBUG), \
     $(if $(strip $(ALL_MODULES.$(m).INSTALLED)),,\
       $(warning $(ALL_MODULES.$(m).MAKEFILE): Module '$(m)' in PRODUCT_PACKAGES_DEBUG has nothing to install!)))
diff --git a/target/product/core_minimal.mk b/target/product/core_minimal.mk
index 79ba0d4..17000ac 100644
--- a/target/product/core_minimal.mk
+++ b/target/product/core_minimal.mk
@@ -37,6 +37,7 @@
     com.android.media.remotedisplay.xml \
     drmserver \
     framework-res \
+    idmap \
     installd \
     ip \
     ip-up-vpn \
diff --git a/target/product/generic_no_telephony.mk b/target/product/generic_no_telephony.mk
index 963d56a..04d6e5f 100644
--- a/target/product/generic_no_telephony.mk
+++ b/target/product/generic_no_telephony.mk
@@ -44,18 +44,12 @@
     SystemUI \
     TeleService \
     CalendarProvider \
-    bluetooth-health \
     WallpaperCropper
 
 PRODUCT_PACKAGES += \
-    audio \
     clatd \
     clatd.conf \
-    dhcpcd.conf \
-    network \
-    pand \
-    pppd \
-    sdptool
+    pppd
 
 PRODUCT_PACKAGES += \
     librs_jni \