Load RBE related env vars from config files

This is as part of an effort to move environment variables into config
files so that configs can be tied down to source.

Test:
Ran a build with "ANDROID_BUILD_ENVIRONMENT_CONFIG=googler m"  which ran
a non-RBE build (since this is vanilla aosp).
Also ran a build with "ANDROID_BUILD_ENVIRONMENT_CONFIG= m" which ran a
non-RBE build.

https://paste.googleplex.com/5637282880028672

Bug: b/194679562
Change-Id: I416e8da75f84aa2b53995f525cf50501488dc972
This commit is contained in:
Kousik Kumar
2021-07-21 03:24:32 -04:00
parent 561923e10d
commit 51c4091309

View File

@@ -16,6 +16,7 @@ package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
@@ -34,6 +35,11 @@ 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 {
@@ -110,6 +116,34 @@ func inList(s string, list []string) bool {
return indexList(s, list) != -1
}
func loadEnvConfig() 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))
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
}
if err := os.Setenv(k, v); err != nil {
return err
}
}
return nil
}
// Main execution of soong_ui. The command format is as follows:
//
// soong_ui <command> [<arg 1> <arg 2> ... <arg n>]
@@ -171,6 +205,11 @@ func main() {
Status: stat,
}}
if err := loadEnvConfig(); 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)