package base
-import "strings"
-
-// EnvForDir returns a copy of the environment
-// suitable for running in the given directory.
-// The environment is the current process's environment
-// but with an updated $PWD, so that an os.Getwd in the
-// child will be faster.
+// EnvForDir returns a modified environment suitable for running in the given
+// directory.
+// The environment is the supplied base environment but with an updated $PWD, so
+// that an os.Getwd in the child will be faster.
func EnvForDir(dir string, base []string) []string {
// Internally we only use rooted paths, so dir is rooted.
// Even if dir is not rooted, no harm done.
- return MergeEnvLists([]string{"PWD=" + dir}, base)
-}
-
-// MergeEnvLists merges the two environment lists such that
-// variables with the same name in "in" replace those in "out".
-// This always returns a newly allocated slice.
-func MergeEnvLists(in, out []string) []string {
- out = append([]string(nil), out...)
-NextVar:
- for _, inkv := range in {
- k := strings.SplitAfterN(inkv, "=", 2)[0]
- for i, outkv := range out {
- if strings.HasPrefix(outkv, k) {
- out[i] = inkv
- continue NextVar
- }
- }
- out = append(out, inkv)
- }
- return out
+ return append(base, "PWD="+dir)
}
cleanup := passLongArgsInResponseFiles(cmd)
defer cleanup()
cmd.Dir = dir
- cmd.Env = base.MergeEnvLists(env, base.EnvForDir(cmd.Dir, os.Environ()))
+ cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
+ cmd.Env = append(cmd.Env, env...)
err := cmd.Run()
// err can be something like 'exit status 1'.
}
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Dir = b.WorkDir
- cmd.Env = base.MergeEnvLists([]string{"LC_ALL=C"}, base.EnvForDir(cmd.Dir, os.Environ()))
+ cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
+ cmd.Env = append(cmd.Env, "LC_ALL=C")
out, _ := cmd.CombinedOutput()
// GCC says "unrecognized command line option".
// clang says "unknown argument".