From c3ac2a249ad4194edee38bcf5f915114b6eb25f7 Mon Sep 17 00:00:00 2001 From: Joe Onorato Date: Wed, 5 Jun 2024 11:25:24 -0700 Subject: [PATCH] Handle absolute paths for the ninja shard writing. Test: OUT_DIR=/source/foo m nothing Change-Id: Id3849e7446cd0cb26a5b5c74ac3bf4521449b716 --- bootstrap/command.go | 18 +++++------------- bootstrap/glob.go | 4 ++-- context.go | 11 +++++++++-- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/bootstrap/command.go b/bootstrap/command.go index 4e0b40e..301f0e3 100644 --- a/bootstrap/command.go +++ b/bootstrap/command.go @@ -20,7 +20,6 @@ import ( "fmt" "io" "os" - "path/filepath" "runtime" "runtime/debug" "runtime/pprof" @@ -64,7 +63,7 @@ func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, conf } if args.Cpuprofile != "" { - f, err := os.Create(joinPath(ctx.SrcDir(), args.Cpuprofile)) + f, err := os.Create(blueprint.JoinPath(ctx.SrcDir(), args.Cpuprofile)) if err != nil { return nil, fmt.Errorf("error opening cpuprofile: %s", err) } @@ -74,7 +73,7 @@ func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, conf } if args.TraceFile != "" { - f, err := os.Create(joinPath(ctx.SrcDir(), args.TraceFile)) + f, err := os.Create(blueprint.JoinPath(ctx.SrcDir(), args.TraceFile)) if err != nil { return nil, fmt.Errorf("error opening trace: %s", err) } @@ -159,12 +158,12 @@ func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, conf ctx.BeginEvent("write_files") defer ctx.EndEvent("write_files") if args.EmptyNinjaFile { - if err := os.WriteFile(joinPath(ctx.SrcDir(), args.OutFile), []byte(nil), blueprint.OutFilePermissions); err != nil { + if err := os.WriteFile(blueprint.JoinPath(ctx.SrcDir(), args.OutFile), []byte(nil), blueprint.OutFilePermissions); err != nil { return nil, fmt.Errorf("error writing empty Ninja file: %s", err) } out = io.Discard.(blueprint.StringWriterWriter) } else { - f, err := os.OpenFile(joinPath(ctx.SrcDir(), args.OutFile), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, blueprint.OutFilePermissions) + f, err := os.OpenFile(blueprint.JoinPath(ctx.SrcDir(), args.OutFile), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, blueprint.OutFilePermissions) if err != nil { return nil, fmt.Errorf("error opening Ninja file: %s", err) } @@ -195,7 +194,7 @@ func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, conf } if args.Memprofile != "" { - f, err := os.Create(joinPath(ctx.SrcDir(), args.Memprofile)) + f, err := os.Create(blueprint.JoinPath(ctx.SrcDir(), args.Memprofile)) if err != nil { return nil, fmt.Errorf("error opening memprofile: %s", err) } @@ -223,10 +222,3 @@ func fatalErrors(errs []error) error { return errors.New("fatal errors encountered") } - -func joinPath(base, path string) string { - if filepath.IsAbs(path) { - return path - } - return filepath.Join(base, path) -} diff --git a/bootstrap/glob.go b/bootstrap/glob.go index 9054341..4611cef 100644 --- a/bootstrap/glob.go +++ b/bootstrap/glob.go @@ -196,7 +196,7 @@ 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. - absoluteFileListFile := joinPath(s.SrcDir, fileListFile) + absoluteFileListFile := blueprint.JoinPath(s.SrcDir, fileListFile) err := pathtools.WriteFileIfChanged(absoluteFileListFile, globs.FileList(), 0666) if err != nil { panic(fmt.Errorf("error writing %s: %s", fileListFile, err)) @@ -217,7 +217,7 @@ func WriteBuildGlobsNinjaFile(glob *GlobSingleton, config interface{}) error { } const outFilePermissions = 0666 - err := ioutil.WriteFile(joinPath(glob.SrcDir, glob.GlobFile), buffer, outFilePermissions) + err := ioutil.WriteFile(blueprint.JoinPath(glob.SrcDir, glob.GlobFile), buffer, outFilePermissions) if err != nil { return fmt.Errorf("error writing %s: %s", glob.GlobFile, err) } diff --git a/context.go b/context.go index 00dd7d8..46f2985 100644 --- a/context.go +++ b/context.go @@ -4544,9 +4544,9 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter, shardNinja bool, ninjaF wg.Add(1) go func(file string, batchModules []*moduleInfo) { defer wg.Done() - f, err := os.OpenFile(filepath.Join(c.SrcDir(), file), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, OutFilePermissions) + f, err := os.OpenFile(JoinPath(c.SrcDir(), file), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, OutFilePermissions) if err != nil { - errorCh <- fmt.Errorf("error opening Ninja file: %s", err) + errorCh <- fmt.Errorf("error opening Ninja file shard: %s", err) return } defer func() { @@ -5259,3 +5259,10 @@ func (pi *PackageIncludes) matchesIncludeTags(ctx *Context) (bool, error) { } return true, nil } + +func JoinPath(base, path string) string { + if filepath.IsAbs(path) { + return path + } + return filepath.Join(base, path) +}