jacoco libs dependency for instrumented libraries

On coverage builds, R8 will fail to properly optimize and fail the build
if ignore_warnings: false, because jacoco injects dependencies on
jacocoagent classes, but the jacocoagent library is not part of the
classpath libraries passed in to R8 in its arguments.

Instead we can add jacocoagent as a libs dependency for these modules so
that it will get pulled into the r8 flags.

Bug: 243903417
Test: add optimize.ignore_warnings: false to NetworkStackAppDefaults &&
  make SKIP_ABI_CHECKS=true PRODUCT=cf_x86_phone-userdebug \
  EMMA_INSTRUMENT=true EMMA_INSTRUMENT_FRAMEWORK=true \
  CLANG_COVERAGE=true COVERAGE_PATHS="" \
  TARGET_USERDATAIMAGE_PARTITION_SIZE=17179869184 \
  SKIP_BOOT_JARS_CHECK=true \
  NetworkStack
Change-Id: I71313b1fd35437ec52890f5c30b3fd1381c7c3d2
(cherry picked from commit c75937264b)
Merged-In: I71313b1fd35437ec52890f5c30b3fd1381c7c3d2
This commit is contained in:
Sam Delmerico
2022-08-31 15:57:52 -04:00
committed by Cherrypicker Worker
parent 4eed981670
commit cfd0caaf78
3 changed files with 31 additions and 4 deletions

View File

@@ -8845,6 +8845,16 @@ func TestApexJavaCoverage(t *testing.T) {
android.FixtureMergeEnv(map[string]string{ android.FixtureMergeEnv(map[string]string{
"EMMA_INSTRUMENT": "true", "EMMA_INSTRUMENT": "true",
}), }),
// need to mock jacocoagent here to satisfy dependency added for
// instrumented libraries at build time
android.FixtureAddFile("jacocoagent/Android.bp", []byte(`
java_library {
name: "jacocoagent",
srcs: ["Test.java"],
system_modules: "none",
sdk_version: "none",
}
`)),
).RunTest(t) ).RunTest(t)
// Make sure jacoco ran on both mylib and mybootclasspathlib // Make sure jacoco ran on both mylib and mybootclasspathlib

View File

@@ -169,6 +169,8 @@ type CommonProperties struct {
Output_params []string Output_params []string
} }
// If true, then jacocoagent is automatically added as a libs dependency so that
// r8 will not strip instrumentation classes out of dexed libraries.
Instrument bool `blueprint:"mutated"` Instrument bool `blueprint:"mutated"`
// If true, then the module supports statically including the jacocoagent // If true, then the module supports statically including the jacocoagent
// into the library. // into the library.
@@ -781,6 +783,9 @@ func (j *Module) deps(ctx android.BottomUpMutatorContext) {
} else if j.shouldInstrumentStatic(ctx) { } else if j.shouldInstrumentStatic(ctx) {
ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent") ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
} }
if j.shouldInstrument(ctx) {
ctx.AddVariationDependencies(nil, libTag, "jacocoagent")
}
if j.useCompose() { if j.useCompose() {
ctx.AddVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), kotlinPluginTag, ctx.AddVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), kotlinPluginTag,

View File

@@ -96,10 +96,22 @@ func TestBootclasspathFragmentInconsistentArtConfiguration_ApexMixture(t *testin
} }
func TestBootclasspathFragment_Coverage(t *testing.T) { func TestBootclasspathFragment_Coverage(t *testing.T) {
prepareForTestWithFrameworkCoverage := android.FixtureMergeEnv(map[string]string{ prepareForTestWithFrameworkCoverage := android.GroupFixturePreparers(
"EMMA_INSTRUMENT": "true", android.FixtureMergeEnv(map[string]string{
"EMMA_INSTRUMENT_FRAMEWORK": "true", "EMMA_INSTRUMENT": "true",
}) "EMMA_INSTRUMENT_FRAMEWORK": "true",
}),
// need to mock jacocoagent here to satisfy dependency added for
// instrumented libraries at build time
android.FixtureAddFile("jacocoagent/Android.bp", []byte(`
java_library {
name: "jacocoagent",
srcs: ["Test.java"],
system_modules: "none",
sdk_version: "none",
}
`)),
)
prepareWithBp := android.FixtureWithRootAndroidBp(` prepareWithBp := android.FixtureWithRootAndroidBp(`
bootclasspath_fragment { bootclasspath_fragment {