Move globbing on top of pathtools.GlobWithExcludes

pathtools.GlobWithExcludes contains all the features of
glob.GlobWithDepFile now, make GlobWithDepFile a wrapper
around GlobWithExcludes that writes the results to a file.

Change-Id: Ie75d9042845505f499aac7fa00d3c90f8ecab4f7
diff --git a/Blueprints b/Blueprints
index 6f3c487..958c79e 100644
--- a/Blueprints
+++ b/Blueprints
@@ -62,6 +62,7 @@
     pkgPath: "android/soong/glob",
     deps: [
         "blueprint-deptools",
+        "blueprint-pathtools",
     ],
     srcs: [
         "glob/glob.go",
diff --git a/build.ninja.in b/build.ninja.in
index 7484518..f579c57 100644
--- a/build.ninja.in
+++ b/build.ninja.in
@@ -53,7 +53,7 @@
 # Variant:
 # Type:    bootstrap_go_binary
 # Factory: github.com/google/blueprint/bootstrap.newGoBinaryModule
-# Defined: build/soong/Blueprints:160:1
+# Defined: build/soong/Blueprints:161:1
 
 build .bootstrap/androidmk/obj/androidmk.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/androidmk/cmd/androidmk/android.go $
@@ -79,7 +79,7 @@
 # Variant:
 # Type:    bootstrap_go_package
 # Factory: github.com/google/blueprint/bootstrap.newGoPackageModule
-# Defined: build/soong/Blueprints:173:1
+# Defined: build/soong/Blueprints:174:1
 
 build .bootstrap/androidmk-parser/pkg/android/soong/androidmk/parser.a: $
         g.bootstrap.gc $
@@ -310,7 +310,7 @@
 # Variant:
 # Type:    bootstrap_go_package
 # Factory: github.com/google/blueprint/bootstrap.newGoPackageModule
-# Defined: build/soong/Blueprints:93:1
+# Defined: build/soong/Blueprints:94:1
 
 build .bootstrap/soong-cc/pkg/android/soong/cc.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/cc/builder.go $
@@ -342,7 +342,7 @@
 # Variant:
 # Type:    bootstrap_go_package
 # Factory: github.com/google/blueprint/bootstrap.newGoPackageModule
-# Defined: build/soong/Blueprints:71:1
+# Defined: build/soong/Blueprints:72:1
 
 build .bootstrap/soong-common/pkg/android/soong/common.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/common/arch.go $
@@ -384,7 +384,7 @@
 # Variant:
 # Type:    bootstrap_go_package
 # Factory: github.com/google/blueprint/bootstrap.newGoPackageModule
-# Defined: build/soong/Blueprints:117:1
+# Defined: build/soong/Blueprints:118:1
 
 build .bootstrap/soong-genrule/pkg/android/soong/genrule.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/genrule/genrule.go | $
@@ -411,8 +411,9 @@
 
 build .bootstrap/soong-glob/pkg/android/soong/glob.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/glob/glob.go | ${g.bootstrap.gcCmd} $
-        .bootstrap/blueprint-deptools/pkg/github.com/google/blueprint/deptools.a
-    incFlags = -I .bootstrap/blueprint-deptools/pkg
+        .bootstrap/blueprint-deptools/pkg/github.com/google/blueprint/deptools.a $
+        .bootstrap/blueprint-pathtools/pkg/github.com/google/blueprint/pathtools.a
+    incFlags = -I .bootstrap/blueprint-deptools/pkg -I .bootstrap/blueprint-pathtools/pkg
     pkgPath = android/soong/glob
 default .bootstrap/soong-glob/pkg/android/soong/glob.a
 
@@ -421,7 +422,7 @@
 # Variant:
 # Type:    bootstrap_go_package
 # Factory: github.com/google/blueprint/bootstrap.newGoPackageModule
-# Defined: build/soong/Blueprints:137:1
+# Defined: build/soong/Blueprints:138:1
 
 build .bootstrap/soong-java/pkg/android/soong/java.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/java/app_builder.go $
@@ -514,14 +515,15 @@
         ${g.bootstrap.srcDir}/build/soong/cmd/soong_glob/soong_glob.go | $
         ${g.bootstrap.gcCmd} $
         .bootstrap/blueprint-deptools/pkg/github.com/google/blueprint/deptools.a $
+        .bootstrap/blueprint-pathtools/pkg/github.com/google/blueprint/pathtools.a $
         .bootstrap/soong-glob/pkg/android/soong/glob.a
-    incFlags = -I .bootstrap/blueprint-deptools/pkg -I .bootstrap/soong-glob/pkg
+    incFlags = -I .bootstrap/blueprint-deptools/pkg -I .bootstrap/blueprint-pathtools/pkg -I .bootstrap/soong-glob/pkg
     pkgPath = soong_glob
 default .bootstrap/soong_glob/obj/soong_glob.a
 
 build .bootstrap/soong_glob/obj/a.out: g.bootstrap.link $
         .bootstrap/soong_glob/obj/soong_glob.a | ${g.bootstrap.linkCmd}
-    libDirFlags = -L .bootstrap/blueprint-deptools/pkg -L .bootstrap/soong-glob/pkg
+    libDirFlags = -L .bootstrap/blueprint-deptools/pkg -L .bootstrap/blueprint-pathtools/pkg -L .bootstrap/soong-glob/pkg
 default .bootstrap/soong_glob/obj/a.out
 
 build .bootstrap/bin/soong_glob: g.bootstrap.cp $
@@ -533,7 +535,7 @@
 # Variant:
 # Type:    bootstrap_go_binary
 # Factory: github.com/google/blueprint/bootstrap.newGoBinaryModule
-# Defined: build/soong/Blueprints:130:1
+# Defined: build/soong/Blueprints:131:1
 
 build .bootstrap/soong_jar/obj/soong_jar.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/cmd/soong_jar/soong_jar.go | $
diff --git a/glob/glob.go b/glob/glob.go
index a07501a..060c9dd 100644
--- a/glob/glob.go
+++ b/glob/glob.go
@@ -21,6 +21,7 @@
 	"strings"
 
 	"github.com/google/blueprint/deptools"
+	"github.com/google/blueprint/pathtools"
 )
 
 func IsGlob(glob string) bool {
@@ -36,55 +37,11 @@
 //
 // Returns a list of file paths, and an error.
 func GlobWithDepFile(glob, fileListFile, depFile string, excludes []string) (files []string, err error) {
-	globPattern := filepath.Base(glob)
-	globDir := filepath.Dir(glob)
-	recursive := false
-
-	if filepath.Base(globDir) == "**" {
-		recursive = true
-		globDir = filepath.Dir(globDir)
+	files, dirs, err := pathtools.GlobWithExcludes(glob, excludes)
+	if err != nil {
+		return nil, err
 	}
 
-	var dirs []string
-
-	err = filepath.Walk(globDir,
-		func(path string, info os.FileInfo, err error) error {
-			if err != nil {
-				return err
-			}
-
-			if info.Mode().IsDir() {
-				dirs = append(dirs, path)
-				if !recursive && path != globDir {
-					return filepath.SkipDir
-				}
-			} else if info.Mode().IsRegular() {
-				match, err := filepath.Match(globPattern, info.Name())
-				if err != nil {
-					return err
-				}
-				if match {
-					for _, e := range excludes {
-						var excludeMatch bool
-						if filepath.Base(e) == e {
-							excludeMatch, err = filepath.Match(e, info.Name())
-						} else {
-							excludeMatch, err = filepath.Match(e, path)
-						}
-						if err != nil {
-							return err
-						}
-						if excludeMatch {
-							return nil
-						}
-					}
-					files = append(files, path)
-				}
-			}
-
-			return nil
-		})
-
 	fileList := strings.Join(files, "\n") + "\n"
 
 	writeFileIfChanged(fileListFile, []byte(fileList), 0666)