[Gradle] Simplify 'isHostSpecificKonanTargetsSet' implementation
The method is called pretty often during import and can return faster and create less objects.
This commit is contained in:
committed by
Space
parent
3d1bde470b
commit
02601745a9
+9
-6
@@ -144,12 +144,15 @@ open class KotlinNativeTarget @Inject constructor(
|
||||
|
||||
private val hostManager by lazy { HostManager() }
|
||||
|
||||
internal fun isHostSpecificKonanTargetsSet(konanTargets: Iterable<KonanTarget>): Boolean {
|
||||
val enabledByHost = hostManager.enabledByHost
|
||||
val allHosts = enabledByHost.keys
|
||||
fun canBeBuiltOnHosts(konanTarget: KonanTarget) = enabledByHost.filterValues { konanTarget in it }.keys
|
||||
return konanTargets.flatMapTo(mutableSetOf(), ::canBeBuiltOnHosts) != allHosts
|
||||
}
|
||||
private val targetsEnabledOnAllHosts by lazy { hostManager.enabledByHost.values.reduce { acc, targets -> acc intersect targets } }
|
||||
|
||||
/**
|
||||
* The set of konanTargets is considered 'host specific' if the shared compilation of said set can *not* be built
|
||||
* on *all* potential hosts. e.g. a set like (iosX64, macosX64) can only be built on macos hosts, and is therefore considered
|
||||
* 'host specific'.
|
||||
*/
|
||||
internal fun isHostSpecificKonanTargetsSet(konanTargets: Iterable<KonanTarget>): Boolean =
|
||||
konanTargets.none { target -> target in targetsEnabledOnAllHosts }
|
||||
|
||||
private fun <T> getHostSpecificElements(
|
||||
fragments: Iterable<T>,
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.gradle.plugin.mpp
|
||||
|
||||
import org.jetbrains.kotlin.konan.target.HostManager
|
||||
import org.jetbrains.kotlin.konan.target.KonanTarget
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class IsHostSpecificKonanTargetsSetTest {
|
||||
|
||||
private val hostManager = HostManager()
|
||||
|
||||
@Test
|
||||
fun `matches previous implementation`() {
|
||||
fun previousImplementation(konanTargets: Iterable<KonanTarget>): Boolean {
|
||||
val enabledByHost = hostManager.enabledByHost
|
||||
val allHosts = enabledByHost.keys
|
||||
fun canBeBuiltOnHosts(konanTarget: KonanTarget) = enabledByHost.filterValues { konanTarget in it }.keys
|
||||
return konanTargets.flatMapTo(mutableSetOf(), ::canBeBuiltOnHosts) != allHosts
|
||||
}
|
||||
|
||||
val testInputs = sequence {
|
||||
yield(emptyList())
|
||||
|
||||
KonanTarget.predefinedTargets.values.forEach { first ->
|
||||
yield(listOf(first))
|
||||
|
||||
KonanTarget.predefinedTargets.values.forEach { second ->
|
||||
yield(listOf(first, second))
|
||||
|
||||
KonanTarget.predefinedTargets.values.forEach { third ->
|
||||
yield(listOf(first, second, third))
|
||||
}
|
||||
}
|
||||
}
|
||||
}.toList()
|
||||
|
||||
testInputs.forEach { targets ->
|
||||
assertEquals(
|
||||
previousImplementation(targets), isHostSpecificKonanTargetsSet(targets),
|
||||
"Expected 'isHostSpecificKonanTargetsSet' to return same as previous implementation for $targets"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user