Handle nameless modules during bp2build conversion.

So far it is `package` module.

Test: treehugger
Change-Id: Ibf3af0b0bf0a532e2ea1d478275a5ac3784ef170
diff --git a/android/module.go b/android/module.go
index 5908233..a0725bc 100644
--- a/android/module.go
+++ b/android/module.go
@@ -1169,7 +1169,9 @@
 	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 5fed4fa..9b21c32 100644
--- a/bp2build/androidbp_to_build_templates.go
+++ b/bp2build/androidbp_to_build_templates.go
@@ -23,7 +23,7 @@
 
 	// 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 @@
     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 ca8185e..49b9144 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -391,18 +391,19 @@
 	// 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 @@
 	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 ac1268c..ee9200a 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -429,7 +429,9 @@
 	}
 
 	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]))
 	}