Refine module name resolving in multilib build

-- Added TARGET_PREFER_32_BIT, which sets LOCAL_32_BIT_ONLY for an
   executable, if LOCAL_NO_2ND_ARCH is not true.

Name resolving in 64-bit multilib build:
-- Name resolving in PRODUCT_PACKAGES:
   foo:32 resolves to foo_32;
   foo:64 resolves to foo;
   foo resolves to both foo and foo_32 (if foo_32 is defined).

-- Name resolving for LOCAL_REQUIRED_MODULES:
   If a module is built for 2nd arch, its required module resolves to
   32-bit variant, if it exits;
   Otherwise for executable and shared library, a required module
   resolves to the default 64-bit variant; for other module classes,
   required module foo resolves to both foo and foo_32 (if foo_32 is
   defined)

Bug: 12898862
Change-Id: I5fda1a77f58814097b10b5ad2743ee25adfaecc4
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/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..df2dea8 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)