Refactor BazelTargetModule
This eliminates the need to remove quotes, delete attributes, and
re-checking that name has correct prefix. Additionally, this allows
assignment directly to the BazelTargetModuleProperties struct, which
allows defaulting unused fields and clarity of which field is being set.
Test: go test soong tests
Test: ran ./build/bazel/scripts/milestone-2/demo.sh
Change-Id: Ia9bfcce76234c793a4ddd5f29a661150f83341c9
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index 41ad409..a4c4a0d 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -19,7 +19,6 @@
"android/soong/bazel"
"fmt"
"reflect"
- "strconv"
"strings"
"github.com/google/blueprint"
@@ -178,13 +177,14 @@
switch ctx.Mode() {
case Bp2Build:
- if _, ok := m.(android.BazelTargetModule); !ok {
+ if b, ok := m.(android.BazelTargetModule); !ok {
// Only include regular Soong modules (non-BazelTargetModules) into the total count.
totalModuleCount += 1
return
+ } else {
+ t = generateBazelTarget(bpCtx, m, b)
+ ruleClassCount[t.ruleClass] += 1
}
- t = generateBazelTarget(bpCtx, m)
- ruleClassCount[t.ruleClass] += 1
case QueryView:
// Blocklist certain module types from being generated.
if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" {
@@ -208,36 +208,13 @@
return buildFileToTargets, metrics
}
-// Helper method to trim quotes around strings.
-func trimQuotes(s string) string {
- if s == "" {
- // strconv.Unquote would error out on empty strings, but this method
- // allows them, so return the empty string directly.
- return ""
- }
- ret, err := strconv.Unquote(s)
- if err != nil {
- // Panic the error immediately.
- panic(fmt.Errorf("Trying to unquote '%s', but got error: %s", s, err))
- }
- return ret
-}
+func generateBazelTarget(ctx bpToBuildContext, m blueprint.Module, b android.BazelTargetModule) BazelTarget {
+ ruleClass := b.RuleClass()
+ bzlLoadLocation := b.BzlLoadLocation()
-func generateBazelTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget {
// extract the bazel attributes from the module.
props := getBuildProperties(ctx, m)
- // extract the rule class name from the attributes. Since the string value
- // will be string-quoted, remove the quotes here.
- ruleClass := trimQuotes(props.Attrs["rule_class"])
- // Delete it from being generated in the BUILD file.
- delete(props.Attrs, "rule_class")
-
- // extract the bzl_load_location, and also remove the quotes around it here.
- bzlLoadLocation := trimQuotes(props.Attrs["bzl_load_location"])
- // Delete it from being generated in the BUILD file.
- delete(props.Attrs, "bzl_load_location")
-
delete(props.Attrs, "bp2build_available")
// Return the Bazel target with rule class and attributes, ready to be
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 9774915..cb50fc8 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -136,9 +136,11 @@
String_list_prop: m.props.String_list_prop,
}
- props := bazel.NewBazelTargetModuleProperties(m.Name(), "custom", "")
+ props := bazel.BazelTargetModuleProperties{
+ Rule_class: "custom",
+ }
- ctx.CreateBazelTargetModule(customBazelModuleFactory, props, attrs)
+ ctx.CreateBazelTargetModule(customBazelModuleFactory, m.Name(), props, attrs)
}
}
@@ -153,26 +155,23 @@
baseName := m.Name()
attrs := &customBazelModuleAttributes{}
- myLibraryProps := bazel.NewBazelTargetModuleProperties(
- baseName,
- "my_library",
- "//build/bazel/rules:rules.bzl",
- )
- ctx.CreateBazelTargetModule(customBazelModuleFactory, myLibraryProps, attrs)
+ myLibraryProps := bazel.BazelTargetModuleProperties{
+ Rule_class: "my_library",
+ Bzl_load_location: "//build/bazel/rules:rules.bzl",
+ }
+ ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName, myLibraryProps, attrs)
- protoLibraryProps := bazel.NewBazelTargetModuleProperties(
- baseName+"_proto_library_deps",
- "proto_library",
- "//build/bazel/rules:proto.bzl",
- )
- ctx.CreateBazelTargetModule(customBazelModuleFactory, protoLibraryProps, attrs)
+ protoLibraryProps := bazel.BazelTargetModuleProperties{
+ Rule_class: "proto_library",
+ Bzl_load_location: "//build/bazel/rules:proto.bzl",
+ }
+ ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_proto_library_deps", protoLibraryProps, attrs)
- myProtoLibraryProps := bazel.NewBazelTargetModuleProperties(
- baseName+"_my_proto_library_deps",
- "my_proto_library",
- "//build/bazel/rules:proto.bzl",
- )
- ctx.CreateBazelTargetModule(customBazelModuleFactory, myProtoLibraryProps, attrs)
+ myProtoLibraryProps := bazel.BazelTargetModuleProperties{
+ Rule_class: "my_proto_library",
+ Bzl_load_location: "//build/bazel/rules:proto.bzl",
+ }
+ ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs)
}
}