diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BaseGradleIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BaseGradleIT.kt index 9452e00b276..96f595f4282 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BaseGradleIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BaseGradleIT.kt @@ -13,7 +13,7 @@ import java.io.File import java.util.regex.Pattern import kotlin.test.* -private val SYSTEM_LINE_SEPARATOR = System.getProperty("line.separator") +val SYSTEM_LINE_SEPARATOR: String = System.getProperty("line.separator") abstract class BaseGradleIT { diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kapt3IT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kapt3IT.kt index 46caa0f3a24..c61b9c38a77 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kapt3IT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kapt3IT.kt @@ -293,4 +293,29 @@ class Kapt3IT : BaseGradleIT() { assertFileExists("processor/build/classes/main/processor/MyProcessor.class") } } + + /** + * Tests that compile arguments are properly copied from compileKotlin to kaptTask + */ + @Test + fun testCopyCompileArguments() { + val project = Project("simple", GRADLE_VERSION, directoryPrefix = "kapt2") + project.setupWorkingDir() + + val arg = "-Xskip-runtime-version-check" + project.projectDir.getFileByName("build.gradle").modify { + it + """ + $SYSTEM_LINE_SEPARATOR + compileKotlin { kotlinOptions.freeCompilerArgs = ['$arg'] } + """.trimIndent() + } + + project.build("build") { + assertSuccessful() + assertKaptSuccessful() + val regex = "(?m)^.*Kotlin compiler args.*-P plugin:org\\.jetbrains\\.kotlin\\.kapt3.*$".toRegex() + val kaptArgs = regex.find(output)?.value ?: error("Kapt compiler arguments are not found!") + assert(kaptArgs.contains(arg)) { "Kapt compiler arguments should contain '$arg'" } + } + } } \ No newline at end of file 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 c0bdf5f365d..79e49c611f4 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 @@ -20,7 +20,6 @@ import java.nio.charset.Charset open class KaptTask : AbstractCompile() { private val rawSourceRoots = FilteringSourceRootsContainer({ !it.isInsideDestinationDir() }) - private val args = K2JVMCompilerArguments().apply { fillDefaultValues() } internal val pluginOptions = CompilerPluginOptions() internal lateinit var kotlinCompileTask: KotlinCompile @@ -51,19 +50,13 @@ open class KaptTask : AbstractCompile() { classesDir.mkdirs() val sourceRoots = SourceRoots.ForJvm.create(getSource(), rawSourceRoots) - val compileClasspath = classpath.toList().filter(File::exists) - val pluginOptionsForKotlinCompile = kotlinCompileTask.pluginOptions + val args = K2JVMCompilerArguments() + kotlinCompileTask.setupCompilerArgs(args) - args.moduleName = kotlinCompileTask.moduleName - args.pluginClasspaths = (pluginOptions.classpath + pluginOptionsForKotlinCompile.classpath).toSet().toTypedArray() - args.pluginOptions = (pluginOptions.arguments + pluginOptionsForKotlinCompile.arguments).toTypedArray() - args.destinationAsFile = destinationDir - args.classpathAsList = compileClasspath + 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 - kotlinCompileTask.friendTaskName?.let { kotlinCompileTask.addFriendPathForTestTask(it, args) } - kotlinCompileTask.parentKotlinOptionsImpl?.updateArguments(args) - KotlinJvmOptionsImpl().updateArguments(args) val messageCollector = GradleMessageCollector(logger) val outputItemCollector = OutputItemsCollectorImpl() diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompileCommon.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompileCommon.kt index 9af913e2d56..c24d98f09db 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompileCommon.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompileCommon.kt @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.incremental.ChangedFiles import java.io.File internal open class KotlinCompileCommon : AbstractKotlinCompile() { - override fun populateCompilerArguments(defaultsOnly: Boolean): K2MetadataCompilerArguments = + override fun createCompilerArgs(): K2MetadataCompilerArguments = K2MetadataCompilerArguments() override fun getSourceRoots(): SourceRoots = 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 c5015a77104..3f38c3dfeb6 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 @@ -51,7 +51,7 @@ const val KOTLIN_BUILD_DIR_NAME = "kotlin" const val USING_EXPERIMENTAL_INCREMENTAL_MESSAGE = "Using experimental kotlin incremental compilation" abstract class AbstractKotlinCompile() : AbstractCompile(), CompilerArgumentAware { - abstract protected fun populateCompilerArguments(defaultsOnly: Boolean = false): T + abstract protected fun createCompilerArgs(): T protected val additionalClasspath = arrayListOf() protected val compileClasspath: Iterable @@ -60,15 +60,15 @@ abstract class AbstractKotlinCompile() : AbstractCo override val serializedCompilerArguments: List get() { - val arguments = populateCompilerArguments() - arguments.setupCommonCompilerArgs() + val arguments = createCompilerArgs() + setupCompilerArgs(arguments) return ArgumentUtils.convertArgumentsToStringList(arguments) } override val defaultSerializedCompilerArguments: List get() { - val arguments = populateCompilerArguments(true) - arguments.setupCommonCompilerArgs() + val arguments = createCompilerArgs() + setupCompilerArgs(arguments, true) return ArgumentUtils.convertArgumentsToStringList(arguments) } @@ -130,8 +130,8 @@ abstract class AbstractKotlinCompile() : AbstractCo } sourceRoots.log(this.name, logger) - val args = populateCompilerArguments() - args.setupCommonCompilerArgs() + val args = createCompilerArgs() + setupCompilerArgs(args) compilerCalled = true callCompiler(args, sourceRoots, ChangedFiles(inputs)) @@ -140,15 +140,15 @@ abstract class AbstractKotlinCompile() : AbstractCo internal abstract fun getSourceRoots(): SourceRoots internal abstract fun callCompiler(args: T, sourceRoots: SourceRoots, changedFiles: ChangedFiles) - private fun CommonCompilerArguments.setupCommonCompilerArgs() { + open fun setupCompilerArgs(args: T, defaultsOnly: Boolean = false) { coroutines.let { - coroutinesEnable = it == Coroutines.ENABLE - coroutinesWarn = it == Coroutines.WARN - coroutinesError = it == Coroutines.ERROR + args.coroutinesEnable = it == Coroutines.ENABLE + args.coroutinesWarn = it == Coroutines.WARN + args.coroutinesError = it == Coroutines.ERROR } if (project.logger.isDebugEnabled) { - verbose = true + args.verbose = true } } } @@ -185,8 +185,12 @@ open class KotlinCompile : AbstractKotlinCompile(), Kotl override fun findKotlinCompilerJar(project: Project): File? = findKotlinJvmCompilerJar(project) - override fun populateCompilerArguments(defaultsOnly: Boolean): K2JVMCompilerArguments { - val args = K2JVMCompilerArguments().apply { fillDefaultValues() } + override fun createCompilerArgs(): K2JVMCompilerArguments = + K2JVMCompilerArguments() + + override fun setupCompilerArgs(args: K2JVMCompilerArguments, defaultsOnly: Boolean) { + super.setupCompilerArgs(args, defaultsOnly) + args.apply { fillDefaultValues() } handleKaptProperties() args.pluginClasspaths = pluginOptions.classpath.toTypedArray() @@ -201,13 +205,14 @@ open class KotlinCompile : AbstractKotlinCompile(), Kotl friendTaskName?.let { addFriendPathForTestTask(it, args) } - if (defaultsOnly) return args + if (defaultsOnly) return + args.classpathAsList = compileClasspath.toList() + args.destinationAsFile = destinationDir parentKotlinOptionsImpl?.updateArguments(args) kotlinOptionsImpl.updateArguments(args) logger.kotlinDebug { "$name destinationDir = $destinationDir" } - return args } internal fun addFriendPathForTestTask(friendKotlinTaskName: String, args: K2JVMCompilerArguments) { @@ -223,8 +228,6 @@ open class KotlinCompile : AbstractKotlinCompile(), Kotl sourceRoots as SourceRoots.ForJvm val messageCollector = GradleMessageCollector(logger) - args.classpathAsList = compileClasspath.toList() - args.destinationAsFile = destinationDir val outputItemCollector = OutputItemsCollectorImpl() val compilerRunner = GradleCompilerRunner(project) val reporter = GradleICReporter(project.rootProject.projectDir) @@ -324,15 +327,17 @@ open class Kotlin2JsCompile() : AbstractKotlinCompile(), override fun findKotlinCompilerJar(project: Project): File? = findKotlinJsCompilerJar(project) - override fun populateCompilerArguments(defaultsOnly: Boolean): K2JSCompilerArguments { - val args = K2JSCompilerArguments().apply { fillDefaultValues() } + override fun createCompilerArgs(): K2JSCompilerArguments = + K2JSCompilerArguments() + override fun setupCompilerArgs(args: K2JSCompilerArguments, defaultsOnly: Boolean) { + super.setupCompilerArgs(args, defaultsOnly) + args.apply { fillDefaultValues() } args.outputFile = outputFile - if (defaultsOnly) return args + if (defaultsOnly) return kotlinOptionsImpl.updateArguments(args) - return args } override fun getSourceRoots() = SourceRoots.KotlinOnly.create(getSource())