Attempt to make loading of compiler plugins from kotlin-platform-native Gradle plugin
Moved SubpluginEnviroment to separate file and made public some methods KotlinNativeCompile task now extends AbstractCompile so it could be passed to SubpluginEnviroment.loadSubplugins KotlinNativeBasePlugin now declares kotlinCompilerPluginClasspath configuration
This commit is contained in:
committed by
Ilya Matveev
parent
bddd6534a6
commit
8dc5af35c5
+19
-9
@@ -23,6 +23,7 @@ import org.gradle.api.Task
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.internal.FeaturePreviews
|
||||
import org.gradle.api.internal.project.ProjectInternal
|
||||
import org.gradle.api.logging.Logging
|
||||
import org.gradle.api.plugins.BasePlugin
|
||||
import org.gradle.api.plugins.HelpTasksPlugin
|
||||
import org.gradle.api.tasks.TaskContainer
|
||||
@@ -30,14 +31,12 @@ import org.gradle.language.base.plugins.LifecycleBasePlugin
|
||||
import org.gradle.language.plugins.NativeBasePlugin
|
||||
import org.gradle.nativeplatform.test.tasks.RunTestExecutable
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.gradle.plugin.KonanPlugin
|
||||
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||
import org.jetbrains.kotlin.gradle.plugin.*
|
||||
import org.jetbrains.kotlin.gradle.plugin.experimental.*
|
||||
import org.jetbrains.kotlin.gradle.plugin.experimental.internal.*
|
||||
import org.jetbrains.kotlin.gradle.plugin.experimental.tasks.CInteropTask
|
||||
import org.jetbrains.kotlin.gradle.plugin.experimental.tasks.KotlinNativeCompile
|
||||
import org.jetbrains.kotlin.gradle.plugin.hasProperty
|
||||
import org.jetbrains.kotlin.gradle.plugin.konanCompilerDownloadDir
|
||||
import org.jetbrains.kotlin.gradle.plugin.setProperty
|
||||
import org.jetbrains.kotlin.gradle.plugin.tasks.KonanCompilerDownloadTask
|
||||
import org.jetbrains.kotlin.konan.target.CompilerOutputKind
|
||||
import org.jetbrains.kotlin.konan.target.HostManager
|
||||
@@ -45,6 +44,8 @@ import org.jetbrains.kotlin.konan.target.KonanTarget
|
||||
|
||||
class KotlinNativeBasePlugin: Plugin<ProjectInternal> {
|
||||
|
||||
private val log = Logging.getLogger(this.javaClass)
|
||||
|
||||
private fun TaskContainer.createRunTestTask(
|
||||
taskName: String,
|
||||
binary: KotlinNativeTestExecutableImpl,
|
||||
@@ -107,6 +108,7 @@ class KotlinNativeBasePlugin: Plugin<ProjectInternal> {
|
||||
|
||||
// TODO: Rework this part: the task should be created in the binary constructor (if it is possible).
|
||||
private fun Project.addCompilationTasks() {
|
||||
val kotlinVersion = this@KotlinNativeBasePlugin.loadKotlinVersionFromResource(log)
|
||||
val assembleTask = tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME)
|
||||
val typeToAssemble = mutableMapOf<KotlinNativeBuildType, Task>()
|
||||
val targetToAssemble = mutableMapOf<KonanTarget, Task>()
|
||||
@@ -117,6 +119,10 @@ class KotlinNativeBasePlugin: Plugin<ProjectInternal> {
|
||||
assembleTask.dependsOn(it)
|
||||
}
|
||||
|
||||
project.configurations.maybeCreate(PLUGIN_CLASSPATH_CONFIGURATION_NAME).apply {
|
||||
isTransitive = false
|
||||
}
|
||||
|
||||
components.withType(AbstractKotlinNativeBinary::class.java) { binary ->
|
||||
val names = binary.names
|
||||
val target = binary.konanTarget
|
||||
@@ -126,9 +132,13 @@ class KotlinNativeBasePlugin: Plugin<ProjectInternal> {
|
||||
names.getCompileTaskName(LANGUAGE_NAME),
|
||||
KotlinNativeCompile::class.java,
|
||||
binary
|
||||
).apply {
|
||||
group = BasePlugin.BUILD_GROUP
|
||||
description = "Compiles Kotlin/Native source set '${binary.sourceSet.name}' into a ${binary.kind.name.toLowerCase()}"
|
||||
).also {
|
||||
it.group = BasePlugin.BUILD_GROUP
|
||||
it.description = "Compiles Kotlin/Native source set '${binary.sourceSet.name}' into a ${binary.kind.name.toLowerCase()}"
|
||||
|
||||
SubpluginEnvironment.loadSubplugins(this, kotlinVersion)
|
||||
.addSubpluginOptions<CommonCompilerArguments>(this, it, it.compilerPluginOptions)
|
||||
it.compilerPluginClasspath = project.configurations.getByName(PLUGIN_CLASSPATH_CONFIGURATION_NAME)
|
||||
|
||||
// Register an API header produced for shared/static library as a task output.
|
||||
if (binary.kind == CompilerOutputKind.DYNAMIC || binary.kind == CompilerOutputKind.STATIC) {
|
||||
@@ -136,10 +146,10 @@ class KotlinNativeBasePlugin: Plugin<ProjectInternal> {
|
||||
with(binary) {
|
||||
val prefix = kind.prefix(konanTarget)
|
||||
val baseName = getBaseName().get().replace('-', '_')
|
||||
outputFile.parentFile.resolve("$prefix${baseName}_api.h")
|
||||
it.outputFile.parentFile.resolve("$prefix${baseName}_api.h")
|
||||
}
|
||||
}
|
||||
outputs.file(headerFileProvider)
|
||||
it.outputs.file(headerFileProvider)
|
||||
}
|
||||
}
|
||||
binary.compileTask.set(compileTask)
|
||||
|
||||
+28
-4
@@ -16,25 +16,26 @@
|
||||
|
||||
package org.jetbrains.kotlin.gradle.plugin.experimental.tasks
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.artifacts.Configuration
|
||||
import org.gradle.api.file.FileCollection
|
||||
import org.gradle.api.file.FileSystemLocation
|
||||
import org.gradle.api.file.FileTree
|
||||
import org.gradle.api.provider.Provider
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.InputFiles
|
||||
import org.gradle.api.tasks.Optional
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.gradle.api.tasks.compile.AbstractCompile
|
||||
import org.jetbrains.kotlin.gradle.plugin.*
|
||||
import org.jetbrains.kotlin.gradle.plugin.experimental.internal.AbstractKotlinNativeBinary
|
||||
import org.jetbrains.kotlin.gradle.tasks.CompilerPluginOptions
|
||||
import org.jetbrains.kotlin.konan.target.CompilerOutputKind
|
||||
import org.jetbrains.kotlin.konan.target.CompilerOutputKind.*
|
||||
import org.jetbrains.kotlin.konan.target.KonanTarget
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
open class KotlinNativeCompile @Inject constructor(internal val binary: AbstractKotlinNativeBinary)
|
||||
: DefaultTask()
|
||||
open class KotlinNativeCompile @Inject constructor(internal val binary: AbstractKotlinNativeBinary) : AbstractCompile()
|
||||
{
|
||||
init {
|
||||
super.dependsOn(KonanPlugin.KONAN_DOWNLOAD_TASK_NAME)
|
||||
@@ -45,6 +46,8 @@ open class KotlinNativeCompile @Inject constructor(internal val binary: Abstract
|
||||
val sources: FileCollection
|
||||
@InputFiles get() = binary.sources
|
||||
|
||||
override fun getSource(): FileTree = sources.asFileTree
|
||||
|
||||
private val commonSources: FileCollection
|
||||
get() = with(binary.sourceSet) {
|
||||
getCommonMultiplatformSources() + getCommonNativeSources()
|
||||
@@ -68,6 +71,10 @@ open class KotlinNativeCompile @Inject constructor(internal val binary: Abstract
|
||||
val entryPoint: String?
|
||||
@Optional @Input get() = binary.component.entryPoint
|
||||
|
||||
val compilerPluginOptions = CompilerPluginOptions()
|
||||
|
||||
var compilerPluginClasspath: FileCollection? = null
|
||||
|
||||
val outputFile: File
|
||||
get() = outputLocationProvider.get().asFile
|
||||
|
||||
@@ -105,10 +112,18 @@ open class KotlinNativeCompile @Inject constructor(internal val binary: Abstract
|
||||
}
|
||||
}
|
||||
|
||||
// initializing AbstractCompile properties
|
||||
init {
|
||||
classpath = libraries
|
||||
destinationDir = if (outputFile.isDirectory) outputFile else outputFile.parentFile
|
||||
sourceCompatibility = "1.6"
|
||||
targetCompatibility = "1.6"
|
||||
}
|
||||
|
||||
// Task action
|
||||
|
||||
@TaskAction
|
||||
fun compile() {
|
||||
override fun compile() {
|
||||
outputFile.parentFile.mkdirs()
|
||||
|
||||
val args = mutableListOf<String>().apply {
|
||||
@@ -124,6 +139,15 @@ open class KotlinNativeCompile @Inject constructor(internal val binary: Abstract
|
||||
|
||||
addArgIfNotNull("-entry", entryPoint)
|
||||
|
||||
compilerPluginClasspath?.let { pluginClasspath ->
|
||||
pluginClasspath.map { it.canonicalPath }.sorted().forEach { path ->
|
||||
add("-Xplugin=$path")
|
||||
}
|
||||
compilerPluginOptions.arguments.forEach {
|
||||
add("-P$it")
|
||||
}
|
||||
}
|
||||
|
||||
addAll(additionalCompilerOptions)
|
||||
|
||||
libraries.files.forEach {library ->
|
||||
|
||||
Reference in New Issue
Block a user