gradle-plugin: Don't use afterEvaluate to configure tasks
Gradle plugin used afterEvaluate method to configure default parameters for tasks. This behavior might lead to problems if user also uses the afterEvaluate hook. This patch moves the default setting logic into the tasks. The following actions have been removed from the afterEvaluate hook: * Setting default source directories for KonanCompileTask * Setting default def-file for KonanInteropTask
This commit is contained in:
@@ -116,6 +116,7 @@ You may get this task using the `compilationTask` property of an artifact or by
|
|||||||
|`inputFiles `|`Collection<FileCollection>`|Compiled files |
|
|`inputFiles `|`Collection<FileCollection>`|Compiled files |
|
||||||
|`libraries `|`Collection<FileCollection>`|*.klib libraries used by the artifact |
|
|`libraries `|`Collection<FileCollection>`|*.klib libraries used by the artifact |
|
||||||
|`nativeLibraries `|`Collection<FileCollection>`|*.bc libraries used by the artifact |
|
|`nativeLibraries `|`Collection<FileCollection>`|*.bc libraries used by the artifact |
|
||||||
|
|`interops `|`Collection<Interop>` |All the interops used by the artifact |
|
||||||
|`linkerOpts `|`List<String>` |Additional options passed to the linker |
|
|`linkerOpts `|`List<String>` |Additional options passed to the linker |
|
||||||
|`enableDebug `|`boolean` |Is the debugging support enabled |
|
|`enableDebug `|`boolean` |Is the debugging support enabled |
|
||||||
|`noStdLib `|`boolean` |Is the artifact not linked with stdlib |
|
|`noStdLib `|`boolean` |Is the artifact not linked with stdlib |
|
||||||
|
|||||||
+26
-28
@@ -103,7 +103,9 @@ open class KonanCompileTask: KonanTargetableTask() {
|
|||||||
|
|
||||||
// Other compilation parameters -------------------------------------------
|
// Other compilation parameters -------------------------------------------
|
||||||
|
|
||||||
@InputFiles val inputFiles = mutableSetOf<FileCollection>()
|
internal val _inputFiles = mutableSetOf<FileCollection>()
|
||||||
|
val inputFiles: Collection<FileCollection>
|
||||||
|
@InputFiles get() = _inputFiles.takeIf { !it.isEmpty() } ?: listOf(project.konanDefaultSrcFiles)
|
||||||
|
|
||||||
@InputFiles val libraries = mutableSetOf<FileCollection>()
|
@InputFiles val libraries = mutableSetOf<FileCollection>()
|
||||||
@InputFiles val nativeLibraries = mutableSetOf<FileCollection>()
|
@InputFiles val nativeLibraries = mutableSetOf<FileCollection>()
|
||||||
@@ -111,8 +113,14 @@ open class KonanCompileTask: KonanTargetableTask() {
|
|||||||
@Input var produce = "program"
|
@Input var produce = "program"
|
||||||
internal set
|
internal set
|
||||||
|
|
||||||
@Input var linkerOpts = mutableListOf<String>()
|
@Internal val interops = mutableSetOf<KonanInteropConfig>()
|
||||||
internal set
|
|
||||||
|
internal var _linkerOpts = mutableListOf<String>()
|
||||||
|
val linkerOpts: List<String>
|
||||||
|
@Input get() = mutableListOf<String>().apply {
|
||||||
|
addAll(_linkerOpts)
|
||||||
|
interops.flatMapTo(this) { it.generateStubsTask.linkerOpts }
|
||||||
|
}
|
||||||
|
|
||||||
@Input var enableDebug = project.properties.containsKey("enableDebug") && project.properties["enableDebug"].toString().toBoolean()
|
@Input var enableDebug = project.properties.containsKey("enableDebug") && project.properties["enableDebug"].toString().toBoolean()
|
||||||
internal set
|
internal set
|
||||||
@@ -151,6 +159,7 @@ open class KonanCompileTask: KonanTargetableTask() {
|
|||||||
addArgIfNotNull("-target", target)
|
addArgIfNotNull("-target", target)
|
||||||
addArgIfNotNull("-language-version", languageVersion)
|
addArgIfNotNull("-language-version", languageVersion)
|
||||||
addArgIfNotNull("-api-version", apiVersion)
|
addArgIfNotNull("-api-version", apiVersion)
|
||||||
|
// TODO: Should we get manifests of the interops used?
|
||||||
addArgIfNotNull("-manifest", manifest?.canonicalPath)
|
addArgIfNotNull("-manifest", manifest?.canonicalPath)
|
||||||
|
|
||||||
addKey("-g", enableDebug)
|
addKey("-g", enableDebug)
|
||||||
@@ -178,7 +187,6 @@ open class KonanCompileTask: KonanTargetableTask() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check debug outputs
|
// TODO: check debug outputs
|
||||||
@@ -190,8 +198,6 @@ open class KonanCompileConfig(
|
|||||||
|
|
||||||
override fun getName() = configName
|
override fun getName() = configName
|
||||||
|
|
||||||
val interops = mutableSetOf<KonanInteropConfig>()
|
|
||||||
|
|
||||||
val compilationTask: KonanCompileTask = project.tasks.create(
|
val compilationTask: KonanCompileTask = project.tasks.create(
|
||||||
"$taskNamePrefix${configName.capitalize()}",
|
"$taskNamePrefix${configName.capitalize()}",
|
||||||
KonanCompileTask::class.java) {
|
KonanCompileTask::class.java) {
|
||||||
@@ -200,28 +206,20 @@ open class KonanCompileConfig(
|
|||||||
it.description = "Compiles the Kotlin/Native artifact '${this@KonanCompileConfig.name}'"
|
it.description = "Compiles the Kotlin/Native artifact '${this@KonanCompileConfig.name}'"
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun addInteropParameters(interop: KonanInteropConfig) {
|
// DSL methods. Interop. --------------------------------------------------
|
||||||
|
|
||||||
|
fun useInterop(interop: KonanInteropConfig) = with(compilationTask) {
|
||||||
val generateStubsTask = interop.generateStubsTask
|
val generateStubsTask = interop.generateStubsTask
|
||||||
val compileStubsTask = interop.compileStubsTask
|
val compileStubsTask = interop.compileStubsTask
|
||||||
|
|
||||||
compilationTask.dependsOn(compileStubsTask)
|
dependsOn(compileStubsTask)
|
||||||
compilationTask.dependsOn(generateStubsTask)
|
dependsOn(generateStubsTask)
|
||||||
|
library(project.files(compileStubsTask.artifact))
|
||||||
linkerOpts(generateStubsTask.linkerOpts)
|
nativeLibrary(project.fileTree(generateStubsTask.libsDir).apply {
|
||||||
library(compileStubsTask.artifact)
|
|
||||||
nativeLibraries(project.fileTree(generateStubsTask.libsDir).apply {
|
|
||||||
builtBy(generateStubsTask)
|
|
||||||
include("**/*.bc")
|
include("**/*.bc")
|
||||||
})
|
})
|
||||||
generateStubsTask.manifest ?.let {manifest(it)}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun processInterops() = interops.forEach(this::addInteropParameters)
|
interops.add(interop)
|
||||||
|
|
||||||
// DSL methods --------------------------------------------------
|
|
||||||
|
|
||||||
fun useInterop(interopConfig: KonanInteropConfig) {
|
|
||||||
interops.add(interopConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun useInterop(interop: String) {
|
fun useInterop(interop: String) {
|
||||||
@@ -241,13 +239,13 @@ open class KonanCompileConfig(
|
|||||||
// DSL. Input/output files
|
// DSL. Input/output files
|
||||||
|
|
||||||
fun inputDir(dir: String) = with(compilationTask) {
|
fun inputDir(dir: String) = with(compilationTask) {
|
||||||
inputFiles.add(project.fileTree(dir))
|
_inputFiles.add(project.fileTree(dir))
|
||||||
}
|
}
|
||||||
fun inputFiles(vararg files: Any) = with(compilationTask) {
|
fun inputFiles(vararg files: Any) = with(compilationTask) {
|
||||||
inputFiles.add(project.files(files))
|
_inputFiles.add(project.files(files))
|
||||||
}
|
}
|
||||||
fun inputFiles(files: FileCollection) = compilationTask.inputFiles.add(files)
|
fun inputFiles(files: FileCollection) = compilationTask._inputFiles.add(files)
|
||||||
fun inputFiles(files: Collection<FileCollection>) = compilationTask.inputFiles.addAll(files)
|
fun inputFiles(files: Collection<FileCollection>) = compilationTask._inputFiles.addAll(files)
|
||||||
|
|
||||||
|
|
||||||
fun outputDir(dir: Any) = with(compilationTask) {
|
fun outputDir(dir: Any) = with(compilationTask) {
|
||||||
@@ -280,7 +278,7 @@ open class KonanCompileConfig(
|
|||||||
|
|
||||||
fun linkerOpts(args: List<String>) = linkerOpts(*args.toTypedArray())
|
fun linkerOpts(args: List<String>) = linkerOpts(*args.toTypedArray())
|
||||||
fun linkerOpts(vararg args: String) = with(compilationTask) {
|
fun linkerOpts(vararg args: String) = with(compilationTask) {
|
||||||
linkerOpts.addAll(args)
|
_linkerOpts.addAll(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun manifest(arg: Any) = with(compilationTask) {
|
fun manifest(arg: Any) = with(compilationTask) {
|
||||||
|
|||||||
+4
-7
@@ -53,6 +53,7 @@ open class KonanInteropTask: KonanTargetableTask() {
|
|||||||
internal fun init(libName: String) {
|
internal fun init(libName: String) {
|
||||||
dependsOn(project.konanCompilerDownloadTask)
|
dependsOn(project.konanCompilerDownloadTask)
|
||||||
this.libName = libName
|
this.libName = libName
|
||||||
|
this.defFile = project.konanDefaultDefFile(libName)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal val INTEROP_JVM_ARGS: List<String>
|
internal val INTEROP_JVM_ARGS: List<String>
|
||||||
@@ -70,7 +71,7 @@ open class KonanInteropTask: KonanTargetableTask() {
|
|||||||
|
|
||||||
// Interop stub generator parameters -------------------------------------
|
// Interop stub generator parameters -------------------------------------
|
||||||
|
|
||||||
@InputFile var defFile: File? = null
|
@InputFile lateinit var defFile: File
|
||||||
internal set
|
internal set
|
||||||
|
|
||||||
@Optional @Input var pkg: String? = null
|
@Optional @Input var pkg: String? = null
|
||||||
@@ -164,7 +165,7 @@ open class KonanInteropConfig(
|
|||||||
compilationTask.dependsOn(generateStubsTask)
|
compilationTask.dependsOn(generateStubsTask)
|
||||||
outputDir("${project.konanInteropCompiledStubsDir}/$name")
|
outputDir("${project.konanInteropCompiledStubsDir}/$name")
|
||||||
produce("library")
|
produce("library")
|
||||||
inputFiles(project.fileTree(generateStubsTask.stubsDir).apply { builtBy(generateStubsTask) })
|
inputFiles(project.fileTree(generateStubsTask.stubsDir))
|
||||||
manifest("${generateStubsTask.stubsDir.path}/manifest.properties")
|
manifest("${generateStubsTask.stubsDir.path}/manifest.properties")
|
||||||
compilationTask.group = BasePlugin.BUILD_GROUP
|
compilationTask.group = BasePlugin.BUILD_GROUP
|
||||||
compilationTask.description = "Compiles stubs for the Kotlin/Native interop '${this@KonanInteropConfig.name}'"
|
compilationTask.description = "Compiles stubs for the Kotlin/Native interop '${this@KonanInteropConfig.name}'"
|
||||||
@@ -174,11 +175,7 @@ open class KonanInteropConfig(
|
|||||||
// DSL methods ------------------------------------------------------------
|
// DSL methods ------------------------------------------------------------
|
||||||
|
|
||||||
fun defFile(file: Any) = with(generateStubsTask) {
|
fun defFile(file: Any) = with(generateStubsTask) {
|
||||||
defFile = project.file(file).also {
|
defFile = project.file(file)
|
||||||
if (!it.exists()) {
|
|
||||||
throw NoSuchFileException(it, reason = "No def-file by the specified path.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun pkg(value: String) = with(generateStubsTask) {
|
fun pkg(value: String) = with(generateStubsTask) {
|
||||||
|
|||||||
+1
-27
@@ -56,7 +56,7 @@ internal val Project.konanInteropStubsOutputDir get() = "${konanBuildRoot}/int
|
|||||||
internal val Project.konanInteropCompiledStubsDir get() = "${konanBuildRoot}/interopCompiledStubs"
|
internal val Project.konanInteropCompiledStubsDir get() = "${konanBuildRoot}/interopCompiledStubs"
|
||||||
internal val Project.konanInteropLibsOutputDir get() = "${konanBuildRoot}/nativelibs"
|
internal val Project.konanInteropLibsOutputDir get() = "${konanBuildRoot}/nativelibs"
|
||||||
|
|
||||||
internal val Project.konanDefaultSrcDir get() = file("${projectDir.canonicalPath}/src/main/kotlin")
|
internal val Project.konanDefaultSrcFiles get() = fileTree("${projectDir.canonicalPath}/src/main/kotlin")
|
||||||
internal fun Project.konanDefaultDefFile(libName: String)
|
internal fun Project.konanDefaultDefFile(libName: String)
|
||||||
= file("${projectDir.canonicalPath}/src/main/c_interop/$libName.def")
|
= file("${projectDir.canonicalPath}/src/main/c_interop/$libName.def")
|
||||||
|
|
||||||
@@ -194,24 +194,6 @@ internal fun dumpProperties(task: Task) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun setDefaultInputs(project: Project) {
|
|
||||||
project.konanArtifactsContainer.asSequence()
|
|
||||||
.filter { it.compilationTask.inputFiles.isEmpty() }
|
|
||||||
.forEach { config ->
|
|
||||||
project.konanDefaultSrcDir.takeIf { it.exists() }?.let {
|
|
||||||
config.inputDir(it.canonicalPath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
project.konanInteropContainer.asSequence()
|
|
||||||
.filter { it.generateStubsTask.defFile == null }
|
|
||||||
.forEach { config ->
|
|
||||||
project.konanDefaultDefFile(config.name).takeIf { it.exists() }?.let {
|
|
||||||
config.defFile(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class KonanPlugin @Inject constructor(private val registry: ToolingModelBuilderRegistry)
|
class KonanPlugin @Inject constructor(private val registry: ToolingModelBuilderRegistry)
|
||||||
: Plugin<Project> {
|
: Plugin<Project> {
|
||||||
|
|
||||||
@@ -280,14 +262,6 @@ class KonanPlugin @Inject constructor(private val registry: ToolingModelBuilderR
|
|||||||
dependsOn(compileKonanTask)
|
dependsOn(compileKonanTask)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add default source paths after project evaluation.
|
|
||||||
project.afterEvaluate(::setDefaultInputs)
|
|
||||||
|
|
||||||
// Set compilation parameters for artifacts using interop.
|
|
||||||
project.afterEvaluate { prj ->
|
|
||||||
prj.konanArtifactsContainer.forEach { it.processInterops() }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create task to run supported executables.
|
// Create task to run supported executables.
|
||||||
project.getOrCreateTask("run").apply {
|
project.getOrCreateTask("run").apply {
|
||||||
dependsOn(project.getTask("build"))
|
dependsOn(project.getTask("build"))
|
||||||
|
|||||||
Reference in New Issue
Block a user