From 980c1f0c728b72aa6b5e0c47ba808269e520514e Mon Sep 17 00:00:00 2001 From: "Lukacs T. Berki" Date: Mon, 16 Aug 2021 15:14:26 +0200 Subject: [PATCH] Remove global variables in command.go . There used to be two: - The absolute source directory is now threaded through the code instead - The command line arguments are moved to soong_build Test: Presubmits. Change-Id: Ie09de1a2862ec4ced2c7385198300359f805127d --- bootstrap/cleanup.go | 2 +- bootstrap/command.go | 58 ++++++-------------------------------------- bootstrap/glob.go | 15 ++++++++---- 3 files changed, 18 insertions(+), 57 deletions(-) diff --git a/bootstrap/cleanup.go b/bootstrap/cleanup.go index 9dbea2a..8aa8318 100644 --- a/bootstrap/cleanup.go +++ b/bootstrap/cleanup.go @@ -70,7 +70,7 @@ func removeAbandonedFilesUnder(ctx *blueprint.Context, for _, filePath := range filePaths { isTarget := targets[filePath] if !isTarget { - err = removeFileAndEmptyDirs(absolutePath(filePath)) + err = removeFileAndEmptyDirs(joinPath(ctx.SrcDir(), filePath)) if err != nil { return err } diff --git a/bootstrap/command.go b/bootstrap/command.go index f1322e4..507c4c0 100644 --- a/bootstrap/command.go +++ b/bootstrap/command.go @@ -16,7 +16,6 @@ package bootstrap import ( "bufio" - "flag" "fmt" "io" "io/ioutil" @@ -28,7 +27,6 @@ import ( "runtime/trace" "github.com/google/blueprint" - "github.com/google/blueprint/deptools" ) type Args struct { @@ -56,46 +54,6 @@ type Args struct { PrimaryBuilderInvocations []PrimaryBuilderInvocation } -var ( - CmdlineArgs Args - absSrcDir string -) - -func init() { - 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.GlobListDir, "globListDir", "", "the directory containing the glob list files") - 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.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) { - if !flag.Parsed() { - flag.Parse() - } - - if flag.NArg() != 1 { - fatalf("no Blueprints file specified") - } - - CmdlineArgs.TopFile = flag.Arg(0) - CmdlineArgs.GeneratingPrimaryBuilder = generatingPrimaryBuilder - ninjaDeps := RunBlueprint(CmdlineArgs, ctx, config) - err := deptools.WriteDepFile(CmdlineArgs.DepFile, CmdlineArgs.OutFile, ninjaDeps) - if err != nil { - fatalf("Cannot write depfile '%s': %s", CmdlineArgs.DepFile, err) - } -} - func PrimaryBuilderExtraFlags(args Args, mainNinjaFile string) []string { result := make([]string, 0) @@ -131,10 +89,8 @@ func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}) []strin debug.SetGCPercent(-1) } - absSrcDir = ctx.SrcDir() - if args.Cpuprofile != "" { - f, err := os.Create(absolutePath(args.Cpuprofile)) + f, err := os.Create(joinPath(ctx.SrcDir(), args.Cpuprofile)) if err != nil { fatalf("error opening cpuprofile: %s", err) } @@ -144,7 +100,7 @@ func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}) []strin } if args.TraceFile != "" { - f, err := os.Create(absolutePath(args.TraceFile)) + f, err := os.Create(joinPath(ctx.SrcDir(), args.TraceFile)) if err != nil { fatalf("error opening trace: %s", err) } @@ -249,13 +205,13 @@ func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}) []strin var buf *bufio.Writer if args.EmptyNinjaFile { - if err := ioutil.WriteFile(absolutePath(args.OutFile), []byte(nil), outFilePermissions); err != nil { + if err := ioutil.WriteFile(joinPath(ctx.SrcDir(), args.OutFile), []byte(nil), outFilePermissions); err != nil { fatalf("error writing empty Ninja file: %s", err) } } if stage != StageMain || !args.EmptyNinjaFile { - f, err = os.OpenFile(absolutePath(args.OutFile), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, outFilePermissions) + f, err = os.OpenFile(joinPath(ctx.SrcDir(), args.OutFile), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, outFilePermissions) if err != nil { fatalf("error opening Ninja file: %s", err) } @@ -297,7 +253,7 @@ func RunBlueprint(args Args, ctx *blueprint.Context, config interface{}) []strin } if args.Memprofile != "" { - f, err := os.Create(absolutePath(args.Memprofile)) + f, err := os.Create(joinPath(ctx.SrcDir(), args.Memprofile)) if err != nil { fatalf("error opening memprofile: %s", err) } @@ -331,9 +287,9 @@ func fatalErrors(errs []error) { os.Exit(1) } -func absolutePath(path string) string { +func joinPath(base, path string) string { if filepath.IsAbs(path) { return path } - return filepath.Join(absSrcDir, path) + return filepath.Join(base, path) } diff --git a/bootstrap/glob.go b/bootstrap/glob.go index 2666cea..617a1d2 100644 --- a/bootstrap/glob.go +++ b/bootstrap/glob.go @@ -151,6 +151,7 @@ func joinWithPrefixAndQuote(strs []string, prefix string) string { type globSingleton struct { globListDir string globLister func() pathtools.MultipleGlobResults + srcDir string writeRule bool } @@ -159,6 +160,7 @@ func globSingletonFactory(globListDir string, ctx *blueprint.Context) func() blu return &globSingleton{ globListDir: globListDir, globLister: ctx.Globs, + srcDir: ctx.SrcDir(), } } } @@ -174,7 +176,8 @@ func (s *globSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) { // The directory for the intermediates needs to be different for bootstrap and the primary // builder. - globsDir := globsDir(ctx.Config().(BootstrapConfig), s.globListDir) + bootstrapConfig := ctx.Config().(BootstrapConfig) + globsDir := globsDir(bootstrapConfig, s.globListDir) for i, globs := range globBuckets { fileListFile := filepath.Join(globsDir, strconv.Itoa(i)) @@ -192,7 +195,8 @@ func (s *globSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) { // We don't need to write the depfile because we're guaranteed that ninja // will run the command at least once (to record it into the ninja_log), so // the depfile will be loaded from that execution. - err := pathtools.WriteFileIfChanged(absolutePath(fileListFile), globs.FileList(), 0666) + absoluteFileListFile := joinPath(s.srcDir, fileListFile) + err := pathtools.WriteFileIfChanged(absoluteFileListFile, globs.FileList(), 0666) if err != nil { panic(fmt.Errorf("error writing %s: %s", fileListFile, err)) } @@ -207,18 +211,18 @@ func (s *globSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) { } func WriteBuildGlobsNinjaFile(globListDir string, ctx *blueprint.Context, args Args, config interface{}) { - buffer, errs := generateGlobNinjaFile(globListDir, config, ctx.Globs) + buffer, errs := generateGlobNinjaFile(ctx.SrcDir(), globListDir, config, ctx.Globs) if len(errs) > 0 { fatalErrors(errs) } const outFilePermissions = 0666 - err := ioutil.WriteFile(absolutePath(args.GlobFile), buffer, outFilePermissions) + err := ioutil.WriteFile(joinPath(ctx.SrcDir(), args.GlobFile), buffer, outFilePermissions) if err != nil { fatalf("error writing %s: %s", args.GlobFile, err) } } -func generateGlobNinjaFile(globListDir string, config interface{}, +func generateGlobNinjaFile(srcDir, globListDir string, config interface{}, globLister func() pathtools.MultipleGlobResults) ([]byte, []error) { ctx := blueprint.NewContext() @@ -226,6 +230,7 @@ func generateGlobNinjaFile(globListDir string, config interface{}, return &globSingleton{ globListDir: globListDir, globLister: globLister, + srcDir: srcDir, writeRule: true, } })