add new gen/ directory for generated sources

Allow modules to generate source into $OUT/gen, which will then
be copied into $OUT/obj and $OUT/obj_$(TARGET_2ND_ARCH) as
necessary.  This allows a single build rule invocation that includes
generated source to build for the first and second architectures.

Modules will need to change calls to local-intermediates-dir into
local-generated-sources-dir.

Change-Id: I62504bad9454b3d9fde7b84ab9f0a487a2ecf0bf
diff --git a/core/base_rules.mk b/core/base_rules.mk
index ac126a7..63abb8d 100644
--- a/core/base_rules.mk
+++ b/core/base_rules.mk
@@ -145,6 +145,7 @@
 
 intermediates := $(call local-intermediates-dir,,$(LOCAL_2ND_ARCH_VAR_PREFIX))
 intermediates.COMMON := $(call local-intermediates-dir,COMMON)
+generated_sources_dir := $(call local-generated-sources-dir)
 
 ###########################################################
 # Pick a name for the intermediate and final targets
diff --git a/core/binary.mk b/core/binary.mk
index 4dad51a..3556b99 100644
--- a/core/binary.mk
+++ b/core/binary.mk
@@ -377,8 +377,15 @@
 ###########################################################
 $(my_generated_sources): PRIVATE_MODULE := $(my_register_name)
 
-ALL_GENERATED_SOURCES += $(my_generated_sources)
+my_gen_sources_copy := $(patsubst $(generated_sources_dir)/%,$(intermediates)/%,$(filter $(generated_sources_dir)/%,$(my_generated_sources)))
 
+$(my_gen_sources_copy): $(intermediates)/% : $(generated_sources_dir)/% | $(ACP)
+	@echo "Copy: $@"
+	$(copy-file-to-target)
+
+my_generated_sources := $(patsubst $(generated_sources_dir)/%,$(intermediates)/%,$(my_generated_sources))
+
+ALL_GENERATED_SOURCES += $(my_generated_sources)
 
 ###########################################################
 ## Compile the .proto files to .cc and then to .o
diff --git a/core/build-system.html b/core/build-system.html
index 182580c..caade58 100644
--- a/core/build-system.html
+++ b/core/build-system.html
@@ -409,8 +409,8 @@
 need to change.</p>
 
 <p>You need to put this after you have declared <code>LOCAL_PATH</code> and
-<code>LOCAL_MODULE</code>, because the <code>$(local-intermediates-dir)</code>
-and <code>$(local-host-intermediates-dir)</code> macros use these variables
+<code>LOCAL_MODULE</code>, because the <code>$(local-generated-sources-dir)</code>
+and <code>$(local-host-generated-sources-dir)</code> macros use these variables
 to determine where to put the files.
 
 <h5>Example 1</h5>
@@ -419,7 +419,7 @@
 built to $(HOST_OUT_EXECUTABLES)/dftables.  Note on the second to last line
 that a dependency is created on the tool.</p>
 <pre>
-intermediates:= $(local-intermediates-dir)
+intermediates:= $(local-generated-sources-dir)
 GEN := $(intermediates)/<font color=red>chartables.c</font>
 $(GEN): PRIVATE_CUSTOM_TOOL = <font color=red>$(HOST_OUT_EXECUTABLES)/dftables $@</font>
 $(GEN): <font color=red>$(HOST_OUT_EXECUTABLES)/dftables</font>
@@ -433,7 +433,7 @@
 target-specific variable called PRIVATE_INPUT_FILE to store the name of the
 input file.</p>
 <pre>
-intermediates:= $(local-intermediates-dir)
+intermediates:= $(local-generated-sources-dir)
 GEN := $(intermediates)/<font color=red>file.c</font>
 $(GEN): PRIVATE_INPUT_FILE := $(LOCAL_PATH)/<font color=red>input.file</font>
 $(GEN): PRIVATE_CUSTOM_TOOL = <font color=red>cat $(PRIVATE_INPUT_FILE) &gt; $@</font>
@@ -447,7 +447,7 @@
 name, and use the same tool, you can combine them.  (here the *.lut.h files are
 the generated ones, and the *.cpp files are the input files)</p>
 <pre>
-intermediates:= $(local-intermediates-dir)
+intermediates:= $(local-generated-sources-dir)
 GEN := $(addprefix $(intermediates)<font color=red>/kjs/, \
             array_object.lut.h \
             bool_object.lut.h \</font>
diff --git a/core/definitions.mk b/core/definitions.mk
index eb462a3..7c29e30 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -462,6 +462,51 @@
 endef
 
 ###########################################################
+## The generated sources directory.  Placing generated
+## source files directly in the intermediates directory
+## causes problems for multiarch builds, where there are
+## two intermediates directories for a single target. Put
+## them in a separate directory, and they will be copied to
+## each intermediates directory automatically.
+###########################################################
+
+# $(1): target class, like "APPS"
+# $(2): target name, like "NotePad"
+# $(3): if non-empty, this is a HOST target.
+# $(4): if non-empty, force the generated sources to be COMMON
+define generated-sources-dir-for
+$(strip \
+    $(eval _idfClass := $(strip $(1))) \
+    $(if $(_idfClass),, \
+        $(error $(LOCAL_PATH): Class not defined in call to generated-sources-dir-for)) \
+    $(eval _idfName := $(strip $(2))) \
+    $(if $(_idfName),, \
+        $(error $(LOCAL_PATH): Name not defined in call to generated-sources-dir-for)) \
+    $(eval _idfPrefix := $(if $(strip $(3)),HOST,TARGET)) \
+    $(if $(filter $(_idfPrefix)-$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
+        $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN_COMMON)) \
+      , \
+        $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN)) \
+     ) \
+    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
+)
+endef
+
+# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
+# to determine the generated sources directory.
+#
+# $(1): if non-empty, force the intermediates to be COMMON
+define local-generated-sources-dir
+$(strip \
+    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
+        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-generated-sources-dir)) \
+    $(if $(strip $(LOCAL_MODULE)),, \
+        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-generated-sources-dir)) \
+    $(call generated-sources-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(LOCAL_IS_HOST_MODULE),$(1)) \
+)
+endef
+
+###########################################################
 ## Convert "path/to/libXXX.so" to "-lXXX".
 ## Any "path/to/libXXX.a" elements pass through unchanged.
 ###########################################################
diff --git a/core/envsetup.mk b/core/envsetup.mk
index a348eb7..4cb389b 100644
--- a/core/envsetup.mk
+++ b/core/envsetup.mk
@@ -208,11 +208,17 @@
 HOST_OUT_NOTICE_FILES := $(HOST_OUT_INTERMEDIATES)/NOTICE_FILES
 HOST_OUT_COMMON_INTERMEDIATES := $(HOST_COMMON_OUT_ROOT)/obj
 
+HOST_OUT_GEN := $(HOST_OUT)/gen
+HOST_OUT_COMMON_GEN := $(HOST_COMMON_OUT_ROOT)/gen
+
 TARGET_OUT_INTERMEDIATES := $(PRODUCT_OUT)/obj
 TARGET_OUT_HEADERS := $(TARGET_OUT_INTERMEDIATES)/include
 TARGET_OUT_INTERMEDIATE_LIBRARIES := $(TARGET_OUT_INTERMEDIATES)/lib
 TARGET_OUT_COMMON_INTERMEDIATES := $(TARGET_COMMON_OUT_ROOT)/obj
 
+TARGET_OUT_GEN := $(PRODUCT_OUT)/gen
+TARGET_OUT_COMMON_GEN := $(TARGET_COMMON_OUT_ROOT)/gen
+
 TARGET_OUT := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_SYSTEM)
 TARGET_OUT_EXECUTABLES := $(TARGET_OUT)/bin
 TARGET_OUT_OPTIONAL_EXECUTABLES := $(TARGET_OUT)/xbin