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