Skip to content

Commit

Permalink
Use compileDependencyFiles for getting compilation dependencies (#3147)
Browse files Browse the repository at this point in the history
Co-authored-by: vmishenev <[email protected]>
  • Loading branch information
antohaby and vmishenev authored Oct 9, 2023
1 parent 62d1143 commit 9809ae7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ kotlinx-collections-immutable = { module = "org.jetbrains.kotlinx:kotlinx-collec
#### Gradle plugins ####
# The Maven coordinates of Gradle plugins that are either used in convention plugins, or in Dokka subprojects
gradlePlugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "gradlePlugin-kotlin" }
gradlePlugin-kotlin-klibCommonizerApi = { module = "org.jetbrains.kotlin:kotlin-klib-commonizer-api", version.ref = "gradlePlugin-kotlin" }
gradlePlugin-android = { module = "com.android.tools.build:gradle", version.ref = "gradlePlugin-android" }
gradlePlugin-dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "gradlePlugin-dokka" }
gradlePlugin-shadow = { module = "gradle.plugin.com.github.johnrengelman:shadow", version.ref = "gradlePlugin-shadow" }
Expand Down
2 changes: 2 additions & 0 deletions runners/gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ dependencies {
api(projects.core)

compileOnly(libs.gradlePlugin.kotlin)
compileOnly(libs.gradlePlugin.kotlin.klibCommonizerApi)
compileOnly(libs.gradlePlugin.android)

testImplementation(kotlin("test"))
testImplementation(libs.gradlePlugin.kotlin)
testImplementation(libs.gradlePlugin.kotlin.klibCommonizerApi)
testImplementation(libs.gradlePlugin.android)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@file:Suppress("INVISIBLE_REFERENCE")
package org.jetbrains.dokka.gradle.kotlin

import java.io.File
import org.gradle.api.Project
import org.jetbrains.kotlin.commonizer.KonanDistribution
import org.jetbrains.kotlin.commonizer.platformLibsDir
import org.jetbrains.kotlin.commonizer.stdlib
import org.jetbrains.kotlin.compilerRunner.konanHome
import org.jetbrains.kotlin.konan.target.KonanTarget

/**
* Provides access to the Kotlin/Native distribution components:
* * [stdlibDir] -- stdlib directory
* * [platformDependencies] -- list of directories to platform dependencies
*
* It uses Kotlin Gradle Plugin API that is guaranteed to be present in:
* 1.5 <= kotlinVersion <= 1.9
*
* It should not be used with Kotlin versions later than 1.9
*/
internal class KotlinNativeDistributionAccessor(
project: Project
) {
private val konanDistribution = KonanDistribution(
@Suppress("INVISIBLE_MEMBER")
project.konanHome
)

val stdlibDir: File = konanDistribution.stdlib

fun platformDependencies(target: KonanTarget): List<File> = konanDistribution
.platformLibsDir
.resolve(target.name)
.listLibraryFiles()

private fun File.listLibraryFiles(): List<File> = listFiles().orEmpty()
.filter { it.isDirectory || it.extension == "klib" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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)
Expand Down Expand Up @@ -43,8 +42,24 @@ private fun KotlinCompilation.compileClasspathOf(project: Project): FileCollecti
}

private fun KotlinCompilation.newCompileClasspathOf(project: Project): FileCollection {
val compilationClasspath = (compileTaskProvider.get() as? KotlinCompileTool)?.libraries ?: project.files()
return compilationClasspath + platformDependencyFiles(project)
if (this.target.isAndroidTarget()) { // Workaround for https://youtrack.jetbrains.com/issue/KT-33893
return this.classpathOf(project)
}

val result = project.objects.fileCollection()
result.from({ compileDependencyFiles })

val kgpVersion = project.getKgpVersion()
// Since Kotlin 2.0 native distributiuon dependencies will be included to compileDependencyFiles
if (kgpVersion != null && kgpVersion <= KotlinGradlePluginVersion(1, 9, 255)) {
if (this is AbstractKotlinNativeCompilation) {
val kotlinNativeDistributionAccessor = KotlinNativeDistributionAccessor(project)
result.from(kotlinNativeDistributionAccessor.stdlibDir)
result.from(kotlinNativeDistributionAccessor.platformDependencies(konanTarget))
}
}

return result
}

private fun KotlinCompilation.oldCompileClasspathOf(project: Project): FileCollection {
Expand Down

0 comments on commit 9809ae7

Please sign in to comment.