Support filegroup in exclude_srcs

Test: add unit-test, m -j32
Bug: b/70351683
Change-Id: Iff83c56d45dd668d9df6131c7df2e23e5c73a21b
diff --git a/android/module.go b/android/module.go
index 9de5294..6f247ab 100644
--- a/android/module.go
+++ b/android/module.go
@@ -1176,15 +1176,24 @@
 func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
 	prefix := PathForModuleSrc(ctx).String()
 
-	for i, e := range excludes {
-		j := findStringInSlice(e, srcFiles)
-		if j != -1 {
-			srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
+	expandedExcludes := make([]string, 0, len(excludes))
+
+	for _, e := range excludes {
+		if m := SrcIsModule(e); m != "" {
+			module := ctx.GetDirectDepWithTag(m, SourceDepTag)
+			if module == nil {
+				// Error will have been handled by ExtractSourcesDeps
+				continue
+			}
+			if srcProducer, ok := module.(SourceFileProducer); ok {
+				expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
+			} else {
+				ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
+			}
+		} else {
+			expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
 		}
-
-		excludes[i] = filepath.Join(prefix, e)
 	}
-
 	expandedSrcFiles := make(Paths, 0, len(srcFiles))
 	for _, s := range srcFiles {
 		if m := SrcIsModule(s); m != "" {
@@ -1194,22 +1203,33 @@
 				continue
 			}
 			if srcProducer, ok := module.(SourceFileProducer); ok {
-				expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
+				moduleSrcs := srcProducer.Srcs()
+				for _, e := range expandedExcludes {
+					for j, ms := range moduleSrcs {
+						if ms.String() == e {
+							moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
+						}
+					}
+				}
+				expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
 			} else {
 				ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
 			}
 		} else if pathtools.IsGlob(s) {
-			globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
+			globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), expandedExcludes)
 			for i, s := range globbedSrcFiles {
 				globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
 			}
 			expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
 		} else {
-			s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
-			expandedSrcFiles = append(expandedSrcFiles, s)
+			p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
+			j := findStringInSlice(p.String(), expandedExcludes)
+			if j == -1 {
+				expandedSrcFiles = append(expandedSrcFiles, p)
+			}
+
 		}
 	}
-
 	return expandedSrcFiles
 }
 
diff --git a/cc/compiler.go b/cc/compiler.go
index 0d8e3a1..dd1cdee 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -202,6 +202,7 @@
 	deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
 
 	android.ExtractSourcesDeps(ctx, compiler.Properties.Srcs)
+	android.ExtractSourcesDeps(ctx, compiler.Properties.Exclude_srcs)
 
 	if compiler.hasSrcExt(".proto") {
 		deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
diff --git a/genrule/filegroup.go b/genrule/filegroup.go
index 8f28638..9f4aa10 100644
--- a/genrule/filegroup.go
+++ b/genrule/filegroup.go
@@ -62,6 +62,7 @@
 
 func (fg *fileGroup) DepsMutator(ctx android.BottomUpMutatorContext) {
 	android.ExtractSourcesDeps(ctx, fg.properties.Srcs)
+	android.ExtractSourcesDeps(ctx, fg.properties.Exclude_srcs)
 }
 
 func (fg *fileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
diff --git a/java/java.go b/java/java.go
index 955b720..f55a7f5 100644
--- a/java/java.go
+++ b/java/java.go
@@ -466,6 +466,7 @@
 	ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
 
 	android.ExtractSourcesDeps(ctx, j.properties.Srcs)
+	android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
 	android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
 	android.ExtractSourceDeps(ctx, j.properties.Manifest)
 
diff --git a/java/java_test.go b/java/java_test.go
index 60d9a40..19c5f21 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -131,16 +131,19 @@
 	}
 
 	mockFS := map[string][]byte{
-		"Android.bp":  []byte(bp),
-		"a.java":      nil,
-		"b.java":      nil,
-		"c.java":      nil,
-		"b.kt":        nil,
-		"a.jar":       nil,
-		"b.jar":       nil,
-		"java-res/a":  nil,
-		"java-res/b":  nil,
-		"java-res2/a": nil,
+		"Android.bp":     []byte(bp),
+		"a.java":         nil,
+		"b.java":         nil,
+		"c.java":         nil,
+		"b.kt":           nil,
+		"a.jar":          nil,
+		"b.jar":          nil,
+		"java-res/a":     nil,
+		"java-res/b":     nil,
+		"java-res2/a":    nil,
+		"java-fg/a.java": nil,
+		"java-fg/b.java": nil,
+		"java-fg/c.java": nil,
 
 		"prebuilts/sdk/14/android.jar":                nil,
 		"prebuilts/sdk/14/framework.aidl":             nil,
@@ -907,6 +910,32 @@
 	}
 }
 
+func TestExcludeFileGroupInSrcs(t *testing.T) {
+	ctx := testJava(t, `
+		java_library {
+			name: "foo",
+			srcs: ["a.java", ":foo-srcs"],
+			exclude_srcs: ["a.java", ":foo-excludes"],
+		}
+
+		filegroup {
+			name: "foo-srcs",
+			srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
+		}
+
+		filegroup {
+			name: "foo-excludes",
+			srcs: ["java-fg/a.java", "java-fg/b.java"],
+		}
+	`)
+
+	javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
+
+	if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
+		t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
+	}
+}
+
 func fail(t *testing.T, errs []error) {
 	t.Helper()
 	if len(errs) > 0 {
diff --git a/python/python.go b/python/python.go
index 9a3b1bb..ed879eb 100644
--- a/python/python.go
+++ b/python/python.go
@@ -266,11 +266,13 @@
 	android.ExtractSourcesDeps(ctx, p.properties.Data)
 	// deps from "srcs".
 	android.ExtractSourcesDeps(ctx, p.properties.Srcs)
+	android.ExtractSourcesDeps(ctx, p.properties.Exclude_srcs)
 
 	switch p.properties.Actual_version {
 	case pyVersion2:
 		// deps from "version.py2.srcs" property.
 		android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Srcs)
+		android.ExtractSourcesDeps(ctx, p.properties.Version.Py2.Exclude_srcs)
 
 		ctx.AddVariationDependencies(nil, pythonLibTag,
 			uniqueLibs(ctx, p.properties.Libs, "version.py2.libs",
@@ -286,6 +288,7 @@
 	case pyVersion3:
 		// deps from "version.py3.srcs" property.
 		android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Srcs)
+		android.ExtractSourcesDeps(ctx, p.properties.Version.Py3.Exclude_srcs)
 
 		ctx.AddVariationDependencies(nil, pythonLibTag,
 			uniqueLibs(ctx, p.properties.Libs, "version.py3.libs",