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
diff --git a/cmd/soong_ui/main.go b/cmd/soong_ui/main.go
index 9ee373e..d8cb47a 100644
--- a/cmd/soong_ui/main.go
+++ b/cmd/soong_ui/main.go
@@ -117,12 +117,23 @@
 	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 @@
 		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 @@
 		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() {