Emit and parse the product config variables from kati/make

Test: cls && rm -rf out/config/ && m product-config-test product-config && java -jar out/host/linux-x86/testcases/product-config-test/product-config-test.jar && time ( product-config --ckati_bin /source/kati/ckati > ~/Desktop/out.txt )
Change-Id: I52e5c07f9aaf899f9d45680313275c6d9e246ff2
diff --git a/core/dumpconfig.mk b/core/dumpconfig.mk
new file mode 100644
index 0000000..cb7fbcb
--- /dev/null
+++ b/core/dumpconfig.mk
@@ -0,0 +1,121 @@
+# Read and dump the product configuration.
+
+# Called from the product-config tool, not from the main build system.
+
+#
+# Ensure we are being called correctly
+#
+ifndef KATI
+    $(warning Kati must be used to call dumpconfig.mk, not make.)
+    $(error stopping)
+endif
+
+ifdef DEFAULT_GOAL
+    $(warning Calling dumpconfig.mk from inside the make build system is not)
+    $(warning supported. It is only meant to be called via kati by product-confing.)
+    $(error stopping)
+endif
+
+ifndef TARGET_PRODUCT
+    $(warning dumpconfig.mk requires TARGET_PRODUCT to be set)
+    $(error stopping)
+endif
+
+ifndef TARGET_BUILD_VARIANT
+    $(warning dumpconfig.mk requires TARGET_BUILD_VARIANT to be set)
+    $(error stopping)
+endif
+
+ifneq (build/make/core/config.mk,$(wildcard build/make/core/config.mk))
+    $(warning dumpconfig must be called from the root of the source tree)
+    $(error stopping)
+endif
+
+ifeq (,$(DUMPCONFIG_FILE))
+    $(warning dumpconfig requires DUMPCONFIG_FILE to be set)
+    $(error stopping)
+endif
+
+# Before we do anything else output the format version.
+$(file > $(DUMPCONFIG_FILE),dumpconfig_version,1)
+$(file >> $(DUMPCONFIG_FILE),dumpconfig_file,$(DUMPCONFIG_FILE))
+
+# Default goal for dumpconfig
+dumpconfig:
+	$(file >> $(DUMPCONFIG_FILE),***DONE***)
+	@echo ***DONE***
+
+# TODO(Remove): These need to be set externally
+OUT_DIR := out
+TMPDIR = /tmp/build-temp
+BUILD_DATETIME_FILE := $(OUT_DIR)/build_date.txt
+
+# Escape quotation marks for CSV, and wraps in quotation marks.
+define escape-for-csv
+"$(subst ","",$1)"
+endef
+
+# Args:
+#   $(1): include stack
+define dump-import-start
+$(eval $(file >> $(DUMPCONFIG_FILE),import,$(strip $(1))))
+endef
+
+# Args:
+#   $(1): include stack
+define dump-import-done
+$(eval $(file >> $(DUMPCONFIG_FILE),imported,$(strip $(1))))
+endef
+
+# Args:
+#   $(1): Current file
+#   $(2): Inherited file
+define dump-inherit
+$(eval $(file >> $(DUMPCONFIG_FILE),inherit,$(strip $(1)),$(strip $(2))))
+endef
+
+# Args:
+#   $(1): Config phase (PRODUCT or DEVICE)
+#   $(2): Root nodes to import
+#   $(3): All variable names
+#   $(4): Single-value variables
+define dump-product-var-names
+$(eval $(file >> $(DUMPCONFIG_FILE),phase,$(strip $(1)),$(strip $(2)))) \
+$(foreach var,$(3), \
+    $(eval $(file >> $(DUMPCONFIG_FILE),var,$(if $(filter $(4),$(var)),single,list),$(var))) \
+)
+endef
+
+define dump-debug
+$(eval $(file >> $(DUMPCONFIG_FILE),debug,$(1)))
+endef
+
+# Skip these when dumping. They're not used and they cause a lot of noise in the dump.
+DUMPCONFIG_SKIP_VARS := \
+	.VARIABLES \
+	.KATI_SYMBOLS \
+	1 \
+	2 \
+	LOCAL_PATH \
+	MAKEFILE_LIST \
+	PARENT_PRODUCT_FILES \
+	current_mk \
+	inherit_var \
+	np \
+	_node_import_context \
+	_included \
+	_include_stack \
+	_in \
+	_nic.%
+
+# Args:
+#   $(1): Makefile that was included
+#   $(2): block (before,import,after)
+define dump-config-vals
+$(foreach var,$(filter-out $(DUMPCONFIG_SKIP_VARS),$(.KATI_SYMBOLS)),\
+    $(eval $(file >> $(DUMPCONFIG_FILE),val,$(call escape-for-csv,$(1)),$(2),$(call escape-for-csv,$(var)),$(call escape-for-csv,$($(var))),$(call escape-for-csv,$(KATI_variable_location $(var))))) \
+)
+endef
+
+include build/make/core/config.mk
+