Skip to content

Commit 0bf44d1

Browse files
committed
Replace KotlinCompilerArguments with CompilerCommonOptions
1 parent e73051e commit 0bf44d1

File tree

6 files changed

+309
-188
lines changed

6 files changed

+309
-188
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
19+
20+
package com.google.devtools.ksp.gradle
21+
22+
import org.gradle.api.model.ObjectFactory
23+
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptions
24+
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptionsDefault
25+
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptions
26+
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptionsDefault
27+
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptions
28+
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptionsDefault
29+
import org.jetbrains.kotlin.gradle.utils.newInstance
30+
31+
// TODO: to be replaced by KotlinJvmFactory, etc.
32+
class CompilerOptionsFactory {
33+
companion object {
34+
fun createCompilerJvmOptions(objectFactory: ObjectFactory): CompilerJvmOptions =
35+
objectFactory.newInstance<CompilerJvmOptionsDefault>()
36+
37+
fun createCompilerJsOptions(objectFactory: ObjectFactory): CompilerJsOptions =
38+
objectFactory.newInstance<CompilerJsOptionsDefault>()
39+
40+
fun createCompilerCommonOptions(objectFactory: ObjectFactory): CompilerCommonOptions =
41+
objectFactory.newInstance<CompilerCommonOptionsDefault>()
42+
}
43+
}

gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KotlinCompilerRunner.kt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import org.gradle.api.provider.ListProperty
2626
import org.gradle.api.provider.Property
2727
import org.gradle.api.tasks.Classpath
2828
import org.gradle.api.tasks.Internal
29+
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptions
30+
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptions
31+
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptions
2932
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilerExecutionStrategy
3033
import org.jetbrains.kotlin.gradle.utils.newInstance
3134
import java.io.File
@@ -49,37 +52,54 @@ interface KotlinCompilerRunner {
4952

5053
interface KotlinJvmCompilerRunner : KotlinCompilerRunner {
5154
fun runJvmCompilerAsync(
52-
args: KotlinJvmCompilerArguments,
55+
options: CompilerJvmOptions,
56+
freeArgs: List<String>,
5357
sources: List<File>,
5458
commonSources: List<File>,
55-
outputs: List<File>
59+
friendPaths: List<File>,
60+
libraries: List<File>,
61+
outputs: List<File>,
62+
destination: File
5663
)
5764
}
5865

5966
interface KotlinJsCompilerRunner : KotlinCompilerRunner {
6067
fun runJsCompilerAsync(
61-
args: KotlinJsCompilerArguments,
68+
options: CompilerJsOptions,
69+
freeArgs: List<String>,
6270
sources: List<File>,
6371
commonSources: List<File>,
64-
outputs: List<File>
72+
friendPaths: List<File>,
73+
libraries: List<File>,
74+
outputs: List<File>,
75+
destination: File
6576
)
6677
}
6778

6879
interface KotlinMetadataCompilerRunner : KotlinCompilerRunner {
6980
fun runMetadataCompilerAsync(
70-
args: KotlinMetadataCompilerArguments,
81+
options: CompilerCommonOptions,
82+
freeArgs: List<String>,
7183
sources: List<File>,
7284
commonSources: List<File>,
73-
outputs: List<File>
85+
friendPaths: List<File>,
86+
libraries: List<File>,
87+
outputs: List<File>,
88+
destination: File
7489
)
7590
}
7691

7792
interface KotlinNativeCompilerRunner : KotlinCompilerRunner {
7893
fun runNativeCompilerAsync(
79-
args: KotlinNativeCompilerArguments,
94+
options: CompilerCommonOptions,
95+
freeArgs: List<String>,
8096
sources: List<File>,
8197
commonSources: List<File>,
98+
friendPaths: List<File>,
99+
libraries: List<File>,
82100
outputs: List<File>,
101+
destination: File,
102+
target: String
83103
)
84104
}
85105

gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KotlinCompilerRunnerImpls.kt

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import org.gradle.process.ExecOperations
2828
import org.gradle.workers.WorkerExecutor
2929
import org.jetbrains.kotlin.build.report.metrics.BuildMetricsReporter
3030
import org.jetbrains.kotlin.build.report.metrics.BuildMetricsReporterImpl
31-
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
3231
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
3332
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
3433
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
@@ -52,6 +51,13 @@ import org.jetbrains.kotlin.utils.JsLibraryUtils
5251
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
5352
import java.io.File
5453
import javax.inject.Inject
54+
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptions
55+
import org.jetbrains.kotlin.gradle.dsl.CompilerCommonOptionsDefault
56+
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptions
57+
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptionsDefault
58+
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptions
59+
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptionsDefault
60+
import org.jetbrains.kotlin.gradle.logging.GradleErrorMessageCollector
5561

5662
internal inline fun <reified T : Any?> ObjectFactory.property() = property(T::class.java)
5763
internal inline fun <reified T : Any?> ObjectFactory.property(initialValue: T) = property<T>().value(initialValue)
@@ -86,9 +92,10 @@ abstract class KotlinCompilerRunnerImpl @Inject constructor(
8692
@Internal
8793
internal fun prepareEnvironment(allWarningsAsErrors: Boolean, outputs: List<File>): GradleCompilerEnvironment {
8894
val messageCollector = GradlePrintingMessageCollector(GradleKotlinLogger(logger), allWarningsAsErrors)
95+
val errorMessageCollector = GradleErrorMessageCollector(messageCollector)
8996
val outputItemCollector = OutputItemsCollectorImpl()
9097
return GradleCompilerEnvironment(
91-
compilerClasspath.files.toList(), messageCollector, outputItemCollector,
98+
compilerClasspath.files.toList(), errorMessageCollector, outputItemCollector,
9299
reportingSettings = ReportingSettings(),
93100
outputFiles = outputs
94101
)
@@ -110,49 +117,32 @@ abstract class KotlinCompilerRunnerImpl @Inject constructor(
110117
}
111118
}
112119

113-
internal fun CommonCompilerArguments.copyFrom(args: KotlinCompilerArguments) {
114-
freeArgs = args.freeArgs
115-
verbose = args.verbose
116-
allWarningsAsErrors = args.allWarningsAsErrors
117-
languageVersion = args.languageVersion
118-
apiVersion = args.apiVersion
119-
useK2 = args.useK2
120-
incrementalCompilation = args.incrementalCompilation
121-
pluginOptions = args.pluginOptions.toTypedArray()
122-
pluginClasspaths = args.pluginClasspaths.map { it.absolutePath }.toTypedArray()
123-
expectActualLinker = args.expectActualLinker
124-
multiPlatform = args.multiPlatform
125-
}
126-
127120
abstract class KotlinJvmCompilerRunnerImpl @Inject constructor(
128121
task: Task,
129122
objectFactory: ObjectFactory,
130123
workerExecutor: WorkerExecutor
131124
) : KotlinCompilerRunnerImpl(task, objectFactory, workerExecutor), KotlinJvmCompilerRunner {
132125

133126
override fun runJvmCompilerAsync(
134-
args: KotlinJvmCompilerArguments,
127+
options: CompilerJvmOptions,
128+
freeArgs: List<String>,
135129
sources: List<File>,
136130
commonSources: List<File>,
137-
outputs: List<File>
131+
friendPaths: List<File>,
132+
libraries: List<File>,
133+
outputs: List<File>,
134+
destination: File
138135
) {
139-
val environment = prepareEnvironment(args.allWarningsAsErrors, outputs)
136+
val environment = prepareEnvironment(options.allWarningsAsErrors.get(), outputs)
140137
val compilerRunner = prepareCompilerRunner()
141138
val compilerArgs = K2JVMCompilerArguments().apply {
142-
copyFrom(args)
143-
144-
friendPaths = args.friendPaths.map { it.absolutePath }.toTypedArray()
145-
classpath = args.libraries.map { it.absolutePath }.joinToString(File.pathSeparator)
146-
destination = args.destination?.absolutePath
147-
148-
noJdk = args.noJdk
149-
noStdlib = args.noStdlib
150-
noReflect = args.noReflect
151-
moduleName = args.moduleName
152-
jvmTarget = args.jvmTarget
153-
jdkRelease = args.jdkRelease
154-
allowNoSourceFiles = args.allowNoSourceFiles
155-
javaSourceRoots = args.javaSourceRoots.map { it.absolutePath }.toTypedArray()
139+
options as CompilerJvmOptionsDefault
140+
options.fillCompilerArguments(this)
141+
142+
this@apply.friendPaths = friendPaths.map { it.absolutePath }.toTypedArray()
143+
this@apply.classpath = libraries.map { it.absolutePath }.joinToString(File.pathSeparator)
144+
this@apply.freeArgs = options.freeCompilerArgs.get() + freeArgs
145+
this@apply.destination = destination.absolutePath
156146
}
157147

158148
compilerRunner.runJvmCompilerAsync(
@@ -191,30 +181,34 @@ abstract class KotlinJsCompilerRunnerImpl @Inject constructor(
191181
}
192182

193183
override fun runJsCompilerAsync(
194-
args: KotlinJsCompilerArguments,
184+
options: CompilerJsOptions,
185+
freeArgs: List<String>,
195186
sources: List<File>,
196187
commonSources: List<File>,
197-
outputs: List<File>
188+
friendPaths: List<File>,
189+
libraries: List<File>,
190+
outputs: List<File>,
191+
destination: File
198192
) {
199-
val environment = prepareEnvironment(args.allWarningsAsErrors, outputs)
193+
val environment = prepareEnvironment(options.allWarningsAsErrors.get(), outputs)
200194
val compilerRunner = prepareCompilerRunner()
201195
val compilerArgs = K2JSCompilerArguments().apply {
202-
copyFrom(args)
196+
options as CompilerJsOptionsDefault
197+
options.fillCompilerArguments(this)
203198

204-
outputFile = args.destination?.absolutePath
199+
this@apply.freeArgs = options.freeCompilerArgs.get() + freeArgs
200+
this@apply.outputFile = destination.absolutePath
205201

206-
noStdlib = args.noStdlib
207-
irOnly = args.irOnly
208-
irProduceJs = args.irProduceJs
209-
irProduceKlibDir = args.irProduceKlibDir
210-
irProduceKlibFile = args.irProduceKlibFile
211-
irBuildCache = args.irBuildCache
212-
wasm = args.wasm
213-
target = args.target
202+
irOnly = this@apply.freeArgs.contains("-Xir-only")
203+
irProduceJs = this@apply.freeArgs.contains("-Xir-produce-js")
204+
irProduceKlibDir = this@apply.freeArgs.contains("-Xir-produce-klib-dir")
205+
irProduceKlibFile = this@apply.freeArgs.contains("-Xir-produce-klib-file")
206+
irBuildCache = this@apply.freeArgs.contains("-Xir-build-cache")
207+
wasm = this@apply.freeArgs.contains("-Xwasm")
214208

215-
friendModules = args.friendPaths.filter { libFilter(this, it) }
209+
this@apply.friendModules = friendPaths.filter { libFilter(this, it) }
216210
.map { it.absolutePath }.joinToString(File.pathSeparator)
217-
libraries = args.libraries.filter { libFilter(this, it) }
211+
this@apply.libraries = libraries.filter { libFilter(this, it) }
218212
.map { it.absolutePath }.joinToString(File.pathSeparator)
219213
}
220214

@@ -229,19 +223,25 @@ abstract class KotlinMetadataCompilerRunnerImpl @Inject constructor(
229223
) : KotlinCompilerRunnerImpl(task, objectFactory, workerExecutor), KotlinMetadataCompilerRunner {
230224

231225
override fun runMetadataCompilerAsync(
232-
args: KotlinMetadataCompilerArguments,
226+
options: CompilerCommonOptions,
227+
freeArgs: List<String>,
233228
sources: List<File>,
234229
commonSources: List<File>,
235-
outputs: List<File>
230+
friendPaths: List<File>,
231+
libraries: List<File>,
232+
outputs: List<File>,
233+
destination: File
236234
) {
237-
val environment = prepareEnvironment(args.allWarningsAsErrors, outputs)
235+
val environment = prepareEnvironment(options.allWarningsAsErrors.get(), outputs)
238236
val compilerRunner = prepareCompilerRunner()
239237
val compilerArgs = K2MetadataCompilerArguments().apply {
240-
copyFrom(args)
238+
options as CompilerCommonOptionsDefault
239+
options.fillCompilerArguments(this)
241240

242-
friendPaths = args.friendPaths.map { it.absolutePath }.toTypedArray()
243-
classpath = args.libraries.map { it.absolutePath }.joinToString(File.pathSeparator)
244-
destination = args.destination?.absolutePath
241+
this@apply.friendPaths = friendPaths.map { it.absolutePath }.toTypedArray()
242+
this@apply.classpath = libraries.map { it.absolutePath }.joinToString(File.pathSeparator)
243+
this@apply.freeArgs = options.freeCompilerArgs.get() + freeArgs
244+
this@apply.destination = destination.absolutePath
245245
}
246246

247247
compilerRunner.runMetadataCompilerAsync(sources, commonSources, compilerArgs, environment)
@@ -259,45 +259,47 @@ abstract class KotlinNativeCompilerRunnerImpl @Inject constructor(
259259
.KotlinNativeCompilerRunner.Settings.fromProject(task.project)
260260

261261
override fun runNativeCompilerAsync(
262-
args: KotlinNativeCompilerArguments,
262+
options: CompilerCommonOptions,
263+
freeArgs: List<String>,
263264
sources: List<File>,
264265
commonSources: List<File>,
266+
friendPaths: List<File>,
267+
libraries: List<File>,
265268
outputs: List<File>,
269+
destination: File,
270+
target: String
266271
) {
267272
val output = File(outputs.first(), "dummy.out")
268273

269-
val target = KonanTarget.predefinedTargets.get(args.target!!)!!
274+
val target = KonanTarget.predefinedTargets.get(target)!!
270275
val buildArgs: MutableList<String> = mutableListOf(
271276
"-o", output.path,
272277
"-target", target.name,
273278
"-p", "library",
274279
"-Xmulti-platform"
275280
)
276-
args.libraries.flatMap { listOf("-l", it.absolutePath) }.let { buildArgs.addAll(it) }
277-
args.friendPaths.ifNotEmpty {
281+
libraries.flatMap { listOf("-l", it.absolutePath) }.let { buildArgs.addAll(it) }
282+
friendPaths.ifNotEmpty {
278283
buildArgs.add("-friend-modules")
279284
buildArgs.add(joinToString(File.pathSeparator))
280285
}
281286

282-
if (args.verbose)
287+
if (options.verbose.get())
283288
buildArgs.add("-verbose")
284-
if (args.allWarningsAsErrors)
289+
if (options.allWarningsAsErrors.get())
285290
buildArgs.add("-Werror")
286291

287-
args.pluginClasspaths.map { "-Xplugin=${it.absolutePath}" }.let { buildArgs.addAll(it) }
288-
args.pluginOptions.flatMap { listOf("-P", it) }.let { buildArgs.addAll(it) }
289-
290-
args.languageVersion?.let {
292+
options.languageVersion.getOrNull()?.let {
291293
buildArgs.add("-language-version")
292-
buildArgs.add(it)
294+
buildArgs.add(it.version)
293295
}
294-
args.apiVersion?.let {
296+
options.apiVersion.getOrNull()?.let {
295297
buildArgs.add("-api-version")
296-
buildArgs.add(it)
298+
buildArgs.add(it.version)
297299
}
298300

299301
buildArgs.addAll(sources.map { it.absolutePath })
300-
buildArgs.addAll(args.freeArgs)
302+
buildArgs.addAll(freeArgs)
301303
buildArgs.addAll(commonSources.map { it.absolutePath })
302304

303305
org.jetbrains.kotlin.compilerRunner.KotlinNativeCompilerRunner(

gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ internal inline fun <reified T : Task> Project.locateTask(name: String): TaskPro
298298
internal fun findJavaTaskForKotlinCompilation(compilation: KotlinCompilation<*>): TaskProvider<out JavaCompile>? =
299299
when (compilation) {
300300
is KotlinJvmAndroidCompilation -> compilation.compileJavaTaskProvider
301-
is KotlinWithJavaCompilation -> compilation.compileJavaTaskProvider
301+
is KotlinWithJavaCompilation<*, *> -> compilation.compileJavaTaskProvider
302302
is KotlinJvmCompilation -> compilation.compileJavaTaskProvider // may be null for Kotlin-only JVM target in MPP
303303
else -> null
304304
}

0 commit comments

Comments
 (0)