Merge "bp2build: refactor BazelTargetModule naming boilerplate."

This commit is contained in:
Treehugger Robot
2021-02-09 03:21:44 +00:00
committed by Gerrit Code Review
6 changed files with 51 additions and 36 deletions

View File

@@ -57,12 +57,7 @@ func FilegroupBp2Build(ctx TopDownMutatorContext) {
Srcs: BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs),
}
// Can we automate this?
name := "__bp2build__" + fg.Name()
props := bazel.BazelTargetModuleProperties{
Name: &name,
Rule_class: "filegroup",
}
props := bazel.NewBazelTargetModuleProperties(fg.Name(), "filegroup", "")
ctx.CreateBazelTargetModule(BazelFileGroupFactory, props, attrs)
}

View File

@@ -16,7 +16,9 @@ package android
import (
"android/soong/bazel"
"fmt"
"reflect"
"strings"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
@@ -513,6 +515,14 @@ func (t *topDownMutatorContext) CreateBazelTargetModule(
factory ModuleFactory,
bazelProps bazel.BazelTargetModuleProperties,
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)
}

View File

@@ -14,6 +14,11 @@
package bazel
import (
"fmt"
"strings"
)
type bazelModuleProperties struct {
// The label of the Bazel target replacing this Soong module.
Label string
@@ -41,6 +46,23 @@ type BazelTargetModuleProperties struct {
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
// string replacement.
type Label struct {

View File

@@ -469,7 +469,7 @@ func makeIndent(indent int) 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 {

View File

@@ -136,11 +136,7 @@ func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
String_list_prop: m.props.String_list_prop,
}
name := "__bp2build__" + m.Name()
props := bazel.BazelTargetModuleProperties{
Name: &name,
Rule_class: "custom",
}
props := bazel.NewBazelTargetModuleProperties(m.Name(), "custom", "")
ctx.CreateBazelTargetModule(customBazelModuleFactory, props, attrs)
}
@@ -157,28 +153,25 @@ func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
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",
}
myLibraryProps := bazel.NewBazelTargetModuleProperties(
baseName,
"my_library",
"//build/bazel/rules:rules.bzl",
)
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",
}
protoLibraryProps := bazel.NewBazelTargetModuleProperties(
baseName+"_proto_library_deps",
"proto_library",
"//build/bazel/rules:proto.bzl",
)
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",
}
myProtoLibraryProps := bazel.NewBazelTargetModuleProperties(
baseName+"_my_proto_library_deps",
"my_proto_library",
"//build/bazel/rules:proto.bzl",
)
ctx.CreateBazelTargetModule(customBazelModuleFactory, myProtoLibraryProps, attrs)
}
}

View File

@@ -853,12 +853,7 @@ func GenruleBp2Build(ctx android.TopDownMutatorContext) {
Tools: tools,
}
// Can we automate this?
name := "__bp2build__" + m.Name()
props := bazel.BazelTargetModuleProperties{
Name: &name,
Rule_class: "genrule",
}
props := bazel.NewBazelTargetModuleProperties(m.Name(), "genrule", "")
// Create the BazelTargetModule.
ctx.CreateBazelTargetModule(BazelGenruleFactory, props, attrs)