Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c81a6e75 authored by Kousik Kumar's avatar Kousik Kumar Committed by Automerger Merge Worker
Browse files

Move env var config loading to be within the config package. am: 2f504db7...

Move env var config loading to be within the config package. am: 2f504db7 am: 4d5f00fe am: 14325e74 am: 5da91781

Original change: https://googleplex-android-review.googlesource.com/c/platform/build/soong/+/18971604



Change-Id: I2697d8db90aed98fd3b69062de8509072f9ea70c
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents cef318d1 5da91781
Loading
Loading
Loading
Loading
+0 −49
Original line number Diff line number Diff line
@@ -16,10 +16,8 @@ package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
@@ -35,11 +33,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 {
@@ -116,43 +109,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>]
@@ -216,11 +172,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() {
+51 −1
Original line number Diff line number Diff line
@@ -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 {
@@ -122,6 +129,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(),
@@ -150,6 +194,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 {
@@ -911,7 +961,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