Remove global variables in command.go . am: 980c1f0c72 am: b636b78072

Original change: https://android-review.googlesource.com/c/platform/build/blueprint/+/1796446

Change-Id: Ia99ddafe6dd6e46ed6865eb0f1d11177d18ef715
This commit is contained in:
Lukacs T. Berki 2021-08-17 15:27:32 +00:00 committed by Automerger Merge Worker
commit 7b565f548c
3 changed files with 18 additions and 57 deletions

View file

@ -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
}

View file

@ -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)
}

View file

@ -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,
}
})