Skip to content

Commit

Permalink
chore: kotlin multiplatform build config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshg committed May 14, 2024
1 parent 1256d4a commit ee2b189
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 124 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ jobs:
env:
GITHUB_USER: ${{ github.repository_owner }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
JOB_CONTEXT: ${{ toJSON(job) }}
STEPS_CONTEXT: ${{ toJSON(steps) }}
RUNNER_CONTEXT: ${{ toJSON(runner) }}
GITHUB_CONTEXT: ${{ toJSON(github) }}
STRATEGY_CONTEXT: ${{ toJSON(strategy) }}
MATRIX_CONTEXT: ${{ toJSON(matrix) }}
# JOB_CONTEXT: ${{ toJSON(job) }}
# STEPS_CONTEXT: ${{ toJSON(steps) }}
# RUNNER_CONTEXT: ${{ toJSON(runner) }}
# GITHUB_CONTEXT: ${{ toJSON(github) }}
# STRATEGY_CONTEXT: ${{ toJSON(strategy) }}
# MATRIX_CONTEXT: ${{ toJSON(matrix) }}

- name: 📤 Uploading ${{ matrix.os }} build artifacts
if: steps.gradle-build.outcome == 'success' && runner.os == 'Linux'
Expand Down
19 changes: 0 additions & 19 deletions backend/native/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,8 @@ description = "Ktor native application"
kotlin {
targets.withType<KotlinNativeTarget>().configureEach {
binaries {

// Creates Release executable
executable(setOf(RELEASE)) {
entryPoint = "main"

// Alpine(apk add gcompat) - https://youtrack.jetbrains.com/issue/KT-38876
// linkerOpts("--as-needed", "--defsym=isnan=isnan")
// freeCompilerArgs += listOf("-Xoverride-konan-properties=linkerGccFlags=-lgcc -lgcc_eh
// -lc")

if (buildType == NativeBuildType.RELEASE) {
mavenPublication {
artifact(outputFile) {
Expand All @@ -45,17 +37,6 @@ kotlin {
// Creates test executable
test(emptySet())
}

compilations.configureEach {
compileTaskProvider.configure {
compilerOptions {
freeCompilerArgs.appendAll(
"-Xverbose-phases=Linker"
// "-Xruntime-logs=gc=info"
)
}
}
}
}

sourceSets {
Expand Down
49 changes: 20 additions & 29 deletions gradle/build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.gradle.kotlin.dsl.support.expectedKotlinDslPluginsVersion
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
idea
Expand All @@ -14,10 +13,7 @@ plugins {
// alias(libs.plugins.kotlin.dsl)
}

/**
* Java version used in Java toolchains and Kotlin compile JVM target for Gradle precompiled script
* plugins.
*/
// Java version used for Kotlin Gradle precompiled script plugins.
val dslJavaVersion = libs.versions.kotlin.dsl.jvmtarget

idea {
Expand All @@ -27,18 +23,30 @@ idea {
}
}

kotlin {
compilerOptions {
jvmTarget = dslJavaVersion.map(JvmTarget::fromTarget)
freeCompilerArgs.appendAll("-Xcontext-receivers", "-Xjdk-release=${dslJavaVersion.get()}")
optIn =
listOf(
"kotlin.ExperimentalStdlibApi",
"kotlin.time.ExperimentalTime",
"kotlin.io.encoding.ExperimentalEncodingApi",
"kotlinx.validation.ExperimentalBCVApi",
"kotlinx.coroutines.ExperimentalCoroutinesApi",
"kotlinx.serialization.ExperimentalSerializationApi",
"org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi",
"org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl",
"org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalDistributionDsl",
"org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalDceDsl")
}
}

tasks {
// Restrict the java release version used in Gradle kotlin DSL to avoid
// accidentally using higher version JDK API in build scripts.
withType<JavaCompile>().configureEach { options.release = dslJavaVersion.map { it.toInt() } }

withType<KotlinCompile>().configureEach {
compilerOptions {
jvmTarget = dslJavaVersion.map(JvmTarget::fromTarget)
freeCompilerArgs.appendAll("-Xcontext-receivers", "-Xjdk-release=${dslJavaVersion.get()}")
}
}

validatePlugins {
failOnWarning = true
enableStricterValidation = true
Expand All @@ -55,23 +63,6 @@ tasks {
}
}

kotlin {
sourceSets.all {
languageSettings.apply {
optIn("kotlin.ExperimentalStdlibApi")
optIn("kotlin.time.ExperimentalTime")
optIn("kotlin.io.encoding.ExperimentalEncodingApi")
optIn("kotlinx.validation.ExperimentalBCVApi")
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
optIn("kotlinx.serialization.ExperimentalSerializationApi")
optIn("org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi")
optIn("org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl")
optIn("org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalDistributionDsl")
optIn("org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalDceDsl")
}
}
}

gradlePlugin {
plugins {

Expand Down
38 changes: 8 additions & 30 deletions gradle/build-logic/src/main/kotlin/common/MultiPlatformExtns.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@ import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithHostTests
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig

context(Project)
fun KotlinMultiplatformExtension.commonTarget() {

jvmToolchain { configureJvmToolchain() }

withSourcesJar(publish = true)

compilerOptions { configureKotlinCommon() }
withSourcesJar(publish = true)
// targets.configureEach {}

sourceSets {
all {
languageSettings { configureKotlinLang() }
// Apply multiplatform library bom to all source sets
dependencies {
api(project.dependencies.platform(libs.kotlin.bom))
Expand Down Expand Up @@ -93,11 +88,8 @@ context(Project)
fun KotlinMultiplatformExtension.jvmTarget() {
jvm {
withJava()
// withSourcesJar(publish = false)
compilations.all {
compileJavaTaskProvider?.configure { configureJavac() }
compileTaskProvider.configure { compilerOptions { configureKotlinJvm() } }
}
compilations.configureEach { compileJavaTaskProvider?.configure { configureJavac() } }
compilerOptions { configureKotlinJvm() }

// ./gradlew jvmRun
mainRun { mainClass = libs.versions.app.mainclass.get() }
Expand Down Expand Up @@ -154,7 +146,6 @@ fun KotlinMultiplatformExtension.jsTarget() {
}

runTask { sourceMaps = false }

testTask {
enabled = true
testLogging { configureLogEvents() }
Expand All @@ -163,10 +154,7 @@ fun KotlinMultiplatformExtension.jsTarget() {
// distribution { outputDirectory = file("$projectDir/docs") }
}
generateTypeScriptDefinitions()

compilations.configureEach {
compileTaskProvider.configure { compilerOptions { configureKotlinJs() } }
}
compilerOptions { configureKotlinJs() }
testRuns.configureEach { executionTask.configure { configureTestReport() } }
}

Expand Down Expand Up @@ -221,20 +209,12 @@ fun KotlinMultiplatformExtension.wasmJsTarget() {
}
// Generate .d.ts files
generateTypeScriptDefinitions()

compilations.configureEach {
compileTaskProvider.configure { compilerOptions { configureKotlinJs() } }
}
compilerOptions { configureKotlinJs() }
testRuns.configureEach { executionTask.configure { configureTestReport() } }
}

sourceSets {
wasmJsMain {
dependencies {
// Webcrypto
api(libs.kotlin.cryptography.webcrypto)
}
}
wasmJsMain { dependencies { api(libs.kotlin.cryptography.webcrypto) } }
wasmJsTest { kotlin {} }
}
}
Expand All @@ -253,15 +233,13 @@ fun KotlinMultiplatformExtension.hostNativeTarget(configure: KotlinNativeTarget.
}

context(Project)
fun KotlinMultiplatformExtension.allNativeTargets(
configure: KotlinNativeTargetWithHostTests.() -> Unit = {}
) {
fun KotlinMultiplatformExtension.allNativeTargets(configure: KotlinNativeTarget.() -> Unit = {}) {
val nativeBuild: String? by project
if (nativeBuild.toBoolean()) {
macosX64 { configure() }
macosArm64 { configure() }
linuxX64 { configure() }
linuxArm64 {}
linuxArm64 { configure() }
mingwX64 { configure() }
}
}
Expand Down
36 changes: 20 additions & 16 deletions gradle/build-logic/src/main/kotlin/common/ProjectExtns.kt
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,21 @@ fun KotlinCommonCompilerOptions.configureKotlinCommon() {
// "-P",
// "plugin:...=..."
)
optIn =
listOf(
"kotlin.ExperimentalStdlibApi",
"kotlin.contracts.ExperimentalContracts",
"kotlin.ExperimentalUnsignedTypes",
"kotlin.io.encoding.ExperimentalEncodingApi",
"kotlin.time.ExperimentalTime",
"kotlinx.coroutines.ExperimentalCoroutinesApi",
"kotlinx.serialization.ExperimentalSerializationApi",
"kotlin.ExperimentalMultiplatform",
"kotlin.js.ExperimentalJsExport",
"kotlin.experimental.ExperimentalNativeApi",
"kotlinx.cinterop.ExperimentalForeignApi",
// "org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi",
)
}

context(Project)
Expand Down Expand Up @@ -462,22 +477,11 @@ fun KotlinJvmCompilerOptions.configureKotlinJvm() {
}

context(Project)
fun LanguageSettingsBuilder.configureKotlinLang() {
progressiveMode = true
languageVersion = kotlinLangVersion.get().version
// enableLanguageFeature("ContextReceivers")
optIn("kotlin.ExperimentalStdlibApi")
optIn("kotlin.contracts.ExperimentalContracts")
optIn("kotlin.ExperimentalUnsignedTypes")
optIn("kotlin.io.encoding.ExperimentalEncodingApi")
optIn("kotlin.time.ExperimentalTime")
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
optIn("kotlinx.serialization.ExperimentalSerializationApi")
optIn("kotlin.ExperimentalMultiplatform")
optIn("kotlin.js.ExperimentalJsExport")
optIn("kotlin.experimental.ExperimentalNativeApi")
optIn("kotlinx.cinterop.ExperimentalForeignApi")
// optIn("org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi")
fun KotlinNativeCompilerOptions.configureKotlinNative() {
freeCompilerArgs.appendAll(
"-Xverbose-phases=Linker"
// "-Xruntime-logs=gc=info"
)
}

context(Project)
Expand Down
18 changes: 6 additions & 12 deletions gradle/build-logic/src/main/kotlin/plugins/kotlin.jvm.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import java.util.jar.Attributes
import kotlinx.kover.gradle.plugin.dsl.CoverageUnit
import org.gradle.internal.os.OperatingSystem
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import tasks.ReallyExecJar

plugins {
Expand All @@ -31,11 +30,12 @@ java {
}

kotlin {
sourceSets.all {
languageSettings { configureKotlinLang() }
// kotlin.setSrcDirs(listOf("src/kotlin"))
}
jvmToolchain { configureJvmToolchain() }
compilerOptions {
configureKotlinCommon()
configureKotlinJvm()
}
// sourceSets.all { kotlin.setSrcDirs(listOf("src/kotlin")) }
}

@Suppress("UnstableApiUsage", "UNUSED_VARIABLE")
Expand Down Expand Up @@ -94,15 +94,9 @@ kover {
val javaAgent by configurations.creating

tasks {

// Configure "compileJava" and "compileTestJava" tasks.
withType<JavaCompile>().configureEach { configureJavac() }

withType<KotlinCompile>().configureEach {
compilerOptions { configureKotlinJvm() }
usePreciseJavaTracking = true
// finalizedBy("spotlessApply")
}
// withType<KotlinJvmCompile>().configureEach { finalizedBy("spotlessApply") }

// Configure jvm args for JavaExec tasks except `run`
withType<JavaExec>().matching { it.name != "run" }.configureEach { jvmArgs(jvmArguments()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ kotlin {
sharedProjectName -> {
jvmTarget()
jsTarget()
allNativeTargets()
allNativeTargets { compilerOptions { configureKotlinNative() } }
// wasmJsTarget()
}
"js",
"chrome",
"web" -> jsTarget()
"wasm" -> wasmJsTarget()
"native" -> allNativeTargets()
"native" -> allNativeTargets { compilerOptions { configureKotlinNative() } }
else -> jvmTarget()
}

Expand Down
Loading

0 comments on commit ee2b189

Please sign in to comment.