Merge changes I0434b003,I1a1a01c5

* changes:
  Fix using aidl files from filegroups
  Add cc_library tests
diff --git a/Android.bp b/Android.bp
index 3badfcd..b57b0cc 100644
--- a/Android.bp
+++ b/Android.bp
@@ -158,6 +158,8 @@
     ],
     testSrcs: [
         "cc/cc_test.go",
+        "cc/gen_test.go",
+        "cc/library_test.go",
         "cc/test_data_test.go",
     ],
     pluginFor: ["soong_build"],
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 32cffe4..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()
@@ -108,12 +112,18 @@
 			name: "crtend_so",
 		}
 
+		cc_library {
+			name: "libprotobuf-cpp-lite",
+		}
+
 `
 
 	ctx.MockFileSystem(map[string][]byte{
 		"Android.bp": []byte(bp),
 		"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 d4ed7c6..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 {
@@ -651,7 +651,7 @@
 func (library *libraryDecorator) link(ctx ModuleContext,
 	flags Flags, deps PathDeps, objs Objects) android.Path {
 
-	objs = objs.Append(deps.Objs)
+	objs = deps.Objs.Copy().Append(objs)
 	var out android.Path
 	if library.static() || library.header() {
 		out = library.linkStatic(ctx, flags, deps, objs)
diff --git a/cc/library_test.go b/cc/library_test.go
new file mode 100644
index 0000000..859b05a
--- /dev/null
+++ b/cc/library_test.go
@@ -0,0 +1,185 @@
+// 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 (
+	"reflect"
+	"testing"
+)
+
+func TestLibraryReuse(t *testing.T) {
+	t.Run("simple", func(t *testing.T) {
+		ctx := testCc(t, `
+		cc_library {
+			name: "libfoo",
+			srcs: ["foo.c"],
+		}`)
+
+		libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
+		libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
+
+		if len(libfooShared.Inputs) != 1 {
+			t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
+		}
+
+		if len(libfooStatic.Inputs) != 1 {
+			t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
+		}
+
+		if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
+			t.Errorf("static object not reused for shared library")
+		}
+	})
+
+	t.Run("extra static source", func(t *testing.T) {
+		ctx := testCc(t, `
+		cc_library {
+			name: "libfoo",
+			srcs: ["foo.c"],
+			static: {
+				srcs: ["bar.c"]
+			},
+		}`)
+
+		libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
+		libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
+
+		if len(libfooShared.Inputs) != 1 {
+			t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
+		}
+
+		if len(libfooStatic.Inputs) != 2 {
+			t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
+		}
+
+		if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
+			t.Errorf("static object not reused for shared library")
+		}
+	})
+
+	t.Run("extra shared source", func(t *testing.T) {
+		ctx := testCc(t, `
+		cc_library {
+			name: "libfoo",
+			srcs: ["foo.c"],
+			shared: {
+				srcs: ["bar.c"]
+			},
+		}`)
+
+		libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
+		libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
+
+		if len(libfooShared.Inputs) != 2 {
+			t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
+		}
+
+		if len(libfooStatic.Inputs) != 1 {
+			t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
+		}
+
+		if libfooShared.Inputs[0] != libfooStatic.Inputs[0] {
+			t.Errorf("static object not reused for shared library")
+		}
+	})
+
+	t.Run("extra static cflags", func(t *testing.T) {
+		ctx := testCc(t, `
+		cc_library {
+			name: "libfoo",
+			srcs: ["foo.c"],
+			static: {
+				cflags: ["-DFOO"],
+			},
+		}`)
+
+		libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
+		libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
+
+		if len(libfooShared.Inputs) != 1 {
+			t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
+		}
+
+		if len(libfooStatic.Inputs) != 1 {
+			t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
+		}
+
+		if libfooShared.Inputs[0] == libfooStatic.Inputs[0] {
+			t.Errorf("static object reused for shared library when it shouldn't be")
+		}
+	})
+
+	t.Run("extra shared cflags", func(t *testing.T) {
+		ctx := testCc(t, `
+		cc_library {
+			name: "libfoo",
+			srcs: ["foo.c"],
+			shared: {
+				cflags: ["-DFOO"],
+			},
+		}`)
+
+		libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
+		libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
+
+		if len(libfooShared.Inputs) != 1 {
+			t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
+		}
+
+		if len(libfooStatic.Inputs) != 1 {
+			t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
+		}
+
+		if libfooShared.Inputs[0] == libfooStatic.Inputs[0] {
+			t.Errorf("static object reused for shared library when it shouldn't be")
+		}
+	})
+
+	t.Run("global cflags for reused generated sources", func(t *testing.T) {
+		ctx := testCc(t, `
+		cc_library {
+			name: "libfoo",
+			srcs: [
+				"foo.c",
+				"a.proto",
+			],
+			shared: {
+				srcs: [
+					"bar.c",
+				],
+			},
+		}`)
+
+		libfooShared := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Rule("ld")
+		libfooStatic := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_static").Output("libfoo.a")
+
+		if len(libfooShared.Inputs) != 3 {
+			t.Fatalf("unexpected inputs to libfoo shared: %#v", libfooShared.Inputs.Strings())
+		}
+
+		if len(libfooStatic.Inputs) != 2 {
+			t.Fatalf("unexpected inputs to libfoo static: %#v", libfooStatic.Inputs.Strings())
+		}
+
+		if !reflect.DeepEqual(libfooShared.Inputs[0:2].Strings(), libfooStatic.Inputs.Strings()) {
+			t.Errorf("static objects not reused for shared library")
+		}
+
+		libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Module().(*Module)
+		if !inList("-DGOOGLE_PROTOBUF_NO_RTTI", libfoo.flags.CFlags) {
+			t.Errorf("missing protobuf cflags")
+		}
+	})
+}
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)
 }