Skip to content

Commit b8cf2fd

Browse files
whyolegIgnatBeresnev
authored andcommitted
Add a workaround for native dependency metadata resolution (#3081)
Addresses #3068 (cherry picked from commit 80549e1)
1 parent 92d697e commit b8cf2fd

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/kotlin/kotlinClasspathUtils.kt

+38-10
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,49 @@ import org.jetbrains.dokka.gradle.isAndroidTarget
66
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
77
import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinNativeCompilation
88
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
9+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompileTool
910

1011
internal fun Project.classpathOf(sourceSet: KotlinSourceSet): FileCollection {
1112
val compilations = compilationsOf(sourceSet)
1213
return if (compilations.isNotEmpty()) {
1314
compilations
1415
.map { compilation -> compilation.compileClasspathOf(project = this) }
15-
.reduce { acc, fileCollection -> acc + fileCollection }
16+
.reduce(FileCollection::plus)
1617
} else {
1718
// Dokka suppresses source sets that do no have compilations
1819
// since such configuration is invalid, it reports a warning or an error
1920
sourceSet.withAllDependentSourceSets()
20-
.toList()
2121
.map { it.kotlin.sourceDirectories }
22-
.reduce { acc, fileCollection -> acc + fileCollection }
22+
.reduce(FileCollection::plus)
2323
}
2424
}
2525

2626
private fun KotlinCompilation.compileClasspathOf(project: Project): FileCollection {
27+
val kgpVersion = project.getKgpVersion()
28+
29+
// if KGP version < 1.9 or org.jetbrains.dokka.classpath.useOldResolution=true
30+
// we will use old (pre 1.9) resolution of classpath
31+
if (kgpVersion == null ||
32+
kgpVersion < KotlinGradlePluginVersion(1, 9, 0) ||
33+
project.classpathProperty("useOldResolution", default = false)
34+
) {
35+
return oldCompileClasspathOf(project)
36+
}
37+
38+
return newCompileClasspathOf(project)
39+
}
40+
41+
private fun KotlinCompilation.newCompileClasspathOf(project: Project): FileCollection {
42+
val compilationClasspath = (compileTaskProvider.get() as? KotlinCompileTool)?.libraries ?: project.files()
43+
return compilationClasspath + platformDependencyFiles(project)
44+
}
45+
46+
private fun KotlinCompilation.oldCompileClasspathOf(project: Project): FileCollection {
2747
if (this.target.isAndroidTarget()) { // Workaround for https://youtrack.jetbrains.com/issue/KT-33893
2848
return this.classpathOf(project)
2949
}
3050

31-
val platformDependencyFiles: FileCollection = (this as? AbstractKotlinNativeCompilation)
32-
?.target?.project?.configurations
33-
?.findByName(@Suppress("DEPRECATION") this.defaultSourceSet.implementationMetadataConfigurationName) // KT-58640
34-
?: project.files()
35-
36-
return this.compileDependencyFiles + platformDependencyFiles + this.classpathOf(project)
51+
return this.compileDependencyFiles + platformDependencyFiles(project) + this.classpathOf(project)
3752
}
3853

3954
private fun KotlinCompilation.classpathOf(project: Project): FileCollection {
@@ -43,7 +58,7 @@ private fun KotlinCompilation.classpathOf(project: Project): FileCollection {
4358
val shouldKeepBackwardsCompatibility = (kgpVersion != null && kgpVersion < KotlinGradlePluginVersion(1, 7, 0))
4459
return if (shouldKeepBackwardsCompatibility) {
4560
// removed since 1.9.0, left for compatibility with < Kotlin 1.7
46-
val classpathGetter= kotlinCompile::class.members
61+
val classpathGetter = kotlinCompile::class.members
4762
.first { it.name == "getClasspath" }
4863
classpathGetter.call(kotlinCompile) as FileCollection
4964
} else {
@@ -60,3 +75,16 @@ private fun KotlinCompilation.getKotlinCompileTask(kgpVersion: KotlinGradlePlugi
6075
this.compileTaskProvider.get() as? KotlinCompile // introduced in 1.8.0
6176
}
6277
}
78+
79+
private fun KotlinCompilation.platformDependencyFiles(project: Project): FileCollection {
80+
val excludePlatformDependencyFiles = project.classpathProperty("excludePlatformDependencyFiles", default = false)
81+
82+
if (excludePlatformDependencyFiles) return project.files()
83+
return (this as? AbstractKotlinNativeCompilation)
84+
?.target?.project?.configurations
85+
?.findByName(@Suppress("DEPRECATION") this.defaultSourceSet.implementationMetadataConfigurationName) // KT-58640
86+
?: project.files()
87+
}
88+
89+
private fun Project.classpathProperty(name: String, default: Boolean): Boolean =
90+
(findProperty("org.jetbrains.dokka.classpath.$name") as? String)?.toBoolean() ?: default

0 commit comments

Comments
 (0)