cd to / before running soong_build .
This lets one avoid any decisions as to when to chdir there during its
execution and leads to better sandboxing because the pwd doesn't leak to
init() functions anymore.
Test: Manual.
Change-Id: I1560da8ed3a621249426f9e8908aa890c21e13ba
diff --git a/android/env.go b/android/env.go
index a8c7777..58ad0b6 100644
--- a/android/env.go
+++ b/android/env.go
@@ -17,7 +17,6 @@
import (
"fmt"
"os"
- "os/exec"
"strings"
"syscall"
@@ -34,37 +33,27 @@
var originalEnv map[string]string
var soongDelveListen string
var soongDelvePath string
-var soongDelveEnv []string
+var isDebugging bool
-func init() {
- // Delve support needs to read this environment variable very early, before NewConfig has created a way to
- // access originalEnv with dependencies. Store the value where soong_build can find it, it will manually
- // ensure the dependencies are created.
- soongDelveListen = os.Getenv("SOONG_DELVE")
- soongDelvePath = os.Getenv("SOONG_DELVE_PATH")
- if soongDelvePath == "" {
- soongDelvePath, _ = exec.LookPath("dlv")
+func InitEnvironment(envFile string) {
+ var err error
+ originalEnv, err = shared.EnvFromFile(envFile)
+ if err != nil {
+ panic(err)
}
- originalEnv = make(map[string]string)
- soongDelveEnv = []string{}
- for _, env := range os.Environ() {
- idx := strings.IndexRune(env, '=')
- if idx != -1 {
- originalEnv[env[:idx]] = env[idx+1:]
- if env[:idx] != "SOONG_DELVE" && env[:idx] != "SOONG_DELVE_PATH" {
- soongDelveEnv = append(soongDelveEnv, env)
- }
- }
- }
-
- // Clear the environment to prevent use of os.Getenv(), which would not provide dependencies on environment
- // variable values. The environment is available through ctx.Config().Getenv, ctx.Config().IsEnvTrue, etc.
- os.Clearenv()
+ soongDelveListen = originalEnv["SOONG_DELVE"]
+ soongDelvePath = originalEnv["SOONG_DELVE_PATH"]
}
+// Returns whether the current process is running under Delve due to
+// ReexecWithDelveMaybe().
+func IsDebugging() bool {
+ return isDebugging
+}
func ReexecWithDelveMaybe() {
- if soongDelveListen == "" {
+ isDebugging = os.Getenv("SOONG_DELVE_REEXECUTED") == "true"
+ if isDebugging || soongDelveListen == "" {
return
}
@@ -72,6 +61,17 @@
fmt.Fprintln(os.Stderr, "SOONG_DELVE is set but failed to find dlv")
os.Exit(1)
}
+
+ soongDelveEnv := []string{}
+ for _, env := range os.Environ() {
+ idx := strings.IndexRune(env, '=')
+ if idx != -1 {
+ soongDelveEnv = append(soongDelveEnv, env)
+ }
+ }
+
+ soongDelveEnv = append(soongDelveEnv, "SOONG_DELVE_REEXECUTED=true")
+
dlvArgv := []string{
soongDelvePath,
"--listen=:" + soongDelveListen,
@@ -88,17 +88,6 @@
os.Exit(1)
}
-// getenv checks either os.Getenv or originalEnv so that it works before or after the init()
-// function above. It doesn't add any dependencies on the environment variable, so it should
-// only be used for values that won't change. For values that might change use ctx.Config().Getenv.
-func getenv(key string) string {
- if originalEnv == nil {
- return os.Getenv(key)
- } else {
- return originalEnv[key]
- }
-}
-
func EnvSingleton() Singleton {
return &envSingleton{}
}
@@ -108,7 +97,7 @@
func (c *envSingleton) GenerateBuildActions(ctx SingletonContext) {
envDeps := ctx.Config().EnvDeps()
- envFile := PathForOutput(ctx, ".soong.environment")
+ envFile := PathForOutput(ctx, "soong.environment.used")
if ctx.Failed() {
return
}