Handle absolute paths for the ninja shard writing.

Test: OUT_DIR=/source/foo m nothing
Change-Id: Id3849e7446cd0cb26a5b5c74ac3bf4521449b716
This commit is contained in:
Joe Onorato 2024-06-05 11:25:24 -07:00
parent d5133cfc64
commit c3ac2a249a
3 changed files with 16 additions and 17 deletions

View file

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"runtime/pprof" "runtime/pprof"
@ -64,7 +63,7 @@ func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, conf
} }
if args.Cpuprofile != "" { 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 { if err != nil {
return nil, fmt.Errorf("error opening cpuprofile: %s", err) 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 != "" { 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 { if err != nil {
return nil, fmt.Errorf("error opening trace: %s", err) 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") ctx.BeginEvent("write_files")
defer ctx.EndEvent("write_files") defer ctx.EndEvent("write_files")
if args.EmptyNinjaFile { 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) return nil, fmt.Errorf("error writing empty Ninja file: %s", err)
} }
out = io.Discard.(blueprint.StringWriterWriter) out = io.Discard.(blueprint.StringWriterWriter)
} else { } 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 { if err != nil {
return nil, fmt.Errorf("error opening Ninja file: %s", err) 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 != "" { 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 { if err != nil {
return nil, fmt.Errorf("error opening memprofile: %s", err) return nil, fmt.Errorf("error opening memprofile: %s", err)
} }
@ -223,10 +222,3 @@ func fatalErrors(errs []error) error {
return errors.New("fatal errors encountered") return errors.New("fatal errors encountered")
} }
func joinPath(base, path string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(base, path)
}

View file

@ -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 // 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 // will run the command at least once (to record it into the ninja_log), so
// the depfile will be loaded from that execution. // 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) err := pathtools.WriteFileIfChanged(absoluteFileListFile, globs.FileList(), 0666)
if err != nil { if err != nil {
panic(fmt.Errorf("error writing %s: %s", fileListFile, err)) panic(fmt.Errorf("error writing %s: %s", fileListFile, err))
@ -217,7 +217,7 @@ func WriteBuildGlobsNinjaFile(glob *GlobSingleton, config interface{}) error {
} }
const outFilePermissions = 0666 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 { if err != nil {
return fmt.Errorf("error writing %s: %s", glob.GlobFile, err) return fmt.Errorf("error writing %s: %s", glob.GlobFile, err)
} }

View file

@ -4544,9 +4544,9 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter, shardNinja bool, ninjaF
wg.Add(1) wg.Add(1)
go func(file string, batchModules []*moduleInfo) { go func(file string, batchModules []*moduleInfo) {
defer wg.Done() 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 { if err != nil {
errorCh <- fmt.Errorf("error opening Ninja file: %s", err) errorCh <- fmt.Errorf("error opening Ninja file shard: %s", err)
return return
} }
defer func() { defer func() {
@ -5259,3 +5259,10 @@ func (pi *PackageIncludes) matchesIncludeTags(ctx *Context) (bool, error) {
} }
return true, nil return true, nil
} }
func JoinPath(base, path string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(base, path)
}