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

Add Gradle toolchain support #262

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ local buildNativeJobs: Mapping<String, BuildNativeJob> = new {

local gradleCheckJobs: Mapping<String, GradleCheckJob> = new {
["gradle-check-jdk11"] {
javaVersion = "11.0"
javaVersion = "11"
isRelease = false
}
["gradle-check-jdk17"] {
javaVersion = "17.0"
javaVersion = "17"
isRelease = false
}
}
Expand Down
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ jobs:
steps:
- checkout
- run:
command: ./gradlew --info --stacktrace check
command: ./gradlew --info --stacktrace check -DPKL_JVM_VERSION=11
name: gradle check
- run:
command: |-
Expand All @@ -601,7 +601,7 @@ jobs:
steps:
- checkout
- run:
command: ./gradlew --info --stacktrace check
command: ./gradlew --info --stacktrace check -DPKL_JVM_VERSION=17
name: gradle check
- run:
command: |-
Expand All @@ -614,7 +614,7 @@ jobs:
environment:
LANG: en_US.UTF-8
docker:
- image: cimg/openjdk:17.0
- image: cimg/openjdk:11.0
bench:
steps:
- checkout
Expand Down
6 changes: 3 additions & 3 deletions .circleci/jobs/GradleCheckJob.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ extends "GradleJob.pkl"

import "package://pkg.pkl-lang.org/pkl-pantry/[email protected]#/Config.pkl"

javaVersion: "11.0"|"17.0"
javaVersion: "11"|"17"

steps {
new Config.RunStep {
name = "gradle check"
command = "./gradlew \(module.gradleArgs) check"
command = "./gradlew \(module.gradleArgs) check -DPKL_JVM_VERSION=\(javaVersion)"
}
}

job {
docker {
new {
image = "cimg/openjdk:\(javaVersion)"
image = "cimg/openjdk:11.0"
}
}
}
2 changes: 0 additions & 2 deletions DEVELOPMENT.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
:uri-gng: https://gng.dsun.org
:uri-jenv: https://www.jenv.be
:uri-intellij: https://www.jetbrains.com/idea/download/
:uri-jdk: https://adoptopenjdk.net/releases.html

== Setup

. (mandatory) Install {uri-jdk}[OpenJDK 11 HotSpot] (as long as we support JDK11)
. (recommended) Install {uri-intellij}[IntelliJ IDEA 2023.x] +
To import the project into IntelliJ, go to File->Open and select the project's root directory.
If the project is opened but not imported, look for a popup in the lower right corner
Expand Down
15 changes: 1 addition & 14 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.jetbrains.kotlin.config.JvmTarget

plugins {
`kotlin-dsl`
}
Expand All @@ -17,17 +15,6 @@ dependencies {
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlin {
target {
compilations.configureEach {
kotlinOptions {
jvmTarget = "11"
}
}
}
jvmToolchain(11)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would it make sense to configure the global toolchain here as well, for consistency, via the java { toolchain { } } block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The build.gradle.kts is only responsible for the code within buildSrc, right?
Since we are using Kotlin only here, why should we set the java.toolchain {}? 🤔

Or what do you mean by that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you are correct that this build script is responsible for buildSrc.

In Gradle, the java plugin is always applied to JVM-based projects, even if the project uses some other language as the core of its implementation. Therefore, for consistency, it is considered idiomatic to use the java plugin methods to configure something first, and use language-specific methods to adjust the configuration, if necessary.

In the toolchain case, it is specified explicitly in Kotlin docs (see here) that the toolchain version in the java block will be used by Kotlin compilation tasks, therefore, it would be conventional to use the java block to set it up.

(BTW, it's curious that the docs state that setting the toolchain in the kotlin extension will also configure it for JavaCompile tasks; this is another reason to set it in the java block, since it is the built-in way to configure the toolchain, and their effects would be similar anyway)

}
14 changes: 14 additions & 0 deletions buildSrc/src/main/kotlin/JvmToolchain.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.jvm.toolchain.JvmVendorSpec
import org.gradle.api.Project

private const val JDK_VERSION = 11

val jvmToolchainVersion: JavaLanguageVersion
get() {
val jvmVersion = System.getProperty("PKL_JVM_VERSION", JDK_VERSION.toString()).toInt()
StefMa marked this conversation as resolved.
Show resolved Hide resolved
StefMa marked this conversation as resolved.
Show resolved Hide resolved
return JavaLanguageVersion.of(jvmVersion)
}

val jvmToolchainVendor: JvmVendorSpec
get() = JvmVendorSpec.ADOPTIUM
7 changes: 0 additions & 7 deletions buildSrc/src/main/kotlin/pklAllProjects.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,8 @@ configurations {
}
}

plugins.withType(JavaPlugin::class).configureEach {
val java = project.extensions.getByType<JavaPluginExtension>()
java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
freeCompilerArgs = freeCompilerArgs + listOf("-Xjsr305=strict", "-Xjvm-default=all")
}
}
Expand Down
11 changes: 9 additions & 2 deletions buildSrc/src/main/kotlin/pklFatJar.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ val relocations = mapOf(
// pkl-doc dependencies
"org.commonmark." to "org.pkl.thirdparty.commonmark.",
"org.jetbrains." to "org.pkl.thirdparty.jetbrains.",

// pkl-config-java dependencies
"io.leangen.geantyref." to "org.pkl.thirdparty.geantyref.",

Expand All @@ -54,6 +54,13 @@ val relocations = mapOf(
"com.squareup.kotlinpoet." to "org.pkl.thirdparty.kotlinpoet.",
)

java {
toolchain {
languageVersion.set(jvmToolchainVersion)
vendor.set(jvmToolchainVendor)
}
}

val nonRelocations = listOf("com/oracle/truffle/")

tasks.shadowJar {
Expand Down Expand Up @@ -117,7 +124,7 @@ val validateFatJar by tasks.registering {
val path = fileDetails.relativePath.pathString
if (!(fileDetails.isDirectory ||
path.startsWith("org/pkl/") ||
path.startsWith("META-INF/") ||
path.startsWith("META-INF/") ||
nonRelocations.any { path.startsWith(it) })) {
// don't throw exception inside `visit`
// as this gives a misleading "Could not expand ZIP" error message
Expand Down
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/pklJavaLibrary.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ val libs = the<LibrariesForLibs>()
java {
withSourcesJar() // creates `sourcesJar` task
withJavadocJar()

toolchain {
languageVersion.set(jvmToolchainVersion)
vendor.set(jvmToolchainVendor)
}
}

artifacts {
Expand Down
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ tasks.compileKotlin {
enabled = true // disabled by pklJavaLibrary
}

kotlin.jvmToolchain {
languageVersion.set(jvmToolchainVersion)
vendor.set(jvmToolchainVendor)
}
Comment on lines +27 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be necessary because this plugin applies pklJavaLibrary, and the latter already applies the toolchain in the java {} block.


spotless {
kotlin {
ktfmt(libs.versions.ktfmt.get()).googleStyle()
Expand Down
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/pklKotlinTest.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ dependencies {
testRuntimeOnly(buildInfo.libs.findLibrary("junitEngine").get())
}

kotlin.jvmToolchain {
languageVersion.set(jvmToolchainVersion)
vendor.set(jvmToolchainVendor)
}

tasks.withType<Test>().configureEach {
val testTask = this

Expand Down
9 changes: 4 additions & 5 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ pluginManagement {
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}

@Suppress("UnstableApiUsage")
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}

val javaVersion = JavaVersion.current()
require(javaVersion.isJava11Compatible) {
"Project requires Java 11 or higher, but found ${javaVersion.majorVersion}."
}

if (gradle.startParameter.taskNames.contains("updateDependencyLocks") ||
gradle.startParameter.taskNames.contains("uDL")
) {
Expand Down