From b8cf2fdefaf95ff4394d6e1cd83425d391491596 Mon Sep 17 00:00:00 2001 From: Oleg Yukhnevich Date: Tue, 8 Aug 2023 16:55:59 +0300 Subject: [PATCH] Add a workaround for native dependency metadata resolution (#3081) Addresses #3068 (cherry picked from commit 80549e1f061d1eb22b12fc9a068536655ae299f7) --- .../gradle/kotlin/kotlinClasspathUtils.kt | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/kotlin/kotlinClasspathUtils.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/kotlin/kotlinClasspathUtils.kt index 1d1d18fc64..83cb87d6a9 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/kotlin/kotlinClasspathUtils.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/kotlin/kotlinClasspathUtils.kt @@ -6,34 +6,49 @@ import org.jetbrains.dokka.gradle.isAndroidTarget import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinNativeCompilation import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.tasks.KotlinCompileTool internal fun Project.classpathOf(sourceSet: KotlinSourceSet): FileCollection { val compilations = compilationsOf(sourceSet) return if (compilations.isNotEmpty()) { compilations .map { compilation -> compilation.compileClasspathOf(project = this) } - .reduce { acc, fileCollection -> acc + fileCollection } + .reduce(FileCollection::plus) } else { // Dokka suppresses source sets that do no have compilations // since such configuration is invalid, it reports a warning or an error sourceSet.withAllDependentSourceSets() - .toList() .map { it.kotlin.sourceDirectories } - .reduce { acc, fileCollection -> acc + fileCollection } + .reduce(FileCollection::plus) } } private fun KotlinCompilation.compileClasspathOf(project: Project): FileCollection { + val kgpVersion = project.getKgpVersion() + + // if KGP version < 1.9 or org.jetbrains.dokka.classpath.useOldResolution=true + // we will use old (pre 1.9) resolution of classpath + if (kgpVersion == null || + kgpVersion < KotlinGradlePluginVersion(1, 9, 0) || + project.classpathProperty("useOldResolution", default = false) + ) { + return oldCompileClasspathOf(project) + } + + return newCompileClasspathOf(project) +} + +private fun KotlinCompilation.newCompileClasspathOf(project: Project): FileCollection { + val compilationClasspath = (compileTaskProvider.get() as? KotlinCompileTool)?.libraries ?: project.files() + return compilationClasspath + platformDependencyFiles(project) +} + +private fun KotlinCompilation.oldCompileClasspathOf(project: Project): FileCollection { if (this.target.isAndroidTarget()) { // Workaround for https://youtrack.jetbrains.com/issue/KT-33893 return this.classpathOf(project) } - val platformDependencyFiles: FileCollection = (this as? AbstractKotlinNativeCompilation) - ?.target?.project?.configurations - ?.findByName(@Suppress("DEPRECATION") this.defaultSourceSet.implementationMetadataConfigurationName) // KT-58640 - ?: project.files() - - return this.compileDependencyFiles + platformDependencyFiles + this.classpathOf(project) + return this.compileDependencyFiles + platformDependencyFiles(project) + this.classpathOf(project) } private fun KotlinCompilation.classpathOf(project: Project): FileCollection { @@ -43,7 +58,7 @@ private fun KotlinCompilation.classpathOf(project: Project): FileCollection { val shouldKeepBackwardsCompatibility = (kgpVersion != null && kgpVersion < KotlinGradlePluginVersion(1, 7, 0)) return if (shouldKeepBackwardsCompatibility) { // removed since 1.9.0, left for compatibility with < Kotlin 1.7 - val classpathGetter= kotlinCompile::class.members + val classpathGetter = kotlinCompile::class.members .first { it.name == "getClasspath" } classpathGetter.call(kotlinCompile) as FileCollection } else { @@ -60,3 +75,16 @@ private fun KotlinCompilation.getKotlinCompileTask(kgpVersion: KotlinGradlePlugi this.compileTaskProvider.get() as? KotlinCompile // introduced in 1.8.0 } } + +private fun KotlinCompilation.platformDependencyFiles(project: Project): FileCollection { + val excludePlatformDependencyFiles = project.classpathProperty("excludePlatformDependencyFiles", default = false) + + if (excludePlatformDependencyFiles) return project.files() + return (this as? AbstractKotlinNativeCompilation) + ?.target?.project?.configurations + ?.findByName(@Suppress("DEPRECATION") this.defaultSourceSet.implementationMetadataConfigurationName) // KT-58640 + ?: project.files() +} + +private fun Project.classpathProperty(name: String, default: Boolean): Boolean = + (findProperty("org.jetbrains.dokka.classpath.$name") as? String)?.toBoolean() ?: default