Add libraries provided by the K/N compiler in dependencies
This commit is contained in:
committed by
Ilya Matveev
parent
e433778268
commit
48d8f5eb39
+23
-1
@@ -6,12 +6,14 @@
|
||||
package org.jetbrains.kotlin.gradle.plugin.mpp
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.file.FileCollection
|
||||
import org.gradle.api.internal.file.FileResolver
|
||||
import org.gradle.api.plugins.JavaPlugin
|
||||
import org.gradle.internal.cleanup.BuildOutputCleanupRegistry
|
||||
import org.gradle.internal.reflect.Instantiator
|
||||
import org.jetbrains.kotlin.compilerRunner.KotlinNativeProjectProperty
|
||||
import org.jetbrains.kotlin.compilerRunner.hasProperty
|
||||
import org.jetbrains.kotlin.compilerRunner.konanHome
|
||||
import org.jetbrains.kotlin.compilerRunner.setProperty
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
|
||||
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
||||
@@ -250,7 +252,19 @@ class KotlinNativeTargetPreset(
|
||||
tasks.maybeCreate(KONAN_DOWNLOAD_TASK_NAME, KonanCompilerDownloadTask::class.java)
|
||||
}
|
||||
|
||||
private fun stdlib(target: KonanTarget): FileCollection = with(project) {
|
||||
files("${konanHome}/klib/common/stdlib").builtBy(createCompilerDownloadingTask())
|
||||
}
|
||||
|
||||
private fun platformLibs(target: KonanTarget): FileCollection = with(project) {
|
||||
files(provider {
|
||||
file("${project.konanHome}/klib/platform/${target.name}").listFiles { file -> file.isDirectory } ?: emptyArray()
|
||||
})
|
||||
}
|
||||
|
||||
override fun createTarget(name: String): KotlinNativeTarget {
|
||||
createCompilerDownloadingTask()
|
||||
|
||||
val result = KotlinNativeTarget(project, konanTarget).apply {
|
||||
targetName = name
|
||||
disambiguationClassifier = name
|
||||
@@ -259,8 +273,16 @@ class KotlinNativeTargetPreset(
|
||||
compilations = project.container(compilationFactory.itemClass, compilationFactory)
|
||||
}
|
||||
|
||||
createCompilerDownloadingTask()
|
||||
KotlinNativeTargetConfigurator(buildOutputCleanupRegistry).configureTarget(result)
|
||||
|
||||
// Allow IDE to resolve the libraries provided by the compiler by adding them into dependencies.
|
||||
result.compilations.all {
|
||||
val target = it.target.konanTarget
|
||||
it.dependencies {
|
||||
implementation(stdlib(target))
|
||||
implementation(platformLibs(target))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
+17
-8
@@ -40,9 +40,7 @@ open class KotlinNativeCompile : DefaultTask() {
|
||||
get() = compilation.allSources
|
||||
|
||||
val libraries: FileCollection
|
||||
@InputFiles get() = compilation.compileDependencyFiles.filter {
|
||||
it.extension == "klib"
|
||||
}
|
||||
@InputFiles get() = compilation.compileDependencyFiles
|
||||
|
||||
@Input
|
||||
var optimized = false
|
||||
@@ -58,6 +56,12 @@ open class KotlinNativeCompile : DefaultTask() {
|
||||
val additionalCompilerOptions: Collection<String>
|
||||
@Input get() = compilation.extraOpts
|
||||
|
||||
val kotlinNativeVersion: String
|
||||
@Input get() = KonanCompilerDownloadTask.compilerVersion.toString()
|
||||
|
||||
val kotlinNativeHome: File
|
||||
@Input get() = project.file(project.konanHome)
|
||||
|
||||
// We manually register this property as output file or directory depending on output kind.
|
||||
@Internal
|
||||
val outputFile: Property<File> = project.objects.property(File::class.java)
|
||||
@@ -107,6 +111,9 @@ open class KotlinNativeCompile : DefaultTask() {
|
||||
}
|
||||
// endregion
|
||||
|
||||
private val File.providedByCompiler: Boolean
|
||||
get() = toPath().startsWith(project.file(project.konanHome).toPath())
|
||||
|
||||
@TaskAction
|
||||
fun compile() {
|
||||
val output = outputFile.get()
|
||||
@@ -126,7 +133,10 @@ open class KotlinNativeCompile : DefaultTask() {
|
||||
|
||||
addAll(additionalCompilerOptions)
|
||||
|
||||
libraries.files.forEach { library ->
|
||||
libraries.files.filter {
|
||||
// Support only klib files for now.
|
||||
it.extension == "klib" && !it.providedByCompiler
|
||||
}.forEach { library ->
|
||||
library.parent?.let { addArg("-r", it) }
|
||||
addArg("-l", library.nameWithoutExtension)
|
||||
}
|
||||
@@ -212,24 +222,23 @@ open class KonanCompilerDownloadTask : DefaultTask() {
|
||||
val archive = configuration.files.single()
|
||||
|
||||
logger.info("Use Kotlin/Native compiler archive: ${archive.absolutePath}")
|
||||
logger.lifecycle("Unpack Kotlin/Native compiler (version $versionString)...")
|
||||
logger.lifecycle("Unpacking Kotlin/Native compiler (version $versionString)...")
|
||||
project.copy {
|
||||
it.from(archiveFileTree(archive))
|
||||
it.into(DependencyDirectories.localKonanDir)
|
||||
}
|
||||
|
||||
removeRepo(repo)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@TaskAction
|
||||
fun checkCompiler() {
|
||||
if (!project.hasProperty(KotlinNativeProjectProperty.DOWNLOAD_COMPILER)) {
|
||||
val konanHome = project.getProperty(KotlinNativeProjectProperty.KONAN_HOME)
|
||||
logger.info("Use a user-defined compiler path: $konanHome")
|
||||
} else {
|
||||
if (!compilerDirectory.exists()) {
|
||||
// TODO: Move Kotlin/Native Distribution class in the Big Kotlin repo and use it here.
|
||||
if (KonanCompilerRunner(project).classpath.isEmpty) {
|
||||
downloadAndExtract()
|
||||
}
|
||||
logger.info("Use Kotlin/Native distribution: $compilerDirectory")
|
||||
|
||||
Reference in New Issue
Block a user