Fix generated sources and headers

Add two cc properties: `generated_sources` and `generated_headers`,
instead of just adding genrule modules to `deps`. Label these with the
dep tagging mechanism, add the generated_headers paths to the include
paths, and add dependencies to generated headers for each compile.

Add dependencies so that regular sources can include generated yacc
headers, not just other generated lex/yacc files. Static/shared specific
sources still don't have dependencies to or from regular sources though.

Switch from an implicit dependency on generated files to an orderonly
dependency, since the depfile will take care of the necessary implicit
dependencies for incremental builds.

Change-Id: I436675acb1f57329d98b81c56dcb4384201a68ea
diff --git a/cc/builder.go b/cc/builder.go
index bcfbb6e..38bb141 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -199,7 +199,7 @@
 			Rule:      cc,
 			Output:    objFile,
 			Input:     srcFile,
-			Implicits: deps,
+			OrderOnly: deps,
 			Args: map[string]string{
 				"cFlags": moduleCflags,
 				"ccCmd":  ccCmd,
diff --git a/cc/cc.go b/cc/cc.go
index 1c1ef10..8910ce7 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -177,6 +177,9 @@
 
 	ObjFiles []string
 
+	GeneratedSources []string
+	GeneratedHeaders []string
+
 	Cflags, ReexportedCflags []string
 
 	CrtBegin, CrtEnd string
@@ -189,6 +192,9 @@
 	ObjFiles               common.Paths
 	WholeStaticLibObjFiles common.Paths
 
+	GeneratedSources common.Paths
+	GeneratedHeaders common.Paths
+
 	Cflags, ReexportedCflags []string
 
 	CrtBegin, CrtEnd common.OptionalPath
@@ -266,6 +272,14 @@
 	// If possible, don't use this.
 	Local_include_files []string `android:"arch_variant"`
 
+	// list of generated sources to compile. These are the names of gensrcs or
+	// genrule modules.
+	Generated_sources []string `android:"arch_variant"`
+
+	// list of generated headers to add to the include path. These are the names
+	// of genrule modules.
+	Generated_headers []string `android:"arch_variant"`
+
 	// pass -frtti instead of -fno-rtti
 	Rtti *bool
 
@@ -457,7 +471,7 @@
 
 type compiler interface {
 	feature
-	compile(ctx ModuleContext, flags Flags) common.Paths
+	compile(ctx ModuleContext, flags Flags, deps PathDeps) common.Paths
 }
 
 type linker interface {
@@ -484,6 +498,8 @@
 	staticDepTag      = dependencyTag{name: "static", library: true}
 	lateStaticDepTag  = dependencyTag{name: "late static", library: true}
 	wholeStaticDepTag = dependencyTag{name: "whole static", library: true}
+	genSourceDepTag   = dependencyTag{name: "gen source"}
+	genHeaderDepTag   = dependencyTag{name: "gen header"}
 	objDepTag         = dependencyTag{name: "obj"}
 	crtBeginDepTag    = dependencyTag{name: "crtbegin"}
 	crtEndDepTag      = dependencyTag{name: "crtend"}
@@ -664,7 +680,7 @@
 
 	var objFiles common.Paths
 	if c.compiler != nil {
-		objFiles = c.compiler.compile(ctx, flags)
+		objFiles = c.compiler.compile(ctx, flags, deps)
 		if ctx.Failed() {
 			return
 		}
@@ -769,6 +785,9 @@
 	actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
 		deps.LateSharedLibs...)
 
+	actx.AddDependency(ctx.module(), genSourceDepTag, deps.GeneratedSources...)
+	actx.AddDependency(ctx.module(), genHeaderDepTag, deps.GeneratedHeaders...)
+
 	actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
 
 	if deps.CrtBegin != "" {
@@ -821,7 +840,25 @@
 
 		c, _ := m.(*Module)
 		if c == nil {
-			if tag != common.DefaultsDepTag {
+			switch tag {
+			case common.DefaultsDepTag:
+			case genSourceDepTag:
+				if genRule, ok := m.(genrule.SourceFileGenerator); ok {
+					depPaths.GeneratedSources = append(depPaths.GeneratedSources,
+						genRule.GeneratedSourceFiles()...)
+				} else {
+					ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
+				}
+			case genHeaderDepTag:
+				if genRule, ok := m.(genrule.SourceFileGenerator); ok {
+					depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
+						genRule.GeneratedSourceFiles()...)
+					depPaths.Cflags = append(depPaths.Cflags,
+						includeDirsToFlags(common.Paths{genRule.GeneratedHeaderDir()}))
+				} else {
+					ctx.ModuleErrorf("module %q is not a genrule", name)
+				}
+			default:
 				ctx.ModuleErrorf("depends on non-cc module %q", name)
 			}
 			return
@@ -922,8 +959,14 @@
 	return []interface{}{&compiler.Properties}
 }
 
-func (compiler *baseCompiler) begin(ctx BaseModuleContext)                {}
-func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps { return deps }
+func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
+
+func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
+	deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
+	deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
+
+	return deps
+}
 
 // Create a Flags struct that collects the compile flags from global values,
 // per-target values, module type values, and per-module Blueprints properties
@@ -1072,36 +1115,30 @@
 	return flags
 }
 
-func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
+func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) common.Paths {
 	// Compile files listed in c.Properties.Srcs into objects
-	objFiles := compiler.compileObjs(ctx, flags, "", compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
+	objFiles := compiler.compileObjs(ctx, flags, "",
+		compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
+		deps.GeneratedSources, deps.GeneratedHeaders)
+
 	if ctx.Failed() {
 		return nil
 	}
 
-	var genSrcs common.Paths
-	ctx.VisitDirectDeps(func(module blueprint.Module) {
-		if gen, ok := module.(genrule.SourceFileGenerator); ok {
-			genSrcs = append(genSrcs, gen.GeneratedSourceFiles()...)
-		}
-	})
-
-	if len(genSrcs) != 0 {
-		genObjs := TransformSourceToObj(ctx, "", genSrcs, flagsToBuilderFlags(flags), nil)
-		objFiles = append(objFiles, genObjs...)
-	}
-
 	return objFiles
 }
 
 // Compile a list of source files into objects a specified subdirectory
 func (compiler *baseCompiler) compileObjs(ctx common.AndroidModuleContext, flags Flags,
-	subdir string, srcFiles, excludes []string) common.Paths {
+	subdir string, srcFiles, excludes []string, extraSrcs, deps common.Paths) common.Paths {
 
 	buildFlags := flagsToBuilderFlags(flags)
 
 	inputFiles := ctx.ExpandSources(srcFiles, excludes)
-	srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
+	inputFiles = append(inputFiles, extraSrcs...)
+	srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
+
+	deps = append(deps, gendeps...)
 
 	return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
 }
@@ -1307,18 +1344,20 @@
 	return flags
 }
 
-func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
+func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) common.Paths {
 	var objFiles common.Paths
 
-	objFiles = library.baseCompiler.compile(ctx, flags)
+	objFiles = library.baseCompiler.compile(ctx, flags, deps)
 	library.reuseObjFiles = objFiles
 
 	if library.linker.static() {
 		objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceStaticLibrary,
-			library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs)...)
+			library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
+			nil, deps.GeneratedHeaders)...)
 	} else {
 		objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceSharedLibrary,
-			library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs)...)
+			library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
+			nil, deps.GeneratedHeaders)...)
 	}
 
 	return objFiles
diff --git a/genrule/genrule.go b/genrule/genrule.go
index 248da49..3c2e331 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -39,6 +39,7 @@
 
 type SourceFileGenerator interface {
 	GeneratedSourceFiles() common.Paths
+	GeneratedHeaderDir() common.Path
 }
 
 type HostToolProvider interface {
@@ -68,6 +69,8 @@
 	deps common.Paths
 	rule blueprint.Rule
 
+	genPath common.Path
+
 	outputFiles common.Paths
 }
 
@@ -82,6 +85,10 @@
 	return g.outputFiles
 }
 
+func (g *generator) GeneratedHeaderDir() common.Path {
+	return g.genPath
+}
+
 func genruleDepsMutator(ctx common.AndroidBottomUpMutatorContext) {
 	if g, ok := ctx.Module().(*generator); ok {
 		if g.properties.Tool != "" {
@@ -111,6 +118,8 @@
 		}
 	})
 
+	g.genPath = common.PathForModuleGen(ctx, "")
+
 	for _, task := range g.tasks(ctx) {
 		g.generateSourceFile(ctx, task)
 	}