diff --git a/ui/build/dumpvars.go b/ui/build/dumpvars.go index 82b275010..1925e87b9 100644 --- a/ui/build/dumpvars.go +++ b/ui/build/dumpvars.go @@ -17,6 +17,8 @@ package build import ( "bytes" "fmt" + "io/ioutil" + "os" "strings" "android/soong/ui/metrics" @@ -51,7 +53,17 @@ func DumpMakeVars(ctx Context, config Config, goals, vars []string) (map[string] var ret map[string]string if len(makeVars) > 0 { - var err error + tmpDir, err := ioutil.TempDir("", "dumpvars") + if err != nil { + return nil, err + } + defer os.RemoveAll(tmpDir) + + // It's not safe to use the same TMPDIR as the build, as that can be removed. + config.Environment().Set("TMPDIR", tmpDir) + + SetupLitePath(ctx, config) + ret, err = dumpMakeVars(ctx, config, goals, makeVars, false) if err != nil { return ret, err diff --git a/ui/build/path.go b/ui/build/path.go index 0e1c02cac..c34ba1b52 100644 --- a/ui/build/path.go +++ b/ui/build/path.go @@ -18,6 +18,7 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "runtime" "strings" @@ -53,6 +54,51 @@ func parsePathDir(dir string) []string { return ret } +// A "lite" version of SetupPath used for dumpvars, or other places that need +// minimal overhead (but at the expense of logging). +func SetupLitePath(ctx Context, config Config) { + if config.pathReplaced { + return + } + + ctx.BeginTrace(metrics.RunSetupTool, "litepath") + defer ctx.EndTrace() + + origPath, _ := config.Environment().Get("PATH") + myPath, _ := config.Environment().Get("TMPDIR") + myPath = filepath.Join(myPath, "path") + ensureEmptyDirectoriesExist(ctx, myPath) + + os.Setenv("PATH", origPath) + for name, pathConfig := range paths.Configuration { + if !pathConfig.Symlink { + continue + } + + origExec, err := exec.LookPath(name) + if err != nil { + continue + } + origExec, err = filepath.Abs(origExec) + if err != nil { + continue + } + + err = os.Symlink(origExec, filepath.Join(myPath, name)) + if err != nil { + ctx.Fatalln("Failed to create symlink:", err) + } + } + + myPath, _ = filepath.Abs(myPath) + + prebuiltsPath, _ := filepath.Abs("prebuilts/build-tools/path/" + runtime.GOOS + "-x86") + myPath = prebuiltsPath + string(os.PathListSeparator) + myPath + + config.Environment().Set("PATH", myPath) + config.pathReplaced = true +} + func SetupPath(ctx Context, config Config) { if config.pathReplaced { return