|
20 | 20 | import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
|
21 | 21 | import com.google.devtools.build.lib.analysis.Allowlist;
|
22 | 22 | import com.google.devtools.build.lib.analysis.ConfiguredTarget;
|
| 23 | +import com.google.devtools.build.lib.analysis.FilesToRunProvider; |
23 | 24 | import com.google.devtools.build.lib.analysis.OutputGroupInfo;
|
24 | 25 | import com.google.devtools.build.lib.analysis.RequiredConfigFragmentsProvider;
|
25 | 26 | import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
|
|
29 | 30 | import com.google.devtools.build.lib.analysis.Runfiles;
|
30 | 31 | import com.google.devtools.build.lib.analysis.RunfilesProvider;
|
31 | 32 | import com.google.devtools.build.lib.analysis.RunfilesSupport;
|
| 33 | +import com.google.devtools.build.lib.analysis.SourceManifestAction; |
32 | 34 | import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
|
33 | 35 | import com.google.devtools.build.lib.analysis.actions.Substitution;
|
34 | 36 | import com.google.devtools.build.lib.analysis.actions.Template;
|
|
66 | 68 | import com.google.devtools.build.lib.rules.java.OneVersionCheckActionBuilder;
|
67 | 69 | import com.google.devtools.build.lib.rules.java.proto.GeneratedExtensionRegistryProvider;
|
68 | 70 | import com.google.devtools.build.lib.util.OS;
|
| 71 | +import com.google.devtools.build.lib.util.Pair; |
69 | 72 | import com.google.devtools.build.lib.vfs.PathFragment;
|
70 | 73 | import java.util.ArrayList;
|
71 | 74 | import java.util.List;
|
@@ -292,7 +295,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
|
292 | 295 | originalMainClass,
|
293 | 296 | filesToBuildBuilder,
|
294 | 297 | javaExecutable,
|
295 |
| - /* createCoverageMetadataJar= */ true); |
| 298 | + /* createCoverageMetadataJar= */ false); |
296 | 299 |
|
297 | 300 | Artifact oneVersionOutputArtifact = null;
|
298 | 301 | JavaConfiguration javaConfig = ruleContext.getFragment(JavaConfiguration.class);
|
@@ -364,6 +367,60 @@ public ConfiguredTarget create(RuleContext ruleContext)
|
364 | 367 |
|
365 | 368 | JavaInfo.Builder javaInfoBuilder = JavaInfo.Builder.create();
|
366 | 369 |
|
| 370 | + NestedSetBuilder<Pair<String, String>> coverageEnvironment = NestedSetBuilder.stableOrder(); |
| 371 | + NestedSetBuilder<Artifact> coverageSupportFiles = NestedSetBuilder.stableOrder(); |
| 372 | + if (ruleContext.getConfiguration().isCodeCoverageEnabled()) { |
| 373 | + |
| 374 | + // Create an artifact that contains the runfiles relative paths of the jars on the runtime |
| 375 | + // classpath. Using SourceManifestAction is the only reliable way to match the runfiles |
| 376 | + // creation code. |
| 377 | + Artifact runtimeClasspathArtifact = |
| 378 | + ruleContext.getUniqueDirectoryArtifact( |
| 379 | + "runtime_classpath_for_coverage", |
| 380 | + "runtime_classpath.txt", |
| 381 | + ruleContext.getBinOrGenfilesDirectory()); |
| 382 | + ruleContext.registerAction( |
| 383 | + new SourceManifestAction( |
| 384 | + SourceManifestAction.ManifestType.SOURCES_ONLY, |
| 385 | + ruleContext.getActionOwner(), |
| 386 | + runtimeClasspathArtifact, |
| 387 | + new Runfiles.Builder( |
| 388 | + ruleContext.getWorkspaceName(), |
| 389 | + ruleContext.getConfiguration().legacyExternalRunfiles()) |
| 390 | + // This matches the code below in collectDefaultRunfiles. |
| 391 | + .addTransitiveArtifactsWrappedInStableOrder(javaCommon.getRuntimeClasspath()) |
| 392 | + .build(), |
| 393 | + null, |
| 394 | + true)); |
| 395 | + filesToBuildBuilder.add(runtimeClasspathArtifact); |
| 396 | + |
| 397 | + // Pass the artifact through an environment variable in the coverage environment so it |
| 398 | + // can be read by the coverage collection script. |
| 399 | + coverageEnvironment.add( |
| 400 | + new Pair<>( |
| 401 | + "JAVA_RUNTIME_CLASSPATH_FOR_COVERAGE", runtimeClasspathArtifact.getExecPathString())); |
| 402 | + // Add the file to coverageSupportFiles so it ends up as an input for the test action |
| 403 | + // when coverage is enabled. |
| 404 | + coverageSupportFiles.add(runtimeClasspathArtifact); |
| 405 | + |
| 406 | + // Make single jar reachable from the coverage environment because it needs to be executed |
| 407 | + // by the coverage collection script. |
| 408 | + FilesToRunProvider singleJar = JavaToolchainProvider.from(ruleContext).getSingleJar(); |
| 409 | + coverageEnvironment.add( |
| 410 | + new Pair<>("SINGLE_JAR_TOOL", singleJar.getExecutable().getExecPathString())); |
| 411 | + coverageSupportFiles.addTransitive(singleJar.getFilesToRun()); |
| 412 | + } |
| 413 | + |
| 414 | + javaCommon.addTransitiveInfoProviders( |
| 415 | + builder, |
| 416 | + javaInfoBuilder, |
| 417 | + filesToBuild, |
| 418 | + classJar, |
| 419 | + coverageEnvironment.build(), |
| 420 | + coverageSupportFiles.build()); |
| 421 | + javaCommon.addGenJarsProvider( |
| 422 | + builder, javaInfoBuilder, outputs.genClass(), outputs.genSource()); |
| 423 | + |
367 | 424 | javaCommon.addTransitiveInfoProviders(builder, javaInfoBuilder, filesToBuild, classJar);
|
368 | 425 | javaCommon.addGenJarsProvider(
|
369 | 426 | builder, javaInfoBuilder, outputs.genClass(), outputs.genSource());
|
|
0 commit comments