Add coverage specific properties to bootclasspath_fragment

This allows a bootclasspath_fragment (specifically the
art-bootclasspath-fragment) to specify additional contents to be
appended when coverage is enabled.

The art-bootclasspath-fragment will use this to add jacocoagent to its
contents to ensure that it is always consistent with the configuration.

Bug: 177892522
Test: m nothing
Change-Id: I50d05fe5e0e9b8c14bdf3dfd63bba0ac97e31d48
diff --git a/java/bootclasspath_fragment.go b/java/bootclasspath_fragment.go
index 90383af..8bb5cb1 100644
--- a/java/bootclasspath_fragment.go
+++ b/java/bootclasspath_fragment.go
@@ -69,16 +69,23 @@
 	return tag == bootclasspathFragmentContentDepTag
 }
 
+// Properties that can be different when coverage is enabled.
+type BootclasspathFragmentCoverageAffectedProperties struct {
+	// The contents of this bootclasspath_fragment, could be either java_library, or java_sdk_library.
+	//
+	// The order of this list matters as it is the order that is used in the bootclasspath.
+	Contents []string
+}
+
 type bootclasspathFragmentProperties struct {
 	// The name of the image this represents.
 	//
 	// If specified then it must be one of "art" or "boot".
 	Image_name *string
 
-	// The contents of this bootclasspath_fragment, could be either java_library, java_sdk_library, or boot_image.
-	//
-	// The order of this list matters as it is the order that is used in the bootclasspath.
-	Contents []string
+	// Properties whose values need to differ with and without coverage.
+	BootclasspathFragmentCoverageAffectedProperties
+	Coverage BootclasspathFragmentCoverageAffectedProperties
 
 	Hidden_api HiddenAPIFlagFileProperties
 }
@@ -97,8 +104,18 @@
 	android.InitSdkAwareModule(m)
 	android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
 
-	// Initialize the contents property from the image_name.
 	android.AddLoadHook(m, func(ctx android.LoadHookContext) {
+		// If code coverage has been enabled for the framework then append the properties with
+		// coverage specific properties.
+		if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
+			err := proptools.AppendProperties(&m.properties.BootclasspathFragmentCoverageAffectedProperties, &m.properties.Coverage, nil)
+			if err != nil {
+				ctx.PropertyErrorf("coverage", "error trying to append coverage specific properties: %s", err)
+				return
+			}
+		}
+
+		// Initialize the contents property from the image_name.
 		bootclasspathFragmentInitContentsFromImage(ctx, m)
 	})
 	return m
diff --git a/java/bootclasspath_fragment_test.go b/java/bootclasspath_fragment_test.go
index 524a226..0db9361 100644
--- a/java/bootclasspath_fragment_test.go
+++ b/java/bootclasspath_fragment_test.go
@@ -125,3 +125,62 @@
 			}
 		`)
 }
+
+func TestBootclasspathFragment_Coverage(t *testing.T) {
+	prepareForTestWithFrameworkCoverage := android.FixtureMergeEnv(map[string]string{
+		"EMMA_INSTRUMENT":           "true",
+		"EMMA_INSTRUMENT_FRAMEWORK": "true",
+	})
+
+	prepareWithBp := android.FixtureWithRootAndroidBp(`
+		bootclasspath_fragment {
+			name: "myfragment",
+			contents: [
+				"mybootlib",
+			],
+			coverage: {
+				contents: [
+					"coveragelib",
+				],
+			},
+		}
+
+		java_library {
+			name: "mybootlib",
+			srcs: ["Test.java"],
+			system_modules: "none",
+			sdk_version: "none",
+			compile_dex: true,
+		}
+
+		java_library {
+			name: "coveragelib",
+			srcs: ["Test.java"],
+			system_modules: "none",
+			sdk_version: "none",
+			compile_dex: true,
+		}
+	`)
+
+	checkContents := func(t *testing.T, result *android.TestResult, expected ...string) {
+		module := result.Module("myfragment", "android_common").(*BootclasspathFragmentModule)
+		android.AssertArrayString(t, "contents property", expected, module.properties.Contents)
+	}
+
+	t.Run("without coverage", func(t *testing.T) {
+		result := android.GroupFixturePreparers(
+			prepareForTestWithBootclasspathFragment,
+			prepareWithBp,
+		).RunTest(t)
+		checkContents(t, result, "mybootlib")
+	})
+
+	t.Run("with coverage", func(t *testing.T) {
+		result := android.GroupFixturePreparers(
+			prepareForTestWithBootclasspathFragment,
+			prepareForTestWithFrameworkCoverage,
+			prepareWithBp,
+		).RunTest(t)
+		checkContents(t, result, "mybootlib", "coveragelib")
+	})
+}