Skip to content

Commit

Permalink
Use jacocorunner from java_toolchain in Bazel.
Browse files Browse the repository at this point in the history
Before jacocorunner from @bazel_tools/tools/jdk:jacocorunner was used, which is implemented with remote_java_tools. remote_java_tools uses select function that reconstructs which java_tools archive is actually used and then point to jacocorunner from the archive.

Since jacocorunner is already deployed in Bazel it is nicer to attach it to java_toolchain. Also it doesn't use fragile selects from remote_java_tools.

PiperOrigin-RevId: 340450013
  • Loading branch information
comius authored and copybara-github committed Nov 3, 2020
1 parent be134b3 commit 2936202
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.google.devtools.build.lib.bazel.rules.java;

import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
import static com.google.devtools.build.lib.packages.Type.BOOLEAN;

Expand Down Expand Up @@ -102,9 +101,6 @@ public Object getDefault(AttributeMap rule) {
return rule.get("create_executable", BOOLEAN);
}
}))
.add(
attr("$jacocorunner", LABEL)
.value(env.getToolsLabel("//tools/jdk:JacocoCoverageRunner")))
.addRequiredToolchains(CppRuleClasses.ccToolchainTypeAttribute(env))
.useToolchainTransition(true)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import com.google.devtools.build.lib.rules.java.JavaConfiguration;
import com.google.devtools.build.lib.rules.java.JavaConfiguration.OneVersionEnforcementLevel;
import com.google.devtools.build.lib.rules.java.JavaHelper;
import com.google.devtools.build.lib.rules.java.JavaInfo;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider;
import com.google.devtools.build.lib.rules.java.JavaSemantics;
import com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider;
Expand Down Expand Up @@ -611,23 +610,7 @@ public Iterable<String> getJvmFlags(
public String addCoverageSupport(JavaCompilationHelper helper, Artifact executable) {
// This method can be called only for *_binary/*_test targets.
Preconditions.checkNotNull(executable);
if (!helper.addCoverageSupport()) {
// Fallback to $jacocorunner attribute if no jacocorunner was found in the toolchain.

// Add the coverage runner to the list of dependencies when compiling in coverage mode.
TransitiveInfoCollection runnerTarget =
helper.getRuleContext().getPrerequisite("$jacocorunner");
if (JavaInfo.getProvider(JavaCompilationArgsProvider.class, runnerTarget) != null) {
helper.addLibrariesToAttributes(ImmutableList.of(runnerTarget));
} else {
helper
.getRuleContext()
.ruleError(
"this rule depends on "
+ helper.getRuleContext().attributes().get("$jacocorunner", BuildType.LABEL)
+ " which is not a java_library rule, or contains errors");
}
}
helper.addCoverageSupport();

// We do not add the instrumented jar to the runtime classpath, but provide it in the shell
// script via an environment variable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,19 @@ public BootClassPathInfo getBootclasspathOrDefault() {
}
}

public boolean addCoverageSupport() {
/** Adds coverage support from java_toolchain. */
public void addCoverageSupport() {
FilesToRunProvider jacocoRunner = javaToolchain.getJacocoRunner();
if (jacocoRunner == null) {
return false;
ruleContext.ruleError(
"jacocorunner not set in java_toolchain:" + javaToolchain.getToolchainLabel());
}
Artifact jacocoRunnerJar = jacocoRunner.getExecutable();
if (isStrict()) {
attributes.addDirectJar(jacocoRunnerJar);
}
attributes.addCompileTimeClassPathEntry(jacocoRunnerJar);
attributes.addRuntimeClassPathEntry(jacocoRunnerJar);
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
" extclasspath = [':extclasspath'],",
" javac = [':langtools'],",
" javabuilder = ['JavaBuilder_deploy.jar'],",
" jacocorunner = ':JacocoCoverage',",
" header_compiler = ['turbine_deploy.jar'],",
" header_compiler_direct = ['TurbineDirect_deploy.jar'],",
" singlejar = ['SingleJar_deploy.jar'],",
Expand All @@ -156,6 +157,7 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
" extclasspath = [':extclasspath'],",
" javac = [':langtools'],",
" javabuilder = ['JavaBuilder_deploy.jar'],",
" jacocorunner = ':JacocoCoverage',",
" header_compiler = ['turbine_deploy.jar'],",
" header_compiler_direct = ['TurbineDirect_deploy.jar'],",
" singlejar = ['SingleJar_deploy.jar'],",
Expand Down Expand Up @@ -191,7 +193,7 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
"filegroup(name='bootclasspath', srcs=['jdk/jre/lib/rt.jar'])",
"filegroup(name='extdir', srcs=glob(['jdk/jre/lib/ext/*']))",
"filegroup(name='java', srcs = ['jdk/jre/bin/java'])",
"filegroup(name='JacocoCoverage', srcs = [])",
"filegroup(name='JacocoCoverage', srcs = ['JacocoCoverage_deploy.jar'])",
"exports_files([",
" 'JavaBuilder_deploy.jar',",
" 'SingleJar_deploy.jar',",
Expand Down

0 comments on commit 2936202

Please sign in to comment.