Untwist the target/host shared library dependencies.

Before this, if there are duplicate module names in both the host and
target spaces, LOCAL_SHARED_LIBRARIES results in mixed dependencies.

Bug: 7026946
Change-Id: I5407e3d078a8903c94978cc6d3e256851c52340f
diff --git a/core/binary.mk b/core/binary.mk
index c136a00..18f67fe 100644
--- a/core/binary.mk
+++ b/core/binary.mk
@@ -102,14 +102,20 @@
   installed_shared_library_module_names := \
       $(LOCAL_SYSTEM_SHARED_LIBRARIES) $(LOCAL_SHARED_LIBRARIES)
 endif
-# The real dependency will be added after all Android.mks are loaded and the install paths
-# of the shared libraries are determined.
-LOCAL_REQUIRED_MODULES += $(installed_shared_library_module_names)
+installed_shared_library_module_names := $(sort $(installed_shared_library_module_names))
 
 #######################################
 include $(BUILD_SYSTEM)/base_rules.mk
 #######################################
 
+# The real dependency will be added after all Android.mks are loaded and the install paths
+# of the shared libraries are determined.
+ifdef LOCAL_INSTALLED_MODULE
+ifdef installed_shared_library_module_names
+$(my_prefix)DEPENDENCIES_ON_SHARED_LIBRARIES += $(LOCAL_INSTALLED_MODULE):$(subst $(space),$(comma),$(installed_shared_library_module_names))
+endif
+endif
+
 ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),true)
   LOCAL_CLANG := true
   LOCAL_CFLAGS += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
diff --git a/core/definitions.mk b/core/definitions.mk
index cb20d8f..69e7baa 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -81,6 +81,11 @@
 # GPL module license files
 ALL_GPL_MODULE_LICENSE_FILES:=
 
+# Target and host installed module's dependencies on shared libraries.
+# They are list of "<installed_file>:lib1,lib2...".
+TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES :=
+HOST_DEPENDENCIES_ON_SHARED_LIBRARIES :=
+
 # Generated class file names for Android resource.
 # They are escaped and quoted so can be passed safely to a bash command.
 ANDROID_RESOURCE_GENERATED_CLASSES := 'R.class' 'R$$*.class' 'Manifest.class' 'Manifest$$*.class'
diff --git a/core/main.mk b/core/main.mk
index 5a7c2e2..e176487 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -533,7 +533,7 @@
 # BUG: the system image won't know to depend on modules that are
 # brought in as requirements of other modules.
 define add-required-deps
-$(1): $(2)
+$(1): | $(2)
 endef
 $(foreach m,$(ALL_MODULES), \
   $(eval r := $(ALL_MODULES.$(m).REQUIRED)) \
@@ -542,9 +542,22 @@
     $(eval $(call add-required-deps,$(ALL_MODULES.$(m).INSTALLED),$(r))) \
    ) \
  )
+
+# Resolve the dependencies on shared libraries.
+$(foreach m,$(TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES), \
+  $(eval p := $(subst :,$(space),$(m))) \
+  $(eval r := $(filter $(TARGET_OUT_ROOT)/%,$(call module-installed-files,\
+    $(subst $(comma),$(space),$(lastword $(p)))))) \
+  $(eval $(call add-required-deps,$(firstword $(p)),$(r))))
+$(foreach m,$(HOST_DEPENDENCIES_ON_SHARED_LIBRARIES), \
+  $(eval p := $(subst :,$(space),$(m))) \
+  $(eval r := $(filter $(HOST_OUT_ROOT)/%,$(call module-installed-files,\
+    $(subst $(comma),$(space),$(lastword $(p)))))) \
+  $(eval $(call add-required-deps,$(firstword $(p)),$(r))))
+
 m :=
 r :=
-i :=
+p :=
 add-required-deps :=
 
 # -------------------------------------------------------------------