Skip to content

Commit

Permalink
Add Kotlin version override (Kotlin#245)
Browse files Browse the repository at this point in the history
- Add a dependency constraint in the root project to allow overriding the Kotlin version.
- The Kotlin version is not overridden in the Benchmark Gradle plugin - this should use the version of Kotlin embedded into Gradle.
- Add a check to ensure that the version is overridden.
- add some comments to explain Kotlin version override
  • Loading branch information
adam-enko authored Jul 15, 2024
1 parent 0764551 commit 819f126
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
27 changes: 26 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt
import tasks.CheckReadmeTask

buildscript {
Expand All @@ -10,6 +11,16 @@ buildscript {

dependencies {
classpath(libs.kotlinx.teamInfraGradlePlugin)

String kotlinVersion = providers.gradleProperty("kotlin_version").orNull
if (kotlinVersion != null && !kotlinVersion.isBlank()) {
// In addition to overriding the Kotlin version in the Version Catalog,
// also enforce the KGP version using a dependency constraint.
// Constraints are stricter than Version Catalog.
constraints {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
}
}
}
}

Expand Down Expand Up @@ -54,8 +65,22 @@ afterEvaluate {
}
//endregion

String currentKgpVersion = KotlinPluginWrapperKt.getKotlinPluginVersion(project)
logger.info("Using Kotlin Gradle Plugin ${currentKgpVersion}")

String kotlinVersionOverride = providers.gradleProperty("kotlin_version").getOrNull()

if (kotlinVersionOverride != null) {
String versionCatalogKotlinVersion = libs.versions.kotlin.get()
if (kotlinVersionOverride != versionCatalogKotlinVersion) {
throw new IllegalStateException("Kotlin version in Version Catalog was not overridden. Expected:$kotlinVersionOverride, actual:$versionCatalogKotlinVersion.")
}
if (kotlinVersionOverride != currentKgpVersion) {
throw new IllegalStateException("Kotlin Gradle Plugin version was not overridden. Expected:$kotlinVersionOverride, actual:$currentKgpVersion.")
}
}

allprojects {
logger.info("Using Kotlin ${libs.versions.kotlin.get()} for project $it")
repositories {
KotlinCommunity.addDevRepositoryIfEnabled(delegate, project)
}
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]

# Note: Kotlin version can be overridden by passing `-Pkotlin_version=<version>`
kotlin = "1.9.21"
kotlinx-binaryCompatibilityValidator = "0.15.0-Beta.1"
kotlinx-teamInfra = "0.4.0-dev-81"
Expand Down
4 changes: 4 additions & 0 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ buildscript {

dependencies {
classpath(libs.kotlinx.teamInfraGradlePlugin)
// Note: unlike the root project, don't override KGP version in this project.
// Gradle plugins should only use the embedded-kotlin version.
// kotlinx-benchmark uses an external KGP the moment... but that should be fixed
// https://github.com/Kotlin/kotlinx-benchmark/issues/244
}
}

Expand Down
14 changes: 14 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ pluginManagement {
}
}

dependencyResolutionManagement {
versionCatalogs {
create("libs") {
String kotlinVersion = providers.gradleProperty("kotlin_version").orNull
if (kotlinVersion != null && !kotlinVersion.isBlank()) {
// Override the default Kotlin version.
// The only intended use-case is for testing dev Kotlin builds using kotlinx-benchmark.
// The Kotlin version should not be overridden during regular development.
version("kotlin", kotlinVersion)
}
}
}
}

rootProject.name = 'kotlinx-benchmark'

includeBuild("plugin")
Expand Down

0 comments on commit 819f126

Please sign in to comment.