Allow to call dist-for-goals multiple times for the same file

You can dist the same file for multiple goals in multiple calls to
dist-for-goals. The first call will establish the real copy rule, while
the rest call just establishes the goals' dependency on the dest file.
This enable uss to remove the bizarre $(if ..) enclosing the droid and
sdk dist while avoiding make's multiple rules warning.

Change-Id: I76475db76a9e6167e0e606dd582b54e80dfcdd22
diff --git a/core/Makefile b/core/Makefile
index 5928a7e..2f86fd7 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -760,12 +760,8 @@
 
 .PHONY: installed-file-list
 installed-file-list: $(INSTALLED_FILES_FILE)
-ifneq ($(filter sdk win_sdk,$(MAKECMDGOALS)),)
-$(call dist-for-goals, sdk win_sdk, $(INSTALLED_FILES_FILE))
-endif
-ifneq ($(filter sdk_addon,$(MAKECMDGOALS)),)
-$(call dist-for-goals, sdk_addon, $(INSTALLED_FILES_FILE))
-endif
+
+$(call dist-for-goals, sdk win_sdk sdk_addon, $(INSTALLED_FILES_FILE))
 
 systemimage_intermediates := \
     $(call intermediates-dir-for,PACKAGING,systemimage)
diff --git a/core/distdir.mk b/core/distdir.mk
index e04938b..51ec46e 100644
--- a/core/distdir.mk
+++ b/core/distdir.mk
@@ -41,6 +41,11 @@
 	$$(copy-file-to-new-target-with-cp)
 endef
 
+# A global variable to remember all dist'ed src:dst pairs.
+# So if a src:dst is already dist'ed by another goal,
+# we should just establish the dependency and don't really call the
+# copy-one-dist-file to avoid multiple rules for the same target.
+_all_dist_src_dst_pairs :=
 # Other parts of the system should use this function to associate
 # certain files with certain goals.  When those goals are built
 # and "dist" is specified, the marked files will be copied to DIST_DIR.
@@ -56,14 +61,13 @@
   $(eval src := $(word 1,$(fw))) \
   $(eval dst := $(word 2,$(fw))) \
   $(eval dst := $(if $(dst),$(dst),$(notdir $(src)))) \
-  $(eval \
-      $(call copy-one-dist-file, \
-          $(src), \
-          $(DIST_DIR)/$(dst), \
-	  $(1) \
-       ) \
-   ) \
- )
+  $(if $(filter $(_all_dist_src_dst_pairs),$(src):$(dst)),\
+    $(eval $(call add-dependency,$(1),$(DIST_DIR)/$(dst))),\
+    $(eval $(call copy-one-dist-file,\
+      $(src),$(DIST_DIR)/$(dst),$(1)))\
+      $(eval _all_dist_src_dst_pairs += $(src):$(dst))\
+  )\
+)
 endef
 
 else # !dist_goal
diff --git a/core/main.mk b/core/main.mk
index 75a75cc..7f3d706 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -779,9 +779,6 @@
 # dist_files only for putting your library into the dist directory with a full build.
 .PHONY: dist_files
 
-# 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))|),)
-
 ifneq ($(TARGET_BUILD_APPS),)
   # If this build is just for apps, only build apps and not the full system by default.
 
@@ -840,9 +837,6 @@
 droid: droidcore dist_files
 
 endif # TARGET_BUILD_APPS
-endif # droid in $(MAKECMDGOALS)
-
-.PHONY: droid
 
 .PHONY: docs
 docs: $(ALL_DOCS)
@@ -850,13 +844,11 @@
 .PHONY: sdk
 ALL_SDK_TARGETS := $(INTERNAL_SDK_TARGET)
 sdk: $(ALL_SDK_TARGETS)
-ifneq ($(filter sdk win_sdk,$(MAKECMDGOALS)),)
 $(call dist-for-goals,sdk win_sdk, \
     $(ALL_SDK_TARGETS) \
     $(SYMBOLS_ZIP) \
     $(INSTALLED_BUILD_PROP_TARGET) \
 )
-endif
 
 # umbrella targets to assit engineers in verifying builds
 .PHONY: java native target host java-host java-target native-host native-target \