export allowlist of environment variables to Bazel

Previously, Bazel was invoked during mixed builds with a stripped down
environment. This CL adds an allowlist of environment variables that are
passed to Bazel.

Test: WITH_TIDY=1 DISABLE_ARTIFACT_PATH_REQUIREMENTS=true mss tidy-packages-modules-NeuralNetworks --bazel-mode-dev
Change-Id: I23147bec59f6522953cf623e7bcaa0f1f99a75a3
This commit is contained in:
Sam Delmerico 2023-02-03 17:40:08 -05:00
parent 28f4af7955
commit cb3c52c766
3 changed files with 59 additions and 10 deletions

View file

@ -29,6 +29,7 @@ import (
"android/soong/android/allowlists"
"android/soong/bazel/cquery"
"android/soong/shared"
"android/soong/starlark_fmt"
"github.com/google/blueprint"
@ -43,6 +44,27 @@ var (
Description: "",
CommandDeps: []string{"${bazelBuildRunfilesTool}"},
}, "outDir")
allowedBazelEnvironmentVars = []string{
"ALLOW_LOCAL_TIDY_TRUE",
"DEFAULT_TIDY_HEADER_DIRS",
"TIDY_TIMEOUT",
"WITH_TIDY",
"WITH_TIDY_FLAGS",
"SKIP_ABI_CHECKS",
"UNSAFE_DISABLE_APEX_ALLOWED_DEPS_CHECK",
"AUTO_ZERO_INITIALIZE",
"AUTO_PATTERN_INITIALIZE",
"AUTO_UNINITIALIZE",
"USE_CCACHE",
"LLVM_NEXT",
"ALLOW_UNKNOWN_WARNING_OPTION",
// Overrides the version in the apex_manifest.json. The version is unique for
// each branch (internal, aosp, mainline releases, dessert releases). This
// enables modules built on an older branch to be installed against a newer
// device for development purposes.
"OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION",
}
)
func init() {
@ -165,7 +187,7 @@ type BazelContext interface {
}
type bazelRunner interface {
createBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) *exec.Cmd
createBazelCommand(config Config, paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) *exec.Cmd
issueBazelCommand(bazelCmd *exec.Cmd) (output string, errorMessage string, error error)
}
@ -535,7 +557,7 @@ type mockBazelRunner struct {
extraFlags []string
}
func (r *mockBazelRunner) createBazelCommand(_ *bazelPaths, _ bazel.RunName,
func (r *mockBazelRunner) createBazelCommand(_ Config, _ *bazelPaths, _ bazel.RunName,
command bazelCommand, extraFlags ...string) *exec.Cmd {
r.commands = append(r.commands, command)
r.extraFlags = append(r.extraFlags, strings.Join(extraFlags, " "))
@ -572,7 +594,7 @@ func (r *builtinBazelRunner) issueBazelCommand(bazelCmd *exec.Cmd) (string, stri
}
}
func (r *builtinBazelRunner) createBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand,
func (r *builtinBazelRunner) createBazelCommand(config Config, paths *bazelPaths, runName bazel.RunName, command bazelCommand,
extraFlags ...string) *exec.Cmd {
cmdFlags := []string{
"--output_base=" + absolutePath(paths.outputBase),
@ -616,6 +638,13 @@ func (r *builtinBazelRunner) createBazelCommand(paths *bazelPaths, runName bazel
// explicitly in BUILD files.
"BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1",
}
for _, envvar := range allowedBazelEnvironmentVars {
val := config.Getenv(envvar)
if val == "" {
continue
}
extraEnv = append(extraEnv, fmt.Sprintf("%s=%s", envvar, val))
}
bazelCmd.Env = append(os.Environ(), extraEnv...)
return bazelCmd
@ -933,13 +962,13 @@ func (context *mixedBuildBazelContext) InvokeBazel(config Config, ctx *Context)
}
}
context.results = make(map[cqueryKey]string)
if err := context.runCquery(ctx); err != nil {
if err := context.runCquery(config, ctx); err != nil {
return err
}
if err := context.runAquery(config, ctx); err != nil {
return err
}
if err := context.generateBazelSymlinks(ctx); err != nil {
if err := context.generateBazelSymlinks(config, ctx); err != nil {
return err
}
@ -948,7 +977,7 @@ func (context *mixedBuildBazelContext) InvokeBazel(config Config, ctx *Context)
return nil
}
func (context *mixedBuildBazelContext) runCquery(ctx *Context) error {
func (context *mixedBuildBazelContext) runCquery(config Config, ctx *Context) error {
if ctx != nil {
ctx.EventHandler.Begin("cquery")
defer ctx.EventHandler.End("cquery")
@ -975,7 +1004,7 @@ func (context *mixedBuildBazelContext) runCquery(ctx *Context) error {
return err
}
cqueryCommandWithFlag := context.createBazelCommand(context.paths, bazel.CqueryBuildRootRunName, cqueryCmd,
cqueryCommandWithFlag := context.createBazelCommand(config, context.paths, bazel.CqueryBuildRootRunName, cqueryCmd,
"--output=starlark", "--starlark:file="+absolutePath(cqueryFileRelpath))
cqueryOutput, cqueryErrorMessage, cqueryErr := context.issueBazelCommand(cqueryCommandWithFlag)
if cqueryErr != nil {
@ -1032,7 +1061,7 @@ func (context *mixedBuildBazelContext) runAquery(config Config, ctx *Context) er
extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ","))
}
}
aqueryOutput, _, err := context.issueBazelCommand(context.createBazelCommand(context.paths, bazel.AqueryBuildRootRunName, aqueryCmd,
aqueryOutput, _, err := context.issueBazelCommand(context.createBazelCommand(config, context.paths, bazel.AqueryBuildRootRunName, aqueryCmd,
extraFlags...))
if err != nil {
return err
@ -1041,7 +1070,7 @@ func (context *mixedBuildBazelContext) runAquery(config Config, ctx *Context) er
return err
}
func (context *mixedBuildBazelContext) generateBazelSymlinks(ctx *Context) error {
func (context *mixedBuildBazelContext) generateBazelSymlinks(config Config, ctx *Context) error {
if ctx != nil {
ctx.EventHandler.Begin("symlinks")
defer ctx.EventHandler.End("symlinks")
@ -1049,7 +1078,7 @@ func (context *mixedBuildBazelContext) generateBazelSymlinks(ctx *Context) error
// Issue a build command of the phony root to generate symlink forests for dependencies of the
// Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
// but some of symlinks may be required to resolve source dependencies of the build.
_, _, err := context.issueBazelCommand(context.createBazelCommand(context.paths, bazel.BazelBuildPhonyRootRunName, buildCmd))
_, _, err := context.issueBazelCommand(context.createBazelCommand(config, context.paths, bazel.BazelBuildPhonyRootRunName, buildCmd))
return err
}
@ -1259,3 +1288,13 @@ func GetConfigKey(ctx BaseModuleContext) configKey {
func bazelDepsetName(contentHash string) string {
return fmt.Sprintf("bazel_depset_%s", contentHash)
}
func EnvironmentVarsFile(config Config) string {
return fmt.Sprintf(bazel.GeneratedBazelFileWarning+`
_env = %s
env = _env
`,
starlark_fmt.PrintStringList(allowedBazelEnvironmentVars, 0),
)
}

View file

@ -58,6 +58,8 @@ func soongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []BazelFile
files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
files = append(files, newFile("allowlists", GeneratedBuildFileName, ""))
files = append(files, newFile("allowlists", "env.bzl", android.EnvironmentVarsFile(cfg)))
// TODO(b/262781701): Create an alternate soong_build entrypoint for writing out these files only when requested
files = append(files, newFile("allowlists", "mixed_build_prod_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelProdMode), "\n")+"\n"))
files = append(files, newFile("allowlists", "mixed_build_staging_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelStagingMode), "\n")+"\n"))

View file

@ -151,6 +151,14 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
dir: "api_levels",
basename: "api_levels.bzl",
},
{
dir: "allowlists",
basename: GeneratedBuildFileName,
},
{
dir: "allowlists",
basename: "env.bzl",
},
{
dir: "allowlists",
basename: "mixed_build_prod_allowlist.txt",