Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator;
import org.jacoco.core.runtime.WildcardMatcher;

/** Instruments compiled java classes using Jacoco instrumentation library. */
public final class JacocoInstrumentationProcessor {
Expand All @@ -48,14 +49,22 @@ public static JacocoInstrumentationProcessor create(List<String> args)
+ ": pathsForCoverageFile");
}

return new JacocoInstrumentationProcessor(args.getFirst());
String coverageIncludes = args.size() >= 2 ? args.get(1) : "";
String coverageExcludes = args.size() >= 3 ? args.get(2) : "";

return new JacocoInstrumentationProcessor(args.getFirst(), coverageIncludes, coverageExcludes);
}

private Path instrumentedClassesDirectory;
private final String coverageInformation;
private final WildcardMatcher includeMatcher;
private final WildcardMatcher excludeMatcher;

private JacocoInstrumentationProcessor(String coverageInfo) {
private JacocoInstrumentationProcessor(String coverageInfo, String coverageIncludes,
String coverageExcludes) {
this.coverageInformation = coverageInfo;
this.includeMatcher = new WildcardMatcher(coverageIncludes.isBlank() ? "*" : coverageIncludes);
this.excludeMatcher = new WildcardMatcher(coverageExcludes);
}

/**
Expand Down Expand Up @@ -110,6 +119,12 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
// it only once during recursive directory traversal while also mutating the directory.
Path instrumentedCopy = file;
Path absoluteUninstrumentedCopy = Path.of(file + ".uninstrumented");
Path relativeClassFile = root.relativize(file);
String className = relativeClassFile.toString().replace(".class", "");
if (!includeMatcher.matches(className) || excludeMatcher.matches(className)) {
return FileVisitResult.CONTINUE;
}

Path uninstrumentedCopy =
instrumentedClassesDirectory.resolve(root.relativize(absoluteUninstrumentedCopy));
Files.createDirectories(uninstrumentedCopy.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public final class JavaCompilationHelper {
private boolean enableJspecify = true;
private boolean enableDirectClasspath = true;
private final String execGroup;
private ImmutableList<String> coverageIncludes;
private ImmutableList<String> coverageExcludes;

public JavaCompilationHelper(
RuleContext ruleContext,
Expand Down Expand Up @@ -104,6 +106,14 @@ public void enableJspecify(boolean enableJspecify) {
this.enableJspecify = enableJspecify;
}

public void setCoverageIncludes(ImmutableList<String> coverageIncludes) {
this.coverageIncludes = coverageIncludes;
}

public void setCoverageExcludes(ImmutableList<String> coverageExcludes) {
this.coverageExcludes = coverageExcludes;
}

JavaTargetAttributes getAttributes() {
if (builtAttributes == null) {
builtAttributes = attributes.build();
Expand Down Expand Up @@ -247,6 +257,8 @@ && getJavaConfiguration().experimentalEnableJspecify()
builder.setTargetLabel(label);
Artifact coverageArtifact = maybeCreateCoverageArtifact(outputs.output());
builder.setCoverageArtifact(coverageArtifact);
builder.setCoverageIncludes(coverageIncludes);
builder.setCoverageExcludes(coverageExcludes);
BootClassPathInfo bootClassPathInfo = getBootclasspathOrDefault();
builder.setBootClassPath(bootClassPathInfo);
NestedSet<Artifact> classpath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public void extend(ExtraActionInfo.Builder builder, ImmutableList<String> argume
private final String execGroup;
private ImmutableSet<Artifact> additionalOutputs = ImmutableSet.of();
private Artifact coverageArtifact;
private ImmutableList<String> coverageIncludes;
private ImmutableList<String> coverageExcludes;
private ImmutableSet<Artifact> sourceFiles = ImmutableSet.of();
private ImmutableList<Artifact> sourceJars = ImmutableList.of();
private StrictDepsMode strictJavaDeps = StrictDepsMode.ERROR;
Expand Down Expand Up @@ -326,6 +328,8 @@ private CustomCommandLine buildParamFileContents(ImmutableList<String> javacOpts
if (coverageArtifact != null) {
result.add("--post_processor");
result.addExecPath(JACOCO_INSTRUMENTATION_PROCESSOR, coverageArtifact);
result.addDynamicString(String.join(":", coverageIncludes));
result.addDynamicString(String.join(":", coverageExcludes));
}
return result.build();
}
Expand Down Expand Up @@ -455,6 +459,18 @@ public JavaCompileActionBuilder setCoverageArtifact(Artifact coverageArtifact) {
return this;
}

@CanIgnoreReturnValue
public JavaCompileActionBuilder setCoverageIncludes(ImmutableList<String> coverageIncludes) {
this.coverageIncludes = coverageIncludes;
return this;
}

@CanIgnoreReturnValue
public JavaCompileActionBuilder setCoverageExcludes(ImmutableList<String> coverageExcludes) {
this.coverageExcludes = coverageExcludes;
return this;
}

@CanIgnoreReturnValue
public JavaCompileActionBuilder setTargetLabel(Label targetLabel) {
this.targetLabel = targetLabel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ public void createCompilationAction(
boolean enableJSpecify,
boolean enableDirectClasspath,
Sequence<?> additionalInputs,
Sequence<?> additionalOutputs)
Sequence<?> additionalOutputs,
Sequence<?> coverageIncludes,
Sequence<?> coverageExcludes)
throws EvalException,
TypeException,
RuleErrorException,
Expand Down Expand Up @@ -261,6 +263,10 @@ public void createCompilationAction(
Depset.cast(javaBuilderJvmFlags, String.class, "javabuilder_jvm_flags"));
compilationHelper.enableJspecify(enableJSpecify);
compilationHelper.enableDirectClasspath(enableDirectClasspath);
compilationHelper.setCoverageIncludes(
Sequence.cast(coverageIncludes, String.class, "coverage_includes").getImmutableList());
compilationHelper.setCoverageExcludes(
Sequence.cast(coverageExcludes, String.class, "coverage_excludes").getImmutableList());
compilationHelper.createCompileAction(outputs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ void createHeaderCompilationAction(
@Param(name = "enable_direct_classpath", defaultValue = "True", named = true),
@Param(name = "additional_inputs", defaultValue = "[]", named = true),
@Param(name = "additional_outputs", defaultValue = "[]", named = true),
@Param(name = "coverage_includes", defaultValue = "[]", named = true),
@Param(name = "coverage_excludes", defaultValue = "[]", named = true)
})
void createCompilationAction(
StarlarkRuleContextT ctx,
Expand Down Expand Up @@ -553,7 +555,9 @@ void createCompilationAction(
boolean enableJSpecify,
boolean enableDirectClasspath,
Sequence<?> additionalInputs,
Sequence<?> additionalOutputs)
Sequence<?> additionalOutputs,
Sequence<?> coverageIncludes,
Sequence<?> coverageExcludes)
throws EvalException,
TypeException,
RuleErrorException,
Expand Down