[Gradle] CInterop IDE dependency resolution: Fix build dependencies

^KT-59596 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-08-17 12:10:23 +02:00
committed by Space Team
parent fac5083dbf
commit 337d87ad54
4 changed files with 63 additions and 9 deletions
@@ -88,7 +88,7 @@ abstract class MppCInteropDependencyTransformationIT : BaseGradleIT() {
/* Assert p2 & p3 compiled for Windows */
assertTasksExecuted(":p2:compileKotlinWindowsX64")
assertTasksExecuted(":p3:compileKotlinWindowsX64")
/* Assert p2 & p3 compiled tests */
assertTasksExecuted(":p2:compileTestKotlinLinuxX64")
assertTasksExecuted(":p3:compileTestKotlinLinuxX64")
@@ -261,6 +261,55 @@ class MppIdeDependencyResolutionIT : KGPBaseTest() {
}
}
@GradleTest
fun `test cinterops - transitive project dependencies`(gradleVersion: GradleVersion) {
project(
"cinterop-MetadataDependencyTransformation",
gradleVersion,
buildOptions = defaultBuildOptions.copy(freeArgs = defaultBuildOptions.freeArgs + "-PdependencyMode=project")
) {
resolveIdeDependencies(":p3") { dependencies ->
/* Check that no compile-tasks are executed */
run {
val compileTaskRegex = Regex(".*[cC]ompile.*")
val compileTasks = tasks.filter { task -> task.path.matches(compileTaskRegex) }
if (compileTasks.isNotEmpty()) {
fail("Expected no compile tasks to be executed. Found $compileTasks")
}
}
dependencies["nativeMain"].cinteropDependencies().assertMatches(
binaryCoordinates(Regex(".*p1-cinterop-simple.*")),
binaryCoordinates(Regex(".*p1-cinterop-withPosix.*"))
)
dependencies["linuxX64Main"].cinteropDependencies().assertMatches(
binaryCoordinates(Regex(".*p1-cinterop-simple.*")),
binaryCoordinates(Regex(".*p1-cinterop-withPosix.*"))
)
}
}
}
@GradleTest
fun `test cinterops - transitive project dependencies - disabled cinterop commonization`(gradleVersion: GradleVersion) {
project(
"cinterop-MetadataDependencyTransformation",
gradleVersion,
buildOptions = defaultBuildOptions.copy(
freeArgs = defaultBuildOptions.freeArgs + "-PdependencyMode=project" + "-Pkotlin.mpp.enableCInteropCommonization=false"
)
) {
resolveIdeDependencies(":p3") { dependencies ->
dependencies["nativeMain"].cinteropDependencies().assertMatches()
dependencies["linuxX64Main"].cinteropDependencies().assertMatches(
binaryCoordinates(Regex(".*p1-cinterop-simple.*")),
binaryCoordinates(Regex(".*p1-cinterop-withPosix.*"))
)
}
}
}
@GradleTest
fun `test dependency on composite build with commonized cinterops`(gradleVersion: GradleVersion, @TempDir tempDir: Path) {
val includedLib = project("composite-build-with-cinterop-commonization/includedLib", gradleVersion, localRepoDir = tempDir)
@@ -23,9 +23,7 @@ import org.jetbrains.kotlin.gradle.utils.future
internal object IdeCInteropMetadataDependencyClasspathResolver : IdeDependencyResolver, IdeDependencyResolver.WithBuildDependencies {
override fun resolve(sourceSet: KotlinSourceSet): Set<IdeaKotlinDependency> {
if (sourceSet !is DefaultKotlinSourceSet) return emptySet()
val project = sourceSet.project
project.locateDependencyTask(sourceSet) ?: return emptySet()
val cinteropFiles = project.future { createCInteropMetadataDependencyClasspathForIde(sourceSet) }.getOrThrow()
return project.resolveCInteropDependencies(cinteropFiles)
@@ -34,10 +32,7 @@ internal object IdeCInteropMetadataDependencyClasspathResolver : IdeDependencyRe
override fun dependencies(project: Project): Iterable<Any> {
return project.multiplatformExtension.sourceSets
.filterIsInstance<DefaultKotlinSourceSet>()
.filter { it.commonizerTarget.getOrThrow() is SharedCommonizerTarget}
.mapNotNull { project.locateDependencyTask(it) }
.filter { it.commonizerTarget.getOrThrow() is SharedCommonizerTarget }
.map { sourceSet -> project.future { createCInteropMetadataDependencyClasspathForIde(sourceSet) }.getOrThrow() }
}
private fun Project.locateDependencyTask(sourceSet: DefaultKotlinSourceSet): TaskProvider<*>? =
locateTask<CInteropMetadataDependencyTransformationTask>(sourceSet.cinteropMetadataDependencyTransformationForIdeTaskName)
}
@@ -5,18 +5,21 @@
package org.jetbrains.kotlin.gradle.plugin.ide.dependencyResolvers
import org.gradle.api.Project
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.idea.tcs.IdeaKotlinDependency
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.ide.IdeDependencyResolver
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.sources.internal
import org.jetbrains.kotlin.gradle.targets.native.internal.locateOrCreateCInteropDependencyConfiguration
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
internal object IdeProjectToProjectCInteropDependencyResolver : IdeDependencyResolver {
internal object IdeProjectToProjectCInteropDependencyResolver : IdeDependencyResolver, IdeDependencyResolver.WithBuildDependencies {
override fun resolve(sourceSet: KotlinSourceSet): Set<IdeaKotlinDependency> {
if (sourceSet !is DefaultKotlinSourceSet) return emptySet()
@@ -34,4 +37,11 @@ internal object IdeProjectToProjectCInteropDependencyResolver : IdeDependencyRes
return project.resolveCInteropDependencies(cinteropFiles)
}
override fun dependencies(project: Project): Iterable<Any> {
val extension = project.multiplatformExtensionOrNull ?: return emptySet()
return extension.targets.filterIsInstance<KotlinNativeTarget>().flatMap { it.compilations }.mapNotNull { compilation ->
project.locateOrCreateCInteropDependencyConfiguration(compilation)
}
}
}