Merge "Streamline NewBazeContext code"

This commit is contained in:
Treehugger Robot 2022-12-07 19:11:49 +00:00 committed by Gerrit Code Review
commit 41ec4e6548

View file

@ -16,7 +16,6 @@ package android
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@ -376,107 +375,74 @@ func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset {
} }
func NewBazelContext(c *config) (BazelContext, error) { func NewBazelContext(c *config) (BazelContext, error) {
var modulesDefaultToBazel bool
disabledModules := map[string]bool{} disabledModules := map[string]bool{}
enabledModules := map[string]bool{} enabledModules := map[string]bool{}
addToStringSet := func(set map[string]bool, items []string) {
for _, item := range items {
set[item] = true
}
}
switch c.BuildMode { switch c.BuildMode {
case BazelProdMode: case BazelProdMode:
modulesDefaultToBazel = false addToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
for _, enabledProdModule := range allowlists.ProdMixedBuildsEnabledList {
enabledModules[enabledProdModule] = true
}
for enabledAdHocModule := range c.BazelModulesForceEnabledByFlag() { for enabledAdHocModule := range c.BazelModulesForceEnabledByFlag() {
enabledModules[enabledAdHocModule] = true enabledModules[enabledAdHocModule] = true
} }
case BazelStagingMode: case BazelStagingMode:
modulesDefaultToBazel = false
// Staging mode includes all prod modules plus all staging modules. // Staging mode includes all prod modules plus all staging modules.
for _, enabledProdModule := range allowlists.ProdMixedBuildsEnabledList { addToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
enabledModules[enabledProdModule] = true addToStringSet(enabledModules, allowlists.StagingMixedBuildsEnabledList)
}
for _, enabledStagingMode := range allowlists.StagingMixedBuildsEnabledList {
enabledModules[enabledStagingMode] = true
}
for enabledAdHocModule := range c.BazelModulesForceEnabledByFlag() { for enabledAdHocModule := range c.BazelModulesForceEnabledByFlag() {
enabledModules[enabledAdHocModule] = true enabledModules[enabledAdHocModule] = true
} }
case BazelDevMode: case BazelDevMode:
modulesDefaultToBazel = true
// Don't use partially-converted cc_library targets in mixed builds, // Don't use partially-converted cc_library targets in mixed builds,
// since mixed builds would generally rely on both static and shared // since mixed builds would generally rely on both static and shared
// variants of a cc_library. // variants of a cc_library.
for staticOnlyModule := range GetBp2BuildAllowList().ccLibraryStaticOnly { for staticOnlyModule := range GetBp2BuildAllowList().ccLibraryStaticOnly {
disabledModules[staticOnlyModule] = true disabledModules[staticOnlyModule] = true
} }
for _, disabledDevModule := range allowlists.MixedBuildsDisabledList { addToStringSet(disabledModules, allowlists.MixedBuildsDisabledList)
disabledModules[disabledDevModule] = true
}
default: default:
return noopBazelContext{}, nil return noopBazelContext{}, nil
} }
p, err := bazelPathsFromConfig(c) paths := bazelPaths{
if err != nil { soongOutDir: c.soongOutDir,
return nil, err }
var missing []string
vars := []struct {
name string
ptr *string
}{
{"BAZEL_HOME", &paths.homeDir},
{"BAZEL_PATH", &paths.bazelPath},
{"BAZEL_OUTPUT_BASE", &paths.outputBase},
{"BAZEL_WORKSPACE", &paths.workspaceDir},
{"BAZEL_METRICS_DIR", &paths.metricsDir},
{"BAZEL_DEPS_FILE", &paths.bazelDepsFile},
}
for _, v := range vars {
if s := c.Getenv(v.name); len(s) > 1 {
*v.ptr = s
} else {
missing = append(missing, v.name)
}
}
if len(missing) > 0 {
return nil, fmt.Errorf("missing required env vars to use bazel: %s", missing)
} }
return &bazelContext{ return &bazelContext{
bazelRunner: &builtinBazelRunner{}, bazelRunner: &builtinBazelRunner{},
paths: p, paths: &paths,
requests: make(map[cqueryKey]bool), requests: make(map[cqueryKey]bool),
modulesDefaultToBazel: modulesDefaultToBazel, modulesDefaultToBazel: c.BuildMode == BazelDevMode,
bazelEnabledModules: enabledModules, bazelEnabledModules: enabledModules,
bazelDisabledModules: disabledModules, bazelDisabledModules: disabledModules,
}, nil }, nil
} }
func bazelPathsFromConfig(c *config) (*bazelPaths, error) {
p := bazelPaths{
soongOutDir: c.soongOutDir,
}
var missingEnvVars []string
if len(c.Getenv("BAZEL_HOME")) > 1 {
p.homeDir = c.Getenv("BAZEL_HOME")
} else {
missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
}
if len(c.Getenv("BAZEL_PATH")) > 1 {
p.bazelPath = c.Getenv("BAZEL_PATH")
} else {
missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
}
if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
} else {
missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
}
if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
p.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
} else {
missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
}
if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
p.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
} else {
missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
}
if len(c.Getenv("BAZEL_DEPS_FILE")) > 1 {
p.bazelDepsFile = c.Getenv("BAZEL_DEPS_FILE")
} else {
missingEnvVars = append(missingEnvVars, "BAZEL_DEPS_FILE")
}
if len(missingEnvVars) > 0 {
return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
} else {
return &p, nil
}
}
func (p *bazelPaths) BazelMetricsDir() string { func (p *bazelPaths) BazelMetricsDir() string {
return p.metricsDir return p.metricsDir
} }