assign labels to merged_txt modules with defs

This is groundwork for allowlist v2, and ensures that rdeps of
merged_txt targets appropriately reflect that these targets exist.

Fixes: 302061007
Test: Unit test update
Change-Id: I1d64635bb3db50ee525f1a557fd4b73d50dba879
diff --git a/api/api.go b/api/api.go
index 431d6d8..692d38f 100644
--- a/api/api.go
+++ b/api/api.go
@@ -114,6 +114,7 @@
 }
 
 type Bazel_module struct {
+	Label              *string
 	Bp2build_available *bool
 }
 type bazelProperties struct {
@@ -140,6 +141,8 @@
 	ModuleTag string
 	// public, system, module-lib or system-server
 	Scope string
+	// True if there is a bp2build definition for this module
+	Bp2buildDefined bool
 }
 
 func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
@@ -152,8 +155,10 @@
 	if txt.Scope != "public" {
 		filename = txt.Scope + "-" + filename
 	}
+	moduleName := ctx.ModuleName() + "-" + filename
+
 	props := genruleProps{}
-	props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
+	props.Name = proptools.StringPtr(moduleName)
 	props.Tools = []string{"metalava"}
 	props.Out = []string{filename}
 	props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --out $(out)")
@@ -171,7 +176,20 @@
 		},
 	}
 	props.Visibility = []string{"//visibility:public"}
-	ctx.CreateModule(genrule.GenRuleFactory, &props, &bp2buildNotAvailable)
+	bazelProps := bazelProperties{
+		&Bazel_module{
+			Bp2build_available: proptools.BoolPtr(false),
+		},
+	}
+	if txt.Bp2buildDefined {
+		moduleDir := ctx.ModuleDir()
+		if moduleDir == android.Bp2BuildTopLevel {
+			moduleDir = ""
+		}
+		label := fmt.Sprintf("//%s:%s", moduleDir, moduleName)
+		bazelProps.Label = &label
+	}
+	ctx.CreateModule(genrule.GenRuleFactory, &props, &bazelProps)
 }
 
 func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
@@ -301,38 +319,43 @@
 
 	tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
 	distFilename := []string{"android.txt", "android-removed.txt"}
+	bp2BuildDefined := []bool{true, false}
 	for i, f := range []string{"current.txt", "removed.txt"} {
 		textFiles = append(textFiles, MergedTxtDefinition{
-			TxtFilename:  f,
-			DistFilename: distFilename[i],
-			BaseTxt:      ":non-updatable-" + f,
-			Modules:      bootclasspath,
-			ModuleTag:    "{.public" + tagSuffix[i],
-			Scope:        "public",
+			TxtFilename:     f,
+			DistFilename:    distFilename[i],
+			BaseTxt:         ":non-updatable-" + f,
+			Modules:         bootclasspath,
+			ModuleTag:       "{.public" + tagSuffix[i],
+			Scope:           "public",
+			Bp2buildDefined: bp2BuildDefined[i],
 		})
 		textFiles = append(textFiles, MergedTxtDefinition{
-			TxtFilename:  f,
-			DistFilename: distFilename[i],
-			BaseTxt:      ":non-updatable-system-" + f,
-			Modules:      bootclasspath,
-			ModuleTag:    "{.system" + tagSuffix[i],
-			Scope:        "system",
+			TxtFilename:     f,
+			DistFilename:    distFilename[i],
+			BaseTxt:         ":non-updatable-system-" + f,
+			Modules:         bootclasspath,
+			ModuleTag:       "{.system" + tagSuffix[i],
+			Scope:           "system",
+			Bp2buildDefined: bp2BuildDefined[i],
 		})
 		textFiles = append(textFiles, MergedTxtDefinition{
-			TxtFilename:  f,
-			DistFilename: distFilename[i],
-			BaseTxt:      ":non-updatable-module-lib-" + f,
-			Modules:      bootclasspath,
-			ModuleTag:    "{.module-lib" + tagSuffix[i],
-			Scope:        "module-lib",
+			TxtFilename:     f,
+			DistFilename:    distFilename[i],
+			BaseTxt:         ":non-updatable-module-lib-" + f,
+			Modules:         bootclasspath,
+			ModuleTag:       "{.module-lib" + tagSuffix[i],
+			Scope:           "module-lib",
+			Bp2buildDefined: bp2BuildDefined[i],
 		})
 		textFiles = append(textFiles, MergedTxtDefinition{
-			TxtFilename:  f,
-			DistFilename: distFilename[i],
-			BaseTxt:      ":non-updatable-system-server-" + f,
-			Modules:      system_server_classpath,
-			ModuleTag:    "{.system-server" + tagSuffix[i],
-			Scope:        "system-server",
+			TxtFilename:     f,
+			DistFilename:    distFilename[i],
+			BaseTxt:         ":non-updatable-system-server-" + f,
+			Modules:         system_server_classpath,
+			ModuleTag:       "{.system-server" + tagSuffix[i],
+			Scope:           "system-server",
+			Bp2buildDefined: bp2BuildDefined[i],
 		})
 	}
 	for _, txt := range textFiles {
diff --git a/api/api_test.go b/api/api_test.go
index 1f4c2af..70f2162 100644
--- a/api/api_test.go
+++ b/api/api_test.go
@@ -33,6 +33,8 @@
 	t.Helper()
 	runCombinedApisTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {
 		ctx.RegisterModuleType("java_defaults", java.DefaultsFactory)
+		ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
+		ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
 	})
 }
 
@@ -44,6 +46,33 @@
     bootclasspath: ["bcp"],
     system_server_classpath: ["ssc"],
 }
+
+java_sdk_library {
+		name: "bcp",
+		srcs: ["a.java", "b.java"],
+		shared_library: false,
+}
+java_sdk_library {
+		name: "ssc",
+		srcs: ["a.java", "b.java"],
+		shared_library: false,
+}
+filegroup {
+    name: "non-updatable-current.txt",
+    srcs: ["current.txt"],
+}
+filegroup {
+    name: "non-updatable-system-current.txt",
+    srcs: ["system-current.txt"],
+}
+filegroup {
+    name: "non-updatable-module-lib-current.txt",
+    srcs: ["system-removed.txt"],
+}
+filegroup {
+    name: "non-updatable-system-server-current.txt",
+    srcs: ["system-lint-baseline.txt"],
+}
 `,
 		Filesystem: map[string]string{
 			"a/Android.bp": `
@@ -51,27 +80,35 @@
 				name: "android.jar_defaults",
 			}
 			`,
+			"api/current.txt":        "",
+			"api/removed.txt":        "",
+			"api/system-current.txt": "",
+			"api/system-removed.txt": "",
+			"api/test-current.txt":   "",
+			"api/test-removed.txt":   "",
 		},
+		StubbedBuildDefinitions:    []string{"bcp", "ssc", "non-updatable-current.txt", "non-updatable-system-current.txt", "non-updatable-module-lib-current.txt", "non-updatable-system-server-current.txt"},
+		ExpectedHandcraftedModules: []string{"foo-current.txt", "foo-system-current.txt", "foo-module-lib-current.txt", "foo-system-server-current.txt"},
 		ExpectedBazelTargets: []string{
 			bp2build.MakeBazelTargetNoRestrictions("merged_txts", "foo-current.txt", bp2build.AttrNameToString{
 				"scope": `"public"`,
-				"base":  `":non-updatable-current.txt__BP2BUILD__MISSING__DEP"`,
-				"deps":  `[":bcp__BP2BUILD__MISSING__DEP"]`,
+				"base":  `":non-updatable-current.txt"`,
+				"deps":  `[":bcp"]`,
 			}),
 			bp2build.MakeBazelTargetNoRestrictions("merged_txts", "foo-system-current.txt", bp2build.AttrNameToString{
 				"scope": `"system"`,
-				"base":  `":non-updatable-system-current.txt__BP2BUILD__MISSING__DEP"`,
-				"deps":  `[":bcp__BP2BUILD__MISSING__DEP"]`,
+				"base":  `":non-updatable-system-current.txt"`,
+				"deps":  `[":bcp"]`,
 			}),
 			bp2build.MakeBazelTargetNoRestrictions("merged_txts", "foo-module-lib-current.txt", bp2build.AttrNameToString{
 				"scope": `"module-lib"`,
-				"base":  `":non-updatable-module-lib-current.txt__BP2BUILD__MISSING__DEP"`,
-				"deps":  `[":bcp__BP2BUILD__MISSING__DEP"]`,
+				"base":  `":non-updatable-module-lib-current.txt"`,
+				"deps":  `[":bcp"]`,
 			}),
 			bp2build.MakeBazelTargetNoRestrictions("merged_txts", "foo-system-server-current.txt", bp2build.AttrNameToString{
 				"scope": `"system-server"`,
-				"base":  `":non-updatable-system-server-current.txt__BP2BUILD__MISSING__DEP"`,
-				"deps":  `[":ssc__BP2BUILD__MISSING__DEP"]`,
+				"base":  `":non-updatable-system-server-current.txt"`,
+				"deps":  `[":ssc"]`,
 			}),
 		},
 	})