From 350ce9a2a69ab2e217d391ba78ab89c76fecbb66 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Tue, 15 Aug 2017 20:01:24 +0700 Subject: [PATCH] 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 --- GRADLE_PLUGIN.md | 1 + .../kotlin/gradle/plugin/KonanCompileTask.kt | 54 +++++++++---------- .../kotlin/gradle/plugin/KonanInteropTask.kt | 11 ++-- .../kotlin/gradle/plugin/KonanPlugin.kt | 28 +--------- 4 files changed, 32 insertions(+), 62 deletions(-) diff --git a/GRADLE_PLUGIN.md b/GRADLE_PLUGIN.md index d54864795dc..aac2ece955a 100644 --- a/GRADLE_PLUGIN.md +++ b/GRADLE_PLUGIN.md @@ -116,6 +116,7 @@ You may get this task using the `compilationTask` property of an artifact or by |`inputFiles `|`Collection`|Compiled files | |`libraries `|`Collection`|*.klib libraries used by the artifact | |`nativeLibraries `|`Collection`|*.bc libraries used by the artifact | + |`interops `|`Collection` |All the interops used by the artifact | |`linkerOpts `|`List` |Additional options passed to the linker | |`enableDebug `|`boolean` |Is the debugging support enabled | |`noStdLib `|`boolean` |Is the artifact not linked with stdlib | diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanCompileTask.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanCompileTask.kt index 209d2d4a2b7..388795f74a6 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanCompileTask.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanCompileTask.kt @@ -103,7 +103,9 @@ open class KonanCompileTask: KonanTargetableTask() { // Other compilation parameters ------------------------------------------- - @InputFiles val inputFiles = mutableSetOf() + internal val _inputFiles = mutableSetOf() + val inputFiles: Collection + @InputFiles get() = _inputFiles.takeIf { !it.isEmpty() } ?: listOf(project.konanDefaultSrcFiles) @InputFiles val libraries = mutableSetOf() @InputFiles val nativeLibraries = mutableSetOf() @@ -111,8 +113,14 @@ open class KonanCompileTask: KonanTargetableTask() { @Input var produce = "program" internal set - @Input var linkerOpts = mutableListOf() - internal set + @Internal val interops = mutableSetOf() + + internal var _linkerOpts = mutableListOf() + val linkerOpts: List + @Input get() = mutableListOf().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() - 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) = compilationTask.inputFiles.addAll(files) + fun inputFiles(files: FileCollection) = compilationTask._inputFiles.add(files) + fun inputFiles(files: Collection) = compilationTask._inputFiles.addAll(files) fun outputDir(dir: Any) = with(compilationTask) { @@ -280,7 +278,7 @@ open class KonanCompileConfig( fun linkerOpts(args: List) = linkerOpts(*args.toTypedArray()) fun linkerOpts(vararg args: String) = with(compilationTask) { - linkerOpts.addAll(args) + _linkerOpts.addAll(args) } fun manifest(arg: Any) = with(compilationTask) { diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanInteropTask.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanInteropTask.kt index dc541f863d7..03a7f0771dd 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanInteropTask.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanInteropTask.kt @@ -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 @@ -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) { diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanPlugin.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanPlugin.kt index 838a0763002..50a0211ab40 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanPlugin.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanPlugin.kt @@ -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 { @@ -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"))