From 55bc820d66a1ce1d56d68da4efcbcef1fa178878 Mon Sep 17 00:00:00 2001 From: Jingwen Chen Date: Tue, 2 Nov 2021 06:40:51 +0000 Subject: [PATCH] bp2build: split Bazel conversion context into smaller ones, and change TopDownMutatorContext signatures to use Bazel conversion context. This minimizes the context interfaces/functions actually needed to convert a module, and makes such interfaces easier to mock/test. Test: CI Change-Id: Id573d97023d59e06ef70e1f54437024d3f7aadbd --- android/bazel.go | 10 +++--- android/bazel_paths.go | 54 ++++++++++++++++++------------ android/variable.go | 2 +- cc/bp2build.go | 74 +++++++++++++++++++++--------------------- 4 files changed, 76 insertions(+), 64 deletions(-) diff --git a/android/bazel.go b/android/bazel.go index 5dda65537..b1493e880 100644 --- a/android/bazel.go +++ b/android/bazel.go @@ -60,8 +60,8 @@ type Bazelable interface { HasHandcraftedLabel() bool HandcraftedLabel() string GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string - ConvertWithBp2build(ctx BazelConversionPathContext) bool - convertWithBp2build(ctx BazelConversionPathContext, module blueprint.Module) bool + ConvertWithBp2build(ctx BazelConversionContext) bool + convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool GetBazelBuildFileContents(c Config, path, name string) (string, error) } @@ -377,7 +377,7 @@ func (b *BazelModuleBase) MixedBuildsEnabled(ctx BazelConversionPathContext) boo } // ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel. -func convertedToBazel(ctx BazelConversionPathContext, module blueprint.Module) bool { +func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool { b, ok := module.(Bazelable) if !ok { return false @@ -386,11 +386,11 @@ func convertedToBazel(ctx BazelConversionPathContext, module blueprint.Module) b } // ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build. -func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionPathContext) bool { +func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionContext) bool { return b.convertWithBp2build(ctx, ctx.Module()) } -func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionPathContext, module blueprint.Module) bool { +func (b *BazelModuleBase) convertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool { if bp2buildModuleDoNotConvert[module.Name()] { return false } diff --git a/android/bazel_paths.go b/android/bazel_paths.go index 23b2e7a68..729c73c8f 100644 --- a/android/bazel_paths.go +++ b/android/bazel_paths.go @@ -68,24 +68,36 @@ import ( // cannot be resolved,the function will panic. This is often due to the dependency not being added // via an AddDependency* method. -// A subset of the ModuleContext methods which are sufficient to resolve references to paths/deps in -// order to form a Bazel-compatible label for conversion. -type BazelConversionPathContext interface { - EarlyModulePathContext +// A minimal context interface to check if a module should be converted by bp2build, +// with functions containing information to match against allowlists and denylists. +// If a module is deemed to be convertible by bp2build, then it should rely on a +// BazelConversionPathContext for more functions for dep/path features. +type BazelConversionContext interface { + Config() Config - GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) - ModuleFromName(name string) (blueprint.Module, bool) Module() Module OtherModuleType(m blueprint.Module) string OtherModuleName(m blueprint.Module) string OtherModuleDir(m blueprint.Module) string +} + +// A subset of the ModuleContext methods which are sufficient to resolve references to paths/deps in +// order to form a Bazel-compatible label for conversion. +type BazelConversionPathContext interface { + EarlyModulePathContext + BazelConversionContext + + ModuleErrorf(fmt string, args ...interface{}) + PropertyErrorf(property, fmt string, args ...interface{}) + GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) + ModuleFromName(name string) (blueprint.Module, bool) AddUnconvertedBp2buildDep(string) } // BazelLabelForModuleDeps expects a list of reference to other modules, ("" // or ":") and returns a Bazel-compatible label which corresponds to dependencies on the // module within the given ctx. -func BazelLabelForModuleDeps(ctx TopDownMutatorContext, modules []string) bazel.LabelList { +func BazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string) bazel.LabelList { return BazelLabelForModuleDepsWithFn(ctx, modules, BazelModuleLabel) } @@ -95,15 +107,15 @@ func BazelLabelForModuleDeps(ctx TopDownMutatorContext, modules []string) bazel. // list which corresponds to dependencies on the module within the given ctx, and the excluded // dependencies. Prebuilt dependencies will be appended with _alwayslink so they can be handled as // whole static libraries. -func BazelLabelForModuleDepsExcludes(ctx TopDownMutatorContext, modules, excludes []string) bazel.LabelList { +func BazelLabelForModuleDepsExcludes(ctx BazelConversionPathContext, modules, excludes []string) bazel.LabelList { return BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, BazelModuleLabel) } // BazelLabelForModuleDepsWithFn expects a list of reference to other modules, ("" // or ":") and applies moduleToLabelFn to determine and return a Bazel-compatible label // which corresponds to dependencies on the module within the given ctx. -func BazelLabelForModuleDepsWithFn(ctx TopDownMutatorContext, modules []string, - moduleToLabelFn func(TopDownMutatorContext, blueprint.Module) string) bazel.LabelList { +func BazelLabelForModuleDepsWithFn(ctx BazelConversionPathContext, modules []string, + moduleToLabelFn func(BazelConversionPathContext, blueprint.Module) string) bazel.LabelList { var labels bazel.LabelList // In some cases, a nil string list is different than an explicitly empty list. if len(modules) == 0 && modules != nil { @@ -131,8 +143,8 @@ func BazelLabelForModuleDepsWithFn(ctx TopDownMutatorContext, modules []string, // to other modules, ("" or ":"). It applies moduleToLabelFn to determine and return a // Bazel-compatible label list which corresponds to dependencies on the module within the given ctx, and // the excluded dependencies. -func BazelLabelForModuleDepsExcludesWithFn(ctx TopDownMutatorContext, modules, excludes []string, - moduleToLabelFn func(TopDownMutatorContext, blueprint.Module) string) bazel.LabelList { +func BazelLabelForModuleDepsExcludesWithFn(ctx BazelConversionPathContext, modules, excludes []string, + moduleToLabelFn func(BazelConversionPathContext, blueprint.Module) string) bazel.LabelList { moduleLabels := BazelLabelForModuleDepsWithFn(ctx, RemoveListFromList(modules, excludes), moduleToLabelFn) if len(excludes) == 0 { return moduleLabels @@ -144,11 +156,11 @@ func BazelLabelForModuleDepsExcludesWithFn(ctx TopDownMutatorContext, modules, e } } -func BazelLabelForModuleSrcSingle(ctx TopDownMutatorContext, path string) bazel.Label { +func BazelLabelForModuleSrcSingle(ctx BazelConversionPathContext, path string) bazel.Label { return BazelLabelForModuleSrcExcludes(ctx, []string{path}, []string(nil)).Includes[0] } -func BazelLabelForModuleDepSingle(ctx TopDownMutatorContext, path string) bazel.Label { +func BazelLabelForModuleDepSingle(ctx BazelConversionPathContext, path string) bazel.Label { return BazelLabelForModuleDepsExcludes(ctx, []string{path}, []string(nil)).Includes[0] } @@ -158,7 +170,7 @@ func BazelLabelForModuleDepSingle(ctx TopDownMutatorContext, path string) bazel. // relative if within the same package). // Properties must have been annotated with struct tag `android:"path"` so that dependencies modules // will have already been handled by the path_deps mutator. -func BazelLabelForModuleSrc(ctx TopDownMutatorContext, paths []string) bazel.LabelList { +func BazelLabelForModuleSrc(ctx BazelConversionPathContext, paths []string) bazel.LabelList { return BazelLabelForModuleSrcExcludes(ctx, paths, []string(nil)) } @@ -168,7 +180,7 @@ func BazelLabelForModuleSrc(ctx TopDownMutatorContext, paths []string) bazel.Lab // (absolute if in a different package or relative if within the same package). // Properties must have been annotated with struct tag `android:"path"` so that dependencies modules // will have already been handled by the path_deps mutator. -func BazelLabelForModuleSrcExcludes(ctx TopDownMutatorContext, paths, excludes []string) bazel.LabelList { +func BazelLabelForModuleSrcExcludes(ctx BazelConversionPathContext, paths, excludes []string) bazel.LabelList { excludeLabels := expandSrcsForBazel(ctx, excludes, []string(nil)) excluded := make([]string, 0, len(excludeLabels.Includes)) for _, e := range excludeLabels.Includes { @@ -288,7 +300,7 @@ func transformSubpackagePaths(ctx BazelConversionPathContext, paths bazel.LabelL // Properties passed as the paths or excludes argument must have been annotated with struct tag // `android:"path"` so that dependencies on other modules will have already been handled by the // path_deps mutator. -func expandSrcsForBazel(ctx TopDownMutatorContext, paths, expandedExcludes []string) bazel.LabelList { +func expandSrcsForBazel(ctx BazelConversionPathContext, paths, expandedExcludes []string) bazel.LabelList { if paths == nil { return bazel.LabelList{} } @@ -336,8 +348,8 @@ func expandSrcsForBazel(ctx TopDownMutatorContext, paths, expandedExcludes []str // getOtherModuleLabel returns a bazel.Label for the given dependency/tag combination for the // module. The label will be relative to the current directory if appropriate. The dependency must // already be resolved by either deps mutator or path deps mutator. -func getOtherModuleLabel(ctx TopDownMutatorContext, dep, tag string, - labelFromModule func(TopDownMutatorContext, blueprint.Module) string) bazel.Label { +func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string, + labelFromModule func(BazelConversionPathContext, blueprint.Module) string) bazel.Label { m, _ := ctx.ModuleFromName(dep) if m == nil { panic(fmt.Errorf("No module named %q found, but was a direct dep of %q", dep, ctx.Module().Name())) @@ -359,7 +371,7 @@ func getOtherModuleLabel(ctx TopDownMutatorContext, dep, tag string, } } -func BazelModuleLabel(ctx TopDownMutatorContext, module blueprint.Module) string { +func BazelModuleLabel(ctx BazelConversionPathContext, module blueprint.Module) string { // TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets. if !convertedToBazel(ctx, module) { return bp2buildModuleLabel(ctx, module) @@ -388,7 +400,7 @@ func samePackage(label1, label2 string) bool { return bazelPackage(label1) == bazelPackage(label2) } -func bp2buildModuleLabel(ctx BazelConversionPathContext, module blueprint.Module) string { +func bp2buildModuleLabel(ctx BazelConversionContext, module blueprint.Module) string { moduleName := ctx.OtherModuleName(module) moduleDir := ctx.OtherModuleDir(module) return fmt.Sprintf("//%s:%s", moduleDir, moduleName) diff --git a/android/variable.go b/android/variable.go index baa6dfac3..e9436401c 100644 --- a/android/variable.go +++ b/android/variable.go @@ -498,7 +498,7 @@ type ProductConfigProperties map[string]map[string]ProductConfigProperty // ProductVariableProperties returns a ProductConfigProperties containing only the properties which // have been set for the module in the given context. -func ProductVariableProperties(ctx BaseMutatorContext) ProductConfigProperties { +func ProductVariableProperties(ctx BazelConversionPathContext) ProductConfigProperties { module := ctx.Module() moduleBase := module.base() diff --git a/cc/bp2build.go b/cc/bp2build.go index e20f7c38c..1b1385480 100644 --- a/cc/bp2build.go +++ b/cc/bp2build.go @@ -50,7 +50,7 @@ type staticOrSharedAttributes struct { System_dynamic_deps bazel.LabelListAttribute } -func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { +func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { // Check that a module is a filegroup type named