summaryrefslogtreecommitdiff
path: root/java/jacoco.go
diff options
context:
space:
mode:
author Sam Delmerico <delmerico@google.com> 2022-09-07 12:07:07 -0400
committer Sam Delmerico <delmerico@google.com> 2022-09-09 16:50:38 -0400
commit1e3f78f866504a99ecf30fd7dc81d2d374c544cf (patch)
treef8afb9589d371a2098ef8f356dd1d9d68a2ef280 /java/jacoco.go
parent9be110d15168531cbcdce5a6bb4a03900bc91047 (diff)
add jacocoagent by default to Java modules
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: m Change-Id: Icc24cc260b896fc800125a0318308d823ccf7a83
Diffstat (limited to 'java/jacoco.go')
-rw-r--r--java/jacoco.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/java/jacoco.go b/java/jacoco.go
index e11c2ce69..f8012b852 100644
--- a/java/jacoco.go
+++ b/java/jacoco.go
@@ -47,6 +47,34 @@ var (
"strippedJar", "stripSpec", "tmpDir", "tmpJar")
)
+func jacocoDepsMutator(ctx android.BottomUpMutatorContext) {
+ type instrumentable interface {
+ shouldInstrument(ctx android.BaseModuleContext) bool
+ shouldInstrumentInApex(ctx android.BaseModuleContext) bool
+ setInstrument(value bool)
+ }
+
+ j, ok := ctx.Module().(instrumentable)
+ if !ctx.Module().Enabled() || !ok {
+ return
+ }
+
+ if j.shouldInstrumentInApex(ctx) {
+ j.setInstrument(true)
+ }
+
+ if j.shouldInstrument(ctx) && ctx.ModuleName() != "jacocoagent" {
+ // We can use AddFarVariationDependencies here because, since this dep
+ // is added as libs only (i.e. a compiletime CLASSPATH entry only),
+ // the first variant of jacocoagent is sufficient to prevent
+ // compile time errors.
+ // At this stage in the build, AddVariationDependencies is not always
+ // able to procure a variant of jacocoagent that matches the calling
+ // module.
+ ctx.AddFarVariationDependencies(ctx.Module().Target().Variations(), libTag, "jacocoagent")
+ }
+}
+
// Instruments a jar using the Jacoco command line interface. Uses stripSpec to extract a subset
// of the classes in inputJar into strippedJar, instruments strippedJar into tmpJar, and then
// combines the classes in tmpJar with inputJar (preferring the instrumented classes in tmpJar)