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 kotlin.test.*
|
||||
|
||||
private val SYSTEM_LINE_SEPARATOR = System.getProperty("line.separator")
|
||||
val SYSTEM_LINE_SEPARATOR: String = System.getProperty("line.separator")
|
||||
|
||||
abstract class BaseGradleIT {
|
||||
|
||||
|
||||
+25
@@ -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'" }
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-11
@@ -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()
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.incremental.ChangedFiles
|
||||
import java.io.File
|
||||
|
||||
internal open class KotlinCompileCommon : AbstractKotlinCompile<K2MetadataCompilerArguments>() {
|
||||
override fun populateCompilerArguments(defaultsOnly: Boolean): K2MetadataCompilerArguments =
|
||||
override fun createCompilerArgs(): K2MetadataCompilerArguments =
|
||||
K2MetadataCompilerArguments()
|
||||
|
||||
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"
|
||||
|
||||
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 compileClasspath: Iterable<File>
|
||||
@@ -60,15 +60,15 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
|
||||
|
||||
override val serializedCompilerArguments: List<String>
|
||||
get() {
|
||||
val arguments = populateCompilerArguments()
|
||||
arguments.setupCommonCompilerArgs()
|
||||
val arguments = createCompilerArgs()
|
||||
setupCompilerArgs(arguments)
|
||||
return ArgumentUtils.convertArgumentsToStringList(arguments)
|
||||
}
|
||||
|
||||
override val defaultSerializedCompilerArguments: List<String>
|
||||
get() {
|
||||
val arguments = populateCompilerArguments(true)
|
||||
arguments.setupCommonCompilerArgs()
|
||||
val arguments = createCompilerArgs()
|
||||
setupCompilerArgs(arguments, true)
|
||||
return ArgumentUtils.convertArgumentsToStringList(arguments)
|
||||
}
|
||||
|
||||
@@ -130,8 +130,8 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : 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<T : CommonCompilerArguments>() : 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<K2JVMCompilerArguments>(), 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<K2JVMCompilerArguments>(), 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<K2JVMCompilerArguments>(), 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<K2JSCompilerArguments>(),
|
||||
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())
|
||||
|
||||
Reference in New Issue
Block a user