Use JavaInfo instead of toolchains. Internal but works and is free of toolchain's quirks.

This commit is contained in:
Dawid Weiss 2021-08-24 12:33:36 +02:00
parent 68cf86ba35
commit 2b0378cd4a
4 changed files with 51 additions and 88 deletions

View File

@ -1,3 +1,5 @@
import org.gradle.internal.jvm.Jvm
import javax.annotation.Nullable import javax.annotation.Nullable
/* /*
@ -287,7 +289,7 @@ class RenderJavadocTask extends DefaultTask {
@Input @Input
@Optional @Optional
Property<String> luceneDocUrl = project.objects.property(String) final Property<String> luceneDocUrl = project.objects.property(String)
// default is to require full javadocs // default is to require full javadocs
@Input @Input
@ -305,10 +307,10 @@ class RenderJavadocTask extends DefaultTask {
@Optional @Optional
ListProperty<String> extraOpts = project.objects.listProperty(String) ListProperty<String> extraOpts = project.objects.listProperty(String)
@Nullable
@Optional @Optional
@Input @Input
def executable final Property<String> executable = project.objects.property(String).convention(
project.provider { Jvm.current().javadocExecutable.toString() })
@Input @Input
def taskResources def taskResources
@ -443,17 +445,8 @@ class RenderJavadocTask extends DefaultTask {
} }
}) })
def javadocCmd = { def javadocCmd = project.file(executable.get())
if (executable == null) { logger.lifecycle("Javadoc executable used: ${javadocCmd}")
//JavaInstallationRegistry registry = project.extensions.getByType(JavaInstallationRegistry)
//JavaInstallation currentJvm = registry.installationForCurrentVirtualMachine.get()
return currentJvm.jdk.get().javadocExecutable.asFile
} else {
return project.file(executable)
}
}()
logger.info("Javadoc executable used: ${javadocCmd}")
project.quietExec { project.quietExec {
executable javadocCmd executable javadocCmd

View File

@ -53,7 +53,14 @@ configure(rootProject) {
"org.gradle.workers.max=${maxWorkers}", "org.gradle.workers.max=${maxWorkers}",
"", "",
"# Maximum number of test JVMs forked per test task.", "# Maximum number of test JVMs forked per test task.",
"tests.jvms=${testsJvms}" "tests.jvms=${testsJvms}",
"",
"# Disable auto JVM provisioning",
"org.gradle.java.installations.auto-download=false",
"",
"# Set these to enable automatic JVM location discovery.",
"# org.gradle.java.installations.fromEnv=JDK11,JDK12,JDK13,JDK14,JDK15,JDK16,JDK17",
"# org.gradle.java.installations.paths=(custom paths)",
].join("\n"), "UTF-8") ].join("\n"), "UTF-8")
logger.log(LogLevel.WARN, "\nIMPORTANT. This is the first time you ran the build. " + logger.log(LogLevel.WARN, "\nIMPORTANT. This is the first time you ran the build. " +

View File

@ -1,7 +1,8 @@
import org.gradle.internal.jvm.JavaInfo
import org.gradle.internal.jvm.Jvm import org.gradle.internal.jvm.Jvm
import org.gradle.internal.jvm.inspection.JvmInstallationMetadata
import org.gradle.internal.jvm.inspection.JvmMetadataDetector import org.gradle.internal.jvm.inspection.JvmMetadataDetector
import org.gradle.jvm.toolchain.internal.JavaInstallationRegistry
import org.gradle.jvm.toolchain.internal.JavaToolchainQueryService
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
@ -21,104 +22,68 @@ import org.gradle.jvm.toolchain.internal.JavaToolchainQueryService
*/ */
// This adds support for compiling and testing against a different Java runtime. // This adds support for compiling and testing against a different Java runtime.
// This is the only way to build against JVMs not yet supported by Gradle itself. //
// I failed to set it up leveraging Gradle's toolchains because
// a toolchain spec is not flexible enough to provide an exact location of the JVM to be used;
// if you have two identical JVM lang. versions in auto-discovered JVMs, an arbitrary one is used (?).
// This situation is not uncommon when debugging low-level stuff (hand-compiled JVM binaries).
//
// The code below is a workaround using internal gradle classes. It may stop working in the future
// but for now it's fine.
// Set up root project's property. JavaInfo jvmGradle = Jvm.current();
rootProject.ext.runtimeJava = null JavaInfo jvmCurrent = {
rootProject.ext.runtimeJavaVersion = null
def home = Jvm.current().javaHome
def otherHome = file("c:\\Tools\\java\\jdk13")
def spec = new org.gradle.jvm.toolchain.internal.SpecificInstallationToolchainSpec(objects, otherHome)
JavaInstallationRegistry installationRegistry = project.services.get(JavaInstallationRegistry)
JvmMetadataDetector jvmDetector = project.services.get(JvmMetadataDetector)
JavaToolchainService toolchainService = project.extensions.getByType(JavaToolchainService)
installationRegistry.listInstallations().each {inst ->
println "# " + inst.source + " " + inst.location
println " " + jvmDetector.getMetadata(inst.location).languageVersion
}
JavaVersion runtimeVersion = Jvm.current().javaVersion
configure(project(":lucene:core"), {
plugins.withType(JavaPlugin) {
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}
task foo {
doFirst {
logger.lifecycle("runtime.version: " + runtimeVersion)
logger.lifecycle("toolchain: " + java.toolchain.displayName + " " + project.path)
logger.lifecycle("compiler: " + toolchainService.launcherFor(java.toolchain).get().executablePath)
logger.lifecycle("compiler2: " +
toolchainService.launcherFor(spec).get().executablePath)
}
}
}
})
/*
JavaInstallationRegistry registry = extensions.getByType(JavaInstallationRegistry)
JavaInstallation currentJvm = registry.installationForCurrentVirtualMachine.get()
JavaInstallation altJvm = {
def runtimeJavaHome = propertyOrDefault("runtime.java.home", System.getenv('RUNTIME_JAVA_HOME')) def runtimeJavaHome = propertyOrDefault("runtime.java.home", System.getenv('RUNTIME_JAVA_HOME'))
if (!runtimeJavaHome) { if (runtimeJavaHome != null) {
return currentJvm return Jvm.forHome(file(runtimeJavaHome))
} else { } else {
return registry.installationForDirectory( return jvmGradle
layout.projectDirectory.dir(runtimeJavaHome)).get()
} }
}() }()
// Set up root project's property. JvmMetadataDetector jvmDetector = project.services.get(JvmMetadataDetector)
rootProject.ext.runtimeJava = altJvm
rootProject.ext.runtimeJavaVersion = altJvm.javaVersion
if (!currentJvm.javaExecutable.equals(altJvm.javaExecutable)) {
// Set up java toolchain tasks to use the alternative Java.
// This is a related Gradle issue for the future:
// https://github.com/gradle/gradle/issues/1652
if (jvmGradle != jvmCurrent) {
configure(rootProject) { configure(rootProject) {
task altJvmWarning() { task altJvmWarning() {
doFirst { doFirst {
def jvmInfo = { JavaInfo javaInfo ->
JvmInstallationMetadata jvmMetadata = jvmDetector.getMetadata(javaInfo.javaHome)
return "${jvmMetadata.languageVersion} (${jvmMetadata.displayName} ${jvmMetadata.runtimeVersion}, home at: ${jvmMetadata.javaHome})"
}
logger.warn("""NOTE: Alternative java toolchain will be used for compilation and tests: logger.warn("""NOTE: Alternative java toolchain will be used for compilation and tests:
Project will use Java ${altJvm.javaVersion} from: ${altJvm.installationDirectory} Project will use ${jvmInfo(jvmCurrent)}
Gradle runs with Java ${currentJvm.javaVersion} from: ${currentJvm.installationDirectory} Gradle runs with ${jvmInfo(jvmGradle)}
""") """)
} }
} }
} }
// Set up toolchain-dependent tasks to use the alternative JVM.
allprojects { allprojects {
// Any tests // Any tests
tasks.withType(Test) { tasks.withType(Test) {
dependsOn ":altJvmWarning" dependsOn ":altJvmWarning"
executable = altJvm.javaExecutable executable = jvmCurrent.javaExecutable
} }
// Any javac compilation tasks // Any javac compilation tasks
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
dependsOn ":altJvmWarning" dependsOn ":altJvmWarning"
options.fork = true options.fork = true
options.forkOptions.javaHome = altJvm.installationDirectory.asFile options.forkOptions.javaHome = jvmCurrent.javaHome
} }
// Javadoc compilation. // Javadoc compilation.
def javadocExecutable = altJvm.jdk.get().javadocExecutable.asFile def javadocExecutable = jvmCurrent.javadocExecutable
tasks.matching { it.name == "renderJavadoc" || it.name == "renderSiteJavadoc" }.all { tasks.matching { it.name == "renderJavadoc" || it.name == "renderSiteJavadoc" }.all {
dependsOn ":altJvmWarning" dependsOn ":altJvmWarning"
executable = javadocExecutable executable = javadocExecutable.toString()
} }
} }
} }
*/
// Set up root project's properties.
rootProject.ext.runtimeJavaHome = jvmCurrent.javaHome
rootProject.ext.runtimeJavaVersion = jvmDetector.getMetadata(jvmCurrent.javaHome).languageVersion

View File

@ -41,11 +41,9 @@ library {
} }
tasks.withType(CppCompile).configureEach { tasks.withType(CppCompile).configureEach {
// TODO: this should use the default java toolchain, not runtime's def javaHome = rootProject.ext.runtimeJavaHome
def javaHome = null // rootProject.ext.runtimeJava.getInstallationDirectory().getAsFile().getPath()
// Assume standard openjdk layout. This means only one architecture-specific include folder // Assume standard openjdk layout. This means only one architecture-specific include folder is present.
// is present.
systemIncludes.from file("${javaHome}/include") systemIncludes.from file("${javaHome}/include")
for (def path : [file("${javaHome}/include/win32")]) { for (def path : [file("${javaHome}/include/win32")]) {