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
|
package org.jetbrains.kotlin.gradle.plugin.mpp
|
||||||
|
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.api.file.FileCollection
|
||||||
import org.gradle.api.internal.file.FileResolver
|
import org.gradle.api.internal.file.FileResolver
|
||||||
import org.gradle.api.plugins.JavaPlugin
|
import org.gradle.api.plugins.JavaPlugin
|
||||||
import org.gradle.internal.cleanup.BuildOutputCleanupRegistry
|
import org.gradle.internal.cleanup.BuildOutputCleanupRegistry
|
||||||
import org.gradle.internal.reflect.Instantiator
|
import org.gradle.internal.reflect.Instantiator
|
||||||
import org.jetbrains.kotlin.compilerRunner.KotlinNativeProjectProperty
|
import org.jetbrains.kotlin.compilerRunner.KotlinNativeProjectProperty
|
||||||
import org.jetbrains.kotlin.compilerRunner.hasProperty
|
import org.jetbrains.kotlin.compilerRunner.hasProperty
|
||||||
|
import org.jetbrains.kotlin.compilerRunner.konanHome
|
||||||
import org.jetbrains.kotlin.compilerRunner.setProperty
|
import org.jetbrains.kotlin.compilerRunner.setProperty
|
||||||
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
|
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
|
||||||
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
||||||
@@ -250,7 +252,19 @@ class KotlinNativeTargetPreset(
|
|||||||
tasks.maybeCreate(KONAN_DOWNLOAD_TASK_NAME, KonanCompilerDownloadTask::class.java)
|
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 {
|
override fun createTarget(name: String): KotlinNativeTarget {
|
||||||
|
createCompilerDownloadingTask()
|
||||||
|
|
||||||
val result = KotlinNativeTarget(project, konanTarget).apply {
|
val result = KotlinNativeTarget(project, konanTarget).apply {
|
||||||
targetName = name
|
targetName = name
|
||||||
disambiguationClassifier = name
|
disambiguationClassifier = name
|
||||||
@@ -259,8 +273,16 @@ class KotlinNativeTargetPreset(
|
|||||||
compilations = project.container(compilationFactory.itemClass, compilationFactory)
|
compilations = project.container(compilationFactory.itemClass, compilationFactory)
|
||||||
}
|
}
|
||||||
|
|
||||||
createCompilerDownloadingTask()
|
|
||||||
KotlinNativeTargetConfigurator(buildOutputCleanupRegistry).configureTarget(result)
|
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
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-8
@@ -40,9 +40,7 @@ open class KotlinNativeCompile : DefaultTask() {
|
|||||||
get() = compilation.allSources
|
get() = compilation.allSources
|
||||||
|
|
||||||
val libraries: FileCollection
|
val libraries: FileCollection
|
||||||
@InputFiles get() = compilation.compileDependencyFiles.filter {
|
@InputFiles get() = compilation.compileDependencyFiles
|
||||||
it.extension == "klib"
|
|
||||||
}
|
|
||||||
|
|
||||||
@Input
|
@Input
|
||||||
var optimized = false
|
var optimized = false
|
||||||
@@ -58,6 +56,12 @@ open class KotlinNativeCompile : DefaultTask() {
|
|||||||
val additionalCompilerOptions: Collection<String>
|
val additionalCompilerOptions: Collection<String>
|
||||||
@Input get() = compilation.extraOpts
|
@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.
|
// We manually register this property as output file or directory depending on output kind.
|
||||||
@Internal
|
@Internal
|
||||||
val outputFile: Property<File> = project.objects.property(File::class.java)
|
val outputFile: Property<File> = project.objects.property(File::class.java)
|
||||||
@@ -107,6 +111,9 @@ open class KotlinNativeCompile : DefaultTask() {
|
|||||||
}
|
}
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
private val File.providedByCompiler: Boolean
|
||||||
|
get() = toPath().startsWith(project.file(project.konanHome).toPath())
|
||||||
|
|
||||||
@TaskAction
|
@TaskAction
|
||||||
fun compile() {
|
fun compile() {
|
||||||
val output = outputFile.get()
|
val output = outputFile.get()
|
||||||
@@ -126,7 +133,10 @@ open class KotlinNativeCompile : DefaultTask() {
|
|||||||
|
|
||||||
addAll(additionalCompilerOptions)
|
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) }
|
library.parent?.let { addArg("-r", it) }
|
||||||
addArg("-l", library.nameWithoutExtension)
|
addArg("-l", library.nameWithoutExtension)
|
||||||
}
|
}
|
||||||
@@ -212,24 +222,23 @@ open class KonanCompilerDownloadTask : DefaultTask() {
|
|||||||
val archive = configuration.files.single()
|
val archive = configuration.files.single()
|
||||||
|
|
||||||
logger.info("Use Kotlin/Native compiler archive: ${archive.absolutePath}")
|
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 {
|
project.copy {
|
||||||
it.from(archiveFileTree(archive))
|
it.from(archiveFileTree(archive))
|
||||||
it.into(DependencyDirectories.localKonanDir)
|
it.into(DependencyDirectories.localKonanDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
removeRepo(repo)
|
removeRepo(repo)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@TaskAction
|
@TaskAction
|
||||||
fun checkCompiler() {
|
fun checkCompiler() {
|
||||||
if (!project.hasProperty(KotlinNativeProjectProperty.DOWNLOAD_COMPILER)) {
|
if (!project.hasProperty(KotlinNativeProjectProperty.DOWNLOAD_COMPILER)) {
|
||||||
val konanHome = project.getProperty(KotlinNativeProjectProperty.KONAN_HOME)
|
val konanHome = project.getProperty(KotlinNativeProjectProperty.KONAN_HOME)
|
||||||
logger.info("Use a user-defined compiler path: $konanHome")
|
logger.info("Use a user-defined compiler path: $konanHome")
|
||||||
} else {
|
} 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()
|
downloadAndExtract()
|
||||||
}
|
}
|
||||||
logger.info("Use Kotlin/Native distribution: $compilerDirectory")
|
logger.info("Use Kotlin/Native distribution: $compilerDirectory")
|
||||||
|
|||||||
Reference in New Issue
Block a user