diff options
author | 2022-01-25 22:11:01 -0500 | |
---|---|---|
committer | 2022-01-26 18:40:43 -0500 | |
commit | 3ff037e3d9b9e5349ff9e183f76c11f218d25f06 (patch) | |
tree | 18c6cad1b1a8336f40937633aad1b3b2a1a8c22d | |
parent | a61dbd457e77b18a160de0574d8b676057acc29e (diff) |
Move env var config loading to be within the config package.
When the environment variable config file is loaded after config object
is initialized, we end up loading the USE_RBE variable in the config
file after the code to start reproxy process is run. This causes a
problem when USE_RBE variable itself is set in the config files. To
avoid this, I've moved the config file loading code to be within the
config package, this makes the main.go file cleaner too.
Test:
Ran a build with USE_RBE:true set in the config json file. The build
fails without this change and succeeds with this change.
Bug: b/209486170
Change-Id: Iab3957b64f4b5456a861057d16ad318f4f78e0cb
-rw-r--r-- | cmd/soong_ui/main.go | 48 | ||||
-rw-r--r-- | ui/build/config.go | 52 |
2 files changed, 51 insertions, 49 deletions
diff --git a/cmd/soong_ui/main.go b/cmd/soong_ui/main.go index d8cb47a44..e3ccb6c5f 100644 --- a/cmd/soong_ui/main.go +++ b/cmd/soong_ui/main.go @@ -16,7 +16,6 @@ package main import ( "context" - "encoding/json" "flag" "fmt" "io/ioutil" @@ -36,11 +35,6 @@ import ( "android/soong/ui/tracer" ) -const ( - configDir = "vendor/google/tools/soong_config" - jsonSuffix = "json" -) - // A command represents an operation to be executed in the soong build // system. type command struct { @@ -117,43 +111,6 @@ func inList(s string, list []string) bool { return indexList(s, list) != -1 } -func loadEnvConfig(config build.Config) error { - bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG") - if bc == "" { - return nil - } - configDirs := []string{ - os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"), - config.OutDir(), - configDir, - } - var cfgFile string - for _, dir := range configDirs { - cfgFile = filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix)) - if _, err := os.Stat(cfgFile); err == nil { - break - } - } - - envVarsJSON, err := ioutil.ReadFile(cfgFile) - if err != nil { - fmt.Fprintf(os.Stderr, "\033[33mWARNING:\033[0m failed to open config file %s: %s\n", cfgFile, err.Error()) - return nil - } - - var envVars map[string]map[string]string - if err := json.Unmarshal(envVarsJSON, &envVars); err != nil { - return fmt.Errorf("env vars config file: %s did not parse correctly: %s", cfgFile, err.Error()) - } - for k, v := range envVars["env"] { - if os.Getenv(k) != "" { - continue - } - config.Environment().Set(k, v) - } - return nil -} - // Main execution of soong_ui. The command format is as follows: // // soong_ui <command> [<arg 1> <arg 2> ... <arg n>] @@ -218,11 +175,6 @@ func main() { config := c.config(buildCtx, args...) - if err := loadEnvConfig(config); err != nil { - fmt.Fprintf(os.Stderr, "failed to parse env config files: %v", err) - os.Exit(1) - } - build.SetupOutDir(buildCtx, config) if config.UseBazel() && config.Dist() { diff --git a/ui/build/config.go b/ui/build/config.go index b6d0d2747..a73193290 100644 --- a/ui/build/config.go +++ b/ui/build/config.go @@ -15,7 +15,9 @@ package build import ( + "encoding/json" "fmt" + "io/ioutil" "os" "path/filepath" "runtime" @@ -30,6 +32,11 @@ import ( smpb "android/soong/ui/metrics/metrics_proto" ) +const ( + envConfigDir = "vendor/google/tools/soong_config" + jsonSuffix = "json" +) + type Config struct{ *configImpl } type configImpl struct { @@ -128,6 +135,43 @@ func checkTopDir(ctx Context) { } } +func loadEnvConfig(config *configImpl) error { + bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG") + if bc == "" { + return nil + } + configDirs := []string{ + os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"), + config.OutDir(), + envConfigDir, + } + var cfgFile string + for _, dir := range configDirs { + cfgFile = filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix)) + if _, err := os.Stat(cfgFile); err == nil { + break + } + } + + envVarsJSON, err := ioutil.ReadFile(cfgFile) + if err != nil { + fmt.Fprintf(os.Stderr, "\033[33mWARNING:\033[0m failed to open config file %s: %s\n", cfgFile, err.Error()) + return nil + } + + var envVars map[string]map[string]string + if err := json.Unmarshal(envVarsJSON, &envVars); err != nil { + return fmt.Errorf("env vars config file: %s did not parse correctly: %s", cfgFile, err.Error()) + } + for k, v := range envVars["env"] { + if os.Getenv(k) != "" { + continue + } + config.Environment().Set(k, v) + } + return nil +} + func NewConfig(ctx Context, args ...string) Config { ret := &configImpl{ environ: OsEnvironment(), @@ -157,6 +201,12 @@ func NewConfig(ctx Context, args ...string) Config { ret.environ.Set("OUT_DIR", outDir) } + // loadEnvConfig needs to know what the OUT_DIR is, so it should + // be called after we determine the appropriate out directory. + if err := loadEnvConfig(ret); err != nil { + ctx.Fatalln("Failed to parse env config files: %v", err) + } + if distDir, ok := ret.environ.Get("DIST_DIR"); ok { ret.distDir = filepath.Clean(distDir) } else { @@ -988,7 +1038,7 @@ func (c *configImpl) StartGoma() bool { } func (c *configImpl) UseRBE() bool { - if v, ok := c.environ.Get("USE_RBE"); ok { + if v, ok := c.Environment().Get("USE_RBE"); ok { v = strings.TrimSpace(v) if v != "" && v != "false" { return true |