Expose build-globs.ninja write function to android/soong.

This enables bp2build to write its own build-globs.ninja file.

Test: soong integration tests
Fixes: 193650250
Change-Id: Icb35f3cf3c30929dab1b2c2e9b244321be30f967
diff --git a/bootstrap/command.go b/bootstrap/command.go
index b9ae7aa..b2e229d 100644
--- a/bootstrap/command.go
+++ b/bootstrap/command.go
@@ -208,7 +208,7 @@
 	ctx.RegisterModuleType("blueprint_go_binary", newGoBinaryModuleFactory(bootstrapConfig, true))
 	ctx.RegisterSingletonType("bootstrap", newSingletonFactory(bootstrapConfig))
 
-	ctx.RegisterSingletonType("glob", globSingletonFactory(bootstrapConfig, ctx))
+	ctx.RegisterSingletonType("glob", globSingletonFactory(stage, ctx))
 
 	blueprintFiles, errs := ctx.ParseFileList(filepath.Dir(args.TopFile), filesToParse, config)
 	if len(errs) > 0 {
@@ -273,15 +273,7 @@
 	}
 
 	if args.GlobFile != "" {
-		buffer, errs := generateGlobNinjaFile(bootstrapConfig, config, ctx.Globs)
-		if len(errs) > 0 {
-			fatalErrors(errs)
-		}
-
-		err = ioutil.WriteFile(absolutePath(args.GlobFile), buffer, outFilePermissions)
-		if err != nil {
-			fatalf("error writing %s: %s", args.GlobFile, err)
-		}
+		WriteBuildGlobsNinjaFile(stage, ctx, args, config)
 	}
 
 	err = ctx.WriteBuildFile(out)
diff --git a/bootstrap/glob.go b/bootstrap/glob.go
index 39c662b..be3fe97 100644
--- a/bootstrap/glob.go
+++ b/bootstrap/glob.go
@@ -19,6 +19,7 @@
 	"fmt"
 	"hash/fnv"
 	"io"
+	"io/ioutil"
 	"path/filepath"
 	"strconv"
 	"strings"
@@ -148,15 +149,15 @@
 // re-evaluate them whenever the contents of the searched directories change, and retrigger the
 // primary builder if the results change.
 type globSingleton struct {
-	config     *Config
+	stage      Stage
 	globLister func() pathtools.MultipleGlobResults
 	writeRule  bool
 }
 
-func globSingletonFactory(config *Config, ctx *blueprint.Context) func() blueprint.Singleton {
+func globSingletonFactory(stage Stage, ctx *blueprint.Context) func() blueprint.Singleton {
 	return func() blueprint.Singleton {
 		return &globSingleton{
-			config:     config,
+			stage:      stage,
 			globLister: ctx.Globs,
 		}
 	}
@@ -173,7 +174,7 @@
 
 	// The directory for the intermediates needs to be different for bootstrap and the primary
 	// builder.
-	globsDir := globsDir(ctx.Config().(BootstrapConfig), s.config.stage)
+	globsDir := globsDir(ctx.Config().(BootstrapConfig), s.stage)
 
 	for i, globs := range globBuckets {
 		fileListFile := filepath.Join(globsDir, strconv.Itoa(i))
@@ -205,13 +206,26 @@
 	}
 }
 
-func generateGlobNinjaFile(bootstrapConfig *Config, config interface{},
+func WriteBuildGlobsNinjaFile(stage Stage, ctx *blueprint.Context, args Args, config interface{}) {
+	buffer, errs := generateGlobNinjaFile(stage, config, ctx.Globs)
+	if len(errs) > 0 {
+		fatalErrors(errs)
+	}
+
+	const outFilePermissions = 0666
+	err := ioutil.WriteFile(absolutePath(args.GlobFile), buffer, outFilePermissions)
+	if err != nil {
+		fatalf("error writing %s: %s", args.GlobFile, err)
+	}
+}
+
+func generateGlobNinjaFile(stage Stage, config interface{},
 	globLister func() pathtools.MultipleGlobResults) ([]byte, []error) {
 
 	ctx := blueprint.NewContext()
 	ctx.RegisterSingletonType("glob", func() blueprint.Singleton {
 		return &globSingleton{
-			config:     bootstrapConfig,
+			stage:      stage,
 			globLister: globLister,
 			writeRule:  true,
 		}