Copy compiler arguments from compile task to kapt task
#KT-15994 fixed
This commit is contained in:
+1
-1
@@ -13,7 +13,7 @@ import java.io.File
|
|||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
private val SYSTEM_LINE_SEPARATOR = System.getProperty("line.separator")
|
val SYSTEM_LINE_SEPARATOR: String = System.getProperty("line.separator")
|
||||||
|
|
||||||
abstract class BaseGradleIT {
|
abstract class BaseGradleIT {
|
||||||
|
|
||||||
|
|||||||
+25
@@ -293,4 +293,29 @@ class Kapt3IT : BaseGradleIT() {
|
|||||||
assertFileExists("processor/build/classes/main/processor/MyProcessor.class")
|
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'" }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+4
-11
@@ -20,7 +20,6 @@ import java.nio.charset.Charset
|
|||||||
|
|
||||||
open class KaptTask : AbstractCompile() {
|
open class KaptTask : AbstractCompile() {
|
||||||
private val rawSourceRoots = FilteringSourceRootsContainer({ !it.isInsideDestinationDir() })
|
private val rawSourceRoots = FilteringSourceRootsContainer({ !it.isInsideDestinationDir() })
|
||||||
private val args = K2JVMCompilerArguments().apply { fillDefaultValues() }
|
|
||||||
|
|
||||||
internal val pluginOptions = CompilerPluginOptions()
|
internal val pluginOptions = CompilerPluginOptions()
|
||||||
internal lateinit var kotlinCompileTask: KotlinCompile
|
internal lateinit var kotlinCompileTask: KotlinCompile
|
||||||
@@ -51,19 +50,13 @@ open class KaptTask : AbstractCompile() {
|
|||||||
classesDir.mkdirs()
|
classesDir.mkdirs()
|
||||||
|
|
||||||
val sourceRoots = SourceRoots.ForJvm.create(getSource(), rawSourceRoots)
|
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 + args.pluginClasspaths).toSet().toTypedArray()
|
||||||
args.pluginClasspaths = (pluginOptions.classpath + pluginOptionsForKotlinCompile.classpath).toSet().toTypedArray()
|
args.pluginOptions = (pluginOptions.arguments + args.pluginOptions).toTypedArray()
|
||||||
args.pluginOptions = (pluginOptions.arguments + pluginOptionsForKotlinCompile.arguments).toTypedArray()
|
|
||||||
args.destinationAsFile = destinationDir
|
|
||||||
args.classpathAsList = compileClasspath
|
|
||||||
args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true
|
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 messageCollector = GradleMessageCollector(logger)
|
||||||
val outputItemCollector = OutputItemsCollectorImpl()
|
val outputItemCollector = OutputItemsCollectorImpl()
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.incremental.ChangedFiles
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
internal open class KotlinCompileCommon : AbstractKotlinCompile<K2MetadataCompilerArguments>() {
|
internal open class KotlinCompileCommon : AbstractKotlinCompile<K2MetadataCompilerArguments>() {
|
||||||
override fun populateCompilerArguments(defaultsOnly: Boolean): K2MetadataCompilerArguments =
|
override fun createCompilerArgs(): K2MetadataCompilerArguments =
|
||||||
K2MetadataCompilerArguments()
|
K2MetadataCompilerArguments()
|
||||||
|
|
||||||
override fun getSourceRoots(): SourceRoots =
|
override fun getSourceRoots(): SourceRoots =
|
||||||
|
|||||||
+27
-22
@@ -51,7 +51,7 @@ const val KOTLIN_BUILD_DIR_NAME = "kotlin"
|
|||||||
const val USING_EXPERIMENTAL_INCREMENTAL_MESSAGE = "Using experimental kotlin incremental compilation"
|
const val USING_EXPERIMENTAL_INCREMENTAL_MESSAGE = "Using experimental kotlin incremental compilation"
|
||||||
|
|
||||||
abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCompile(), CompilerArgumentAware {
|
abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCompile(), CompilerArgumentAware {
|
||||||
abstract protected fun populateCompilerArguments(defaultsOnly: Boolean = false): T
|
abstract protected fun createCompilerArgs(): T
|
||||||
|
|
||||||
protected val additionalClasspath = arrayListOf<File>()
|
protected val additionalClasspath = arrayListOf<File>()
|
||||||
protected val compileClasspath: Iterable<File>
|
protected val compileClasspath: Iterable<File>
|
||||||
@@ -60,15 +60,15 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
|
|||||||
|
|
||||||
override val serializedCompilerArguments: List<String>
|
override val serializedCompilerArguments: List<String>
|
||||||
get() {
|
get() {
|
||||||
val arguments = populateCompilerArguments()
|
val arguments = createCompilerArgs()
|
||||||
arguments.setupCommonCompilerArgs()
|
setupCompilerArgs(arguments)
|
||||||
return ArgumentUtils.convertArgumentsToStringList(arguments)
|
return ArgumentUtils.convertArgumentsToStringList(arguments)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val defaultSerializedCompilerArguments: List<String>
|
override val defaultSerializedCompilerArguments: List<String>
|
||||||
get() {
|
get() {
|
||||||
val arguments = populateCompilerArguments(true)
|
val arguments = createCompilerArgs()
|
||||||
arguments.setupCommonCompilerArgs()
|
setupCompilerArgs(arguments, true)
|
||||||
return ArgumentUtils.convertArgumentsToStringList(arguments)
|
return ArgumentUtils.convertArgumentsToStringList(arguments)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,8 +130,8 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
|
|||||||
}
|
}
|
||||||
|
|
||||||
sourceRoots.log(this.name, logger)
|
sourceRoots.log(this.name, logger)
|
||||||
val args = populateCompilerArguments()
|
val args = createCompilerArgs()
|
||||||
args.setupCommonCompilerArgs()
|
setupCompilerArgs(args)
|
||||||
|
|
||||||
compilerCalled = true
|
compilerCalled = true
|
||||||
callCompiler(args, sourceRoots, ChangedFiles(inputs))
|
callCompiler(args, sourceRoots, ChangedFiles(inputs))
|
||||||
@@ -140,15 +140,15 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
|
|||||||
internal abstract fun getSourceRoots(): SourceRoots
|
internal abstract fun getSourceRoots(): SourceRoots
|
||||||
internal abstract fun callCompiler(args: T, sourceRoots: SourceRoots, changedFiles: ChangedFiles)
|
internal abstract fun callCompiler(args: T, sourceRoots: SourceRoots, changedFiles: ChangedFiles)
|
||||||
|
|
||||||
private fun CommonCompilerArguments.setupCommonCompilerArgs() {
|
open fun setupCompilerArgs(args: T, defaultsOnly: Boolean = false) {
|
||||||
coroutines.let {
|
coroutines.let {
|
||||||
coroutinesEnable = it == Coroutines.ENABLE
|
args.coroutinesEnable = it == Coroutines.ENABLE
|
||||||
coroutinesWarn = it == Coroutines.WARN
|
args.coroutinesWarn = it == Coroutines.WARN
|
||||||
coroutinesError = it == Coroutines.ERROR
|
args.coroutinesError = it == Coroutines.ERROR
|
||||||
}
|
}
|
||||||
|
|
||||||
if (project.logger.isDebugEnabled) {
|
if (project.logger.isDebugEnabled) {
|
||||||
verbose = true
|
args.verbose = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,8 +185,12 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
override fun findKotlinCompilerJar(project: Project): File? =
|
override fun findKotlinCompilerJar(project: Project): File? =
|
||||||
findKotlinJvmCompilerJar(project)
|
findKotlinJvmCompilerJar(project)
|
||||||
|
|
||||||
override fun populateCompilerArguments(defaultsOnly: Boolean): K2JVMCompilerArguments {
|
override fun createCompilerArgs(): K2JVMCompilerArguments =
|
||||||
val args = K2JVMCompilerArguments().apply { fillDefaultValues() }
|
K2JVMCompilerArguments()
|
||||||
|
|
||||||
|
override fun setupCompilerArgs(args: K2JVMCompilerArguments, defaultsOnly: Boolean) {
|
||||||
|
super.setupCompilerArgs(args, defaultsOnly)
|
||||||
|
args.apply { fillDefaultValues() }
|
||||||
|
|
||||||
handleKaptProperties()
|
handleKaptProperties()
|
||||||
args.pluginClasspaths = pluginOptions.classpath.toTypedArray()
|
args.pluginClasspaths = pluginOptions.classpath.toTypedArray()
|
||||||
@@ -201,13 +205,14 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
|
|
||||||
friendTaskName?.let { addFriendPathForTestTask(it, args) }
|
friendTaskName?.let { addFriendPathForTestTask(it, args) }
|
||||||
|
|
||||||
if (defaultsOnly) return args
|
if (defaultsOnly) return
|
||||||
|
|
||||||
|
args.classpathAsList = compileClasspath.toList()
|
||||||
|
args.destinationAsFile = destinationDir
|
||||||
parentKotlinOptionsImpl?.updateArguments(args)
|
parentKotlinOptionsImpl?.updateArguments(args)
|
||||||
kotlinOptionsImpl.updateArguments(args)
|
kotlinOptionsImpl.updateArguments(args)
|
||||||
|
|
||||||
logger.kotlinDebug { "$name destinationDir = $destinationDir" }
|
logger.kotlinDebug { "$name destinationDir = $destinationDir" }
|
||||||
return args
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun addFriendPathForTestTask(friendKotlinTaskName: String, args: K2JVMCompilerArguments) {
|
internal fun addFriendPathForTestTask(friendKotlinTaskName: String, args: K2JVMCompilerArguments) {
|
||||||
@@ -223,8 +228,6 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
sourceRoots as SourceRoots.ForJvm
|
sourceRoots as SourceRoots.ForJvm
|
||||||
|
|
||||||
val messageCollector = GradleMessageCollector(logger)
|
val messageCollector = GradleMessageCollector(logger)
|
||||||
args.classpathAsList = compileClasspath.toList()
|
|
||||||
args.destinationAsFile = destinationDir
|
|
||||||
val outputItemCollector = OutputItemsCollectorImpl()
|
val outputItemCollector = OutputItemsCollectorImpl()
|
||||||
val compilerRunner = GradleCompilerRunner(project)
|
val compilerRunner = GradleCompilerRunner(project)
|
||||||
val reporter = GradleICReporter(project.rootProject.projectDir)
|
val reporter = GradleICReporter(project.rootProject.projectDir)
|
||||||
@@ -324,15 +327,17 @@ open class Kotlin2JsCompile() : AbstractKotlinCompile<K2JSCompilerArguments>(),
|
|||||||
override fun findKotlinCompilerJar(project: Project): File? =
|
override fun findKotlinCompilerJar(project: Project): File? =
|
||||||
findKotlinJsCompilerJar(project)
|
findKotlinJsCompilerJar(project)
|
||||||
|
|
||||||
override fun populateCompilerArguments(defaultsOnly: Boolean): K2JSCompilerArguments {
|
override fun createCompilerArgs(): K2JSCompilerArguments =
|
||||||
val args = K2JSCompilerArguments().apply { fillDefaultValues() }
|
K2JSCompilerArguments()
|
||||||
|
|
||||||
|
override fun setupCompilerArgs(args: K2JSCompilerArguments, defaultsOnly: Boolean) {
|
||||||
|
super.setupCompilerArgs(args, defaultsOnly)
|
||||||
|
args.apply { fillDefaultValues() }
|
||||||
args.outputFile = outputFile
|
args.outputFile = outputFile
|
||||||
|
|
||||||
if (defaultsOnly) return args
|
if (defaultsOnly) return
|
||||||
|
|
||||||
kotlinOptionsImpl.updateArguments(args)
|
kotlinOptionsImpl.updateArguments(args)
|
||||||
return args
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getSourceRoots() = SourceRoots.KotlinOnly.create(getSource())
|
override fun getSourceRoots() = SourceRoots.KotlinOnly.create(getSource())
|
||||||
|
|||||||
Reference in New Issue
Block a user