diff --git a/android/module.go b/android/module.go index 5908233a2..a0725bc8c 100644 --- a/android/module.go +++ b/android/module.go @@ -1169,7 +1169,9 @@ func (attrs *CommonAttributes) fillCommonBp2BuildModuleAttrs(ctx *topDownMutator mod := ctx.Module().base() // Assert passed-in attributes include Name if len(attrs.Name) == 0 { - ctx.ModuleErrorf("CommonAttributes in fillCommonBp2BuildModuleAttrs expects a `.Name`!") + if ctx.ModuleType() != "package" { + ctx.ModuleErrorf("CommonAttributes in fillCommonBp2BuildModuleAttrs expects a `.Name`!") + } } depsToLabelList := func(deps []string) bazel.LabelListAttribute { diff --git a/bp2build/androidbp_to_build_templates.go b/bp2build/androidbp_to_build_templates.go index 5fed4fab5..9b21c3278 100644 --- a/bp2build/androidbp_to_build_templates.go +++ b/bp2build/androidbp_to_build_templates.go @@ -23,7 +23,7 @@ load("//build/bazel/queryview_rules:soong_module.bzl", "soong_module") // A macro call in the BUILD file representing a Soong module, with space // for expanding more attributes. - soongModuleTarget = `soong_module( + soongModuleTargetTemplate = `soong_module( name = "%s", soong_module_name = "%s", soong_module_type = "%s", @@ -31,10 +31,13 @@ load("//build/bazel/queryview_rules:soong_module.bzl", "soong_module") soong_module_deps = %s, %s)` - bazelTarget = `%s( + ruleTargetTemplate = `%s( name = "%s", %s)` + unnamedRuleTargetTemplate = `%s( +%s)` + // A simple provider to mark and differentiate Soong module rule shims from // regular Bazel rules. Every Soong module rule shim returns a // SoongModuleInfo provider, and can only depend on rules returning diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go index ca8185e57..49b914472 100644 --- a/bp2build/build_conversion.go +++ b/bp2build/build_conversion.go @@ -391,18 +391,19 @@ func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) (BazelTarget, e // Return the Bazel target with rule class and attributes, ready to be // code-generated. attributes := propsToAttributes(props.Attrs) + var content string targetName := m.TargetName() + if targetName != "" { + content = fmt.Sprintf(ruleTargetTemplate, ruleClass, targetName, attributes) + } else { + content = fmt.Sprintf(unnamedRuleTargetTemplate, ruleClass, attributes) + } return BazelTarget{ name: targetName, packageName: m.TargetPackage(), ruleClass: ruleClass, bzlLoadLocation: bzlLoadLocation, - content: fmt.Sprintf( - bazelTarget, - ruleClass, - targetName, - attributes, - ), + content: content, }, nil } @@ -436,7 +437,7 @@ func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) (BazelT return BazelTarget{ name: targetName, content: fmt.Sprintf( - soongModuleTarget, + soongModuleTargetTemplate, targetName, ctx.ModuleName(m), canonicalizeModuleType(ctx.ModuleType(m)), diff --git a/bp2build/testing.go b/bp2build/testing.go index ac1268cb5..ee9200af8 100644 --- a/bp2build/testing.go +++ b/bp2build/testing.go @@ -429,7 +429,9 @@ func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod a } attrStrings := make([]string, 0, len(attrs)+1) - attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) + if name != "" { + attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) + } for _, k := range android.SortedStringKeys(attrs) { attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k])) }