2021-08-24 06:33:36 -04:00
|
|
|
|
|
|
|
import org.gradle.internal.jvm.JavaInfo
|
2021-08-23 16:22:19 -04:00
|
|
|
import org.gradle.internal.jvm.Jvm
|
2021-08-24 06:33:36 -04:00
|
|
|
import org.gradle.internal.jvm.inspection.JvmInstallationMetadata
|
2021-08-23 16:22:19 -04:00
|
|
|
import org.gradle.internal.jvm.inspection.JvmMetadataDetector
|
|
|
|
|
2020-07-21 03:19:38 -04:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This adds support for compiling and testing against a different Java runtime.
|
2021-08-24 06:33:36 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
JavaInfo jvmGradle = Jvm.current();
|
|
|
|
JavaInfo jvmCurrent = {
|
2021-08-20 02:18:33 -04:00
|
|
|
def runtimeJavaHome = propertyOrDefault("runtime.java.home", System.getenv('RUNTIME_JAVA_HOME'))
|
2021-08-24 06:33:36 -04:00
|
|
|
if (runtimeJavaHome != null) {
|
|
|
|
return Jvm.forHome(file(runtimeJavaHome))
|
2021-08-20 02:18:33 -04:00
|
|
|
} else {
|
2021-08-24 06:33:36 -04:00
|
|
|
return jvmGradle
|
2021-08-20 02:18:33 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-08-24 06:33:36 -04:00
|
|
|
JvmMetadataDetector jvmDetector = project.services.get(JvmMetadataDetector)
|
2021-08-20 02:18:33 -04:00
|
|
|
|
2021-08-24 06:33:36 -04:00
|
|
|
if (jvmGradle != jvmCurrent) {
|
2021-08-20 02:18:33 -04:00
|
|
|
configure(rootProject) {
|
|
|
|
task altJvmWarning() {
|
|
|
|
doFirst {
|
2021-08-24 06:33:36 -04:00
|
|
|
|
|
|
|
def jvmInfo = { JavaInfo javaInfo ->
|
|
|
|
JvmInstallationMetadata jvmMetadata = jvmDetector.getMetadata(javaInfo.javaHome)
|
|
|
|
return "${jvmMetadata.languageVersion} (${jvmMetadata.displayName} ${jvmMetadata.runtimeVersion}, home at: ${jvmMetadata.javaHome})"
|
|
|
|
}
|
|
|
|
|
2021-08-20 02:18:33 -04:00
|
|
|
logger.warn("""NOTE: Alternative java toolchain will be used for compilation and tests:
|
2021-08-24 06:33:36 -04:00
|
|
|
Project will use ${jvmInfo(jvmCurrent)}
|
|
|
|
Gradle runs with ${jvmInfo(jvmGradle)}
|
2021-08-20 02:18:33 -04:00
|
|
|
""")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allprojects {
|
|
|
|
// Any tests
|
|
|
|
tasks.withType(Test) {
|
|
|
|
dependsOn ":altJvmWarning"
|
2021-08-24 06:33:36 -04:00
|
|
|
executable = jvmCurrent.javaExecutable
|
2021-08-20 02:18:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Any javac compilation tasks
|
|
|
|
tasks.withType(JavaCompile) {
|
|
|
|
dependsOn ":altJvmWarning"
|
|
|
|
options.fork = true
|
2021-08-24 06:33:36 -04:00
|
|
|
options.forkOptions.javaHome = jvmCurrent.javaHome
|
2021-08-20 02:18:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Javadoc compilation.
|
2021-08-24 06:33:36 -04:00
|
|
|
def javadocExecutable = jvmCurrent.javadocExecutable
|
2021-08-20 02:18:33 -04:00
|
|
|
tasks.matching { it.name == "renderJavadoc" || it.name == "renderSiteJavadoc" }.all {
|
|
|
|
dependsOn ":altJvmWarning"
|
2021-08-24 06:33:36 -04:00
|
|
|
executable = javadocExecutable.toString()
|
2021-08-20 02:18:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 06:33:36 -04:00
|
|
|
|
|
|
|
// Set up root project's properties.
|
2021-09-16 02:49:26 -04:00
|
|
|
rootProject.ext.runtimeJavaExecutable = jvmCurrent.javaExecutable
|
2021-08-24 06:33:36 -04:00
|
|
|
rootProject.ext.runtimeJavaHome = jvmCurrent.javaHome
|
|
|
|
rootProject.ext.runtimeJavaVersion = jvmDetector.getMetadata(jvmCurrent.javaHome).languageVersion
|
2022-01-07 13:19:46 -05:00
|
|
|
rootProject.ext.usesAltJvm = (jvmGradle != jvmCurrent);
|
|
|
|
|