Merge "Allow exporting bazel mixed build allowlists to simple text files"

This commit is contained in:
Cole Faust 2022-12-19 03:09:11 +00:00 committed by Gerrit Code Review
commit abc182cc94
3 changed files with 38 additions and 4 deletions

View file

@ -22,6 +22,7 @@ import (
"path"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
@ -374,7 +375,7 @@ func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset {
return []bazel.AqueryDepset{}
}
func NewBazelContext(c *config) (BazelContext, error) {
func GetBazelEnabledAndDisabledModules(buildMode SoongBuildMode, forceEnabled map[string]struct{}) (map[string]bool, map[string]bool) {
disabledModules := map[string]bool{}
enabledModules := map[string]bool{}
addToStringSet := func(set map[string]bool, items []string) {
@ -383,17 +384,17 @@ func NewBazelContext(c *config) (BazelContext, error) {
}
}
switch c.BuildMode {
switch buildMode {
case BazelProdMode:
addToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
for enabledAdHocModule := range c.BazelModulesForceEnabledByFlag() {
for enabledAdHocModule := range forceEnabled {
enabledModules[enabledAdHocModule] = true
}
case BazelStagingMode:
// Staging mode includes all prod modules plus all staging modules.
addToStringSet(enabledModules, allowlists.ProdMixedBuildsEnabledList)
addToStringSet(enabledModules, allowlists.StagingMixedBuildsEnabledList)
for enabledAdHocModule := range c.BazelModulesForceEnabledByFlag() {
for enabledAdHocModule := range forceEnabled {
enabledModules[enabledAdHocModule] = true
}
case BazelDevMode:
@ -405,9 +406,30 @@ func NewBazelContext(c *config) (BazelContext, error) {
}
addToStringSet(disabledModules, allowlists.MixedBuildsDisabledList)
default:
panic("Expected BazelProdMode, BazelStagingMode, or BazelDevMode")
}
return enabledModules, disabledModules
}
func GetBazelEnabledModules(buildMode SoongBuildMode) []string {
enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(buildMode, nil)
enabledList := make([]string, 0, len(enabledModules))
for module := range enabledModules {
if !disabledModules[module] {
enabledList = append(enabledList, module)
}
}
sort.Strings(enabledList)
return enabledList
}
func NewBazelContext(c *config) (BazelContext, error) {
if c.BuildMode != BazelProdMode && c.BuildMode != BazelStagingMode && c.BuildMode != BazelDevMode {
return noopBazelContext{}, nil
}
enabledModules, disabledModules := GetBazelEnabledAndDisabledModules(c.BuildMode, c.BazelModulesForceEnabledByFlag())
paths := bazelPaths{
soongOutDir: c.soongOutDir,
}

View file

@ -55,6 +55,10 @@ func CreateSoongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []Baz
files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(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"))
return files
}

View file

@ -147,6 +147,14 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
dir: "api_levels",
basename: "api_levels.bzl",
},
{
dir: "allowlists",
basename: "mixed_build_prod_allowlist.txt",
},
{
dir: "allowlists",
basename: "mixed_build_staging_allowlist.txt",
},
}
if len(files) != len(expectedFilePaths) {