Support excludes in globs

Java resource support requires globbing directories while ignoring
specific filenames.  Add support for multiple -e options to
soong_glob to specify filenames to exclude, and split out Glob
into GlobRule to insert a rule to generate a glob file list.

Change-Id: Ia911dd68bd1638452881d18378572d015fd4e31a
diff --git a/common/glob.go b/common/glob.go
index 5568cbc..a3cfab2 100644
--- a/common/glob.go
+++ b/common/glob.go
@@ -17,6 +17,7 @@
 import (
 	"fmt"
 	"path/filepath"
+	"strings"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/bootstrap"
@@ -46,7 +47,7 @@
 	// and writes it to $out if it has changed, and writes the directories to $out.d
 	globRule = pctx.StaticRule("globRule",
 		blueprint.RuleParams{
-			Command:     fmt.Sprintf(`%s -o $out "$glob"`, globCmd),
+			Command:     fmt.Sprintf(`%s -o $out $excludes "$glob"`, globCmd),
 			Description: "glob $glob",
 
 			Restat:    true,
@@ -54,7 +55,7 @@
 			Deps:      blueprint.DepsGCC,
 			Depfile:   "$out.d",
 		},
-		"glob")
+		"glob", "excludes")
 )
 
 func hasGlob(in []string) bool {
@@ -88,13 +89,30 @@
 	fileListFile := filepath.Join(ModuleOutDir(ctx), "glob", globToString(globPattern))
 	depFile := fileListFile + ".d"
 
+	var excludes []string
+
 	// Get a globbed file list, and write out fileListFile and depFile
-	files, err := glob.GlobWithDepFile(globPattern, fileListFile, depFile)
+	files, err := glob.GlobWithDepFile(globPattern, fileListFile, depFile, excludes)
 	if err != nil {
 		ctx.ModuleErrorf("glob: %s", err.Error())
 		return []string{globPattern}
 	}
 
+	GlobRule(ctx, globPattern, excludes, fileListFile, depFile)
+
+	// Make build.ninja depend on the fileListFile
+	ctx.AddNinjaFileDeps(fileListFile)
+
+	return files
+}
+
+func GlobRule(ctx AndroidModuleContext, globPattern string, excludes []string,
+	fileListFile, depFile string) {
+	var excludeArgs []string
+	for _, e := range excludes {
+		excludeArgs = append(excludeArgs, "-e "+e)
+	}
+
 	// Create a rule to rebuild fileListFile if a directory in depFile changes.  fileListFile
 	// will only be rewritten if it has changed, preventing unnecesary build.ninja regenerations.
 	ctx.Build(pctx, blueprint.BuildParams{
@@ -102,7 +120,8 @@
 		Outputs:   []string{fileListFile},
 		Implicits: []string{globCmd},
 		Args: map[string]string{
-			"glob": globPattern,
+			"glob":     globPattern,
+			"excludes": strings.Join(excludeArgs, " "),
 		},
 	})
 
@@ -111,11 +130,6 @@
 		Rule:    blueprint.Phony,
 		Outputs: []string{depFile},
 	})
-
-	// Make build.ninja depend on the fileListFile
-	ctx.AddNinjaFileDeps(fileListFile)
-
-	return files
 }
 
 func globToString(glob string) string {