Skip to content

Commit 6206b02

Browse files
committed
Kotlin 2.1.0 cross compilation support
Closes #291
1 parent 49b5b55 commit 6206b02

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/functionalTest/kotlin/kotlinx/validation/test/KlibVerificationTests.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,4 +850,25 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
850850
.contains("+// Targets: [linuxArm64]")
851851
}
852852
}
853+
854+
@Test
855+
fun `check cross compilation support`() {
856+
Assume.assumeFalse(HostManager().isEnabled(KonanTarget.MACOS_ARM64))
857+
858+
val runner = test {
859+
settingsGradleKts {
860+
resolve("/examples/gradle/settings/settings-name-testproject.gradle.kts")
861+
}
862+
buildGradleKts {
863+
resolve("/examples/gradle/base/withNativePluginAndCrossCompilation.gradle.kts")
864+
}
865+
additionalBuildConfig("/examples/gradle/configuration/appleTargets/targets.gradle.kts")
866+
addToSrcSet("/examples/classes/TopLevelDeclarations.kt")
867+
runner {
868+
arguments.addAll(listOf(":apiDump", "-Pkotlin.native.enableKlibsCrossCompilation=true"))
869+
}
870+
}
871+
872+
checkKlibDump(runner.build(), "/examples/classes/TopLevelDeclarations.klib.all.dump")
873+
}
853874
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2016-2025 JetBrains s.r.o.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
plugins {
7+
kotlin("multiplatform") version "2.1.0"
8+
id("org.jetbrains.kotlinx.binary-compatibility-validator")
9+
}
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
kotlin {
16+
linuxX64()
17+
linuxArm64()
18+
mingwX64()
19+
androidNativeArm32()
20+
androidNativeArm64()
21+
androidNativeX64()
22+
androidNativeX86()
23+
24+
sourceSets {
25+
val commonMain by getting
26+
val commonTest by getting {
27+
dependencies {
28+
implementation(kotlin("stdlib"))
29+
implementation(kotlin("test-common"))
30+
implementation(kotlin("test-annotations-common"))
31+
}
32+
}
33+
}
34+
}
35+
36+
apiValidation {
37+
klib.enabled = true
38+
}

src/main/kotlin/BinaryCompatibilityValidatorPlugin.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import org.jetbrains.kotlin.gradle.plugin.*
1717
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
1818
import org.jetbrains.kotlin.konan.target.HostManager
1919
import org.jetbrains.kotlin.library.abi.ExperimentalLibraryAbiReader
20-
import org.jetbrains.kotlin.library.abi.LibraryAbiReader
2120
import java.io.*
2221
import java.util.*
2322

@@ -320,6 +319,7 @@ private inline fun <reified T : Task> Project.task(
320319
private const val BANNED_TARGETS_PROPERTY_NAME = "binary.compatibility.validator.klib.targets.disabled.for.testing"
321320
private const val KLIB_DUMPS_DIRECTORY = "klib"
322321
private const val KLIB_INFERRED_DUMPS_DIRECTORY = "klib-all"
322+
private const val ENABLE_CROSS_COMPILATION_PROPERTY_NAME = "kotlin.native.enableKlibsCrossCompilation"
323323

324324
/**
325325
* KLib ABI dump validation and dump extraction consists of multiple steps that extracts and transforms dumps for klibs.
@@ -544,10 +544,14 @@ private class KlibValidationPipelineBuilder(
544544

545545
private fun Project.targetIsSupported(target: KotlinTarget): Boolean {
546546
if (bannedTargets().contains(target.targetName)) return false
547-
return when (target) {
548-
is KotlinNativeTarget -> HostManager().isEnabled(target.konanTarget)
549-
else -> true
547+
if (target !is KotlinNativeTarget || HostManager().isEnabled(target.konanTarget)) {
548+
return true
550549
}
550+
551+
// Starting from Kotlin 2.1.0, cross compilation could be enabled via property
552+
if (!isKgpVersionAtLeast2_1(getKotlinPluginVersion())) return false
553+
554+
return (project.properties[ENABLE_CROSS_COMPILATION_PROPERTY_NAME] as String?).toBoolean()
551555
}
552556

553557
// Compilable targets not supported by the host compiler
@@ -770,3 +774,11 @@ private var Configuration.isCanBeDeclaredCompat: Boolean
770774
isCanBeDeclared = value
771775
}
772776
}
777+
778+
private fun isKgpVersionAtLeast2_1(kgpVersion: String): Boolean {
779+
val parts= kgpVersion.split('.')
780+
if (parts.size < 2) return false
781+
val major = parts[0].toIntOrNull() ?: return false
782+
val minor = parts[1].toIntOrNull() ?: return false
783+
return major > 2 || (major == 2 && minor >= 1)
784+
}

0 commit comments

Comments
 (0)