Merge "bp2build: refactor BazelTargetModule naming boilerplate."
This commit is contained in:
@@ -57,12 +57,7 @@ func FilegroupBp2Build(ctx TopDownMutatorContext) {
|
|||||||
Srcs: BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs),
|
Srcs: BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can we automate this?
|
props := bazel.NewBazelTargetModuleProperties(fg.Name(), "filegroup", "")
|
||||||
name := "__bp2build__" + fg.Name()
|
|
||||||
props := bazel.BazelTargetModuleProperties{
|
|
||||||
Name: &name,
|
|
||||||
Rule_class: "filegroup",
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.CreateBazelTargetModule(BazelFileGroupFactory, props, attrs)
|
ctx.CreateBazelTargetModule(BazelFileGroupFactory, props, attrs)
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,9 @@ package android
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"android/soong/bazel"
|
"android/soong/bazel"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/google/blueprint"
|
"github.com/google/blueprint"
|
||||||
"github.com/google/blueprint/proptools"
|
"github.com/google/blueprint/proptools"
|
||||||
@@ -513,6 +515,14 @@ func (t *topDownMutatorContext) CreateBazelTargetModule(
|
|||||||
factory ModuleFactory,
|
factory ModuleFactory,
|
||||||
bazelProps bazel.BazelTargetModuleProperties,
|
bazelProps bazel.BazelTargetModuleProperties,
|
||||||
attrs interface{}) BazelTargetModule {
|
attrs interface{}) BazelTargetModule {
|
||||||
|
if !strings.HasPrefix(*bazelProps.Name, bazel.BazelTargetModuleNamePrefix) {
|
||||||
|
panic(fmt.Errorf(
|
||||||
|
"bp2build error: the bazel target module name must start with '%s': %s",
|
||||||
|
bazel.BazelTargetModuleNamePrefix,
|
||||||
|
*bazelProps.Name,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
return t.CreateModule(factory, &bazelProps, attrs).(BazelTargetModule)
|
return t.CreateModule(factory, &bazelProps, attrs).(BazelTargetModule)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -14,6 +14,11 @@
|
|||||||
|
|
||||||
package bazel
|
package bazel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type bazelModuleProperties struct {
|
type bazelModuleProperties struct {
|
||||||
// The label of the Bazel target replacing this Soong module.
|
// The label of the Bazel target replacing this Soong module.
|
||||||
Label string
|
Label string
|
||||||
@@ -41,6 +46,23 @@ type BazelTargetModuleProperties struct {
|
|||||||
Bzl_load_location string
|
Bzl_load_location string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const BazelTargetModuleNamePrefix = "__bp2build__"
|
||||||
|
|
||||||
|
func NewBazelTargetModuleProperties(name string, ruleClass string, bzlLoadLocation string) BazelTargetModuleProperties {
|
||||||
|
if strings.HasPrefix(name, BazelTargetModuleNamePrefix) {
|
||||||
|
panic(fmt.Errorf(
|
||||||
|
"The %s name prefix is added automatically, do not set it manually: %s",
|
||||||
|
BazelTargetModuleNamePrefix,
|
||||||
|
name))
|
||||||
|
}
|
||||||
|
name = BazelTargetModuleNamePrefix + name
|
||||||
|
return BazelTargetModuleProperties{
|
||||||
|
Name: &name,
|
||||||
|
Rule_class: ruleClass,
|
||||||
|
Bzl_load_location: bzlLoadLocation,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Label is used to represent a Bazel compatible Label. Also stores the original bp text to support
|
// Label is used to represent a Bazel compatible Label. Also stores the original bp text to support
|
||||||
// string replacement.
|
// string replacement.
|
||||||
type Label struct {
|
type Label struct {
|
||||||
|
@@ -469,7 +469,7 @@ func makeIndent(indent int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func targetNameForBp2Build(c bpToBuildContext, logicModule blueprint.Module) string {
|
func targetNameForBp2Build(c bpToBuildContext, logicModule blueprint.Module) string {
|
||||||
return strings.Replace(c.ModuleName(logicModule), "__bp2build__", "", 1)
|
return strings.Replace(c.ModuleName(logicModule), bazel.BazelTargetModuleNamePrefix, "", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
|
func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
|
||||||
|
@@ -136,11 +136,7 @@ func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
|
|||||||
String_list_prop: m.props.String_list_prop,
|
String_list_prop: m.props.String_list_prop,
|
||||||
}
|
}
|
||||||
|
|
||||||
name := "__bp2build__" + m.Name()
|
props := bazel.NewBazelTargetModuleProperties(m.Name(), "custom", "")
|
||||||
props := bazel.BazelTargetModuleProperties{
|
|
||||||
Name: &name,
|
|
||||||
Rule_class: "custom",
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.CreateBazelTargetModule(customBazelModuleFactory, props, attrs)
|
ctx.CreateBazelTargetModule(customBazelModuleFactory, props, attrs)
|
||||||
}
|
}
|
||||||
@@ -157,28 +153,25 @@ func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
|
|||||||
baseName := m.Name()
|
baseName := m.Name()
|
||||||
attrs := &customBazelModuleAttributes{}
|
attrs := &customBazelModuleAttributes{}
|
||||||
|
|
||||||
myLibraryName := "__bp2build__" + baseName
|
myLibraryProps := bazel.NewBazelTargetModuleProperties(
|
||||||
myLibraryProps := bazel.BazelTargetModuleProperties{
|
baseName,
|
||||||
Name: &myLibraryName,
|
"my_library",
|
||||||
Rule_class: "my_library",
|
"//build/bazel/rules:rules.bzl",
|
||||||
Bzl_load_location: "//build/bazel/rules:rules.bzl",
|
)
|
||||||
}
|
|
||||||
ctx.CreateBazelTargetModule(customBazelModuleFactory, myLibraryProps, attrs)
|
ctx.CreateBazelTargetModule(customBazelModuleFactory, myLibraryProps, attrs)
|
||||||
|
|
||||||
protoLibraryName := "__bp2build__" + baseName + "_proto_library_deps"
|
protoLibraryProps := bazel.NewBazelTargetModuleProperties(
|
||||||
protoLibraryProps := bazel.BazelTargetModuleProperties{
|
baseName+"_proto_library_deps",
|
||||||
Name: &protoLibraryName,
|
"proto_library",
|
||||||
Rule_class: "proto_library",
|
"//build/bazel/rules:proto.bzl",
|
||||||
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
)
|
||||||
}
|
|
||||||
ctx.CreateBazelTargetModule(customBazelModuleFactory, protoLibraryProps, attrs)
|
ctx.CreateBazelTargetModule(customBazelModuleFactory, protoLibraryProps, attrs)
|
||||||
|
|
||||||
myProtoLibraryName := "__bp2build__" + baseName + "_my_proto_library_deps"
|
myProtoLibraryProps := bazel.NewBazelTargetModuleProperties(
|
||||||
myProtoLibraryProps := bazel.BazelTargetModuleProperties{
|
baseName+"_my_proto_library_deps",
|
||||||
Name: &myProtoLibraryName,
|
"my_proto_library",
|
||||||
Rule_class: "my_proto_library",
|
"//build/bazel/rules:proto.bzl",
|
||||||
Bzl_load_location: "//build/bazel/rules:proto.bzl",
|
)
|
||||||
}
|
|
||||||
ctx.CreateBazelTargetModule(customBazelModuleFactory, myProtoLibraryProps, attrs)
|
ctx.CreateBazelTargetModule(customBazelModuleFactory, myProtoLibraryProps, attrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -853,12 +853,7 @@ func GenruleBp2Build(ctx android.TopDownMutatorContext) {
|
|||||||
Tools: tools,
|
Tools: tools,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can we automate this?
|
props := bazel.NewBazelTargetModuleProperties(m.Name(), "genrule", "")
|
||||||
name := "__bp2build__" + m.Name()
|
|
||||||
props := bazel.BazelTargetModuleProperties{
|
|
||||||
Name: &name,
|
|
||||||
Rule_class: "genrule",
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the BazelTargetModule.
|
// Create the BazelTargetModule.
|
||||||
ctx.CreateBazelTargetModule(BazelGenruleFactory, props, attrs)
|
ctx.CreateBazelTargetModule(BazelGenruleFactory, props, attrs)
|
||||||
|
Reference in New Issue
Block a user