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:
Ilya Matveev
2017-08-15 20:01:24 +07:00
committed by ilmat192
parent 4c52639ee7
commit 350ce9a2a6
4 changed files with 32 additions and 62 deletions
+1
View File
@@ -116,6 +116,7 @@ You may get this task using the `compilationTask` property of an artifact or by
|`inputFiles `|`Collection<FileCollection>`|Compiled files |
|`libraries `|`Collection<FileCollection>`|*.klib 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 |
|`enableDebug `|`boolean` |Is the debugging support enabled |
|`noStdLib `|`boolean` |Is the artifact not linked with stdlib |
@@ -103,7 +103,9 @@ open class KonanCompileTask: KonanTargetableTask() {
// 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 nativeLibraries = mutableSetOf<FileCollection>()
@@ -111,8 +113,14 @@ open class KonanCompileTask: KonanTargetableTask() {
@Input var produce = "program"
internal set
@Input var linkerOpts = mutableListOf<String>()
internal set
@Internal val interops = mutableSetOf<KonanInteropConfig>()
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()
internal set
@@ -151,6 +159,7 @@ open class KonanCompileTask: KonanTargetableTask() {
addArgIfNotNull("-target", target)
addArgIfNotNull("-language-version", languageVersion)
addArgIfNotNull("-api-version", apiVersion)
// TODO: Should we get manifests of the interops used?
addArgIfNotNull("-manifest", manifest?.canonicalPath)
addKey("-g", enableDebug)
@@ -178,7 +187,6 @@ open class KonanCompileTask: KonanTargetableTask() {
}
}
}
}
// TODO: check debug outputs
@@ -190,8 +198,6 @@ open class KonanCompileConfig(
override fun getName() = configName
val interops = mutableSetOf<KonanInteropConfig>()
val compilationTask: KonanCompileTask = project.tasks.create(
"$taskNamePrefix${configName.capitalize()}",
KonanCompileTask::class.java) {
@@ -200,28 +206,20 @@ open class KonanCompileConfig(
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 compileStubsTask = interop.compileStubsTask
val compileStubsTask = interop.compileStubsTask
compilationTask.dependsOn(compileStubsTask)
compilationTask.dependsOn(generateStubsTask)
linkerOpts(generateStubsTask.linkerOpts)
library(compileStubsTask.artifact)
nativeLibraries(project.fileTree(generateStubsTask.libsDir).apply {
builtBy(generateStubsTask)
dependsOn(compileStubsTask)
dependsOn(generateStubsTask)
library(project.files(compileStubsTask.artifact))
nativeLibrary(project.fileTree(generateStubsTask.libsDir).apply {
include("**/*.bc")
})
generateStubsTask.manifest ?.let {manifest(it)}
}
internal fun processInterops() = interops.forEach(this::addInteropParameters)
// DSL methods --------------------------------------------------
fun useInterop(interopConfig: KonanInteropConfig) {
interops.add(interopConfig)
interops.add(interop)
}
fun useInterop(interop: String) {
@@ -241,13 +239,13 @@ open class KonanCompileConfig(
// DSL. Input/output files
fun inputDir(dir: String) = with(compilationTask) {
inputFiles.add(project.fileTree(dir))
_inputFiles.add(project.fileTree(dir))
}
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: Collection<FileCollection>) = compilationTask.inputFiles.addAll(files)
fun inputFiles(files: FileCollection) = compilationTask._inputFiles.add(files)
fun inputFiles(files: Collection<FileCollection>) = compilationTask._inputFiles.addAll(files)
fun outputDir(dir: Any) = with(compilationTask) {
@@ -280,7 +278,7 @@ open class KonanCompileConfig(
fun linkerOpts(args: List<String>) = linkerOpts(*args.toTypedArray())
fun linkerOpts(vararg args: String) = with(compilationTask) {
linkerOpts.addAll(args)
_linkerOpts.addAll(args)
}
fun manifest(arg: Any) = with(compilationTask) {
@@ -53,6 +53,7 @@ open class KonanInteropTask: KonanTargetableTask() {
internal fun init(libName: String) {
dependsOn(project.konanCompilerDownloadTask)
this.libName = libName
this.defFile = project.konanDefaultDefFile(libName)
}
internal val INTEROP_JVM_ARGS: List<String>
@@ -70,7 +71,7 @@ open class KonanInteropTask: KonanTargetableTask() {
// Interop stub generator parameters -------------------------------------
@InputFile var defFile: File? = null
@InputFile lateinit var defFile: File
internal set
@Optional @Input var pkg: String? = null
@@ -164,7 +165,7 @@ open class KonanInteropConfig(
compilationTask.dependsOn(generateStubsTask)
outputDir("${project.konanInteropCompiledStubsDir}/$name")
produce("library")
inputFiles(project.fileTree(generateStubsTask.stubsDir).apply { builtBy(generateStubsTask) })
inputFiles(project.fileTree(generateStubsTask.stubsDir))
manifest("${generateStubsTask.stubsDir.path}/manifest.properties")
compilationTask.group = BasePlugin.BUILD_GROUP
compilationTask.description = "Compiles stubs for the Kotlin/Native interop '${this@KonanInteropConfig.name}'"
@@ -174,11 +175,7 @@ open class KonanInteropConfig(
// DSL methods ------------------------------------------------------------
fun defFile(file: Any) = with(generateStubsTask) {
defFile = project.file(file).also {
if (!it.exists()) {
throw NoSuchFileException(it, reason = "No def-file by the specified path.")
}
}
defFile = project.file(file)
}
fun pkg(value: String) = with(generateStubsTask) {
@@ -56,7 +56,7 @@ internal val Project.konanInteropStubsOutputDir get() = "${konanBuildRoot}/int
internal val Project.konanInteropCompiledStubsDir get() = "${konanBuildRoot}/interopCompiledStubs"
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)
= 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)
: Plugin<Project> {
@@ -280,14 +262,6 @@ class KonanPlugin @Inject constructor(private val registry: ToolingModelBuilderR
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.
project.getOrCreateTask("run").apply {
dependsOn(project.getTask("build"))