From 7b7dca431615cb76a105dbaa72b549bcab80a2c5 Mon Sep 17 00:00:00 2001 From: Kousik Kumar Date: Fri, 14 Jan 2022 00:22:32 -0500 Subject: [PATCH] Add additional directories from which env config can be loaded This is useful for external users to be able to specify their own config files that can be loaded by soong during startup. In addition, we need this for upcoming changes to incorporate an experiments framework in Soong since the config file will be fetched from CDPush and put into the OUT_DIR folder by the config file fetcher binary. Note: Once this is merged into internal branch, I'll fully get rid of the vendor/google/ path from Soong in aosp. Test: 1. Ran a build in aosp with these changes and no config file was loaded. 2. Ran a build in internal master with these changes and the current config file inside vendor/google was loaded as expected. Bug: b/214035335 Change-Id: I9af83687d4eaeee1ffb0f88a750cfeb7c6d2bafb --- cmd/soong_ui/main.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/cmd/soong_ui/main.go b/cmd/soong_ui/main.go index 9ee373e79..d8cb47a44 100644 --- a/cmd/soong_ui/main.go +++ b/cmd/soong_ui/main.go @@ -117,12 +117,23 @@ func inList(s string, list []string) bool { return indexList(s, list) != -1 } -func loadEnvConfig() error { +func loadEnvConfig(config build.Config) error { bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG") if bc == "" { return nil } - cfgFile := filepath.Join(os.Getenv("TOP"), configDir, fmt.Sprintf("%s.%s", bc, jsonSuffix)) + 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 { @@ -138,9 +149,7 @@ func loadEnvConfig() error { if os.Getenv(k) != "" { continue } - if err := os.Setenv(k, v); err != nil { - return err - } + config.Environment().Set(k, v) } return nil } @@ -207,13 +216,13 @@ func main() { Status: stat, }} - if err := loadEnvConfig(); err != nil { + 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) } - config := c.config(buildCtx, args...) - build.SetupOutDir(buildCtx, config) if config.UseBazel() && config.Dist() {