Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the native stdlib and platform libraries from K/N distribution #3145

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ 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
import org.jetbrains.kotlin.gradle.utils.NativeCompilerDownloader
import org.jetbrains.kotlin.konan.library.KONAN_DISTRIBUTION_COMMON_LIBS_DIR
import org.jetbrains.kotlin.konan.library.KONAN_DISTRIBUTION_KLIB_DIR
import org.jetbrains.kotlin.konan.library.KONAN_DISTRIBUTION_PLATFORM_LIBS_DIR
import org.jetbrains.kotlin.konan.library.KONAN_STDLIB_NAME
import org.jetbrains.kotlin.konan.target.KonanTarget
import java.io.File

internal fun Project.classpathOf(sourceSet: KotlinSourceSet): FileCollection {
val compilations = compilationsOf(sourceSet)
Expand Down Expand Up @@ -79,7 +86,7 @@ private fun KotlinCompilation.getKotlinCompileTask(kgpVersion: KotlinGradlePlugi
private fun KotlinCompilation.platformDependencyFiles(project: Project): FileCollection {
val excludePlatformDependencyFiles = project.classpathProperty("excludePlatformDependencyFiles", default = false)

if (excludePlatformDependencyFiles) return project.files()
if (excludePlatformDependencyFiles) return getPlatfromDependenciesFromKonanDistribution(project)
return (this as? AbstractKotlinNativeCompilation)
?.target?.project?.configurations
?.findByName(@Suppress("DEPRECATION") this.defaultSourceSet.implementationMetadataConfigurationName) // KT-58640
Expand All @@ -88,3 +95,37 @@ private fun KotlinCompilation.platformDependencyFiles(project: Project): FileCol

private fun Project.classpathProperty(name: String, default: Boolean): Boolean =
(findProperty("org.jetbrains.dokka.classpath.$name") as? String)?.toBoolean() ?: default

// -------- The hack for platform dependencies from compiler ------------------
// adapted from https://github.com/jetbrains/kotlin/blob/b6a215d681695a2fe0cc798308966c5675de447f/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/KotlinNativePlatformDependencies.kt#L39

private fun KotlinCompilation.getPlatfromDependenciesFromKonanDistribution(project: Project): FileCollection {
return if (this is AbstractKotlinNativeCompilation)
project.files(project.getStdLibFromKonanDistribution()) + project.files(project.getPlatformLibsFromKonanDistribution(this.konanTarget))
else
project.files()
}

private fun Project.nativeHome(): String? =
this.findProperty("org.jetbrains.kotlin.native.home") as? String

// copy-pasted from https://github.com/jetbrains/kotlin/blob/c9aeadd31f763646237faffab38a57923c520fa1/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/nativeToolRunners.kt#L29
private fun Project.konanHome(): String {
return nativeHome()?.let { file(it).absolutePath }
?: NativeCompilerDownloader(project).compilerDirectory.absolutePath
}

private fun Project.getStdLibFromKonanDistribution(): File {
val root = file(this.konanHome())
val konanCommonLibraries = root.resolve(KONAN_DISTRIBUTION_KLIB_DIR).resolve(KONAN_DISTRIBUTION_COMMON_LIBS_DIR)
val stdlib = konanCommonLibraries.resolve(KONAN_STDLIB_NAME)
return stdlib
}
//copy-pasted from https://github.com/jetbrains/kotlin/blob/05a6d89151e6a7230faf733e51161b5f07ae10a7/native/commonizer/src/org/jetbrains/kotlin/commonizer/repository/KonanDistributionRepository.kt#L20
// https://github.com/jetbrains/kotlin/blob/b6a215d681695a2fe0cc798308966c5675de447f/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/KotlinNativePlatformDependencies.kt#L83
private fun Project.getPlatformLibsFromKonanDistribution(target: KonanTarget): Array<out File>? {
val root = file(this.konanHome())
val platformLibsDir = root.resolve(KONAN_DISTRIBUTION_KLIB_DIR).resolve(KONAN_DISTRIBUTION_PLATFORM_LIBS_DIR)
val libs = platformLibsDir.resolve(target.name).takeIf { it.isDirectory }?.listFiles()
return libs
}
Loading