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

Commit 39a301c9 authored by Sasha Smundak's avatar Sasha Smundak Committed by Alexander Smundak
Browse files

Clean up mixed builds module names filtering

* Remove `Bp2buildCcLibraryStaticOnlyList` it is never set
* Remove the rest of the sutff related to it
* Streamline MixedBuildsEnabled
* Rename BazelContext.BazelAllowlisted to BazelContext.NameFilter
  to reflect its action
* Rename bazelContext to mixedBuildBazelContext
* Fix minor warnings from Go static analyzer

Test: treehugger
Change-Id: Ie245c992f94bbfd5c7c23dd06917026200a28263
parent bf51151b
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -1318,8 +1318,6 @@ var (
		"libc_musl_sysroot",
	}

	Bp2buildCcLibraryStaticOnlyList = []string{}

	MixedBuildsDisabledList = []string{
		"libruy_static", "libtflite_kernel_utils", // TODO(b/237315968); Depend on prebuilt stl, not from source

+10 −48
Original line number Diff line number Diff line
@@ -66,8 +66,8 @@ type bazelModuleProperties struct {
	//
	// This is a bool pointer to support tristates: true, false, not set.
	//
	// To opt-in a module, set bazel_module: { bp2build_available: true }
	// To opt-out a module, set bazel_module: { bp2build_available: false }
	// To opt in a module, set bazel_module: { bp2build_available: true }
	// To opt out a module, set bazel_module: { bp2build_available: false }
	// To defer the default setting for the directory, do not set the value.
	Bp2build_available *bool

@@ -126,7 +126,7 @@ type Bazelable interface {
	// one with the single member called Soong_config_variables, which itself is
	// a struct containing fields for each supported feature in that namespace.
	//
	// The reason for using an slice of interface{} is to support defaults
	// The reason for using a slice of interface{} is to support defaults
	// propagation of the struct pointers.
	namespacedVariableProps() namespacedVariableProperties
	setNamespacedVariableProps(props namespacedVariableProperties)
@@ -237,16 +237,6 @@ type Bp2BuildConversionAllowlist struct {

	// Per-module denylist to always opt modules out of bp2build conversion.
	moduleDoNotConvert map[string]bool

	// Per-module denylist of cc_library modules to only generate the static
	// variant if their shared variant isn't ready or buildable by Bazel.
	ccLibraryStaticOnly map[string]bool
}

// GenerateCcLibraryStaticOnly returns whether a cc_library module should only
// generate a static version of itself based on the current global configuration.
func (a Bp2BuildConversionAllowlist) GenerateCcLibraryStaticOnly(moduleName string) bool {
	return a.ccLibraryStaticOnly[moduleName]
}

// NewBp2BuildAllowlist creates a new, empty Bp2BuildConversionAllowlist
@@ -258,7 +248,6 @@ func NewBp2BuildAllowlist() Bp2BuildConversionAllowlist {
		map[string]bool{},
		map[string]bool{},
		map[string]bool{},
		map[string]bool{},
	}
}

@@ -322,18 +311,6 @@ func (a Bp2BuildConversionAllowlist) SetModuleDoNotConvertList(moduleDoNotConver
	return a
}

// SetCcLibraryStaticOnlyList copies the entries from ccLibraryStaticOnly into the allowlist
func (a Bp2BuildConversionAllowlist) SetCcLibraryStaticOnlyList(ccLibraryStaticOnly []string) Bp2BuildConversionAllowlist {
	if a.ccLibraryStaticOnly == nil {
		a.ccLibraryStaticOnly = map[string]bool{}
	}
	for _, m := range ccLibraryStaticOnly {
		a.ccLibraryStaticOnly[m] = true
	}

	return a
}

// ShouldKeepExistingBuildFileForDir returns whether an existing BUILD file should be
// added to the build symlink forest based on the current global configuration.
func (a Bp2BuildConversionAllowlist) ShouldKeepExistingBuildFileForDir(dir string) bool {
@@ -365,8 +342,7 @@ func GetBp2BuildAllowList() Bp2BuildConversionAllowlist {
			SetKeepExistingBuildFile(allowlists.Bp2buildKeepExistingBuildFile).
			SetModuleAlwaysConvertList(allowlists.Bp2buildModuleAlwaysConvertList).
			SetModuleTypeAlwaysConvertList(allowlists.Bp2buildModuleTypeAlwaysConvertList).
			SetModuleDoNotConvertList(allowlists.Bp2buildModuleDoNotConvertList).
			SetCcLibraryStaticOnlyList(allowlists.Bp2buildCcLibraryStaticOnlyList)
			SetModuleDoNotConvertList(allowlists.Bp2buildModuleDoNotConvertList)
	}).(Bp2BuildConversionAllowlist)
}

@@ -375,30 +351,16 @@ func GetBp2BuildAllowList() Bp2BuildConversionAllowlist {
// method will also log whether this module is mixed build enabled for
// metrics reporting.
func MixedBuildsEnabled(ctx BaseModuleContext) bool {
	mixedBuildEnabled := mixedBuildPossible(ctx)
	module := ctx.Module()
	mixedBuildEnabled := ctx.Config().IsMixedBuildsEnabled() &&
		ctx.Os() != Windows && // Windows toolchains are not currently supported.
		module.Enabled() &&
		convertedToBazel(ctx, module) &&
		ctx.Config().BazelContext.IsModuleNameAllowed(module.Name())
	ctx.Config().LogMixedBuild(ctx, mixedBuildEnabled)
	return mixedBuildEnabled
}

// mixedBuildPossible returns true if a module is ready to be replaced by a
// converted or handcrafted Bazel target.
func mixedBuildPossible(ctx BaseModuleContext) bool {
	if !ctx.Config().IsMixedBuildsEnabled() {
		return false
	}
	if ctx.Os() == Windows {
		// Windows toolchains are not currently supported.
		return false
	}
	if !ctx.Module().Enabled() {
		return false
	}
	if !convertedToBazel(ctx, ctx.Module()) {
		return false
	}
	return ctx.Config().BazelContext.BazelAllowlisted(ctx.Module().Name())
}

// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
	b, ok := module.(Bazelable)
+24 −30
Original line number Diff line number Diff line
@@ -152,7 +152,7 @@ type BazelContext interface {
	// Note that this only implies "bazel mixed build" allowlisting. The caller
	// should independently verify the module is eligible for Bazel handling
	// (for example, that it is MixedBuildBuildable).
	BazelAllowlisted(moduleName string) bool
	IsModuleNameAllowed(moduleName string) bool

	// Returns the bazel output base (the root directory for all bazel intermediate outputs).
	OutputBase() string
@@ -181,7 +181,7 @@ type bazelPaths struct {

// A context object which tracks queued requests that need to be made to Bazel,
// and their results after the requests have been made.
type bazelContext struct {
type mixedBuildBazelContext struct {
	bazelRunner
	paths        *bazelPaths
	requests     map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
@@ -210,7 +210,7 @@ type bazelContext struct {
	targetBuildVariant string
}

var _ BazelContext = &bazelContext{}
var _ BazelContext = &mixedBuildBazelContext{}

// A bazel context to use when Bazel is disabled.
type noopBazelContext struct{}
@@ -261,7 +261,7 @@ func (m MockBazelContext) InvokeBazel(_ Config, _ *Context) error {
	panic("unimplemented")
}

func (m MockBazelContext) BazelAllowlisted(_ string) bool {
func (m MockBazelContext) IsModuleNameAllowed(_ string) bool {
	return true
}

@@ -277,14 +277,14 @@ func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset {

var _ BazelContext = MockBazelContext{}

func (bazelCtx *bazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
func (bazelCtx *mixedBuildBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
	key := makeCqueryKey(label, requestType, cfgKey)
	bazelCtx.requestMutex.Lock()
	defer bazelCtx.requestMutex.Unlock()
	bazelCtx.requests[key] = true
}

func (bazelCtx *bazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) {
func (bazelCtx *mixedBuildBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) {
	key := makeCqueryKey(label, cquery.GetOutputFiles, cfgKey)
	if rawString, ok := bazelCtx.results[key]; ok {
		bazelOutput := strings.TrimSpace(rawString)
@@ -294,7 +294,7 @@ func (bazelCtx *bazelContext) GetOutputFiles(label string, cfgKey configKey) ([]
	return nil, fmt.Errorf("no bazel response found for %v", key)
}

func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
func (bazelCtx *mixedBuildBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
	key := makeCqueryKey(label, cquery.GetCcInfo, cfgKey)
	if rawString, ok := bazelCtx.results[key]; ok {
		bazelOutput := strings.TrimSpace(rawString)
@@ -303,7 +303,7 @@ func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.
	return cquery.CcInfo{}, fmt.Errorf("no bazel response found for %v", key)
}

func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) {
func (bazelCtx *mixedBuildBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) {
	key := makeCqueryKey(label, cquery.GetPythonBinary, cfgKey)
	if rawString, ok := bazelCtx.results[key]; ok {
		bazelOutput := strings.TrimSpace(rawString)
@@ -312,7 +312,7 @@ func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (s
	return "", fmt.Errorf("no bazel response found for %v", key)
}

func (bazelCtx *bazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexInfo, error) {
func (bazelCtx *mixedBuildBazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexInfo, error) {
	key := makeCqueryKey(label, cquery.GetApexInfo, cfgKey)
	if rawString, ok := bazelCtx.results[key]; ok {
		return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString))
@@ -320,7 +320,7 @@ func (bazelCtx *bazelContext) GetApexInfo(label string, cfgKey configKey) (cquer
	return cquery.ApexInfo{}, fmt.Errorf("no bazel response found for %v", key)
}

func (bazelCtx *bazelContext) GetCcUnstrippedInfo(label string, cfgKey configKey) (cquery.CcUnstrippedInfo, error) {
func (bazelCtx *mixedBuildBazelContext) GetCcUnstrippedInfo(label string, cfgKey configKey) (cquery.CcUnstrippedInfo, error) {
	key := makeCqueryKey(label, cquery.GetCcUnstrippedInfo, cfgKey)
	if rawString, ok := bazelCtx.results[key]; ok {
		return cquery.GetCcUnstrippedInfo.ParseResult(strings.TrimSpace(rawString))
@@ -361,7 +361,7 @@ func (m noopBazelContext) OutputBase() string {
	return ""
}

func (n noopBazelContext) BazelAllowlisted(_ string) bool {
func (n noopBazelContext) IsModuleNameAllowed(_ string) bool {
	return false
}

@@ -396,12 +396,6 @@ func GetBazelEnabledAndDisabledModules(buildMode SoongBuildMode, forceEnabled ma
			enabledModules[enabledAdHocModule] = true
		}
	case BazelDevMode:
		// Don't use partially-converted cc_library targets in mixed builds,
		// since mixed builds would generally rely on both static and shared
		// variants of a cc_library.
		for staticOnlyModule := range GetBp2BuildAllowList().ccLibraryStaticOnly {
			disabledModules[staticOnlyModule] = true
		}
		addToStringSet(disabledModules, allowlists.MixedBuildsDisabledList)
	default:
		panic("Expected BazelProdMode, BazelStagingMode, or BazelDevMode")
@@ -475,7 +469,7 @@ func NewBazelContext(c *config) (BazelContext, error) {
		targetProduct = c.DeviceProduct()
	}

	return &bazelContext{
	return &mixedBuildBazelContext{
		bazelRunner:           &builtinBazelRunner{},
		paths:                 &paths,
		requests:              make(map[cqueryKey]bool),
@@ -491,7 +485,7 @@ func (p *bazelPaths) BazelMetricsDir() string {
	return p.metricsDir
}

func (context *bazelContext) BazelAllowlisted(moduleName string) bool {
func (context *mixedBuildBazelContext) IsModuleNameAllowed(moduleName string) bool {
	if context.bazelDisabledModules[moduleName] {
		return false
	}
@@ -618,7 +612,7 @@ func printableCqueryCommand(bazelCmd *exec.Cmd) string {

}

func (context *bazelContext) mainBzlFileContents() []byte {
func (context *mixedBuildBazelContext) mainBzlFileContents() []byte {
	// TODO(cparsons): Define configuration transitions programmatically based
	// on available archs.
	contents := `
@@ -687,7 +681,7 @@ phony_root = rule(
	return []byte(productReplacer.Replace(contents))
}

func (context *bazelContext) mainBuildFileContents() []byte {
func (context *mixedBuildBazelContext) mainBuildFileContents() []byte {
	// TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
	// architecture mapping.
	formatString := `
@@ -751,10 +745,10 @@ func indent(original string) string {

// Returns the file contents of the buildroot.cquery file that should be used for the cquery
// expression in order to obtain information about buildroot and its dependencies.
// The contents of this file depend on the bazelContext's requests; requests are enumerated
// The contents of this file depend on the mixedBuildBazelContext's requests; requests are enumerated
// and grouped by their request type. The data retrieved for each label depends on its
// request type.
func (context *bazelContext) cqueryStarlarkFileContents() []byte {
func (context *mixedBuildBazelContext) cqueryStarlarkFileContents() []byte {
	requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
	for val := range context.requests {
		cqueryId := getCqueryId(val)
@@ -912,7 +906,7 @@ var (

// Issues commands to Bazel to receive results for all cquery requests
// queued in the BazelContext.
func (context *bazelContext) InvokeBazel(config Config, ctx *Context) error {
func (context *mixedBuildBazelContext) InvokeBazel(config Config, ctx *Context) error {
	if ctx != nil {
		ctx.EventHandler.Begin("bazel")
		defer ctx.EventHandler.End("bazel")
@@ -939,7 +933,7 @@ func (context *bazelContext) InvokeBazel(config Config, ctx *Context) error {
	return nil
}

func (context *bazelContext) runCquery(ctx *Context) error {
func (context *mixedBuildBazelContext) runCquery(ctx *Context) error {
	if ctx != nil {
		ctx.EventHandler.Begin("cquery")
		defer ctx.EventHandler.End("cquery")
@@ -994,7 +988,7 @@ func (context *bazelContext) runCquery(ctx *Context) error {
	return nil
}

func (context *bazelContext) runAquery(config Config, ctx *Context) error {
func (context *mixedBuildBazelContext) runAquery(config Config, ctx *Context) error {
	if ctx != nil {
		ctx.EventHandler.Begin("aquery")
		defer ctx.EventHandler.End("aquery")
@@ -1032,7 +1026,7 @@ func (context *bazelContext) runAquery(config Config, ctx *Context) error {
	return err
}

func (context *bazelContext) generateBazelSymlinks(ctx *Context) error {
func (context *mixedBuildBazelContext) generateBazelSymlinks(ctx *Context) error {
	if ctx != nil {
		ctx.EventHandler.Begin("symlinks")
		defer ctx.EventHandler.End("symlinks")
@@ -1044,15 +1038,15 @@ func (context *bazelContext) generateBazelSymlinks(ctx *Context) error {
	return err
}

func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
func (context *mixedBuildBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
	return context.buildStatements
}

func (context *bazelContext) AqueryDepsets() []bazel.AqueryDepset {
func (context *mixedBuildBazelContext) AqueryDepsets() []bazel.AqueryDepset {
	return context.depsets
}

func (context *bazelContext) OutputBase() string {
func (context *mixedBuildBazelContext) OutputBase() string {
	return context.paths.outputBase
}

+2 −2
Original line number Diff line number Diff line
@@ -189,7 +189,7 @@ func verifyExtraFlags(t *testing.T, config Config, expected string) string {
	return actual
}

func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string) (*bazelContext, string) {
func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string) (*mixedBuildBazelContext, string) {
	t.Helper()
	p := bazelPaths{
		soongOutDir:  t.TempDir(),
@@ -201,7 +201,7 @@ func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string)
		bazelCommandResults[aqueryCommand] = ""
	}
	runner := &mockBazelRunner{bazelCommandResults: bazelCommandResults}
	return &bazelContext{
	return &mixedBuildBazelContext{
		bazelRunner: runner,
		paths:       &p,
		requests:    map[cqueryKey]bool{},
+0 −5
Original line number Diff line number Diff line
@@ -417,11 +417,6 @@ func TestBp2buildAllowList(t *testing.T) {
			t.Errorf("bp2build module do not convert of %s: expected: true, got: %v", k, allowlist.moduleDoNotConvert[k])
		}
	}
	for _, k := range allowlists.Bp2buildCcLibraryStaticOnlyList {
		if !allowlist.ccLibraryStaticOnly[k] {
			t.Errorf("bp2build cc library static only of %s: expected: true, got: %v", k, allowlist.ccLibraryStaticOnly[k])
		}
	}
}

func TestShouldKeepExistingBuildFileForDir(t *testing.T) {
Loading