Merge "bp2build: Refactor CreateBazelTargetModule API."
This commit is contained in:
@@ -17,8 +17,6 @@ package android
|
|||||||
import (
|
import (
|
||||||
"android/soong/bazel"
|
"android/soong/bazel"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/blueprint/proptools"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -28,7 +26,6 @@ func init() {
|
|||||||
|
|
||||||
// https://docs.bazel.build/versions/master/be/general.html#filegroup
|
// https://docs.bazel.build/versions/master/be/general.html#filegroup
|
||||||
type bazelFilegroupAttributes struct {
|
type bazelFilegroupAttributes struct {
|
||||||
Name *string
|
|
||||||
Srcs bazel.LabelList
|
Srcs bazel.LabelList
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,20 +47,23 @@ func (bfg *bazelFilegroup) Name() string {
|
|||||||
|
|
||||||
func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
|
func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
|
||||||
|
|
||||||
// TODO: Create helper functions to avoid this boilerplate.
|
|
||||||
func FilegroupBp2Build(ctx TopDownMutatorContext) {
|
func FilegroupBp2Build(ctx TopDownMutatorContext) {
|
||||||
fg, ok := ctx.Module().(*fileGroup)
|
fg, ok := ctx.Module().(*fileGroup)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
attrs := &bazelFilegroupAttributes{
|
||||||
name := "__bp2build__" + fg.base().BaseModuleName()
|
|
||||||
ctx.CreateModule(BazelFileGroupFactory, &bazelFilegroupAttributes{
|
|
||||||
Name: proptools.StringPtr(name),
|
|
||||||
Srcs: BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs),
|
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",
|
Rule_class: "filegroup",
|
||||||
})
|
}
|
||||||
|
|
||||||
|
ctx.CreateBazelTargetModule(BazelFileGroupFactory, props, attrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileGroupProperties struct {
|
type fileGroupProperties struct {
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
package android
|
package android
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"android/soong/bazel"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/google/blueprint"
|
"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
|
// 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.
|
// the specified property structs to it as if the properties were set in a blueprint file.
|
||||||
CreateModule(ModuleFactory, ...interface{}) Module
|
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 {
|
type topDownMutatorContext struct {
|
||||||
@@ -502,6 +509,13 @@ func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
|
|||||||
ctx.BottomUp("deps", depsMutator).Parallel()
|
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{}) {
|
func (t *topDownMutatorContext) AppendProperties(props ...interface{}) {
|
||||||
for _, p := range props {
|
for _, p := range props {
|
||||||
err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
|
err := proptools.AppendMatchingProperties(t.Module().base().customizableProperties,
|
||||||
|
@@ -29,6 +29,8 @@ type Properties struct {
|
|||||||
// BazelTargetModuleProperties contain properties and metadata used for
|
// BazelTargetModuleProperties contain properties and metadata used for
|
||||||
// Blueprint to BUILD file conversion.
|
// Blueprint to BUILD file conversion.
|
||||||
type BazelTargetModuleProperties struct {
|
type BazelTargetModuleProperties struct {
|
||||||
|
Name *string
|
||||||
|
|
||||||
// The Bazel rule class for this target.
|
// The Bazel rule class for this target.
|
||||||
Rule_class string
|
Rule_class string
|
||||||
|
|
||||||
|
@@ -3,8 +3,6 @@ package bp2build
|
|||||||
import (
|
import (
|
||||||
"android/soong/android"
|
"android/soong/android"
|
||||||
"android/soong/bazel"
|
"android/soong/bazel"
|
||||||
|
|
||||||
"github.com/google/blueprint/proptools"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type nestedProps struct {
|
type nestedProps struct {
|
||||||
@@ -105,7 +103,6 @@ func customDefaultsModuleFactory() android.Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type customBazelModuleAttributes struct {
|
type customBazelModuleAttributes struct {
|
||||||
Name *string
|
|
||||||
String_prop string
|
String_prop string
|
||||||
String_list_prop []string
|
String_list_prop []string
|
||||||
}
|
}
|
||||||
@@ -127,14 +124,18 @@ func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContex
|
|||||||
|
|
||||||
func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
|
func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
|
||||||
if m, ok := ctx.Module().(*customModule); ok {
|
if m, ok := ctx.Module().(*customModule); ok {
|
||||||
name := "__bp2build__" + m.Name()
|
attrs := &customBazelModuleAttributes{
|
||||||
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
|
|
||||||
Name: proptools.StringPtr(name),
|
|
||||||
String_prop: m.props.String_prop,
|
String_prop: m.props.String_prop,
|
||||||
String_list_prop: m.props.String_list_prop,
|
String_list_prop: m.props.String_list_prop,
|
||||||
}, &bazel.BazelTargetModuleProperties{
|
}
|
||||||
|
|
||||||
|
name := "__bp2build__" + m.Name()
|
||||||
|
props := bazel.BazelTargetModuleProperties{
|
||||||
|
Name: &name,
|
||||||
Rule_class: "custom",
|
Rule_class: "custom",
|
||||||
})
|
}
|
||||||
|
|
||||||
|
ctx.CreateBazelTargetModule(customBazelModuleFactory, props, attrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,24 +143,31 @@ func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
|
|||||||
// module to target.
|
// module to target.
|
||||||
func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
|
func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
|
||||||
if m, ok := ctx.Module().(*customModule); ok {
|
if m, ok := ctx.Module().(*customModule); ok {
|
||||||
baseName := "__bp2build__" + m.Name()
|
baseName := m.Name()
|
||||||
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
|
attrs := &customBazelModuleAttributes{}
|
||||||
Name: proptools.StringPtr(baseName),
|
|
||||||
}, &bazel.BazelTargetModuleProperties{
|
myLibraryName := "__bp2build__" + baseName
|
||||||
|
myLibraryProps := bazel.BazelTargetModuleProperties{
|
||||||
|
Name: &myLibraryName,
|
||||||
Rule_class: "my_library",
|
Rule_class: "my_library",
|
||||||
Bzl_load_location: "//build/bazel/rules:rules.bzl",
|
Bzl_load_location: "//build/bazel/rules:rules.bzl",
|
||||||
})
|
}
|
||||||
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
|
ctx.CreateBazelTargetModule(customBazelModuleFactory, myLibraryProps, attrs)
|
||||||
Name: proptools.StringPtr(baseName + "_proto_library_deps"),
|
|
||||||
}, &bazel.BazelTargetModuleProperties{
|
protoLibraryName := "__bp2build__" + baseName + "_proto_library_deps"
|
||||||
|
protoLibraryProps := bazel.BazelTargetModuleProperties{
|
||||||
|
Name: &protoLibraryName,
|
||||||
Rule_class: "proto_library",
|
Rule_class: "proto_library",
|
||||||
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
||||||
})
|
}
|
||||||
ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
|
ctx.CreateBazelTargetModule(customBazelModuleFactory, protoLibraryProps, attrs)
|
||||||
Name: proptools.StringPtr(baseName + "_my_proto_library_deps"),
|
|
||||||
}, &bazel.BazelTargetModuleProperties{
|
myProtoLibraryName := "__bp2build__" + baseName + "_my_proto_library_deps"
|
||||||
|
myProtoLibraryProps := bazel.BazelTargetModuleProperties{
|
||||||
|
Name: &myProtoLibraryName,
|
||||||
Rule_class: "my_proto_library",
|
Rule_class: "my_proto_library",
|
||||||
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
||||||
})
|
}
|
||||||
|
ctx.CreateBazelTargetModule(customBazelModuleFactory, myProtoLibraryProps, attrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -780,7 +780,6 @@ type genRuleProperties struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type bazelGenruleAttributes struct {
|
type bazelGenruleAttributes struct {
|
||||||
Name *string
|
|
||||||
Srcs bazel.LabelList
|
Srcs bazel.LabelList
|
||||||
Outs []string
|
Outs []string
|
||||||
Tools bazel.LabelList
|
Tools bazel.LabelList
|
||||||
@@ -804,7 +803,7 @@ func GenruleBp2Build(ctx android.TopDownMutatorContext) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
name := "__bp2build__" + m.Name()
|
|
||||||
// Bazel only has the "tools" attribute.
|
// Bazel only has the "tools" attribute.
|
||||||
tools := android.BazelLabelForModuleDeps(ctx, m.properties.Tools)
|
tools := android.BazelLabelForModuleDeps(ctx, m.properties.Tools)
|
||||||
tool_files := android.BazelLabelForModuleSrc(ctx, m.properties.Tool_files)
|
tool_files := android.BazelLabelForModuleSrc(ctx, m.properties.Tool_files)
|
||||||
@@ -847,16 +846,22 @@ func GenruleBp2Build(ctx android.TopDownMutatorContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the BazelTargetModule.
|
attrs := &bazelGenruleAttributes{
|
||||||
ctx.CreateModule(BazelGenruleFactory, &bazelGenruleAttributes{
|
|
||||||
Name: proptools.StringPtr(name),
|
|
||||||
Srcs: srcs,
|
Srcs: srcs,
|
||||||
Outs: outs,
|
Outs: outs,
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
Tools: tools,
|
Tools: tools,
|
||||||
}, &bazel.BazelTargetModuleProperties{
|
}
|
||||||
|
|
||||||
|
// Can we automate this?
|
||||||
|
name := "__bp2build__" + m.Name()
|
||||||
|
props := bazel.BazelTargetModuleProperties{
|
||||||
|
Name: &name,
|
||||||
Rule_class: "genrule",
|
Rule_class: "genrule",
|
||||||
})
|
}
|
||||||
|
|
||||||
|
// Create the BazelTargetModule.
|
||||||
|
ctx.CreateBazelTargetModule(BazelGenruleFactory, props, attrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *bazelGenrule) Name() string {
|
func (m *bazelGenrule) Name() string {
|
||||||
|
Reference in New Issue
Block a user