Strengthen dependencies on LOCAL_REQUIRED_MODULES

Bug: 6418863
This change expands the PRODUCT_PACKAGES with the required modules.
Before this change, since the required modules are not in the
PRODUCT_PACKAGES, they are not directly depended on by the image files.
If the dependent is not to be included in the image but the required
modules are to be included, the dependency of the image on the required
modules gets lost!
That would lead to build race condition.

Change-Id: I0b656db1538ca43d3785dbf17364ffa88b80ac41
diff --git a/core/definitions.mk b/core/definitions.mk
index 9943d5f..f82bfa5 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -1999,6 +1999,19 @@
   ,)
 endef
 
+###########################################################
+## Expand a module name list with REQUIRED modules
+###########################################################
+# $(1): The variable name that holds the initial module name list.
+#       the variable will be modified to hold the expanded results.
+# $(2): The initial module name list.
+# Returns empty string (maybe with some whitespaces).
+define expand-required-modules
+$(eval _erm_new_modules := $(sort $(filter-out $($(1)),\
+  $(foreach m,$(2),$(ALL_MODULES.$(m).REQUIRED)))))\
+$(if $(_erm_new_modules),$(eval $(1) += $(_erm_new_modules))\
+  $(call expand-required-modules,$(1),$(_erm_new_modules)))
+endef
 
 ###########################################################
 ## Other includes
diff --git a/core/main.mk b/core/main.mk
index c22c967..fa56b83 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -546,13 +546,9 @@
   # 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.
-  user_PACKAGES := $(call module-installed-files, \
-                       $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES))
-  ifeq (0,1)
-    $(info user packages for $(TARGET_DEVICE) ($(INTERNAL_PRODUCT)):)
-    $(foreach p,$(user_PACKAGES),$(info :   $(p)))
-    $(error done)
-  endif
+  user_PACKAGES := $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES)
+  $(call expand-required-modules,user_PACKAGES,$(user_PACKAGES))
+  user_PACKAGES := $(call module-installed-files, $(user_PACKAGES))
 else
   # We're not doing a full build, and are probably only including
   # a subset of the module makefiles.  Don't try to build any modules
diff --git a/core/tasks/vendor_module_check.mk b/core/tasks/vendor_module_check.mk
index 65c4258..d26a045 100644
--- a/core/tasks/vendor_module_check.mk
+++ b/core/tasks/vendor_module_check.mk
@@ -30,21 +30,8 @@
 
 ifneq (,$(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_RESTRICT_VENDOR_FILES))
 
-_check_modules := $(sort $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES))
-
-# expand with the required modules
-# $(1) the module name set to expand
-define _expand_required_modules
-$(eval _erm_new_modules:=)\
-$(foreach m, $(1), $(eval r:=$(ALL_MODULES.$(m).REQUIRED))\
-  $(if $(r), $(if $(filter $(_check_modules), $(r)),,\
-    $(eval _check_modules := $(_check_modules) $(r))\
-    $(eval _erm_new_modules := $(_erm_new_modules) $(r)))))\
-$(if $(_erm_new_modules), $(call _expand_required_modules, $(_erm_new_modules)))
-endef
-
-$(call _expand_required_modules, $(_check_modules))
-
+_vendor_check_modules := $(sort $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGES))
+$(call expand-required-modules,_vendor_check_modules,$(_vendor_check_modules))
 
 # Restrict owners
 ifneq (,$(filter true owner all, $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_RESTRICT_VENDOR_FILES)))
@@ -58,7 +45,7 @@
     $(filter vendor/%, $(PRODUCT_COPY_FILES)))
 endif
 
-$(foreach m, $(_check_modules), \
+$(foreach m, $(_vendor_check_modules), \
   $(if $(filter vendor/%, $(ALL_MODULES.$(m).PATH)),\
     $(if $(filter $(_vendor_owner_whitelist), $(ALL_MODULES.$(m).OWNER)),,\
       $(error Error: vendor module "$(m)" in $(ALL_MODULES.$(m).PATH) with unknown owner \
@@ -70,7 +57,7 @@
 # Restrict paths
 ifneq (,$(filter path all, $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_RESTRICT_VENDOR_FILES)))
 
-$(foreach m, $(_check_modules), \
+$(foreach m, $(_vendor_check_modules), \
   $(if $(filter vendor/%, $(ALL_MODULES.$(m).PATH)),\
     $(if $(filter $(TARGET_OUT_VENDOR)/%, $(ALL_MODULES.$(m).INSTALLED)),,\
       $(error Error: vendor module "$(m)" in $(ALL_MODULES.$(m).PATH) \
@@ -79,4 +66,5 @@
 
 endif
 
+_vendor_check_modules :=
 endif