Skip to content

Commit

Permalink
Simplify runtime version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
wmdietl committed Aug 6, 2024
1 parent 20a8d33 commit 353bd1a
Showing 1 changed file with 21 additions and 37 deletions.
58 changes: 21 additions & 37 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,26 @@ repositories {
mavenCentral()
}


def majorVersionToInt(majorVersionString) {
if (majorVersionString.endsWith("-ea")) {
majorVersionString = majorVersionString.substring(0, majorVersionString.length() - 3)
}
return Integer.valueOf(majorVersionString)
}

ext {
release = false

// On a Java 8 JVM, use error-prone javac and source/target 8.
// On a Java 9+ JVM, use the host javac, default source/target, and required module flags.
isJava8 = JavaVersion.current() == JavaVersion.VERSION_1_8
isJava14 = JavaVersion.current() == JavaVersion.VERSION_14
isJava15 = JavaVersion.current() == JavaVersion.VERSION_15
isJava16 = JavaVersion.current() == JavaVersion.VERSION_16
isJava17 = JavaVersion.current() == JavaVersion.VERSION_17
isJava18 = JavaVersion.current() == JavaVersion.VERSION_18
isJava19 = JavaVersion.current() == JavaVersion.VERSION_19
isJava20 = JavaVersion.current() == JavaVersion.VERSION_20
isJava21 = JavaVersion.current() == JavaVersion.VERSION_21

isJava21plus = isJava21
isJava20plus = isJava20 || isJava21plus
isJava19plus = isJava19 || isJava20plus
isJava18plus = isJava18 || isJava19plus
isJava17plus = isJava17 || isJava18plus
isJava16plus = isJava16 || isJava17plus
isJava15plus = isJava15 || isJava16plus
isJava14plus = isJava14 || isJava15plus
isJava11plus = JavaVersion.current() >= JavaVersion.VERSION_11

// As of 2023-09-23, delombok doesn't yet support JDK 22; see https://projectlombok.org/changelog .
skipDelombok = JavaVersion.current() > JavaVersion.VERSION_21

// The int corresponding to the major version of the current JVM.
currentRuntimeJavaVersion = majorVersionToInt(JavaVersion.current().getMajorVersion())

// As of 2024-08-06, delombok doesn't yet support JDK 23; see https://projectlombok.org/changelog .
skipDelombok = currentRuntimeJavaVersion >= 23

parentDir = file("${rootDir}/../").absolutePath

Expand Down Expand Up @@ -122,7 +115,7 @@ task installGitHooks(type: Copy, dependsOn: 'setLocalRepo') {
into localRepo + '/hooks'
}

if (isJava11plus) {
if (currentRuntimeJavaVersion >= 11) {
apply plugin: 'com.diffplug.spotless'
spotless {
// Resolve the Spotless plugin dependencies from the buildscript repositories rather than the
Expand Down Expand Up @@ -153,13 +146,6 @@ if (isJava11plus) {
}
}

def majorVersionToInt(majorVersionString) {
if (majorVersionString.endsWith("-ea")) {
majorVersionString = majorVersionString.substring(0, majorVersionString.length() - 3)
}
return Integer.valueOf(majorVersionString)
}

allprojects {
// Increment the minor version (second number) rather than just the patch
// level (third number) if:
Expand Down Expand Up @@ -250,7 +236,7 @@ allprojects {
]
}

if (isJava11plus) {
if (currentRuntimeJavaVersion >= 11) {
apply plugin: 'com.diffplug.spotless'
spotless {
// If you add any formatters to this block that require dependencies, then you must also
Expand All @@ -266,20 +252,19 @@ allprojects {
'**/build/**',
'*/dist/**',
]
if (!isJava14plus) {
if (currentRuntimeJavaVersion < 14) {
doNotFormat += ['**/*record*/']
}
if (!isJava16plus) {
if (currentRuntimeJavaVersion < 16) {
// TODO: directories should be renamed `-switchexpr` or some such,
// as they only contain examples for switch expressions, which were
// added in Java 14, not Java 17.
doNotFormat += ['**/java17/']
}
if (!isJava21plus) {
if (currentRuntimeJavaVersion < 21) {
doNotFormat += ['**/java21/']
}


format 'misc', {
// define the files to apply `misc` to
target '*.md', '*.tex', '.gitignore', 'Makefile'
Expand Down Expand Up @@ -395,14 +380,13 @@ allprojects {
tasks.withType(JavaCompile) { compilationTask ->
dependsOn(':installGitHooks')
String useJdkCompilerProp = project.getProperties().get('useJdkCompiler')
int runtimeCompiler = majorVersionToInt(JavaVersion.current().getMajorVersion())
int useJdkCompiler
if (useJdkCompilerProp == null) {
// If the property is not given, use the same version as the runtime.
useJdkCompiler = runtimeCompiler
useJdkCompiler = currentRuntimeJavaVersion
} else {
useJdkCompiler = majorVersionToInt(useJdkCompilerProp)
boolean useToolchains = (runtimeCompiler != useJdkCompiler)
boolean useToolchains = (currentRuntimeJavaVersion != useJdkCompiler)
if (!isJava8 && useToolchains) {
// This uses the requested Java compiler to compile all code.
// CI test test-cftests-junit-jdk21 runs the JUnit tests on the different JDK versions,
Expand Down

0 comments on commit 353bd1a

Please sign in to comment.