Refactor the kapt Gradle plugin to use the new plugins API and TCA
This commit is contained in:
+281
-216
@@ -7,44 +7,56 @@ package org.jetbrains.kotlin.gradle.internal
|
|||||||
|
|
||||||
import com.android.build.gradle.BaseExtension
|
import com.android.build.gradle.BaseExtension
|
||||||
import com.android.build.gradle.api.AndroidSourceSet
|
import com.android.build.gradle.api.AndroidSourceSet
|
||||||
import com.android.builder.model.SourceProvider
|
import com.android.build.gradle.api.BaseVariant
|
||||||
|
import com.android.build.gradle.api.SourceKind
|
||||||
import com.intellij.openapi.util.SystemInfo
|
import com.intellij.openapi.util.SystemInfo
|
||||||
import org.gradle.api.GradleException
|
import org.gradle.api.GradleException
|
||||||
import org.gradle.api.Plugin
|
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.Task
|
|
||||||
import org.gradle.api.artifacts.Configuration
|
import org.gradle.api.artifacts.Configuration
|
||||||
import org.gradle.api.artifacts.Dependency
|
import org.gradle.api.artifacts.Dependency
|
||||||
import org.gradle.api.attributes.Attribute
|
import org.gradle.api.attributes.Attribute
|
||||||
import org.gradle.api.attributes.Usage
|
import org.gradle.api.attributes.Usage
|
||||||
import org.gradle.api.tasks.SourceSet
|
import org.gradle.api.file.FileCollection
|
||||||
import org.gradle.api.tasks.TaskDependency
|
import org.gradle.api.provider.Provider
|
||||||
|
import org.gradle.api.tasks.*
|
||||||
import org.gradle.api.tasks.compile.AbstractCompile
|
import org.gradle.api.tasks.compile.AbstractCompile
|
||||||
import org.gradle.api.tasks.compile.JavaCompile
|
import org.gradle.api.tasks.compile.JavaCompile
|
||||||
import org.gradle.process.CommandLineArgumentProvider
|
import org.gradle.process.CommandLineArgumentProvider
|
||||||
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
||||||
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.getKaptGeneratedClassesDir
|
|
||||||
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.getKaptGeneratedKotlinSourcesDir
|
|
||||||
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.getKaptGeneratedSourcesDir
|
|
||||||
import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.Companion.KAPT_WORKER_DEPENDENCIES_CONFIGURATION_NAME
|
|
||||||
import org.jetbrains.kotlin.gradle.internal.kapt.incremental.CLASS_STRUCTURE_ARTIFACT_TYPE
|
import org.jetbrains.kotlin.gradle.internal.kapt.incremental.CLASS_STRUCTURE_ARTIFACT_TYPE
|
||||||
import org.jetbrains.kotlin.gradle.internal.kapt.incremental.StructureArtifactTransform
|
import org.jetbrains.kotlin.gradle.internal.kapt.incremental.StructureArtifactTransform
|
||||||
import org.jetbrains.kotlin.gradle.model.builder.KaptModelBuilder
|
import org.jetbrains.kotlin.gradle.model.builder.KaptModelBuilder
|
||||||
import org.jetbrains.kotlin.gradle.plugin.*
|
import org.jetbrains.kotlin.gradle.plugin.*
|
||||||
import org.jetbrains.kotlin.gradle.tasks.CompilerPluginOptions
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompileTaskData
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompileTaskData
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
import org.jetbrains.kotlin.gradle.tasks.locateTask
|
||||||
|
import org.jetbrains.kotlin.gradle.tasks.registerTask
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.ObjectOutputStream
|
import java.io.ObjectOutputStream
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import java.util.concurrent.Callable
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
// apply plugin: 'kotlin-kapt'
|
// apply plugin: 'kotlin-kapt'
|
||||||
class Kapt3GradleSubplugin @Inject internal constructor(private val registry: ToolingModelBuilderRegistry) : Plugin<Project> {
|
class Kapt3GradleSubplugin @Inject internal constructor(private val registry: ToolingModelBuilderRegistry) :
|
||||||
companion object {
|
KotlinCompilerPluginSupportPlugin {
|
||||||
fun isEnabled(project: Project) = project.plugins.findPlugin(Kapt3GradleSubplugin::class.java) != null
|
|
||||||
|
|
||||||
|
override fun apply(project: Project) {
|
||||||
|
project.extensions.create("kapt", KaptExtension::class.java)
|
||||||
|
|
||||||
|
project.configurations.create(KAPT_WORKER_DEPENDENCIES_CONFIGURATION_NAME).apply {
|
||||||
|
project.getKotlinPluginVersion()?.let { kotlinPluginVersion ->
|
||||||
|
val kaptDependency = getPluginArtifact().run { "$groupId:$artifactId:$kotlinPluginVersion" }
|
||||||
|
dependencies.add(project.dependencies.create(kaptDependency))
|
||||||
|
} ?: throw GradleException("Kotlin plugin should be enabled before 'kotlin-kapt'")
|
||||||
|
}
|
||||||
|
|
||||||
|
registry.register(KaptModelBuilder())
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getKaptGeneratedClassesDir(project: Project, sourceSetName: String) =
|
fun getKaptGeneratedClassesDir(project: Project, sourceSetName: String) =
|
||||||
File(project.buildDir, "tmp/kapt3/classes/$sourceSetName")
|
File(project.buildDir, "tmp/kapt3/classes/$sourceSetName")
|
||||||
@@ -56,41 +68,7 @@ class Kapt3GradleSubplugin @Inject internal constructor(private val registry: To
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getKaptGeneratedKotlinSourcesDir(project: Project, sourceSetName: String) =
|
fun getKaptGeneratedKotlinSourcesDir(project: Project, sourceSetName: String) =
|
||||||
File(project.buildDir, "generated/source/kaptKotlin/$sourceSetName")
|
File(project.buildDir, "generated/source/kaptKotlin/$sourceSetName")
|
||||||
}
|
|
||||||
|
|
||||||
override fun apply(project: Project) {
|
|
||||||
project.extensions.create("kapt", KaptExtension::class.java)
|
|
||||||
|
|
||||||
Kapt3KotlinGradleSubplugin().run {
|
|
||||||
project.configurations.create(KAPT_WORKER_DEPENDENCIES_CONFIGURATION_NAME).apply {
|
|
||||||
project.getKotlinPluginVersion()?.let { kotlinPluginVersion ->
|
|
||||||
val kaptDependency = getPluginArtifact().run { "$groupId:$artifactId:$kotlinPluginVersion" }
|
|
||||||
dependencies.add(project.dependencies.create(kaptDependency))
|
|
||||||
} ?: throw GradleException("Kotlin plugin should be enabled before 'kotlin-kapt'")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
registry.register(KaptModelBuilder())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class KaptVariantData<T>(val variantData: T) {
|
|
||||||
abstract val name: String
|
|
||||||
abstract val sourceProviders: Iterable<SourceProvider>
|
|
||||||
abstract fun addJavaSourceFoldersToModel(generatedFilesDir: File)
|
|
||||||
abstract val annotationProcessorOptions: Map<String, String>?
|
|
||||||
abstract fun registerGeneratedJavaSource(
|
|
||||||
project: Project,
|
|
||||||
kaptTask: KaptTask,
|
|
||||||
javaTask: AbstractCompile
|
|
||||||
)
|
|
||||||
|
|
||||||
open val annotationProcessorOptionProviders: List<*>
|
|
||||||
get() = emptyList<Any>()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subplugin for the Kotlin Gradle plugin
|
|
||||||
class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|
||||||
companion object {
|
|
||||||
private val VERBOSE_OPTION_NAME = "kapt.verbose"
|
private val VERBOSE_OPTION_NAME = "kapt.verbose"
|
||||||
private val USE_WORKER_API = "kapt.use.worker.api"
|
private val USE_WORKER_API = "kapt.use.worker.api"
|
||||||
private val INFO_AS_WARNINGS = "kapt.info.as.warnings"
|
private val INFO_AS_WARNINGS = "kapt.info.as.warnings"
|
||||||
@@ -139,7 +117,7 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
fun findMainKaptConfiguration(project: Project) = project.findKaptConfiguration(SourceSet.MAIN_SOURCE_SET_NAME)
|
fun findMainKaptConfiguration(project: Project) = project.findKaptConfiguration(SourceSet.MAIN_SOURCE_SET_NAME)
|
||||||
|
|
||||||
fun createAptConfigurationIfNeeded(project: Project, sourceSetName: String): Configuration {
|
fun createAptConfigurationIfNeeded(project: Project, sourceSetName: String): Configuration {
|
||||||
val configurationName = Kapt3KotlinGradleSubplugin.getKaptConfigurationName(sourceSetName)
|
val configurationName = getKaptConfigurationName(sourceSetName)
|
||||||
|
|
||||||
project.configurations.findByName(configurationName)?.let { return it }
|
project.configurations.findByName(configurationName)?.let { return it }
|
||||||
val aptConfiguration = project.configurations.create(configurationName).apply {
|
val aptConfiguration = project.configurations.create(configurationName).apply {
|
||||||
@@ -148,7 +126,7 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, Usage.JAVA_RUNTIME))
|
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, Usage.JAVA_RUNTIME))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (aptConfiguration.name != Kapt3KotlinGradleSubplugin.MAIN_KAPT_CONFIGURATION_NAME) {
|
if (aptConfiguration.name != MAIN_KAPT_CONFIGURATION_NAME) {
|
||||||
// The main configuration can be created after the current one. We should handle this case
|
// The main configuration can be created after the current one. We should handle this case
|
||||||
val mainConfiguration = findMainKaptConfiguration(project)
|
val mainConfiguration = findMainKaptConfiguration(project)
|
||||||
?: createAptConfigurationIfNeeded(project, SourceSet.MAIN_SOURCE_SET_NAME)
|
?: createAptConfigurationIfNeeded(project, SourceSet.MAIN_SOURCE_SET_NAME)
|
||||||
@@ -158,11 +136,13 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
|
|
||||||
return aptConfiguration
|
return aptConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isEnabled(project: Project) =
|
||||||
|
project.plugins.any { it is Kapt3GradleSubplugin }
|
||||||
}
|
}
|
||||||
|
|
||||||
private val kotlinToKaptGenerateStubsTasksMap = mutableMapOf<KotlinCompile, KaptGenerateStubsTask>()
|
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>) =
|
||||||
|
(kotlinCompilation.platformType == KotlinPlatformType.jvm || kotlinCompilation.platformType == KotlinPlatformType.androidJvm)
|
||||||
override fun isApplicable(project: Project, task: AbstractCompile) = task is KotlinCompile && Kapt3GradleSubplugin.isEnabled(project)
|
|
||||||
|
|
||||||
private fun Kapt3SubpluginContext.getKaptStubsDir() = temporaryKaptDirectory("stubs")
|
private fun Kapt3SubpluginContext.getKaptStubsDir() = temporaryKaptDirectory("stubs")
|
||||||
|
|
||||||
@@ -178,13 +158,12 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
return dir
|
return dir
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class Kapt3SubpluginContext(
|
internal inner class Kapt3SubpluginContext(
|
||||||
val project: Project,
|
val project: Project,
|
||||||
val kotlinCompile: KotlinCompile,
|
val javaCompile: TaskProvider<out AbstractCompile>?,
|
||||||
val javaCompile: AbstractCompile?,
|
val variantData: Any?,
|
||||||
val kaptVariantData: KaptVariantData<*>?,
|
|
||||||
val sourceSetName: String,
|
val sourceSetName: String,
|
||||||
val kotlinCompilation: KotlinCompilation<*>?,
|
val kotlinCompilation: KotlinCompilation<*>,
|
||||||
val kaptExtension: KaptExtension,
|
val kaptExtension: KaptExtension,
|
||||||
val kaptClasspathConfigurations: List<Configuration>
|
val kaptClasspathConfigurations: List<Configuration>
|
||||||
) {
|
) {
|
||||||
@@ -193,19 +172,18 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
val classesOutputDir = getKaptGeneratedClassesDir(project, sourceSetName)
|
val classesOutputDir = getKaptGeneratedClassesDir(project, sourceSetName)
|
||||||
val includeCompileClasspath =
|
val includeCompileClasspath =
|
||||||
kaptExtension.includeCompileClasspath
|
kaptExtension.includeCompileClasspath
|
||||||
?: Kapt3KotlinGradleSubplugin.includeCompileClasspath(project)
|
?: includeCompileClasspath(project)
|
||||||
?: true
|
?: true
|
||||||
|
|
||||||
|
val kotlinCompile: TaskProvider<KotlinCompile>
|
||||||
|
// Can't use just kotlinCompilation.compileKotlinTaskProvider, as the latter is not statically-known to be KotlinCompile
|
||||||
|
get() = checkNotNull(project.locateTask(kotlinCompilation.compileKotlinTaskName))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun apply(
|
override fun applyToCompilation(
|
||||||
project: Project,
|
kotlinCompilation: KotlinCompilation<*>
|
||||||
kotlinCompile: KotlinCompile,
|
): Provider<List<SubpluginOption>> {
|
||||||
javaCompile: AbstractCompile?,
|
val project = kotlinCompilation.target.project
|
||||||
variantData: Any?,
|
|
||||||
androidProjectHandler: Any?,
|
|
||||||
kotlinCompilation: KotlinCompilation<*>?
|
|
||||||
): List<SubpluginOption> {
|
|
||||||
assert((variantData != null) xor (kotlinCompilation != null))
|
|
||||||
|
|
||||||
val buildDependencies = arrayListOf<TaskDependency>()
|
val buildDependencies = arrayListOf<TaskDependency>()
|
||||||
val kaptConfigurations = arrayListOf<Configuration>()
|
val kaptConfigurations = arrayListOf<Configuration>()
|
||||||
@@ -217,17 +195,14 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val kaptVariantData = variantData as? KaptVariantData<*>
|
val androidVariantData: BaseVariant? = (kotlinCompilation as? KotlinJvmAndroidCompilation)?.androidVariant
|
||||||
|
|
||||||
val sourceSetName = if (kaptVariantData != null) {
|
val sourceSetName = if (androidVariantData != null) {
|
||||||
for (provider in kaptVariantData.sourceProviders) {
|
for (provider in androidVariantData.sourceSets) {
|
||||||
handleSourceSet((provider as AndroidSourceSet).name)
|
handleSourceSet((provider as AndroidSourceSet).name)
|
||||||
}
|
}
|
||||||
|
androidVariantData.name
|
||||||
kaptVariantData.name
|
|
||||||
} else {
|
} else {
|
||||||
if (kotlinCompilation == null) error("In non-Android projects, Kotlin compilation should not be null")
|
|
||||||
|
|
||||||
handleSourceSet(kotlinCompilation.compilationName)
|
handleSourceSet(kotlinCompilation.compilationName)
|
||||||
kotlinCompilation.compilationName
|
kotlinCompilation.compilationName
|
||||||
}
|
}
|
||||||
@@ -236,75 +211,103 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
|
|
||||||
val nonEmptyKaptConfigurations = kaptConfigurations.filter { it.dependencies.isNotEmpty() }
|
val nonEmptyKaptConfigurations = kaptConfigurations.filter { it.dependencies.isNotEmpty() }
|
||||||
|
|
||||||
|
val javaCompileOrNull = findJavaTaskForKotlinCompilation(kotlinCompilation)
|
||||||
|
|
||||||
val context = Kapt3SubpluginContext(
|
val context = Kapt3SubpluginContext(
|
||||||
project, kotlinCompile, javaCompile,
|
project, javaCompileOrNull,
|
||||||
kaptVariantData, sourceSetName, kotlinCompilation, kaptExtension, nonEmptyKaptConfigurations
|
androidVariantData, sourceSetName, kotlinCompilation, kaptExtension, nonEmptyKaptConfigurations
|
||||||
)
|
)
|
||||||
|
|
||||||
val kaptGenerateStubsTask = context.createKaptGenerateStubsTask()
|
val kaptGenerateStubsTaskProvider: TaskProvider<KaptGenerateStubsTask> = context.createKaptGenerateStubsTask()
|
||||||
val kaptTask = context.createKaptKotlinTask(useWorkerApi = project.isUseWorkerApi())
|
val kaptTaskProvider: TaskProvider<out KaptTask> = context.createKaptKotlinTask(useWorkerApi = project.isUseWorkerApi())
|
||||||
|
|
||||||
kaptGenerateStubsTask.source(*kaptConfigurations.toTypedArray())
|
kaptGenerateStubsTaskProvider.configure { kaptGenerateStubsTask ->
|
||||||
|
kaptGenerateStubsTask.source(*kaptConfigurations.toTypedArray())
|
||||||
|
|
||||||
kaptGenerateStubsTask.dependsOn(*buildDependencies.toTypedArray())
|
kaptGenerateStubsTask.dependsOn(*buildDependencies.toTypedArray())
|
||||||
kaptGenerateStubsTask.dependsOn(*kotlinCompile.dependsOn.toTypedArray())
|
kaptGenerateStubsTask.dependsOn(
|
||||||
|
project.provider {
|
||||||
|
kotlinCompilation.compileKotlinTask.dependsOn.filter { it !is TaskProvider<*> || it.name != kaptTaskProvider.name }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
kaptTask.dependsOn(kaptGenerateStubsTask)
|
if (androidVariantData != null) {
|
||||||
kotlinCompile.dependsOn(kaptTask)
|
kaptGenerateStubsTask.inputs.files(
|
||||||
|
Callable {
|
||||||
|
// Avoid circular dependency: the stubs task need the Java sources, but the Java sources generated by Kapt should be
|
||||||
|
// excluded, as the Kapt tasks depend on the stubs ones, and having them in the input would lead to a cycle
|
||||||
|
val kaptJavaOutput = kaptTaskProvider.get().destinationDir
|
||||||
|
androidVariantData.getSourceFolders(SourceKind.JAVA).filter { it.dir != kaptJavaOutput }
|
||||||
|
}
|
||||||
|
).withPathSensitivity(PathSensitivity.RELATIVE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
kaptTaskProvider.configure { kaptTask ->
|
||||||
|
kaptTask.dependsOn(kaptGenerateStubsTaskProvider)
|
||||||
|
}
|
||||||
|
context.kotlinCompile.configure { it.dependsOn(kaptTaskProvider) }
|
||||||
|
|
||||||
/** Plugin options are applied to kapt*Compile inside [createKaptKotlinTask] */
|
/** Plugin options are applied to kapt*Compile inside [createKaptKotlinTask] */
|
||||||
return emptyList()
|
return project.provider { emptyList<SubpluginOption>() }
|
||||||
}
|
|
||||||
|
|
||||||
override fun getSubpluginKotlinTasks(project: Project, kotlinCompile: KotlinCompile): List<AbstractCompile> {
|
|
||||||
val kaptGenerateStubsTask = kotlinToKaptGenerateStubsTasksMap[kotlinCompile]
|
|
||||||
return if (kaptGenerateStubsTask == null) emptyList() else listOf(kaptGenerateStubsTask)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method should be called no more than once for each Kapt3SubpluginContext
|
// This method should be called no more than once for each Kapt3SubpluginContext
|
||||||
private fun Kapt3SubpluginContext.buildOptions(aptMode: String, javacOptions: Map<String, String>): List<SubpluginOption> {
|
private fun Kapt3SubpluginContext.buildOptions(
|
||||||
val pluginOptions = mutableListOf<SubpluginOption>()
|
aptMode: String,
|
||||||
|
javacOptions: Provider<Map<String, String>>
|
||||||
val generatedFilesDir = getKaptGeneratedSourcesDir(project, sourceSetName)
|
): Provider<List<SubpluginOption>> {
|
||||||
kaptVariantData?.addJavaSourceFoldersToModel(generatedFilesDir)
|
|
||||||
|
|
||||||
pluginOptions += SubpluginOption("aptMode", aptMode)
|
|
||||||
disableAnnotationProcessingInJavaTask()
|
disableAnnotationProcessingInJavaTask()
|
||||||
|
|
||||||
pluginOptions += FilesSubpluginOption("sources", listOf(generatedFilesDir))
|
return project.provider {
|
||||||
pluginOptions += FilesSubpluginOption("classes", listOf(getKaptGeneratedClassesDir(project, sourceSetName)))
|
val pluginOptions = mutableListOf<SubpluginOption>()
|
||||||
|
|
||||||
pluginOptions += FilesSubpluginOption("incrementalData", listOf(getKaptIncrementalDataDir()))
|
val generatedFilesDir = getKaptGeneratedSourcesDir(project, sourceSetName)
|
||||||
|
KaptWithAndroid.androidVariantData(this)?.addJavaSourceFoldersToModel(generatedFilesDir)
|
||||||
|
|
||||||
val annotationProcessors = kaptExtension.processors
|
pluginOptions += SubpluginOption("aptMode", aptMode)
|
||||||
if (annotationProcessors.isNotEmpty()) {
|
|
||||||
pluginOptions += SubpluginOption("processors", annotationProcessors)
|
pluginOptions += FilesSubpluginOption("sources", listOf(generatedFilesDir))
|
||||||
|
pluginOptions += FilesSubpluginOption("classes", listOf(getKaptGeneratedClassesDir(project, sourceSetName)))
|
||||||
|
|
||||||
|
pluginOptions += FilesSubpluginOption("incrementalData", listOf(getKaptIncrementalDataDir()))
|
||||||
|
|
||||||
|
val annotationProcessors = kaptExtension.processors
|
||||||
|
if (annotationProcessors.isNotEmpty()) {
|
||||||
|
pluginOptions += SubpluginOption("processors", annotationProcessors)
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlinSourcesOutputDir.mkdirs()
|
||||||
|
|
||||||
|
val apOptions = getAPOptions().get()
|
||||||
|
|
||||||
|
pluginOptions += CompositeSubpluginOption(
|
||||||
|
"apoptions",
|
||||||
|
lazy { encodeList(apOptions.associate { it.key to it.value }) },
|
||||||
|
apOptions
|
||||||
|
)
|
||||||
|
|
||||||
|
pluginOptions += SubpluginOption("javacArguments", encodeList(javacOptions.get()))
|
||||||
|
|
||||||
|
pluginOptions += SubpluginOption("includeCompileClasspath", includeCompileClasspath.toString())
|
||||||
|
|
||||||
|
addMiscOptions(pluginOptions)
|
||||||
|
|
||||||
|
pluginOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlinSourcesOutputDir.mkdirs()
|
|
||||||
|
|
||||||
val apOptions = getAPOptions()
|
|
||||||
|
|
||||||
pluginOptions += CompositeSubpluginOption("apoptions", lazy { encodeList(apOptions.associate { it.key to it.value }) }, apOptions)
|
|
||||||
|
|
||||||
pluginOptions += SubpluginOption("javacArguments", encodeList(javacOptions))
|
|
||||||
|
|
||||||
pluginOptions += SubpluginOption("includeCompileClasspath", includeCompileClasspath.toString())
|
|
||||||
|
|
||||||
addMiscOptions(pluginOptions)
|
|
||||||
|
|
||||||
return pluginOptions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Kapt3SubpluginContext.getAPOptions(): List<SubpluginOption> {
|
private fun Kapt3SubpluginContext.getAPOptions(): Provider<List<SubpluginOption>> = project.provider {
|
||||||
val androidPlugin = kaptVariantData?.let {
|
val androidVariantData = KaptWithAndroid.androidVariantData(this)
|
||||||
|
|
||||||
|
val androidPlugin = androidVariantData?.let {
|
||||||
project.extensions.findByName("android") as? BaseExtension
|
project.extensions.findByName("android") as? BaseExtension
|
||||||
}
|
}
|
||||||
|
|
||||||
val androidOptions = kaptVariantData?.annotationProcessorOptions ?: emptyMap()
|
val androidOptions = androidVariantData?.annotationProcessorOptions ?: emptyMap()
|
||||||
|
|
||||||
val apOptionsFromProviders =
|
val apOptionsFromProviders =
|
||||||
kaptVariantData?.annotationProcessorOptionProviders
|
androidVariantData?.annotationProcessorOptionProviders
|
||||||
?.flatMap { (it as CommandLineArgumentProvider).asArguments() }
|
?.flatMap { (it as CommandLineArgumentProvider).asArguments() }
|
||||||
.orEmpty()
|
.orEmpty()
|
||||||
|
|
||||||
@@ -317,30 +320,44 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val apOptionsPairsList: List<Pair<String, String>> =
|
val apOptionsPairsList: List<Pair<String, String>> =
|
||||||
kaptExtension.getAdditionalArguments(project, kaptVariantData?.variantData, androidPlugin).toList() +
|
kaptExtension.getAdditionalArguments(project, androidVariantData, androidPlugin).toList() +
|
||||||
androidOptions.toList()
|
androidOptions.toList()
|
||||||
|
|
||||||
return apOptionsPairsList.map { SubpluginOption(it.first, it.second) } +
|
apOptionsPairsList.map { SubpluginOption(it.first, it.second) } +
|
||||||
FilesSubpluginOption(KAPT_KOTLIN_GENERATED, listOf(kotlinSourcesOutputDir)) +
|
FilesSubpluginOption(KAPT_KOTLIN_GENERATED, listOf(kotlinSourcesOutputDir)) +
|
||||||
subluginOptionsFromProvidedApOptions
|
subluginOptionsFromProvidedApOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Kapt3SubpluginContext.registerSubpluginOptions(
|
private fun Kapt3SubpluginContext.registerSubpluginOptions(
|
||||||
task: Task,
|
taskProvider: TaskProvider<*>,
|
||||||
container: CompilerPluginOptions,
|
optionsProvider: Provider<List<SubpluginOption>>
|
||||||
options: List<SubpluginOption>
|
|
||||||
) {
|
) {
|
||||||
val compilerPluginId = getCompilerPluginId()
|
taskProvider.configure { taskInstance ->
|
||||||
task.registerSubpluginOptionsAsInputs(compilerPluginId, options)
|
val container = when (taskInstance) {
|
||||||
|
is KaptGenerateStubsTask -> taskInstance.pluginOptions
|
||||||
|
is KaptWithKotlincTask -> taskInstance.pluginOptions
|
||||||
|
is KaptWithoutKotlincTask -> taskInstance.processorOptions
|
||||||
|
else -> error("Unexpected task ${taskInstance.name} (${taskInstance.javaClass})")
|
||||||
|
}
|
||||||
|
|
||||||
for (option in options) {
|
|
||||||
container.addPluginArgument(compilerPluginId, option)
|
val compilerPluginId = getCompilerPluginId()
|
||||||
|
|
||||||
|
val options = optionsProvider.get()
|
||||||
|
|
||||||
|
taskInstance.registerSubpluginOptionsAsInputs(compilerPluginId, options)
|
||||||
|
|
||||||
|
for (option in options) {
|
||||||
|
container.addPluginArgument(compilerPluginId, option)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also register all the subplugin options from the Kotlin task:
|
// Also register all the subplugin options from the Kotlin task:
|
||||||
project.afterEvaluate {
|
project.whenEvaluated {
|
||||||
kotlinCompile.pluginOptions.subpluginOptionsByPluginId.forEach { (pluginId, options) ->
|
taskProvider.configure { taskInstance ->
|
||||||
task.registerSubpluginOptionsAsInputs("kotlinCompile.$pluginId", options)
|
kotlinCompile.get().pluginOptions.subpluginOptionsByPluginId.forEach { (pluginId, options) ->
|
||||||
|
taskInstance.registerSubpluginOptionsAsInputs("kotlinCompile.$pluginId", options)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -379,95 +396,111 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Kapt3SubpluginContext.createKaptKotlinTask(useWorkerApi: Boolean): KaptTask {
|
private fun Kapt3SubpluginContext.createKaptKotlinTask(useWorkerApi: Boolean): TaskProvider<out KaptTask> {
|
||||||
val taskClass = if (useWorkerApi) KaptWithoutKotlincTask::class.java else KaptWithKotlincTask::class.java
|
val taskClass = if (useWorkerApi) KaptWithoutKotlincTask::class.java else KaptWithKotlincTask::class.java
|
||||||
val taskName = getKaptTaskName("kapt")
|
val taskName = getKaptTaskName("kapt")
|
||||||
val kaptTask = project.tasks.create(taskName, taskClass)
|
|
||||||
|
|
||||||
kaptTask.useBuildCache = kaptExtension.useBuildCache
|
var classStructureIfIncremental: Configuration? = null
|
||||||
|
|
||||||
kaptTask.kotlinCompileTask = kotlinCompile
|
|
||||||
|
|
||||||
kaptTask.stubsDir = getKaptStubsDir()
|
|
||||||
kaptTask.destinationDir = sourcesOutputDir
|
|
||||||
kaptTask.kotlinSourcesDestinationDir = kotlinSourcesOutputDir
|
|
||||||
kaptTask.classesDir = classesOutputDir
|
|
||||||
kaptTask.includeCompileClasspath = includeCompileClasspath
|
|
||||||
|
|
||||||
kaptTask.isIncremental = project.isIncrementalKapt()
|
|
||||||
if (kaptTask.isIncremental) {
|
|
||||||
kaptTask.incAptCache = getKaptIncrementalAnnotationProcessingCache()
|
|
||||||
kaptTask.localState.register(kaptTask.incAptCache)
|
|
||||||
|
|
||||||
|
if (project.isIncrementalKapt()) {
|
||||||
maybeRegisterTransform(project)
|
maybeRegisterTransform(project)
|
||||||
val classStructure = project.configurations.create("_classStructure${taskName}")
|
|
||||||
|
classStructureIfIncremental = project.configurations.create("_classStructure${taskName}")
|
||||||
|
|
||||||
// Wrap the `kotlinCompile.classpath` into a file collection, so that, if the classpath is represented by a configuration,
|
// Wrap the `kotlinCompile.classpath` into a file collection, so that, if the classpath is represented by a configuration,
|
||||||
// the configuration is not extended (via extendsFrom, which normally happens when one configuration is _added_ into another)
|
// the configuration is not extended (via extendsFrom, which normally happens when one configuration is _added_ into another)
|
||||||
// but is instead included as the (lazily) resolved files. This is needed because the class structure configuration doesn't have
|
// but is instead included as the (lazily) resolved files. This is needed because the class structure configuration doesn't have
|
||||||
// the attributes that are potentially needed to resolve dependencies on MPP modules, and the classpath configuration does.
|
// the attributes that are potentially needed to resolve dependencies on MPP modules, and the classpath configuration does.
|
||||||
project.dependencies.add(classStructure.name, project.files(project.provider { kotlinCompile.classpath }))
|
project.dependencies.add(classStructureIfIncremental.name, project.files(project.provider { kotlinCompile.get().classpath }))
|
||||||
|
|
||||||
kaptTask.classpathStructure = classStructure.incoming.artifactView { viewConfig ->
|
|
||||||
viewConfig.attributes.attribute(artifactType, CLASS_STRUCTURE_ARTIFACT_TYPE)
|
|
||||||
}.files
|
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlinCompilation?.run {
|
val kaptTaskProvider = project.registerTask(taskName, taskClass, emptyList()) { kaptTask ->
|
||||||
output.apply {
|
kaptTask.useBuildCache = kaptExtension.useBuildCache
|
||||||
addClassesDir { project.files(classesOutputDir).builtBy(kaptTask) }
|
|
||||||
|
kaptTask.kotlinCompileTask = kotlinCompilation.compileKotlinTaskProvider.get() as KotlinCompile
|
||||||
|
|
||||||
|
kaptTask.stubsDir = getKaptStubsDir()
|
||||||
|
|
||||||
|
kaptTask.destinationDir = sourcesOutputDir
|
||||||
|
kaptTask.kotlinSourcesDestinationDir = kotlinSourcesOutputDir
|
||||||
|
kaptTask.classesDir = classesOutputDir
|
||||||
|
kaptTask.includeCompileClasspath = includeCompileClasspath
|
||||||
|
|
||||||
|
kaptTask.isIncremental = project.isIncrementalKapt()
|
||||||
|
|
||||||
|
if (kaptTask.isIncremental) {
|
||||||
|
kaptTask.incAptCache = getKaptIncrementalAnnotationProcessingCache()
|
||||||
|
kaptTask.localState.register(kaptTask.incAptCache)
|
||||||
|
|
||||||
|
kaptTask.classpathStructure = classStructureIfIncremental!!.incoming.artifactView { viewConfig ->
|
||||||
|
viewConfig.attributes.attribute(artifactType, CLASS_STRUCTURE_ARTIFACT_TYPE)
|
||||||
|
}.files
|
||||||
|
|
||||||
|
if (kaptTask is KaptWithKotlincTask) {
|
||||||
|
kaptTask.pluginOptions.addPluginArgument(
|
||||||
|
getCompilerPluginId(),
|
||||||
|
SubpluginOption("incrementalCache", kaptTask.incAptCache!!.absolutePath)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
kaptTask.kaptClasspathConfigurations = kaptClasspathConfigurations
|
||||||
|
|
||||||
|
KaptWithAndroid.androidVariantData(this)?.annotationProcessorOptionProviders?.let {
|
||||||
|
kaptTask.annotationProcessorOptionProviders.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlinCompile.source(sourcesOutputDir, kotlinSourcesOutputDir)
|
kotlinCompilation.output.apply {
|
||||||
|
addClassesDir { project.files(classesOutputDir).builtBy(kaptTaskProvider) }
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlinCompilation.compileKotlinTaskProvider.configure {
|
||||||
|
it as SourceTask
|
||||||
|
it.source(sourcesOutputDir, kotlinSourcesOutputDir)
|
||||||
|
}
|
||||||
|
|
||||||
if (javaCompile != null) {
|
if (javaCompile != null) {
|
||||||
if (kaptVariantData != null) {
|
val androidVariantData = KaptWithAndroid.androidVariantData(this)
|
||||||
kaptVariantData.registerGeneratedJavaSource(project, kaptTask, javaCompile)
|
if (androidVariantData != null) {
|
||||||
|
KaptWithAndroid.registerGeneratedJavaSourceForAndroid(this, project, androidVariantData, kaptTaskProvider)
|
||||||
} else {
|
} else {
|
||||||
registerGeneratedJavaSource(kaptTask, javaCompile)
|
registerGeneratedJavaSource(kaptTaskProvider, javaCompile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kaptTask.kaptClasspathConfigurations = kaptClasspathConfigurations
|
val dslJavacOptions: Provider<Map<String, String>> = project.provider {
|
||||||
|
kaptExtension.getJavacOptions().toMutableMap().also { result ->
|
||||||
kaptVariantData?.annotationProcessorOptionProviders?.let {
|
if (javaCompile != null && "-source" !in result && "--source" !in result && "--release" !in result) {
|
||||||
kaptTask.annotationProcessorOptionProviders.add(it)
|
val sourceOptionKey = if (SystemInfo.isJavaVersionAtLeast(12, 0, 0)) {
|
||||||
|
"--source"
|
||||||
|
} else {
|
||||||
|
"-source"
|
||||||
|
}
|
||||||
|
result[sourceOptionKey] = javaCompile.get().sourceCompatibility
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val dslJavacOptions = kaptExtension.getJavacOptions().toMutableMap()
|
if (taskClass == KaptWithKotlincTask::class.java) {
|
||||||
if (javaCompile != null && "-source" !in dslJavacOptions && "--source" !in dslJavacOptions && "--release" !in dslJavacOptions) {
|
|
||||||
val sourceOptionKey = if (SystemInfo.isJavaVersionAtLeast(12, 0, 0)) {
|
|
||||||
"--source"
|
|
||||||
} else {
|
|
||||||
"-source"
|
|
||||||
}
|
|
||||||
dslJavacOptions[sourceOptionKey] = javaCompile.sourceCompatibility
|
|
||||||
}
|
|
||||||
if (kaptTask is KaptWithKotlincTask) {
|
|
||||||
if (kaptTask.isIncremental) {
|
|
||||||
kaptTask.pluginOptions.addPluginArgument(
|
|
||||||
getCompilerPluginId(),
|
|
||||||
SubpluginOption("incrementalCache", kaptTask.incAptCache!!.absolutePath))
|
|
||||||
}
|
|
||||||
|
|
||||||
val subpluginOptions = buildOptions("apt", dslJavacOptions)
|
val subpluginOptions = buildOptions("apt", dslJavacOptions)
|
||||||
registerSubpluginOptions(kaptTask, kaptTask.pluginOptions, subpluginOptions)
|
registerSubpluginOptions(kaptTaskProvider, subpluginOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kaptTask is KaptWithoutKotlincTask) {
|
if (taskClass == KaptWithoutKotlincTask::class.java) {
|
||||||
with(kaptTask) {
|
kaptTaskProvider.configure {
|
||||||
isVerbose = project.isKaptVerbose()
|
it as KaptWithoutKotlincTask
|
||||||
mapDiagnosticLocations = kaptExtension.mapDiagnosticLocations
|
it.isVerbose = project.isKaptVerbose()
|
||||||
annotationProcessorFqNames = kaptExtension.processors.split(',').filter { it.isNotEmpty() }
|
it.mapDiagnosticLocations = kaptExtension.mapDiagnosticLocations
|
||||||
javacOptions = dslJavacOptions
|
it.annotationProcessorFqNames = kaptExtension.processors.split(',').filter { it.isNotEmpty() }
|
||||||
|
it.javacOptions = dslJavacOptions.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
val subpluginOptions = getAPOptions()
|
val subpluginOptions = getAPOptions()
|
||||||
registerSubpluginOptions(kaptTask, kaptTask.processorOptions, subpluginOptions)
|
registerSubpluginOptions(kaptTaskProvider, subpluginOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
return kaptTask
|
return kaptTaskProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun maybeRegisterTransform(project: Project) {
|
private fun maybeRegisterTransform(project: Project) {
|
||||||
@@ -487,33 +520,35 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Kapt3SubpluginContext.createKaptGenerateStubsTask(): KaptGenerateStubsTask {
|
private fun Kapt3SubpluginContext.createKaptGenerateStubsTask(): TaskProvider<KaptGenerateStubsTask> {
|
||||||
val kaptTaskName = getKaptTaskName("kaptGenerateStubs")
|
val kaptTaskName = getKaptTaskName("kaptGenerateStubs")
|
||||||
|
|
||||||
KotlinCompileTaskData.register(kaptTaskName, KotlinCompileTaskData.get(project, kotlinCompile.name).compilation).apply {
|
KotlinCompileTaskData.register(kaptTaskName, kotlinCompilation).apply {
|
||||||
useModuleDetection.set(KotlinCompileTaskData.get(project, kotlinCompile.name).useModuleDetection)
|
useModuleDetection.set(KotlinCompileTaskData.get(project, kotlinCompile.name).useModuleDetection)
|
||||||
|
destinationDir.set(project.provider { getKaptIncrementalDataDir() })
|
||||||
}
|
}
|
||||||
|
|
||||||
val kaptTask = project.tasks.create(
|
val kaptTaskProvider = project.registerTask<KaptGenerateStubsTask>(kaptTaskName) { kaptTask ->
|
||||||
kaptTaskName,
|
kaptTask.kotlinCompileTask = kotlinCompile.get()
|
||||||
KaptGenerateStubsTask::class.java
|
|
||||||
)
|
|
||||||
|
|
||||||
kaptTask.kotlinCompileTask = kotlinCompile
|
kaptTask.stubsDir = getKaptStubsDir()
|
||||||
kotlinToKaptGenerateStubsTasksMap[kotlinCompile] = kaptTask
|
kaptTask.setDestinationDir { getKaptIncrementalDataDir() }
|
||||||
|
kaptTask.mapClasspath { kaptTask.kotlinCompileTask.classpath }
|
||||||
|
kaptTask.generatedSourcesDir = sourcesOutputDir
|
||||||
|
|
||||||
kaptTask.stubsDir = getKaptStubsDir()
|
kaptTask.kaptClasspathConfigurations = kaptClasspathConfigurations
|
||||||
kaptTask.setDestinationDir { getKaptIncrementalDataDir() }
|
|
||||||
kaptTask.mapClasspath { kotlinCompile.classpath }
|
|
||||||
kaptTask.generatedSourcesDir = sourcesOutputDir
|
|
||||||
PropertiesProvider(project).mapKotlinTaskProperties(kaptTask)
|
|
||||||
|
|
||||||
kaptTask.kaptClasspathConfigurations = kaptClasspathConfigurations
|
PropertiesProvider(project).mapKotlinTaskProperties(kaptTask)
|
||||||
|
}
|
||||||
|
|
||||||
val subpluginOptions = buildOptions("stubs", kaptExtension.getJavacOptions())
|
project.whenEvaluated {
|
||||||
registerSubpluginOptions(kaptTask, kaptTask.pluginOptions, subpluginOptions)
|
addCompilationSourcesToExternalCompileTask(kotlinCompilation, kaptTaskProvider)
|
||||||
|
}
|
||||||
|
|
||||||
return kaptTask
|
val subpluginOptions = buildOptions("stubs", project.provider { kaptExtension.getJavacOptions() })
|
||||||
|
registerSubpluginOptions(kaptTaskProvider, subpluginOptions)
|
||||||
|
|
||||||
|
return kaptTaskProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Kapt3SubpluginContext.getKaptTaskName(prefix: String): String {
|
private fun Kapt3SubpluginContext.getKaptTaskName(prefix: String): String {
|
||||||
@@ -524,8 +559,11 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun Kapt3SubpluginContext.disableAnnotationProcessingInJavaTask() {
|
private fun Kapt3SubpluginContext.disableAnnotationProcessingInJavaTask() {
|
||||||
(javaCompile as? JavaCompile)?.let { javaCompile ->
|
javaCompile?.configure { javaCompileInstance ->
|
||||||
val options = javaCompile.options
|
if (javaCompileInstance !is JavaCompile)
|
||||||
|
return@configure
|
||||||
|
|
||||||
|
val options = javaCompileInstance.options
|
||||||
// 'android-apt' (com.neenbedankt) adds a File instance to compilerArgs (List<String>).
|
// 'android-apt' (com.neenbedankt) adds a File instance to compilerArgs (List<String>).
|
||||||
// Although it's not our problem, we need to handle this case properly.
|
// Although it's not our problem, we need to handle this case properly.
|
||||||
val oldCompilerArgs: List<Any> = options.compilerArgs
|
val oldCompilerArgs: List<Any> = options.compilerArgs
|
||||||
@@ -539,8 +577,8 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
// Filter out the argument providers that are related to annotation processing and therefore already used by Kapt.
|
// Filter out the argument providers that are related to annotation processing and therefore already used by Kapt.
|
||||||
// This is done to avoid outputs intersections between Kapt and and javaCompile and make the up-to-date check for
|
// This is done to avoid outputs intersections between Kapt and and javaCompile and make the up-to-date check for
|
||||||
// javaCompile more granular as it does not perform annotation processing:
|
// javaCompile more granular as it does not perform annotation processing:
|
||||||
if (kaptVariantData != null) {
|
KaptWithAndroid.androidVariantData(this)?.let { androidVariantData ->
|
||||||
options.compilerArgumentProviders.removeAll(kaptVariantData.annotationProcessorOptionProviders)
|
options.compilerArgumentProviders.removeAll(androidVariantData.annotationProcessorOptionProviders)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -552,11 +590,38 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
private val artifactType = Attribute.of("artifactType", String::class.java)
|
private val artifactType = Attribute.of("artifactType", String::class.java)
|
||||||
|
|
||||||
|
// Don't reference the BaseVariant type in the Kapt plugin signatures, as those type references will fail to link when there's no Android
|
||||||
|
// Gradle plugin on the project's plugin classpath
|
||||||
|
private object KaptWithAndroid {
|
||||||
|
// Avoid loading the BaseVariant type at call sites and instead lazily load it when evaluation reaches it in the body using inline:
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun androidVariantData(context: Kapt3GradleSubplugin.Kapt3SubpluginContext): BaseVariant? = context.variantData as? BaseVariant
|
||||||
|
|
||||||
internal fun registerGeneratedJavaSource(kaptTask: KaptTask, javaTask: AbstractCompile) {
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
val generatedJavaSources = javaTask.project.fileTree(kaptTask.destinationDir)
|
// Avoid loading the BaseVariant type at call sites and instead lazily load it when evaluation reaches it in the body using inline:
|
||||||
generatedJavaSources.include("**/*.java")
|
inline fun registerGeneratedJavaSourceForAndroid(
|
||||||
javaTask.source(generatedJavaSources)
|
kapt3SubpluginContext: Kapt3GradleSubplugin.Kapt3SubpluginContext,
|
||||||
|
project: Project,
|
||||||
|
variantData: BaseVariant,
|
||||||
|
kaptTask: TaskProvider<out KaptTask>
|
||||||
|
) {
|
||||||
|
val kaptSourceOutput = project.fileTree(kapt3SubpluginContext.sourcesOutputDir).builtBy(kaptTask)
|
||||||
|
kaptSourceOutput.include("**/*.java")
|
||||||
|
variantData.registerExternalAptJavaOutput(kaptSourceOutput)
|
||||||
|
kaptTask.configure { kaptTaskInstance ->
|
||||||
|
variantData.dataBindingDependencyArtifactsIfSupported?.let { dataBindingArtifacts ->
|
||||||
|
kaptTaskInstance.dependsOn(dataBindingArtifacts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun registerGeneratedJavaSource(kaptTask: TaskProvider<out KaptTask>, javaTaskProvider: TaskProvider<out AbstractCompile>) {
|
||||||
|
javaTaskProvider.configure { javaTask ->
|
||||||
|
val generatedJavaSources = javaTask.project.fileTree(kaptTask.map { it.destinationDir })
|
||||||
|
generatedJavaSources.include("**/*.java")
|
||||||
|
javaTask.source(generatedJavaSources)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun Configuration.getNamedDependencies(): List<Dependency> = allDependencies.filter { it.group != null && it.name != null }
|
internal fun Configuration.getNamedDependencies(): List<Dependency> = allDependencies.filter { it.group != null && it.name != null }
|
||||||
@@ -583,7 +648,7 @@ internal fun checkAndroidAnnotationProcessorDependencyUsage(project: Project) {
|
|||||||
ANNOTATION_PROCESSOR -> "kapt"
|
ANNOTATION_PROCESSOR -> "kapt"
|
||||||
else -> {
|
else -> {
|
||||||
val configurationName = apConfigurationName.dropLast(ANNOTATION_PROCESSOR_CAP.length)
|
val configurationName = apConfigurationName.dropLast(ANNOTATION_PROCESSOR_CAP.length)
|
||||||
Kapt3KotlinGradleSubplugin.getKaptConfigurationName(configurationName)
|
Kapt3GradleSubplugin.getKaptConfigurationName(configurationName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -9,7 +9,7 @@ import org.gradle.api.tasks.*
|
|||||||
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
|
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
|
||||||
import org.gradle.workers.IsolationMode
|
import org.gradle.workers.IsolationMode
|
||||||
import org.gradle.workers.WorkerExecutor
|
import org.gradle.workers.WorkerExecutor
|
||||||
import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.Companion.KAPT_WORKER_DEPENDENCIES_CONFIGURATION_NAME
|
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.KAPT_WORKER_DEPENDENCIES_CONFIGURATION_NAME
|
||||||
import org.jetbrains.kotlin.gradle.internal.kapt.incremental.KaptIncrementalChanges
|
import org.jetbrains.kotlin.gradle.internal.kapt.incremental.KaptIncrementalChanges
|
||||||
import org.jetbrains.kotlin.gradle.plugin.CompositeSubpluginOption
|
import org.jetbrains.kotlin.gradle.plugin.CompositeSubpluginOption
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper
|
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper
|
||||||
@@ -49,7 +49,7 @@ open class KaptWithoutKotlincTask @Inject constructor(private val workerExecutor
|
|||||||
lateinit var javacOptions: Map<String, String>
|
lateinit var javacOptions: Map<String, String>
|
||||||
|
|
||||||
private fun getAnnotationProcessorOptions(): Map<String, String> {
|
private fun getAnnotationProcessorOptions(): Map<String, String> {
|
||||||
val options = processorOptions.subpluginOptionsByPluginId[Kapt3KotlinGradleSubplugin.KAPT_SUBPLUGIN_ID] ?: return emptyMap()
|
val options = processorOptions.subpluginOptionsByPluginId[Kapt3GradleSubplugin.KAPT_SUBPLUGIN_ID] ?: return emptyMap()
|
||||||
|
|
||||||
val result = mutableMapOf<String, String>()
|
val result = mutableMapOf<String, String>()
|
||||||
for (option in options) {
|
for (option in options) {
|
||||||
|
|||||||
+1
-1
@@ -53,7 +53,7 @@ internal fun CompilerPluginOptions.withWrappedKaptOptions(
|
|||||||
val resultOptionsByPluginId: MutableMap<String, List<SubpluginOption>> =
|
val resultOptionsByPluginId: MutableMap<String, List<SubpluginOption>> =
|
||||||
subpluginOptionsByPluginId.toMutableMap()
|
subpluginOptionsByPluginId.toMutableMap()
|
||||||
|
|
||||||
resultOptionsByPluginId.compute(Kapt3KotlinGradleSubplugin.KAPT_SUBPLUGIN_ID) { _, kaptOptions ->
|
resultOptionsByPluginId.compute(Kapt3GradleSubplugin.KAPT_SUBPLUGIN_ID) { _, kaptOptions ->
|
||||||
val changedFilesOption = changedFiles.map { SubpluginOption("changedFile", it.canonicalPath) }
|
val changedFilesOption = changedFiles.map { SubpluginOption("changedFile", it.canonicalPath) }
|
||||||
val classpathChangesOption = classpathChanges.map { SubpluginOption("classpathChange", it) }
|
val classpathChangesOption = classpathChanges.map { SubpluginOption("classpathChange", it) }
|
||||||
val processIncrementallyOption = SubpluginOption("processIncrementally", processIncrementally.toString())
|
val processIncrementallyOption = SubpluginOption("processIncrementally", processIncrementally.toString())
|
||||||
|
|||||||
+3
-4
@@ -33,8 +33,7 @@ import org.gradle.api.tasks.compile.AbstractCompile
|
|||||||
import org.gradle.jvm.tasks.Jar
|
import org.gradle.jvm.tasks.Jar
|
||||||
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
||||||
import org.jetbrains.kotlin.gradle.dsl.*
|
import org.jetbrains.kotlin.gradle.dsl.*
|
||||||
import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin
|
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin
|
||||||
import org.jetbrains.kotlin.gradle.internal.KaptVariantData
|
|
||||||
import org.jetbrains.kotlin.gradle.internal.checkAndroidAnnotationProcessorDependencyUsage
|
import org.jetbrains.kotlin.gradle.internal.checkAndroidAnnotationProcessorDependencyUsage
|
||||||
import org.jetbrains.kotlin.gradle.logging.kotlinDebug
|
import org.jetbrains.kotlin.gradle.logging.kotlinDebug
|
||||||
import org.jetbrains.kotlin.gradle.model.builder.KotlinModelBuilder
|
import org.jetbrains.kotlin.gradle.model.builder.KotlinModelBuilder
|
||||||
@@ -202,7 +201,7 @@ internal class Kotlin2JvmSourceSetProcessor(
|
|||||||
|
|
||||||
override fun doTargetSpecificProcessing() {
|
override fun doTargetSpecificProcessing() {
|
||||||
ifKaptEnabled(project) {
|
ifKaptEnabled(project) {
|
||||||
Kapt3KotlinGradleSubplugin.createAptConfigurationIfNeeded(project, kotlinCompilation.compilationName)
|
Kapt3GradleSubplugin.createAptConfigurationIfNeeded(project, kotlinCompilation.compilationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptingGradleSubplugin.configureForSourceSet(project, kotlinCompilation.compilationName)
|
ScriptingGradleSubplugin.configureForSourceSet(project, kotlinCompilation.compilationName)
|
||||||
@@ -785,7 +784,7 @@ abstract class AbstractAndroidProjectHandler(private val kotlinConfigurationTool
|
|||||||
sourceSet.addConvention(KOTLIN_DSL_NAME, kotlinSourceSet)
|
sourceSet.addConvention(KOTLIN_DSL_NAME, kotlinSourceSet)
|
||||||
|
|
||||||
ifKaptEnabled(project) {
|
ifKaptEnabled(project) {
|
||||||
Kapt3KotlinGradleSubplugin.createAptConfigurationIfNeeded(project, sourceSet.name)
|
Kapt3GradleSubplugin.createAptConfigurationIfNeeded(project, sourceSet.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.gradle.plugin.COMPILER_CLASSPATH_CONFIGURATION_NAME
|
|||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
|
import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformPluginBase
|
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformPluginBase
|
||||||
import org.jetbrains.kotlin.gradle.plugin.PLUGIN_CLASSPATH_CONFIGURATION_NAME
|
import org.jetbrains.kotlin.gradle.plugin.PLUGIN_CLASSPATH_CONFIGURATION_NAME
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinCompilation
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.associateWithTransitiveClosure
|
import org.jetbrains.kotlin.gradle.plugin.mpp.associateWithTransitiveClosure
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.ownModuleName
|
import org.jetbrains.kotlin.gradle.plugin.mpp.ownModuleName
|
||||||
import org.jetbrains.kotlin.gradle.report.BuildReportMode
|
import org.jetbrains.kotlin.gradle.report.BuildReportMode
|
||||||
@@ -251,6 +252,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKo
|
|||||||
@get:Internal // takes part in the compiler arguments
|
@get:Internal // takes part in the compiler arguments
|
||||||
val friendPaths: Array<String> by project.provider {
|
val friendPaths: Array<String> by project.provider {
|
||||||
taskData.compilation.run {
|
taskData.compilation.run {
|
||||||
|
if (this !is AbstractKotlinCompilation<*>) return@run emptyArray<String>()
|
||||||
associateWithTransitiveClosure
|
associateWithTransitiveClosure
|
||||||
.flatMap { it.output.classesDirs }
|
.flatMap { it.output.classesDirs }
|
||||||
.plus(friendArtifacts)
|
.plus(friendArtifacts)
|
||||||
|
|||||||
+3
-2
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.tasks
|
|||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.plugins.ExtraPropertiesExtension
|
import org.gradle.api.plugins.ExtraPropertiesExtension
|
||||||
import org.gradle.api.provider.Property
|
import org.gradle.api.provider.Property
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinCompilation
|
import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinCompilation
|
||||||
import org.jetbrains.kotlin.gradle.utils.getValue
|
import org.jetbrains.kotlin.gradle.utils.getValue
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -15,7 +16,7 @@ import java.io.File
|
|||||||
internal open class KotlinCompileTaskData(
|
internal open class KotlinCompileTaskData(
|
||||||
val taskName: String,
|
val taskName: String,
|
||||||
@field:Transient // cannot be serialized for Gradle Instant Execution, but actually is not needed when a task is deserialized
|
@field:Transient // cannot be serialized for Gradle Instant Execution, but actually is not needed when a task is deserialized
|
||||||
val compilation: AbstractKotlinCompilation<*>,
|
val compilation: KotlinCompilation<*>,
|
||||||
val destinationDir: Property<File>,
|
val destinationDir: Property<File>,
|
||||||
val useModuleDetection: Property<Boolean>
|
val useModuleDetection: Property<Boolean>
|
||||||
) {
|
) {
|
||||||
@@ -40,7 +41,7 @@ internal open class KotlinCompileTaskData(
|
|||||||
|
|
||||||
fun register(
|
fun register(
|
||||||
taskName: String,
|
taskName: String,
|
||||||
compilation: AbstractKotlinCompilation<*>
|
compilation: KotlinCompilation<*>
|
||||||
): KotlinCompileTaskData {
|
): KotlinCompileTaskData {
|
||||||
val project = compilation.target.project
|
val project = compilation.target.project
|
||||||
val container = project.getTaskDataMap()
|
val container = project.getTaskDataMap()
|
||||||
|
|||||||
-1
@@ -1,3 +1,2 @@
|
|||||||
org.jetbrains.kotlin.gradle.internal.AndroidSubplugin
|
org.jetbrains.kotlin.gradle.internal.AndroidSubplugin
|
||||||
org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin
|
|
||||||
org.jetbrains.kotlin.gradle.scripting.internal.ScriptingKotlinGradleSubplugin
|
org.jetbrains.kotlin.gradle.scripting.internal.ScriptingKotlinGradleSubplugin
|
||||||
Reference in New Issue
Block a user