From cb3c52c7665b2bded1a800f33718d57a5db5c6c5 Mon Sep 17 00:00:00 2001 From: Sam Delmerico Date: Fri, 3 Feb 2023 17:40:08 -0500 Subject: [PATCH] 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 --- android/bazel_handler.go | 59 ++++++++++++++++++++++++++++++------- bp2build/conversion.go | 2 ++ bp2build/conversion_test.go | 8 +++++ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/android/bazel_handler.go b/android/bazel_handler.go index ac4ced85d..17d012447 100644 --- a/android/bazel_handler.go +++ b/android/bazel_handler.go @@ -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), + ) +} diff --git a/bp2build/conversion.go b/bp2build/conversion.go index c43fbd86e..5b3e19fa2 100644 --- a/bp2build/conversion.go +++ b/bp2build/conversion.go @@ -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")) diff --git a/bp2build/conversion_test.go b/bp2build/conversion_test.go index b9c06bc04..8c240934c 100644 --- a/bp2build/conversion_test.go +++ b/bp2build/conversion_test.go @@ -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",