Merge "bp2build: refactor BazelTargetModule naming boilerplate."
diff --git a/android/filegroup.go b/android/filegroup.go
index 98d0eaf..674a196 100644
--- a/android/filegroup.go
+++ b/android/filegroup.go
@@ -57,12 +57,7 @@
 		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)
 }
diff --git a/android/mutator.go b/android/mutator.go
index 91753d1..c387193 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -16,7 +16,9 @@
 
 import (
 	"android/soong/bazel"
+	"fmt"
 	"reflect"
+	"strings"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
@@ -513,6 +515,14 @@
 	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)
 }
 
diff --git a/bazel/properties.go b/bazel/properties.go
index 65f37e3..8055306 100644
--- a/bazel/properties.go
+++ b/bazel/properties.go
@@ -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 @@
 	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 {
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index 9013377..7ffcfa4 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -469,7 +469,7 @@
 }
 
 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 {
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 1f0ada2..2e59999 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -136,11 +136,7 @@
 			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 @@
 		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)
 	}
 }
diff --git a/genrule/genrule.go b/genrule/genrule.go
index a500c27..9fa6c48 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -853,12 +853,7 @@
 		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)