diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCompile.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCompile.kt index 1b9386bb72a..2dd8b065a2a 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCompile.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCompile.kt @@ -18,14 +18,30 @@ package org.jetbrains.kotlin.gradle.dsl import groovy.lang.Closure import org.gradle.api.Task +import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal +import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments +import org.jetbrains.kotlin.compilerRunner.ArgumentUtils -interface CompilerArgumentAware { +interface CompilerArgumentAware { + @get:Input val serializedCompilerArguments: List + get() = ArgumentUtils.convertArgumentsToStringList(prepareCompilerArguments()) + + @get:Internal val defaultSerializedCompilerArguments: List + get() = createCompilerArgs() + .also { setupCompilerArgs(it, defaultsOnly = true) } + .let(ArgumentUtils::convertArgumentsToStringList) + + fun createCompilerArgs(): T + fun setupCompilerArgs(args: T, defaultsOnly: Boolean = false) } -interface KotlinCompile : Task, CompilerArgumentAware { +internal fun CompilerArgumentAware.prepareCompilerArguments() = + createCompilerArgs().also { setupCompilerArgs(it) } + +interface KotlinCompile : Task { @get:Internal val kotlinOptions: T diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptGenerateStubsTask.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptGenerateStubsTask.kt index eca0a29da40..fa9b1f51a0d 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptGenerateStubsTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptGenerateStubsTask.kt @@ -19,6 +19,8 @@ package org.jetbrains.kotlin.gradle.internal import com.intellij.openapi.util.io.FileUtil import org.gradle.api.tasks.* import org.gradle.api.tasks.incremental.IncrementalTaskInputs +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments +import org.jetbrains.kotlin.gradle.dsl.prepareCompilerArguments import org.jetbrains.kotlin.gradle.plugin.kotlinDebug import org.jetbrains.kotlin.gradle.tasks.FilteringSourceRootsContainer import org.jetbrains.kotlin.gradle.tasks.KotlinCompile @@ -58,6 +60,15 @@ open class KaptGenerateStubsTask : KotlinCompile() { !source.isInside(generatedSourcesDir) } + override fun setupCompilerArgs(args: K2JVMCompilerArguments, defaultsOnly: Boolean) { + kotlinCompileTask.setupCompilerArgs(args) + args.pluginClasspaths = (pluginOptions.classpath + args.pluginClasspaths!!).toSet().toTypedArray() + args.pluginOptions = (pluginOptions.arguments + args.pluginOptions!!).toTypedArray() + args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true + args.classpathAsList = this.compileClasspath.toList() + args.destinationAsFile = this.destinationDir + } + override fun execute(inputs: IncrementalTaskInputs) { val sourceRoots = kotlinCompileTask.getSourceRoots() val allKotlinSources = sourceRoots.kotlinSourceFiles @@ -70,15 +81,7 @@ open class KaptGenerateStubsTask : KotlinCompile() { } sourceRoots.log(this.name, logger) - // todo handle the args like those of the compile tasks - val args = createCompilerArgs() - - kotlinCompileTask.setupCompilerArgs(args) - args.pluginClasspaths = (pluginOptions.classpath + args.pluginClasspaths!!).toSet().toTypedArray() - args.pluginOptions = (pluginOptions.arguments + args.pluginOptions!!).toTypedArray() - args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true - args.classpathAsList = this.compileClasspath.toList() - args.destinationAsFile = this.destinationDir + val args = prepareCompilerArguments() compilerCalled = true callCompiler(args, sourceRoots, ChangedFiles(inputs)) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptTask.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptTask.kt index 94f7415b532..b7db4640759 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptTask.kt @@ -1,6 +1,7 @@ package org.jetbrains.kotlin.gradle.internal import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.util.text.StringUtil.compareVersionNumbers import org.gradle.api.GradleException import org.gradle.api.file.FileCollection import org.gradle.api.internal.ConventionTask @@ -9,11 +10,13 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments import org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner import org.jetbrains.kotlin.compilerRunner.OutputItemsCollectorImpl -import org.jetbrains.kotlin.gradle.plugin.compareVersionNumbers +import org.jetbrains.kotlin.gradle.dsl.CompilerArgumentAware +import org.jetbrains.kotlin.gradle.dsl.prepareCompilerArguments import org.jetbrains.kotlin.gradle.tasks.* import java.io.File -open class KaptTask : ConventionTask() { +open class KaptTask : ConventionTask(), CompilerArgumentAware { + @get:Internal internal val pluginOptions = CompilerPluginOptions() @@ -37,6 +40,16 @@ open class KaptTask : ConventionTask() { @get:OutputDirectory lateinit var destinationDir: File + override fun createCompilerArgs(): K2JVMCompilerArguments = K2JVMCompilerArguments() + + override fun setupCompilerArgs(args: K2JVMCompilerArguments, defaultsOnly: Boolean) { + kotlinCompileTask.setupCompilerArgs(args) + + args.pluginClasspaths = (pluginOptions.classpath + args.pluginClasspaths!!).toSet().toTypedArray() + args.pluginOptions = (pluginOptions.arguments + args.pluginOptions!!).toTypedArray() + args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true + } + @get:Classpath @get:InputFiles val classpath: FileCollection get() = kotlinCompileTask.classpath @@ -61,13 +74,7 @@ open class KaptTask : ConventionTask() { val rawSourceRoots = FilteringSourceRootsContainer(sourceRootsFromKotlin, { !isInsideDestinationDirs(it) }) val sourceRoots = SourceRoots.ForJvm.create(kotlinCompileTask.source, rawSourceRoots) - // todo handle the args like those of the compile tasks - val args = K2JVMCompilerArguments() - kotlinCompileTask.setupCompilerArgs(args) - - args.pluginClasspaths = (pluginOptions.classpath + args.pluginClasspaths!!).toSet().toTypedArray() - args.pluginOptions = (pluginOptions.arguments + args.pluginOptions!!).toTypedArray() - args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true + val args = prepareCompilerArguments() val messageCollector = GradleMessageCollector(logger) val outputItemCollector = OutputItemsCollectorImpl() diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt index 2367648098f..535aea2b7ca 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt @@ -32,6 +32,14 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinJsDceOptionsImpl import java.io.File open class KotlinJsDce : AbstractKotlinCompileTool(), KotlinJsDce { + + override fun createCompilerArgs(): K2JSDceArguments = K2JSDceArguments() + + override fun setupCompilerArgs(args: K2JSDceArguments, defaultsOnly: Boolean) { + dceOptionsImpl.updateArguments(args) + args.declarationsToKeep = keep.toTypedArray() + } + private val dceOptionsImpl = KotlinJsDceOptionsImpl() @get:Internal @@ -55,12 +63,7 @@ open class KotlinJsDce : AbstractKotlinCompileTool(), KotlinJs val outputDirArgs = arrayOf("-output-dir", destinationDir.path) - val args = K2JSDceArguments() - dceOptionsImpl.updateArguments(args) - args.declarationsToKeep = keep.toTypedArray() - - //todo handle the args like those of the compile tasks - val argsArray = ArgumentUtils.convertArgumentsToStringList(args).toTypedArray() + val argsArray = serializedCompilerArguments.toTypedArray() val log = GradleKotlinLogger(project.logger) val allArgs = argsArray + outputDirArgs + inputFiles diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt index b505116b780..1a21d10317d 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt @@ -36,6 +36,9 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.compilerRunner.* import org.jetbrains.kotlin.gradle.dsl.* +import org.jetbrains.kotlin.gradle.internal.CompilerArgumentAware +import org.jetbrains.kotlin.gradle.internal.CompilerArgumentAware +import org.jetbrains.kotlin.gradle.internal.prepareCompilerArguments import org.jetbrains.kotlin.gradle.plugin.kotlinDebug import org.jetbrains.kotlin.gradle.plugin.kotlinInfo import org.jetbrains.kotlin.gradle.utils.ParsedGradleVersion @@ -52,7 +55,7 @@ const val KOTLIN_BUILD_DIR_NAME = "kotlin" const val USING_INCREMENTAL_COMPILATION_MESSAGE = "Using Kotlin incremental compilation" const val USING_EXPERIMENTAL_JS_INCREMENTAL_COMPILATION_MESSAGE = "Using experimental Kotlin/JS incremental compilation" -abstract class AbstractKotlinCompileTool() : AbstractCompile() { +abstract class AbstractKotlinCompileTool() : AbstractCompile(), CompilerArgumentAware { // TODO: deprecate and remove @get:Internal var compilerJarFile: File? = null @@ -73,7 +76,7 @@ abstract class AbstractKotlinCompileTool() : AbstractCo protected abstract fun findKotlinCompilerClasspath(project: Project): List } -abstract class AbstractKotlinCompile() : AbstractKotlinCompileTool(), CompilerArgumentAware { +abstract class AbstractKotlinCompile() : AbstractKotlinCompileTool() { @get:LocalState internal val taskBuildDirectory: File get() = File(File(project.buildDir, KOTLIN_BUILD_DIR_NAME), name).apply { mkdirs() } @@ -101,22 +104,6 @@ abstract class AbstractKotlinCompile() : AbstractKo get() = (classpath + additionalClasspath) .filterTo(LinkedHashSet(), File::exists) - @get:Input - override val serializedCompilerArguments: List - get() { - val arguments = createCompilerArgs() - setupCompilerArgs(arguments) - return ArgumentUtils.convertArgumentsToStringList(arguments) - } - - @get:Internal - override val defaultSerializedCompilerArguments: List - get() { - val arguments = createCompilerArgs() - setupCompilerArgs(arguments, true) - return ArgumentUtils.convertArgumentsToStringList(arguments) - } - private val kotlinExt: KotlinProjectExtension get() = project.extensions.findByType(KotlinProjectExtension::class.java)!! @@ -213,8 +200,7 @@ abstract class AbstractKotlinCompile() : AbstractKo } sourceRoots.log(this.name, logger) - val args = createCompilerArgs() - setupCompilerArgs(args) + val args = prepareCompilerArguments() compilerCalled = true callCompiler(args, sourceRoots, ChangedFiles(inputs)) @@ -225,7 +211,7 @@ abstract class AbstractKotlinCompile() : AbstractKo internal abstract fun callCompiler(args: T, sourceRoots: SourceRoots, changedFiles: ChangedFiles) - open fun setupCompilerArgs(args: T, defaultsOnly: Boolean = false) { + override fun setupCompilerArgs(args: T, defaultsOnly: Boolean) { args.coroutinesState = when (coroutines) { Coroutines.ENABLE -> CommonCompilerArguments.ENABLE Coroutines.WARN -> CommonCompilerArguments.WARN