Don't create a new module for bp2build conversion.
A performance improvement for bp2build as Blueprint/Soong no longer have
the overhead of additional modules. The creation of these modules
results in:
* traversal of additional modules for each subsequent mutator
* synchronization over a go channel to collect newly created modules:
https://cs.android.com/android/platform/superproject/+/master:build/blueprint/context.go;l=2594,2600;drc=1602226f23181b8c3fbfcaf3358f0297e839d7d3
We avoid both of these by storing the information directly in the
underlying module.
Also as a fringe benefit, removes some necessary boilerplate for
conversion.
For benchmarks, reduces runtime ~1% for 1% converted, ~24% for 100%
converted. See more: go/benchmarks-for-https:-r.android.com-1792714
Test: ran benchmarks/tests in bp2build
Test: build/bazel/ci/bp2build.sh
Change-Id: Ie9273b8cbab5bc6edac1728067ce184382feb211
diff --git a/android/filegroup.go b/android/filegroup.go
index 97dd136..54d01d3 100644
--- a/android/filegroup.go
+++ b/android/filegroup.go
@@ -34,24 +34,6 @@
Srcs bazel.LabelListAttribute
}
-type bazelFilegroup struct {
- BazelTargetModuleBase
- bazelFilegroupAttributes
-}
-
-func BazelFileGroupFactory() Module {
- module := &bazelFilegroup{}
- module.AddProperties(&module.bazelFilegroupAttributes)
- InitBazelTargetModule(module)
- return module
-}
-
-func (bfg *bazelFilegroup) Name() string {
- return bfg.BaseModuleName()
-}
-
-func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
-
func FilegroupBp2Build(ctx TopDownMutatorContext) {
fg, ok := ctx.Module().(*fileGroup)
if !ok || !fg.ConvertWithBp2build(ctx) {
@@ -69,7 +51,7 @@
Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
}
- ctx.CreateBazelTargetModule(BazelFileGroupFactory, fg.Name(), props, attrs)
+ ctx.CreateBazelTargetModule(fg.Name(), props, attrs)
}
type fileGroupProperties struct {
diff --git a/android/module.go b/android/module.go
index 7451e09..b571f15 100644
--- a/android/module.go
+++ b/android/module.go
@@ -491,6 +491,11 @@
AddProperties(props ...interface{})
GetProperties() []interface{}
+ // IsConvertedByBp2build returns whether this module was converted via bp2build
+ IsConvertedByBp2build() bool
+ // Bp2buildTargets returns the target(s) generated for Bazel via bp2build for this module
+ Bp2buildTargets() []bp2buildInfo
+
BuildParamsForTests() []BuildParams
RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
VariablesForTests() map[string]string
@@ -878,6 +883,11 @@
// for example "" for core or "recovery" for recovery. It will often be set to one of the
// constants in image.go, but can also be set to a custom value by individual module types.
ImageVariation string `blueprint:"mutated"`
+
+ // Information about _all_ bp2build targets generated by this module. Multiple targets are
+ // supported as Soong handles some things within a single target that we may choose to split into
+ // multiple targets, e.g. renderscript, protos, yacc within a cc module.
+ Bp2buildInfo []bp2buildInfo `blueprint:"mutated"`
}
type distProperties struct {
@@ -1204,6 +1214,54 @@
vintfFragmentsPaths Paths
}
+// A struct containing all relevant information about a Bazel target converted via bp2build.
+type bp2buildInfo struct {
+ Name string
+ Dir string
+ BazelProps bazel.BazelTargetModuleProperties
+ Attrs interface{}
+}
+
+// TargetName returns the Bazel target name of a bp2build converted target.
+func (b bp2buildInfo) TargetName() string {
+ return b.Name
+}
+
+// TargetPackage returns the Bazel package of a bp2build converted target.
+func (b bp2buildInfo) TargetPackage() string {
+ return b.Dir
+}
+
+// BazelRuleClass returns the Bazel rule class of a bp2build converted target.
+func (b bp2buildInfo) BazelRuleClass() string {
+ return b.BazelProps.Rule_class
+}
+
+// BazelRuleLoadLocation returns the location of the Bazel rule of a bp2build converted target.
+// This may be empty as native Bazel rules do not need to be loaded.
+func (b bp2buildInfo) BazelRuleLoadLocation() string {
+ return b.BazelProps.Bzl_load_location
+}
+
+// BazelAttributes returns the Bazel attributes of a bp2build converted target.
+func (b bp2buildInfo) BazelAttributes() interface{} {
+ return b.Attrs
+}
+
+func (m *ModuleBase) addBp2buildInfo(info bp2buildInfo) {
+ m.commonProperties.Bp2buildInfo = append(m.commonProperties.Bp2buildInfo, info)
+}
+
+// IsConvertedByBp2build returns whether this module was converted via bp2build.
+func (m *ModuleBase) IsConvertedByBp2build() bool {
+ return len(m.commonProperties.Bp2buildInfo) > 0
+}
+
+// Bp2buildTargets returns the Bazel targets bp2build generated for this module.
+func (m *ModuleBase) Bp2buildTargets() []bp2buildInfo {
+ return m.commonProperties.Bp2buildInfo
+}
+
func (m *ModuleBase) AddJSONData(d *map[string]interface{}) {
(*d)["Android"] = map[string]interface{}{}
}
diff --git a/android/mutator.go b/android/mutator.go
index d895669..20ec621 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -270,7 +270,7 @@
// factory method, just like in CreateModule, but also requires
// BazelTargetModuleProperties containing additional metadata for the
// bp2build codegenerator.
- CreateBazelTargetModule(ModuleFactory, string, bazel.BazelTargetModuleProperties, interface{}) BazelTargetModule
+ CreateBazelTargetModule(string, bazel.BazelTargetModuleProperties, interface{})
}
type topDownMutatorContext struct {
@@ -516,26 +516,24 @@
}
func (t *topDownMutatorContext) CreateBazelTargetModule(
- factory ModuleFactory,
name string,
bazelProps bazel.BazelTargetModuleProperties,
- attrs interface{}) BazelTargetModule {
+ attrs interface{}) {
if strings.HasPrefix(name, bazel.BazelTargetModuleNamePrefix) {
panic(fmt.Errorf(
"The %s name prefix is added automatically, do not set it manually: %s",
bazel.BazelTargetModuleNamePrefix,
name))
}
- name = bazel.BazelTargetModuleNamePrefix + name
- nameProp := struct {
- Name *string
- }{
- Name: &name,
+
+ info := bp2buildInfo{
+ Name: name,
+ Dir: t.OtherModuleDir(t.Module()),
+ BazelProps: bazelProps,
+ Attrs: attrs,
}
- b := t.createModuleWithoutInheritance(factory, &nameProp, attrs).(BazelTargetModule)
- b.SetBazelTargetModuleProperties(bazelProps)
- return b
+ t.Module().base().addBp2buildInfo(info)
}
func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {