@@ -32,6 +32,7 @@ import com.google.devtools.ksp.gradle.KspGradleSubplugin.Companion.getKspKotlinO
32
32
import com.google.devtools.ksp.gradle.KspGradleSubplugin.Companion.getKspResourceOutputDir
33
33
import java.io.File
34
34
import javax.inject.Inject
35
+ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
35
36
36
37
class KspGradleSubplugin @Inject internal constructor(private val registry : ToolingModelBuilderRegistry ) : Plugin<Project> {
37
38
companion object {
@@ -67,6 +68,8 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
67
68
class KspKotlinGradleSubplugin : KotlinGradleSubplugin <AbstractCompile > {
68
69
companion object {
69
70
const val KSP_ARTIFACT_NAME = " symbol-processing"
71
+ const val KSP_PLUGIN_ID = " com.google.devtools.ksp.symbol-processing"
72
+
70
73
}
71
74
72
75
override fun isApplicable (project : Project , task : AbstractCompile ) = KspGradleSubplugin .isEnabled(project)
@@ -88,8 +91,6 @@ class KspKotlinGradleSubplugin : KotlinGradleSubplugin<AbstractCompile> {
88
91
89
92
kotlinCompile.dependsOn(kspConfiguration.buildDependencies)
90
93
91
- kotlinCompile.setProperty(" incremental" , false )
92
-
93
94
val options = mutableListOf<SubpluginOption >()
94
95
95
96
options + = FilesSubpluginOption (" apclasspath" , kspConfiguration)
@@ -114,10 +115,47 @@ class KspKotlinGradleSubplugin : KotlinGradleSubplugin<AbstractCompile> {
114
115
javaCompile.source(generatedJavaSources)
115
116
}
116
117
117
- return options
118
+ val kspTaskName = kotlinCompile.name.replaceFirst(" compile" , " ksp" )
119
+ InternalTrampoline .KotlinCompileTaskData_register (kspTaskName, kotlinCompilation)
120
+
121
+ val kspTaskProvider = project.tasks.register(kspTaskName, KspTask ::class .java) { kspTask ->
122
+ kspTask.setDestinationDir(File (project.buildDir, " generated/ksp" ))
123
+ kspTask.mapClasspath { kotlinCompile.classpath }
124
+ kspTask.options = options
125
+ }.apply {
126
+ configure {
127
+ kotlinCompilation?.allKotlinSourceSets?.forEach { sourceSet -> it.source(sourceSet.kotlin) }
128
+ }
129
+ }
130
+
131
+ kotlinCompile.dependsOn(kspTaskProvider)
132
+ kotlinCompile.source(kotlinOutputDir, javaOutputDir)
133
+
134
+ return emptyList()
118
135
}
119
136
120
- override fun getCompilerPluginId () = " com.google.devtools.ksp.symbol-processing "
137
+ override fun getCompilerPluginId () = KSP_PLUGIN_ID
121
138
override fun getPluginArtifact (): SubpluginArtifact =
122
139
SubpluginArtifact (groupId = " com.google.devtools.ksp" , artifactId = KSP_ARTIFACT_NAME , version = javaClass.`package`.implementationVersion)
123
140
}
141
+
142
+ open class KspTask : KotlinCompile () {
143
+ lateinit var options: List <SubpluginOption >
144
+
145
+ init {
146
+ // kotlinc's incremental compilation isn't compatible with symbol processing in a few ways:
147
+ // * It doesn't consider private / internal changes when computing dirty sets.
148
+ // * It compiles iteratively; Sources can be compiled in different rounds.
149
+ incremental = false
150
+ }
151
+
152
+ override fun setupCompilerArgs (
153
+ args : org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments ,
154
+ defaultsOnly : Boolean ,
155
+ ignoreClasspathResolutionErrors : Boolean
156
+ ) {
157
+ fun SubpluginOption.toArg () = " plugin:${KspKotlinGradleSubplugin .KSP_PLUGIN_ID } :${key} =${value} "
158
+ super .setupCompilerArgs(args, defaultsOnly, ignoreClasspathResolutionErrors)
159
+ args.pluginOptions = (options.map { it.toArg() } + args.pluginOptions!! ).toTypedArray()
160
+ }
161
+ }
0 commit comments