From 464be35d6b302f1bccbe7ea1843894ee9f5de4be Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Tue, 5 Nov 2024 14:48:40 +0300 Subject: [PATCH] Shortcuts for configuring compiler --- impl/build.gradle.kts | 19 ++++++-- .../kmp_gradle_build_helper/compilations.kt | 43 +++++++++++++++++++ test/build.gradle.kts | 2 + test/src/commonMain/kotlin/contracts_test.kt | 8 ++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 impl/src/jvmMain/kotlin/ru/landgrafhomyak/kotlin/kmp_gradle_build_helper/compilations.kt create mode 100644 test/src/commonMain/kotlin/contracts_test.kt diff --git a/impl/build.gradle.kts b/impl/build.gradle.kts index f25242c..1670dda 100644 --- a/impl/build.gradle.kts +++ b/impl/build.gradle.kts @@ -1,4 +1,5 @@ import java.util.Properties +import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { kotlin("multiplatform") version "2.0.20" @@ -23,12 +24,25 @@ repositories { kotlin { explicitApi() jvmToolchain(8) + + targets.configureEach { + this.compilations.configureEach { + compileTaskProvider.configure { + this.compilerOptions { + this.languageVersion.set(KotlinVersion.KOTLIN_1_8) + this.apiVersion.set(KotlinVersion.KOTLIN_1_8) + } + } + } + } + jvm() + sourceSets { jvmMain { dependencies { - compileOnly("dev.gradleplugins:gradle-api:7.5.1") + compileOnly("dev.gradleplugins:gradle-api:${versions.getProperty("gradle")}") compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:${targetKotlinVersion}") } } @@ -36,7 +50,6 @@ kotlin { } publishing { - // other settings of publication repositories { maven { name = "XomrkMaven" @@ -44,7 +57,7 @@ publishing { credentials(HttpHeaderCredentials::class) { name = "Authorization" - value = "token ${System.getenv("XOMRK_MAVEN_REPO_DEPLOY_TOKEN")}" + value = "token ${System.getenv("XOMRK_GITEA_MAVEN_REPO_DEPLOY_TOKEN")}" } authentication { diff --git a/impl/src/jvmMain/kotlin/ru/landgrafhomyak/kotlin/kmp_gradle_build_helper/compilations.kt b/impl/src/jvmMain/kotlin/ru/landgrafhomyak/kotlin/kmp_gradle_build_helper/compilations.kt new file mode 100644 index 0000000..9929a64 --- /dev/null +++ b/impl/src/jvmMain/kotlin/ru/landgrafhomyak/kotlin/kmp_gradle_build_helper/compilations.kt @@ -0,0 +1,43 @@ +@file:JvmName("Compilations") + +package ru.landgrafhomyak.kotlin.kmp_gradle_build_helper + +import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions +import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension +import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation +import org.jetbrains.kotlin.gradle.dsl.KotlinVersion + +public fun KotlinMultiplatformExtension.configureAllCompilations(action: KotlinCompilation.() -> Unit) { + this.targets.configureEach { t -> + t.compilations.configureEach(action) + } +} + +public fun KotlinMultiplatformExtension.configureAllCompilersOptions(action: KotlinCommonCompilerOptions.() -> Unit) { + this.targets.configureEach { t -> + t.compilations.configureEach { c -> + c.compileTaskProvider.configure { t -> + t.compilerOptions(action) + } + } + } +} + +public fun KotlinMultiplatformExtension.noWarnExpectActual(): Unit = + this.configureAllCompilersOptions { this@configureAllCompilersOptions.freeCompilerArgs.add("-Xexpect-actual-classes") } + +public fun KotlinMultiplatformExtension.warningsAsErrors(): Unit = + this.configureAllCompilersOptions { this@configureAllCompilersOptions.allWarningsAsErrors.set(true) } + +public fun KotlinMultiplatformExtension.optIn(classQualname: String): Unit = + this.configureAllCompilersOptions { this@configureAllCompilersOptions.optIn.add(classQualname) } + +public fun KotlinMultiplatformExtension.optInContracts(): Unit = + this.optIn("kotlin.contracts.ExperimentalContracts") + +public fun KotlinMultiplatformExtension.setCompatibilityWithKotlin(version: KotlinVersion): Unit = + this.configureAllCompilersOptions { + this@configureAllCompilersOptions.apiVersion.set(version) + this@configureAllCompilersOptions.languageVersion.set(version) + } \ No newline at end of file diff --git a/test/build.gradle.kts b/test/build.gradle.kts index 1315591..cfc190a 100644 --- a/test/build.gradle.kts +++ b/test/build.gradle.kts @@ -1,5 +1,6 @@ import ru.landgrafhomyak.kotlin.kmp_gradle_build_helper.defineAllMultiplatformTargets import ru.landgrafhomyak.kotlin.kmp_gradle_build_helper.defineXomrkGiteaMavenRepo +import ru.landgrafhomyak.kotlin.kmp_gradle_build_helper.optInContracts buildscript { val versions = java.util.Properties() @@ -28,4 +29,5 @@ repositories { kotlin { jvmToolchain(8) defineAllMultiplatformTargets() + optInContracts() } diff --git a/test/src/commonMain/kotlin/contracts_test.kt b/test/src/commonMain/kotlin/contracts_test.kt new file mode 100644 index 0000000..fac78c5 --- /dev/null +++ b/test/src/commonMain/kotlin/contracts_test.kt @@ -0,0 +1,8 @@ +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract + +fun lambdaReceiver(f: () -> Unit) { + contract { + callsInPlace(f, InvocationKind.EXACTLY_ONCE) + } +} \ No newline at end of file