Support for unbundled app build

With this CL, run "make APP-{appname}" to build unbundled app in
the unbundled app src tree.
See http://b/issue?id=2667113

Change-Id: I1d753db795142508bc841ae66b4408220ab687c5
diff --git a/core/main.mk b/core/main.mk
index 078aae5..6287087 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -691,7 +691,14 @@
 # The actual files built by the droidcore target changes depending
 # on the build variant.
 .PHONY: droid tests
-droid tests: droidcore
+ifeq ($(strip $(is_unbundled_app_build)),true)
+# We build all modules in the source tree for an unbundled app build.
+unbundled_build_modules := $(sort $(call get-tagged-modules,$(ALL_MODULE_TAGS)))
+droid: $(unbundled_build_modules)
+else
+droid: droidcore
+endif
+tests: droidcore
 
 # Dist for droid if droid is among the cmd goals, or no cmd goal is given.
 ifneq ($(filter droid,$(MAKECMDGOALS))$(filter ||,|$(filter-out $(INTERNAL_MODIFIER_TARGETS),$(MAKECMDGOALS))|),)
diff --git a/core/product.mk b/core/product.mk
index be32e9e..7594f6f 100644
--- a/core/product.mk
+++ b/core/product.mk
@@ -29,13 +29,13 @@
 endef
 
 #
-# Returns the sorted concatenation of all PRODUCT_MAKEFILES
-# variables set in all AndroidProducts.mk files.
-# $(call ) isn't necessary.
+# Returns the sorted concatenation of PRODUCT_MAKEFILES
+# variables set in the given AndroidProducts.mk files.
+# $(1): the list of AndroidProducts.mk files.
 #
-define get-all-product-makefiles
+define get-product-makefiles
 $(sort \
-  $(foreach f,$(_find-android-products-files), \
+  $(foreach f,$(1), \
     $(eval PRODUCT_MAKEFILES :=) \
     $(eval LOCAL_DIR := $(patsubst %/,%,$(dir $(f)))) \
     $(eval include $(f)) \
@@ -47,6 +47,15 @@
 endef
 
 #
+# Returns the sorted concatenation of all PRODUCT_MAKEFILES
+# variables set in all AndroidProducts.mk files.
+# $(call ) isn't necessary.
+#
+define get-all-product-makefiles
+$(call get-product-makefiles,$(_find-android-products-files))
+endef
+
+#
 # Functions for including product makefiles
 #
 
diff --git a/core/product_config.mk b/core/product_config.mk
index 02334cf..7e608f6 100644
--- a/core/product_config.mk
+++ b/core/product_config.mk
@@ -149,6 +149,27 @@
 # else: Use the value set in the environment or buildspec.mk.
 
 # ---------------------------------------------------------------
+# Provide "APP-<appname>" targets, which lets you build
+# an unbundled app.
+#
+unbundled_goals := $(strip $(filter APP-%,$(MAKECMDGOALS)))
+ifdef unbundled_goals
+  ifneq ($(words $(unbundled_goals)),1)
+    $(error Only one APP-* goal may be specified; saw "$(unbundled_goals)"))
+  endif
+  UNBUNDLED_APP := $(patsubst APP-%,%,$(unbundled_goals))
+  ifneq ($(filter $(DEFAULT_GOAL),$(MAKECMDGOALS)),)
+    MAKECMDGOALS := $(patsubst $(unbundled_goals),,$(MAKECMDGOALS))
+  else
+    MAKECMDGOALS := $(patsubst $(unbundled_goals),$(DEFAULT_GOAL),$(MAKECMDGOALS))
+  endif
+  is_unbundled_app_build := true
+
+.PHONY: $(unbundled_goals)
+$(unbundled_goals): $(MAKECMDGOALS)
+endif # unbundled_goals
+
+# ---------------------------------------------------------------
 # Include the product definitions.
 # We need to do this to translate TARGET_PRODUCT into its
 # underlying TARGET_DEVICE before we start defining any rules.
@@ -157,12 +178,18 @@
 include $(BUILD_SYSTEM)/product.mk
 include $(BUILD_SYSTEM)/device.mk
 
-# Read in all of the product definitions specified by the AndroidProducts.mk
-# files in the tree.
-#
-#TODO: when we start allowing direct pointers to product files,
-#    guarantee that they're in this list.
-$(call import-products, $(get-all-product-makefiles))
+ifeq ($(strip $(is_unbundled_app_build)),true)
+  # An unbundled app build needs only the core product makefiles.
+  $(call import-products,$(call get-product-makefiles,\
+      $(SRC_TARGET_DIR)/product/AndroidProducts.mk))
+else
+  # Read in all of the product definitions specified by the AndroidProducts.mk
+  # files in the tree.
+  #
+  #TODO: when we start allowing direct pointers to product files,
+  #    guarantee that they're in this list.
+  $(call import-products, $(get-all-product-makefiles))
+endif # is_unbundled_app_build
 $(check-all-products)
 #$(dump-products)
 #$(error done)