Make Blueprint better callable from Go:

- Make the parsed command line arguments accessible from other packages
- Separate writing the depfile from writing the Ninja file
- Extract a method to compute the name of the file list file for globs

Test: Presubmits.
Change-Id: I4d0daf0f3776a9336da305be0108ebf70a314776
This commit is contained in:
Lukacs T. Berki 2021-04-14 13:47:52 +02:00
parent 78df853e07
commit 98e0efb9a4
4 changed files with 36 additions and 50 deletions

View file

@ -55,43 +55,25 @@ type Args struct {
}
var (
cmdline Args
absSrcDir string
CmdlineArgs Args
absSrcDir string
)
func CmdlineOutFile() string {
return cmdline.OutFile
}
// Returns the build dir as parsed from the command line. This is necessary
// because even though these flags are defined here, soong_build accesses them.
// The plan is to move these flags to soong_build.
func CmdlineBuildDir() string {
return cmdline.BuildDir
}
// Returns the module list file as parsed from the command line. This is necessary
// because even though these flags are defined here, soong_build accesses them.
// The plan is to move these flags to soong_build.
func CmdlineModuleListFile() string {
return cmdline.ModuleListFile
}
func init() {
flag.StringVar(&cmdline.OutFile, "o", "build.ninja", "the Ninja file to output")
flag.StringVar(&cmdline.GlobFile, "globFile", "build-globs.ninja", "the Ninja file of globs to output")
flag.StringVar(&cmdline.BuildDir, "b", ".", "the build output directory")
flag.StringVar(&cmdline.NinjaBuildDir, "n", "", "the ninja builddir directory")
flag.StringVar(&cmdline.DepFile, "d", "", "the dependency file to output")
flag.StringVar(&cmdline.DocFile, "docs", "", "build documentation file to output")
flag.StringVar(&cmdline.Cpuprofile, "cpuprofile", "", "write cpu profile to file")
flag.StringVar(&cmdline.TraceFile, "trace", "", "write trace to file")
flag.StringVar(&cmdline.Memprofile, "memprofile", "", "write memory profile to file")
flag.BoolVar(&cmdline.NoGC, "nogc", false, "turn off GC for debugging")
flag.BoolVar(&cmdline.RunGoTests, "t", false, "build and run go tests during bootstrap")
flag.BoolVar(&cmdline.UseValidations, "use-validations", false, "use validations to depend on go tests")
flag.StringVar(&cmdline.ModuleListFile, "l", "", "file that lists filepaths to parse")
flag.BoolVar(&cmdline.EmptyNinjaFile, "empty-ninja-file", false, "write out a 0-byte ninja file")
flag.StringVar(&CmdlineArgs.OutFile, "o", "build.ninja", "the Ninja file to output")
flag.StringVar(&CmdlineArgs.GlobFile, "globFile", "build-globs.ninja", "the Ninja file of globs to output")
flag.StringVar(&CmdlineArgs.BuildDir, "b", ".", "the build output directory")
flag.StringVar(&CmdlineArgs.NinjaBuildDir, "n", "", "the ninja builddir directory")
flag.StringVar(&CmdlineArgs.DepFile, "d", "", "the dependency file to output")
flag.StringVar(&CmdlineArgs.DocFile, "docs", "", "build documentation file to output")
flag.StringVar(&CmdlineArgs.Cpuprofile, "cpuprofile", "", "write cpu profile to file")
flag.StringVar(&CmdlineArgs.TraceFile, "trace", "", "write trace to file")
flag.StringVar(&CmdlineArgs.Memprofile, "memprofile", "", "write memory profile to file")
flag.BoolVar(&CmdlineArgs.NoGC, "nogc", false, "turn off GC for debugging")
flag.BoolVar(&CmdlineArgs.RunGoTests, "t", false, "build and run go tests during bootstrap")
flag.BoolVar(&CmdlineArgs.UseValidations, "use-validations", false, "use validations to depend on go tests")
flag.StringVar(&CmdlineArgs.ModuleListFile, "l", "", "file that lists filepaths to parse")
flag.BoolVar(&CmdlineArgs.EmptyNinjaFile, "empty-ninja-file", false, "write out a 0-byte ninja file")
}
func Main(ctx *blueprint.Context, config interface{}, generatingPrimaryBuilder bool, extraNinjaFileDeps ...string) {
@ -103,9 +85,13 @@ func Main(ctx *blueprint.Context, config interface{}, generatingPrimaryBuilder b
fatalf("no Blueprints file specified")
}
cmdline.TopFile = flag.Arg(0)
cmdline.GeneratingPrimaryBuilder = generatingPrimaryBuilder
RunBlueprint(cmdline, ctx, config, extraNinjaFileDeps...)
CmdlineArgs.TopFile = flag.Arg(0)
CmdlineArgs.GeneratingPrimaryBuilder = generatingPrimaryBuilder
deps := RunBlueprint(CmdlineArgs, ctx, config, extraNinjaFileDeps...)
err := deptools.WriteDepFile(CmdlineArgs.DepFile, CmdlineArgs.OutFile, deps)
if err != nil {
fatalf("Cannot write depfile '%s': %s", CmdlineArgs.DepFile, err)
}
}
func PrimaryBuilderExtraFlags(args Args, globFile, mainNinjaFile string) []string {
@ -147,7 +133,7 @@ func writeEmptyGlobFile(path string) {
}
}
}
func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}, extraNinjaFileDeps ...string) {
func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}, extraNinjaFileDeps ...string) []string {
runtime.GOMAXPROCS(runtime.NumCPU())
if args.NoGC {
@ -253,12 +239,12 @@ func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}, extraNi
if err != nil {
fatalErrors([]error{err})
}
return
return nil
}
if c, ok := config.(ConfigStopBefore); ok {
if c.StopBefore() == StopBeforePrepareBuildActions {
return
return deps
}
}
@ -270,7 +256,7 @@ func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}, extraNi
if c, ok := config.(ConfigStopBefore); ok {
if c.StopBefore() == StopBeforeWriteNinja {
return
return deps
}
}
@ -308,13 +294,6 @@ func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}, extraNi
}
}
if args.DepFile != "" {
err := deptools.WriteDepFile(absolutePath(args.DepFile), args.OutFile, deps)
if err != nil {
fatalf("error writing depfile: %s", err)
}
}
err = ctx.WriteBuildFile(out)
if err != nil {
fatalf("error writing Ninja file contents: %s", err)
@ -350,6 +329,8 @@ func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}, extraNi
defer f.Close()
pprof.WriteHeapProfile(f)
}
return deps
}
func fatalf(format string, args ...interface{}) {

View file

@ -121,7 +121,7 @@ func globSingletonFactory(ctx *blueprint.Context) func() blueprint.Singleton {
func (s *globSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
for _, g := range s.globLister() {
fileListFile := filepath.Join(ctx.Config().(BootstrapConfig).BuildDir(), ".glob", g.Name)
fileListFile := g.FileListFile(ctx.Config().(BootstrapConfig).BuildDir())
if s.writeRule {
// We need to write the file list here so that it has an older modified date

View file

@ -32,7 +32,7 @@ type Config struct {
}
func (c Config) SrcDir() string {
return bootstrap.CmdlineBuildDir()
return bootstrap.CmdlineArgs.BuildDir
}
func (c Config) RemoveAbandonedFilesUnder(buildDir string) (under, exempt []string) {

View file

@ -17,6 +17,7 @@ package blueprint
import (
"crypto/md5"
"fmt"
"path/filepath"
"sort"
"strings"
@ -28,6 +29,10 @@ type GlobPath struct {
Name string
}
func (g *GlobPath) FileListFile(buildDir string) string {
return filepath.Join(buildDir, ".glob", g.Name)
}
func verifyGlob(fileName, pattern string, excludes []string, g GlobPath) {
if pattern != g.Pattern {
panic(fmt.Errorf("Mismatched patterns %q and %q for glob file %q", pattern, g.Pattern, fileName))