Fix using aidl files from filegroups

Compute sources including from filegroup and genrule dependencies
before determining if any sources will cause flags to be added.

Test: gen_test.go
Change-Id: I0434b003bbda07a58bb2ce1a0a72997918c8fae2
diff --git a/cc/cc.go b/cc/cc.go
index 86a60c9..384b240 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -230,7 +230,7 @@
 type compiler interface {
 	compilerInit(ctx BaseModuleContext)
 	compilerDeps(ctx DepsContext, deps Deps) Deps
-	compilerFlags(ctx ModuleContext, flags Flags) Flags
+	compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
 	compilerProps() []interface{}
 
 	appendCflags([]string)
@@ -589,12 +589,17 @@
 	}
 	ctx.ctx = ctx
 
+	deps := c.depsToPaths(ctx)
+	if ctx.Failed() {
+		return
+	}
+
 	flags := Flags{
 		Toolchain: c.toolchain(ctx),
 		Clang:     c.clang(ctx),
 	}
 	if c.compiler != nil {
-		flags = c.compiler.compilerFlags(ctx, flags)
+		flags = c.compiler.compilerFlags(ctx, flags, deps)
 	}
 	if c.linker != nil {
 		flags = c.linker.linkerFlags(ctx, flags)
@@ -625,10 +630,6 @@
 	flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
 	flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
 
-	deps := c.depsToPaths(ctx)
-	if ctx.Failed() {
-		return
-	}
 	flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
 	c.flags = flags
 	// We need access to all the flags seen by a source file.
diff --git a/cc/cc_test.go b/cc/cc_test.go
index b451f17..60c33c2 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -2,6 +2,8 @@
 
 import (
 	"android/soong/android"
+	"android/soong/genrule"
+
 	"fmt"
 	"io/ioutil"
 	"os"
@@ -42,9 +44,11 @@
 
 	ctx := android.NewTestArchContext()
 	ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(LibraryFactory))
+	ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(LibrarySharedFactory))
 	ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(toolchainLibraryFactory))
 	ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(llndkLibraryFactory))
 	ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(objectFactory))
+	ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
 	ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
 		ctx.BottomUp("image", vendorMutator).Parallel()
 		ctx.BottomUp("link", linkageMutator).Parallel()
@@ -119,6 +123,7 @@
 		"foo.c":      nil,
 		"bar.c":      nil,
 		"a.proto":    nil,
+		"b.aidl":     nil,
 	})
 
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
diff --git a/cc/compiler.go b/cc/compiler.go
index 4112930..ca68a00 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -158,8 +158,15 @@
 	Properties BaseCompilerProperties
 	Proto      android.ProtoProperties
 	deps       android.Paths
-	srcs       android.Paths
 	flags      builderFlags
+
+	// Sources that were passed to the C/C++ compiler
+	srcs android.Paths
+
+	// Sources that were passed in the Android.bp file, including generated sources generated by
+	// other modules and filegroups. May include source files that have not yet been translated to
+	// C/C++ (.aidl, .proto, etc.)
+	srcsBeforeGen android.Paths
 }
 
 var _ compiler = (*baseCompiler)(nil)
@@ -201,9 +208,12 @@
 
 // Create a Flags struct that collects the compile flags from global values,
 // per-target values, module type values, and per-module Blueprints properties
-func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
+func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
 	tc := ctx.toolchain()
 
+	compiler.srcsBeforeGen = ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
+	compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
+
 	CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
 	CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
 	CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
@@ -458,6 +468,11 @@
 }
 
 func (compiler *baseCompiler) hasSrcExt(ext string) bool {
+	for _, src := range compiler.srcsBeforeGen {
+		if src.Ext() == ext {
+			return true
+		}
+	}
 	for _, src := range compiler.Properties.Srcs {
 		if filepath.Ext(src) == ext {
 			return true
@@ -487,11 +502,10 @@
 	pathDeps := deps.GeneratedHeaders
 	pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
 
-	srcs := ctx.ExpandSources(compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
-	srcs = append(srcs, deps.GeneratedSources...)
-
 	buildFlags := flagsToBuilderFlags(flags)
 
+	srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
+
 	srcs, genDeps := genSources(ctx, srcs, buildFlags)
 
 	pathDeps = append(pathDeps, genDeps...)
diff --git a/cc/gen_test.go b/cc/gen_test.go
new file mode 100644
index 0000000..a0f7308
--- /dev/null
+++ b/cc/gen_test.go
@@ -0,0 +1,63 @@
+// Copyright 2017 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package cc
+
+import (
+	"testing"
+)
+
+func TestGen(t *testing.T) {
+	t.Run("simple", func(t *testing.T) {
+		ctx := testCc(t, `
+		cc_library_shared {
+			name: "libfoo",
+			srcs: [
+				"foo.c",
+				"b.aidl",
+			],
+		}`)
+
+		aidl := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("aidl")
+		libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Module().(*Module)
+
+		if !inList("-I"+aidl.Args["outDir"], libfoo.flags.GlobalFlags) {
+			t.Errorf("missing aidl includes in global flags")
+		}
+	})
+
+	t.Run("filegroup", func(t *testing.T) {
+		ctx := testCc(t, `
+		filegroup {
+			name: "fg",
+			srcs: ["b.aidl"],
+		}
+
+		cc_library_shared {
+			name: "libfoo",
+			srcs: [
+				"foo.c",
+				":fg",
+			],
+		}`)
+
+		aidl := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("aidl")
+		libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Module().(*Module)
+
+		if !inList("-I"+aidl.Args["outDir"], libfoo.flags.GlobalFlags) {
+			t.Errorf("missing aidl includes in global flags")
+		}
+	})
+
+}
diff --git a/cc/library.go b/cc/library.go
index 23e4f44..192496a 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -317,7 +317,7 @@
 	return flags
 }
 
-func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
+func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
 	exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
 	if len(exportIncludeDirs) > 0 {
 		f := includeDirsToFlags(exportIncludeDirs)
@@ -325,7 +325,7 @@
 		flags.YasmFlags = append(flags.YasmFlags, f)
 	}
 
-	return library.baseCompiler.compilerFlags(ctx, flags)
+	return library.baseCompiler.compilerFlags(ctx, flags, deps)
 }
 
 func extractExportIncludesFromFlags(flags []string) []string {
diff --git a/cc/llndk_library.go b/cc/llndk_library.go
index 154b3f4..9a29964 100644
--- a/cc/llndk_library.go
+++ b/cc/llndk_library.go
@@ -70,8 +70,8 @@
 	versionScriptPath      android.ModuleGenPath
 }
 
-func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
-	flags = stub.baseCompiler.compilerFlags(ctx, flags)
+func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
+	flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
 	return addStubLibraryCompilerFlags(flags)
 }
 
diff --git a/cc/ndk_library.go b/cc/ndk_library.go
index 066fb98..c517523 100644
--- a/cc/ndk_library.go
+++ b/cc/ndk_library.go
@@ -258,8 +258,8 @@
 	return flags
 }
 
-func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
-	flags = stub.baseCompiler.compilerFlags(ctx, flags)
+func (stub *stubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
+	flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
 	return addStubLibraryCompilerFlags(flags)
 }