Skip to content
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
33 changes: 27 additions & 6 deletions src/main/kotlin/BinaryCompatibilityValidatorPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ private class KlibValidationPipelineBuilder(
it.dumps.addAll(inferredDumps)
}

val kgpVersion = readVersion()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shanshin while looking through the code, I figured out that that should be a better way to read a KGP version, so I switched to it.


kotlin.targets.matching { it.emitsKlib }.configureEach { currentTarget ->
val mainCompilation = currentTarget.mainCompilationOrNull ?: return@configureEach

Expand All @@ -502,7 +504,7 @@ private class KlibValidationPipelineBuilder(
val targetConfig = TargetConfig(project, extension, targetName, intermediateFilesConfig)
val apiBuildDir =
targetConfig.apiDir.flatMap { f -> project.layout.buildDirectory.asFile.map { it.resolve(f) } }
val targetSupported = targetIsSupported(currentTarget)
val targetSupported = targetIsSupported(currentTarget, kgpVersion)
// If a target is supported, the workflow is simple: create a dump, then merge it along with other dumps.
if (targetSupported) {
val buildTargetAbi = configureKlibCompilation(
Expand Down Expand Up @@ -542,24 +544,32 @@ private class KlibValidationPipelineBuilder(
}
}

private fun Project.targetIsSupported(target: KotlinTarget): Boolean {
private fun Project.targetIsSupported(target: KotlinTarget, kgpVersion: String?): Boolean {
if (bannedTargets().contains(target.targetName)) return false
if (target !is KotlinNativeTarget || HostManager().isEnabled(target.konanTarget)) {
return true
}

if (kgpVersion == null) return false

// Starting from Kotlin 2.1.0, cross compilation could be enabled via property
if (!isKgpVersionAtLeast2_1(getKotlinPluginVersion())) return false
if (!isKgpVersionAtLeast2_1(kgpVersion)) return false

return (project.findProperty(ENABLE_CROSS_COMPILATION_PROPERTY_NAME) as String?).toBoolean()
val propertyValue = project.findProperty(ENABLE_CROSS_COMPILATION_PROPERTY_NAME) as String?
if (propertyValue == null) {
// Starting from Kotlin 2.2.20, cross compilation is enabled by default (KT-76421)
if (isKgpVersionAtLeast2_2_20(kgpVersion)) return true
}
return propertyValue.toBoolean()
}

// Compilable targets not supported by the host compiler
private fun Project.unsupportedTargets(): Provider<Set<KlibTarget>> {
return project.provider {
val kgpVersion = readVersion()
project.kotlinMultiplatform.targets.matching { it.emitsKlib }
.asSequence()
.filterNot { targetIsSupported(it) }
.filterNot { targetIsSupported(it, kgpVersion) }
.map { it.toKlibTarget() }
.toSet()
}
Expand Down Expand Up @@ -767,10 +777,21 @@ private var Configuration.isCanBeDeclaredCompat: Boolean
}
}

private fun isKgpVersionAtLeast2_1(kgpVersion: String): Boolean {
internal fun isKgpVersionAtLeast2_1(kgpVersion: String): Boolean {
val parts = kgpVersion.split('.')
if (parts.size < 2) return false
val major = parts[0].toIntOrNull() ?: return false
val minor = parts[1].toIntOrNull() ?: return false
return major > 2 || (major == 2 && minor >= 1)
}

internal fun isKgpVersionAtLeast2_2_20(kgpVersion: String): Boolean {
val parts = kgpVersion.split('.')
if (parts.size < 3) return false
val major = parts[0].toIntOrNull() ?: return false
val minor = parts[1].toIntOrNull() ?: return false
if (major > 2 || (major == 2 && minor > 2)) return true

val patch = parts[2].split('-')[0].toIntOrNull() ?: return false
return major == 2 && minor == 2 && patch >= 20
}
39 changes: 39 additions & 0 deletions src/test/kotlin/tests/KgpVersionParsingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2016-2025 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package tests

import kotlinx.validation.*
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class KgpVersionParsingTest {
@Test
fun isKgpVersionAtLeast2_1() {
assertFalse(isKgpVersionAtLeast2_1(""))
assertFalse(isKgpVersionAtLeast2_1("3"))
assertFalse(isKgpVersionAtLeast2_1("1.9.20"))
assertTrue(isKgpVersionAtLeast2_1("2.1"))
assertTrue(isKgpVersionAtLeast2_1("2.1.0"))
assertTrue(isKgpVersionAtLeast2_1("2.2.0-RC3"))
assertTrue(isKgpVersionAtLeast2_1("2.2.20"))
}

@Test
fun isKgpVersionAtLeast2_2_20() {
assertFalse(isKgpVersionAtLeast2_2_20(""))
assertFalse(isKgpVersionAtLeast2_2_20("3"))
assertFalse(isKgpVersionAtLeast2_2_20("1.9.20"))
assertFalse(isKgpVersionAtLeast2_2_20("2.1"))
assertFalse(isKgpVersionAtLeast2_2_20("2.1.0"))
assertFalse(isKgpVersionAtLeast2_2_20("2.2.0-RC3"))
assertTrue(isKgpVersionAtLeast2_2_20("2.2.20"))
assertTrue(isKgpVersionAtLeast2_2_20("2.2.20-Beta2"))
assertTrue(isKgpVersionAtLeast2_2_20("2.2.21"))
assertTrue(isKgpVersionAtLeast2_2_20("2.3.0"))
assertTrue(isKgpVersionAtLeast2_2_20("3.0.0"))
}
}