Skip to content

Remove deprecated APIs usage and enable warning-as-errors for build scripts #2925

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

Merged
merged 10 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ publishing {
forEach { pub ->
pub as DefaultMavenPublication
pub.unsetModuleDescriptorGenerator()
tasks.matching { it.name == "generateMetadataFileFor${pub.name.capitalize()}Publication" }.all {
onlyIf { false }

tasks.configureEach {
if (name == "generateMetadataFileFor${pub.name.capitalizeCompat()}Publication") {
onlyIf { false }
}
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import kotlinx.validation.*
import org.jetbrains.dokka.gradle.*
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion

plugins {
base
Expand Down Expand Up @@ -159,10 +160,8 @@ tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstall
args.add("--ignore-engines")
}

// == compiler version setup ==
gradle.taskGraph.whenReady {
println("Using Kotlin compiler version: ${org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION}")
}
// == KGP version setup ==
logger.warn("Project is using Kotlin Gradle plugin version: ${project.getKotlinPluginVersion()}")

// == projects lists and flags ==
// getters are required because of variable lazy initialization in Gradle
Expand Down
6 changes: 6 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ repositories {
mavenLocal()
}

kotlin {
compilerOptions {
allWarningsAsErrors = true
}
}

dependencies {
implementation(libs.gradlePlugin.kotlin)
implementation(libs.gradlePlugin.kover)
Expand Down
1 change: 1 addition & 0 deletions buildSrc/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.kotlin.dsl.allWarningsAsErrors=true
59 changes: 36 additions & 23 deletions buildSrc/src/main/kotlin/Java9Modularity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ import org.jetbrains.kotlin.gradle.*
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.*
import org.jetbrains.kotlin.gradle.targets.jvm.*
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
import org.jetbrains.kotlin.gradle.utils.*
import org.jetbrains.kotlin.tooling.core.*
import java.io.*
import kotlin.reflect.*
import kotlin.reflect.full.*

object Java9Modularity {
private val KotlinProjectExtension.targets: Iterable<KotlinTarget>
get() = when (this) {
is KotlinSingleTargetExtension<*> -> listOf(this.target)
is KotlinMultiplatformExtension -> targets
else -> error("Unexpected 'kotlin' extension $this")
}


@JvmStatic
@JvmOverloads
Expand All @@ -50,16 +55,19 @@ object Java9Modularity {
}

target.compilations.forEach { compilation ->
val compileKotlinTask = compilation.compileKotlinTask as KotlinCompile
@Suppress("UNCHECKED_CAST")
val compileKotlinTask = compilation.compileTaskProvider as TaskProvider<KotlinCompile>
val defaultSourceSet = compilation.defaultSourceSet

// derive the names of the source set and compile module task
val sourceSetName = defaultSourceSet.name + "Module"

kotlin.sourceSets.create(sourceSetName) {
val sourceFile = this.kotlin.find { it.name == "module-info.java" }
val targetDirectory = compileKotlinTask.destinationDirectory.map {
it.dir("../${it.asFile.name}Module")
val targetDirectory = compileKotlinTask.flatMap { task ->
task.destinationDirectory.map {
it.dir("../${it.asFile.name}Module")
}
}

// only configure the compilation if necessary
Expand Down Expand Up @@ -110,7 +118,7 @@ object Java9Modularity {
* but it currently won't compile to a module-info.class file.
*/
private fun Project.registerVerifyModuleTask(
compileTask: KotlinCompile,
compileTask: TaskProvider<KotlinCompile>,
sourceFile: File
): TaskProvider<out KotlinJvmCompile> {
apply<KotlinApiPlugin>()
Expand All @@ -120,28 +128,28 @@ object Java9Modularity {
val kotlinApiPlugin = plugins.getPlugin(KotlinApiPlugin::class)
val verifyModuleTask = kotlinApiPlugin.registerKotlinJvmCompileTask(
verifyModuleTaskName,
compileTask.compilerOptions.moduleName.get()
compilerOptions = compileTask.get().compilerOptions,
explicitApiMode = provider { ExplicitApiMode.Disabled }
)
verifyModuleTask {
group = VERIFICATION_GROUP
description = "Verify Kotlin sources for JPMS problems"
libraries.from(compileTask.libraries)
source(compileTask.sources)
source(compileTask.javaSources)
libraries.from(compileTask.map { it.libraries })
source(compileTask.map { it.sources })
source(compileTask.map { it.javaSources })
// part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
@Suppress("INVISIBLE_MEMBER")
source(compileTask.scriptSources)
source(compileTask.map {
@Suppress("INVISIBLE_MEMBER")
it.scriptSources
})
source(sourceFile)
destinationDirectory.set(temporaryDir)
multiPlatformEnabled.set(compileTask.multiPlatformEnabled)
multiPlatformEnabled.set(compileTask.get().multiPlatformEnabled)
compilerOptions {
jvmTarget.set(JvmTarget.JVM_9)
// To support LV override when set in aggregate builds
languageVersion.set(compileTask.compilerOptions.languageVersion)
freeCompilerArgs.addAll(
listOf("-Xjdk-release=9", "-Xsuppress-version-warnings", "-Xexpect-actual-classes")
)
optIn.addAll(compileTask.compilerOptions.optIn)
}
// work-around for https://youtrack.jetbrains.com/issue/KT-60583
inputs.files(
Expand All @@ -162,18 +170,21 @@ object Java9Modularity {
.declaredMemberProperties
.find { it.name == "ownModuleName" }
?.get(this) as? Property<String>
ownModuleNameProp?.set(compileTask.compilerOptions.moduleName)
ownModuleNameProp?.set(compileTask.flatMap { it.compilerOptions.moduleName})
}

val taskKotlinLanguageVersion = compilerOptions.languageVersion.orElse(KotlinVersion.DEFAULT)
@OptIn(InternalKotlinGradlePluginApi::class)
if (taskKotlinLanguageVersion.get() < KotlinVersion.KOTLIN_2_0) {
// part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
@Suppress("INVISIBLE_MEMBER")
commonSourceSet.from(compileTask.commonSourceSet)
commonSourceSet.from(compileTask.map {
@Suppress("INVISIBLE_MEMBER")
it.commonSourceSet
})
} else {
multiplatformStructure.refinesEdges.set(compileTask.multiplatformStructure.refinesEdges)
multiplatformStructure.fragments.set(compileTask.multiplatformStructure.fragments)
multiplatformStructure.refinesEdges.set(compileTask.flatMap { it.multiplatformStructure.refinesEdges })
multiplatformStructure.fragments.set(compileTask.flatMap { it.multiplatformStructure.fragments })
}
// part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
// and work-around for https://youtrack.jetbrains.com/issue/KT-60582
Expand All @@ -183,7 +194,7 @@ object Java9Modularity {
}

private fun Project.registerCompileModuleTask(
compileTask: KotlinCompile,
compileTask: TaskProvider<KotlinCompile>,
sourceFile: File,
targetDirectory: Provider<out Directory>
) = tasks.register("${compileTask.name}Module", JavaCompile::class) {
Expand All @@ -203,10 +214,12 @@ object Java9Modularity {

options.compilerArgumentProviders.add(object : CommandLineArgumentProvider {
@get:CompileClasspath
val compileClasspath = compileTask.libraries
val compileClasspath = objects.fileCollection().from(
compileTask.map { it.libraries }
)

@get:CompileClasspath
val compiledClasses = compileTask.destinationDirectory
val compiledClasses = compileTask.flatMap { it.destinationDirectory }

@get:Input
val moduleName = sourceFile
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/dokka-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import org.jetbrains.dokka.gradle.*
import java.net.URL
import java.net.URI

/*
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Expand Down Expand Up @@ -71,7 +71,7 @@ tasks.withType<DokkaTaskPartial>().named("dokkaHtmlPartial") {
sourceLink {
localDirectory.set(rootDir)

remoteUrl.set(URL("https://github.com/Kotlin/kotlinx.serialization/tree/master"))
remoteUrl.set(URI("https://github.com/Kotlin/kotlinx.serialization/tree/master").toURL())
remoteLineSuffix.set("#L")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ kotlin {
androidNativeX64()

// Deprecated, but not removed
@Suppress("DEPRECATION")
linuxArm32Hfp()
}

Expand Down
10 changes: 3 additions & 7 deletions buildSrc/src/main/kotlin/source-sets-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

@file:OptIn(ExperimentalWasmDsl::class)

import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.*
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.native.tasks.*
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.testing.*

plugins {
kotlin("multiplatform")
Expand All @@ -28,6 +21,7 @@ kotlin {
explicitApi()

jvm {
@Suppress("DEPRECATION") // Migrate on Kotlin 2.1.20 update
withJava()
@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
Expand All @@ -53,10 +47,12 @@ kotlin {
}
}

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
nodejs()
}

@OptIn(ExperimentalWasmDsl::class)
wasmWasi {
nodejs()
}
Expand Down
8 changes: 4 additions & 4 deletions buildSrc/src/main/kotlin/teamcity-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

import org.gradle.kotlin.dsl.*

val teamcitySuffix = findProperty("teamcitySuffix")?.toString()
if (teamcityInteractionEnabled && hasProperty("teamcity") && !propertyIsTrue("build_snapshot_train")) {
// Tell teamcity about version number
val postfix = if (teamcitySuffix == null) "" else " ($teamcitySuffix)"
println("##teamcity[buildNumber '${project.version}${postfix}']")

gradle.taskGraph.beforeTask {
println("##teamcity[progressMessage 'Gradle: ${path}:${name}']")
tasks.configureEach {
doFirst {
println("##teamcity[progressMessage 'Gradle: ${path}:${name}']")
}
}
}
9 changes: 9 additions & 0 deletions buildSrc/src/main/kotlin/utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.util.Locale

/*
* Copyright 2017-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

fun String.capitalizeCompat() = replaceFirstChar {
if (it.isLowerCase()) it.titlecase(locale = Locale.getDefault()) else it.toString()
}
2 changes: 1 addition & 1 deletion formats/json-io/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tasks.named<DokkaTaskPartial>("dokkaHtmlPartial") {
dokkaSourceSets {
configureEach {
externalDocumentationLink {
url.set(URL("https://kotlin.github.io/kotlinx-io/"))
url.set(URI("https://kotlin.github.io/kotlinx-io/").toURL())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion formats/json-okio/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tasks.named<DokkaTaskPartial>("dokkaHtmlPartial") {
dokkaSourceSets {
configureEach {
externalDocumentationLink {
url.set(URL("https://square.github.io/okio/3.x/okio/"))
url.set(URI("https://square.github.io/okio/3.x/okio/").toURL())
packageListUrl.set(
file("dokka/okio.package.list").toURI().toURL()
)
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ kover.enabled=true

org.gradle.parallel=true
org.gradle.caching=true
org.gradle.kotlin.dsl.allWarningsAsErrors=true

#kotlin.native.jvmArgs=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5007
#kotlin.native.disableCompilerDaemon=true
Expand Down
26 changes: 15 additions & 11 deletions integration-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.dsl.JsModuleKind
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl

val serialization_version = property("mainLibVersion") as String

Expand Down Expand Up @@ -33,16 +35,16 @@ kotlin {
// Switching module kind for JS is required to run tests
js {
nodejs {}
compilations.matching { it.name == "main" || it.name == "test" }.configureEach {
kotlinOptions {
sourceMap = true
moduleKind = "umd"
}
compilerOptions {
sourceMap = true
moduleKind = JsModuleKind.MODULE_UMD
}
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
nodejs()
}
@OptIn(ExperimentalWasmDsl::class)
wasmWasi {
nodejs()
}
Expand Down Expand Up @@ -127,14 +129,16 @@ kotlin {

targets.all {
compilations.all {
kotlinOptions {
freeCompilerArgs += "-Xexpect-actual-classes"
compileTaskProvider.configure {
compilerOptions.freeCompilerArgs.add("-Xexpect-actual-classes")
}
}
compilations["main"].kotlinOptions {
allWarningsAsErrors = true
// Suppress 'K2 kapt is an experimental feature' warning:
freeCompilerArgs += "-Xsuppress-version-warnings"
compilations["main"].compileTaskProvider.configure {
compilerOptions {
allWarningsAsErrors = true
// Suppress 'K2 kapt is an experimental feature' warning:
freeCompilerArgs.add("-Xsuppress-version-warnings")
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion integration-test/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mainKotlinVersion=2.1.0
mainLibVersion=1.8.0-SNAPSHOT

kotlin.code.style=official
kotlin.js.compiler=ir

gradle_node_version = 1.2.0
node_version = 8.9.3
Expand All @@ -17,6 +16,8 @@ source_map_support_version = 0.5.3

kapt.use.k2=true

org.gradle.kotlin.dsl.allWarningsAsErrors=true

# Uncommend & insert path to local Native distribution if you want to test with SNAPSHOT compiler
#kotlin.native.home=

Expand Down