Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incremental compilation with JDK16 using toolchains #16066

Merged
merged 14 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import org.gradle.util.Requires
import spock.lang.IgnoreIf
import spock.lang.Unroll

import static org.junit.Assume.assumeNotNull

class JavaCompileToolchainIntegrationTest extends AbstractPluginIntegrationTest {

@Unroll
Expand Down Expand Up @@ -337,11 +339,50 @@ public class Foo {
outputContains("task.targetCompatibility = $targetOut")

where:
source | target | sourceOut | targetOut
'9' | '10' | '1.9' | '1.10'
'9' | 'none' | '1.9' | '11'
'none' | 'none' | '11' | '11'
source | target | sourceOut | targetOut
'9' | '10' | '1.9' | '1.10'
'9' | 'none' | '1.9' | '11'
'none' | 'none' | '11' | '11'

}

def "can compile Java using different JDKs"() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use #javaVersion to see which iterations are skipped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get this information without explicitly embedding the variable in the test header (since Spock 2 I believe): https://ge.gradle.org/s/vhyeam6vqgtqi/tests
image

def jdk = AvailableJavaHomes.getJdk(javaVersion)
assumeNotNull(jdk)

buildFile << """
plugins {
id("java")
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(${jdk.javaVersion.majorVersion})
}
}
"""

file("src/main/java/Foo.java") << "public class Foo {}"

when:
runWithToolchainConfigured(jdk)

then:
outputDoesNotContain("Compiling with Java command line compiler")
outputContains("Compiling with toolchain '${jdk.javaHome.absolutePath}'.")
javaClassFile("Foo.class").exists()

where:
javaVersion << [
JavaVersion.VERSION_1_8,
JavaVersion.VERSION_1_9,
JavaVersion.VERSION_11,
JavaVersion.VERSION_12,
JavaVersion.VERSION_13,
JavaVersion.VERSION_14,
JavaVersion.VERSION_15,
JavaVersion.VERSION_16,
JavaVersion.VERSION_17
]
}

def runWithToolchainConfigured(Jvm jvm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -254,7 +255,7 @@ private <T extends CompileSpec> Compiler<T> createToolchainCompiler() {

private Provider<JavaCompiler> getCompilerTool() {
JavaToolchainSpec explicitToolchain = determineExplicitToolchain();
if(explicitToolchain == null) {
if (explicitToolchain == null) {
if(javaCompiler.isPresent()) {
return this.javaCompiler;
} else {
Expand Down Expand Up @@ -307,7 +308,6 @@ DefaultJavaCompileSpec createSpec() {
List<File> sourcesRoots = CompilationSourceDirs.inferSourceRoots((FileTreeInternal) getStableSources().getAsFileTree());
JavaModuleDetector javaModuleDetector = getJavaModuleDetector();
boolean isModule = JavaModuleDetector.isModuleSource(modularity.getInferModulePath().get(), sourcesRoots);
boolean toolchainCompatibleWithJava8 = isToolchainCompatibleWithJava8();

final DefaultJavaCompileSpec spec = createBaseSpec();

Expand All @@ -323,7 +323,7 @@ DefaultJavaCompileSpec createSpec() {
configureCompatibilityOptions(spec);
spec.setSourcesRoots(sourcesRoots);

if (!toolchainCompatibleWithJava8) {
if (!isToolchainCompatibleWithJava8()) {
spec.getCompileOptions().setHeaderOutputDirectory(null);
}
return spec;
Expand All @@ -333,6 +333,10 @@ private boolean isToolchainCompatibleWithJava8() {
return getCompilerTool().get().getMetadata().getLanguageVersion().canCompileOrRun(8);
}

private boolean isToolchainCompatibleWithJava16() {
return getCompilerTool().get().getMetadata().getLanguageVersion().canCompileOrRun(16);
}

@Input
JavaVersion getJavaVersion() {
return JavaVersion.toVersion(getCompilerTool().get().getMetadata().getLanguageVersion().asInt());
Expand All @@ -349,6 +353,10 @@ private DefaultJavaCompileSpec createBaseSpec() {
private void applyToolchain(ForkOptions forkOptions) {
final JavaInstallationMetadata metadata = getToolchain();
forkOptions.setJavaHome(metadata.getInstallationPath().getAsFile());
if (isToolchainCompatibleWithJava16()) {
// used by the incremental compiler plugin, see org.gradle.internal.compiler.java.ClassNameCollector
forkOptions.getJvmArgs().addAll(Arrays.asList("--add-opens", "jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be worthing looking to actually fixing this without the add opens. I pushed a small WIP here that I played around with, we might not need this workaround for this. WDYT?

2fc4346

}
}

@Nullable
Expand Down