bp2build: Refactor CreateBazelTargetModule API.

This CL refactors the CreateBazelTargetModule API to minimize boilerplate, and to establish a well defined function signature for the expected metadata about a BazelTargetModule.

Test: soong tests

Test: TH
Change-Id: I474ff5a2b0db8deeed49ba4ca73b416ccb494fdd
This commit is contained in:
Jingwen Chen
2021-02-05 03:01:50 -05:00
parent b7eab01167
commit 1fd14691dd
5 changed files with 67 additions and 38 deletions

View File

@@ -17,8 +17,6 @@ package android
import (
"android/soong/bazel"
"strings"
"github.com/google/blueprint/proptools"
)
func init() {
@@ -28,7 +26,6 @@ func init() {
// https://docs.bazel.build/versions/master/be/general.html#filegroup
type bazelFilegroupAttributes struct {
Name *string
Srcs bazel.LabelList
}
@@ -50,20 +47,23 @@ func (bfg *bazelFilegroup) Name() string {
func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
// TODO: Create helper functions to avoid this boilerplate.
func FilegroupBp2Build(ctx TopDownMutatorContext) {
fg, ok := ctx.Module().(*fileGroup)
if !ok {
return
}
name := "__bp2build__" + fg.base().BaseModuleName()
ctx.CreateModule(BazelFileGroupFactory, &bazelFilegroupAttributes{
Name: proptools.StringPtr(name),
attrs := &bazelFilegroupAttributes{
Srcs: BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs),
}, &bazel.BazelTargetModuleProperties{
}
// Can we automate this?
name := "__bp2build__" + fg.Name()
props := bazel.BazelTargetModuleProperties{
Name: &name,
Rule_class: "filegroup",
})
}
ctx.CreateBazelTargetModule(BazelFileGroupFactory, props, attrs)
}
type fileGroupProperties struct {

View File

@@ -15,6 +15,7 @@
package android
import (
"android/soong/bazel"
"reflect"
"github.com/google/blueprint"
@@ -275,6 +276,12 @@ type TopDownMutatorContext interface {
// CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
// the specified property structs to it as if the properties were set in a blueprint file.
CreateModule(ModuleFactory, ...interface{}) Module
// CreateBazelTargetModule creates a BazelTargetModule by calling the
// factory method, just like in CreateModule, but also requires
// BazelTargetModuleProperties containing additional metadata for the
// bp2build codegenerator.
CreateBazelTargetModule(ModuleFactory, bazel.BazelTargetModuleProperties, interface{}) BazelTargetModule
}
type topDownMutatorContext struct {
@@ -502,6 +509,13 @@ func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
ctx.BottomUp("deps", depsMutator).Parallel()
}
func (t *topDownMutatorContext) CreateBazelTargetModule(
factory ModuleFactory,
bazelProps bazel.BazelTargetModuleProperties,
attrs interface{}) BazelTargetModule {
return t.CreateModule(factory, &bazelProps, attrs).(BazelTargetModule)
}
func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
for _, p := range props {
err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,

View File

@@ -29,6 +29,8 @@ type Properties struct {
// BazelTargetModuleProperties contain properties and metadata used for
// Blueprint to BUILD file conversion.
type BazelTargetModuleProperties struct {
Name *string
// The Bazel rule class for this target.
Rule_class string

View File

@@ -3,8 +3,6 @@ package bp2build
import (
"android/soong/android"
"android/soong/bazel"
"github.com/google/blueprint/proptools"
)
type nestedProps struct {
@@ -105,7 +103,6 @@ func customDefaultsModuleFactory() android.Module {
}
type customBazelModuleAttributes struct {
Name *string
String_prop string
String_list_prop []string
}
@@ -127,14 +124,18 @@ func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContex
func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
if m, ok := ctx.Module().(*customModule); ok {
name := "__bp2build__" + m.Name()
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
Name: proptools.StringPtr(name),
attrs := &customBazelModuleAttributes{
String_prop: m.props.String_prop,
String_list_prop: m.props.String_list_prop,
}, &bazel.BazelTargetModuleProperties{
}
name := "__bp2build__" + m.Name()
props := bazel.BazelTargetModuleProperties{
Name: &name,
Rule_class: "custom",
})
}
ctx.CreateBazelTargetModule(customBazelModuleFactory, props, attrs)
}
}
@@ -142,24 +143,31 @@ func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
// module to target.
func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
if m, ok := ctx.Module().(*customModule); ok {
baseName := "__bp2build__" + m.Name()
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
Name: proptools.StringPtr(baseName),
}, &bazel.BazelTargetModuleProperties{
baseName := m.Name()
attrs := &customBazelModuleAttributes{}
myLibraryName := "__bp2build__" + baseName
myLibraryProps := bazel.BazelTargetModuleProperties{
Name: &myLibraryName,
Rule_class: "my_library",
Bzl_load_location: "//build/bazel/rules:rules.bzl",
})
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
Name: proptools.StringPtr(baseName + "_proto_library_deps"),
}, &bazel.BazelTargetModuleProperties{
}
ctx.CreateBazelTargetModule(customBazelModuleFactory, myLibraryProps, attrs)
protoLibraryName := "__bp2build__" + baseName + "_proto_library_deps"
protoLibraryProps := bazel.BazelTargetModuleProperties{
Name: &protoLibraryName,
Rule_class: "proto_library",
Bzl_load_location: "//build/bazel/rules:proto.bzl",
})
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
Name: proptools.StringPtr(baseName + "_my_proto_library_deps"),
}, &bazel.BazelTargetModuleProperties{
}
ctx.CreateBazelTargetModule(customBazelModuleFactory, protoLibraryProps, attrs)
myProtoLibraryName := "__bp2build__" + baseName + "_my_proto_library_deps"
myProtoLibraryProps := bazel.BazelTargetModuleProperties{
Name: &myProtoLibraryName,
Rule_class: "my_proto_library",
Bzl_load_location: "//build/bazel/rules:proto.bzl",
})
}
ctx.CreateBazelTargetModule(customBazelModuleFactory, myProtoLibraryProps, attrs)
}
}

View File

@@ -780,7 +780,6 @@ type genRuleProperties struct {
}
type bazelGenruleAttributes struct {
Name *string
Srcs bazel.LabelList
Outs []string
Tools bazel.LabelList
@@ -804,7 +803,7 @@ func GenruleBp2Build(ctx android.TopDownMutatorContext) {
if !ok {
return
}
name := "__bp2build__" + m.Name()
// Bazel only has the "tools" attribute.
tools := android.BazelLabelForModuleDeps(ctx, m.properties.Tools)
tool_files := android.BazelLabelForModuleSrc(ctx, m.properties.Tool_files)
@@ -847,16 +846,22 @@ func GenruleBp2Build(ctx android.TopDownMutatorContext) {
}
}
// Create the BazelTargetModule.
ctx.CreateModule(BazelGenruleFactory, &bazelGenruleAttributes{
Name: proptools.StringPtr(name),
attrs := &bazelGenruleAttributes{
Srcs: srcs,
Outs: outs,
Cmd: cmd,
Tools: tools,
}, &bazel.BazelTargetModuleProperties{
}
// Can we automate this?
name := "__bp2build__" + m.Name()
props := bazel.BazelTargetModuleProperties{
Name: &name,
Rule_class: "genrule",
})
}
// Create the BazelTargetModule.
ctx.CreateBazelTargetModule(BazelGenruleFactory, props, attrs)
}
func (m *bazelGenrule) Name() string {