Skip to content

Commit

Permalink
Add a workaround for native dependency metadata resolution (#3081)
Browse files Browse the repository at this point in the history
Addresses #3068
  • Loading branch information
whyoleg authored Aug 8, 2023
1 parent 0c21de1 commit 80549e1
Showing 1 changed file with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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

0 comments on commit 80549e1

Please sign in to comment.