Clean up DIST_DIR usages, move to dist-for-goals

soong_ui now guarantees DIST_DIR is set in the environment, so remove
the default.

Use dist-for-goals instead of writing directly into DIST_DIR.

When building the system image, use DIST_DIR from the environment
instead of from make. This will eventually stop working, but this will
work for now.

Bug: 117463001
Test: m dist  (check gpl_source.tgz, logs/product_copy_files_ignored.txt)
Test: m out/target/product/generic/product_copy_files_ignored.txt
Change-Id: Ice557e0a148602cb8eb154efb747d416f0d0db59
diff --git a/core/Makefile b/core/Makefile
index d6fb471..c1309c7 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -42,11 +42,14 @@
         $(eval unique_product_copy_files_destinations += $(_dest))))
 
 # Dump a list of overriden (and ignored PRODUCT_COPY_FILES entries)
-$(file >$(PRODUCT_OUT)/product_copy_files_ignored.txt,$(subst $(space),$(newline),$(strip $(product_copy_files_ignored))))
-ifdef dist_goal
-$(file >$(DIST_DIR)/logs/product_copy_files_ignored.txt,$(subst $(space),$(newline),$(strip $(product_copy_files_ignored))))
-endif
+pcf_ignored_file := $(PRODUCT_OUT)/product_copy_files_ignored.txt
+$(pcf_ignored_file): PRIVATE_IGNORED := $(sort $(product_copy_files_ignored))
+$(pcf_ignored_file):
+	echo "$(PRIVATE_IGNORED)" | tr " " "\n" >$@
 
+$(call dist-for-goals,droidcore,$(pcf_ignored_file):logs/$(notdir $(pcf_ignored_file)))
+
+pcf_ignored_file :=
 product_copy_files_ignored :=
 unique_product_copy_files_pairs :=
 unique_product_copy_files_destinations :=
@@ -1918,7 +1921,7 @@
       build/make/tools/releasetools/build_image.py \
       $(TARGET_OUT) $(systemimage_intermediates)/system_image_info.txt $(1) $(TARGET_OUT) \
       $(systemimage_intermediates)/generated_system_image_info.txt \
-      || ( mkdir -p $(DIST_DIR); cp $(INSTALLED_FILES_FILE) $(DIST_DIR)/installed-files-rescued.txt; \
+      || ( mkdir -p $$(DIST_DIR); cp $(INSTALLED_FILES_FILE) $$(DIST_DIR)/installed-files-rescued.txt; \
            exit 1 )
 endef