Skip to content

Commit 29814cb

Browse files
committed
ksp: support ksp options
1 parent 06a36ed commit 29814cb

File tree

8 files changed

+37
-1
lines changed

8 files changed

+37
-1
lines changed

kotlin/internal/defs.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ KtCompilerPluginInfo = provider(
6363
KspPluginInfo = provider(
6464
fields = {
6565
"plugins": "List of JavaPLuginInfo providers for the plugins to run with KSP",
66+
"options": "List of plugin options, represented as structs with key=value, to be passed to the compiler",
6667
},
6768
)

kotlin/internal/jvm/compile.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ def _run_kapt_builder_actions(
317317
compile_deps = compile_deps,
318318
deps_artifacts = deps_artifacts,
319319
annotation_processors = annotation_processors,
320+
ksp_options = [],
320321
transitive_runtime_jars = transitive_runtime_jars,
321322
plugins = plugins,
322323
outputs = {
@@ -343,6 +344,7 @@ def _run_ksp_builder_actions(
343344
compile_deps,
344345
deps_artifacts,
345346
annotation_processors,
347+
options,
346348
transitive_runtime_jars,
347349
plugins):
348350
"""Runs KSP using the KotlinBuilder tool
@@ -362,6 +364,7 @@ def _run_ksp_builder_actions(
362364
compile_deps = compile_deps,
363365
deps_artifacts = deps_artifacts,
364366
annotation_processors = annotation_processors,
367+
ksp_options = options,
365368
transitive_runtime_jars = transitive_runtime_jars,
366369
plugins = plugins,
367370
outputs = {
@@ -383,6 +386,7 @@ def _run_kt_builder_action(
383386
compile_deps,
384387
deps_artifacts,
385388
annotation_processors,
389+
ksp_options,
386390
transitive_runtime_jars,
387391
plugins,
388392
outputs,
@@ -408,6 +412,7 @@ def _run_kt_builder_action(
408412
args.add_all("--deps_artifacts", deps_artifacts, omit_if_empty = True)
409413
args.add_all("--kotlin_friend_paths", associates.jars, map_each = _associate_utils.flatten_jars)
410414
args.add("--instrument_coverage", ctx.coverage_instrumented())
415+
args.add_all("--ksp_options", ksp_options, omit_if_empty = True)
411416

412417
# Collect and prepare plugin descriptor for the worker.
413418
args.add_all(
@@ -535,6 +540,8 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
535540
)
536541
annotation_processors = _plugin_mappers.targets_to_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
537542
ksp_annotation_processors = _plugin_mappers.targets_to_ksp_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
543+
ksp_options = _plugin_mappers.targets_to_ksp_options(ctx.attr.plugins + ctx.attr.deps).to_list()
544+
538545
transitive_runtime_jars = _plugin_mappers.targets_to_transitive_runtime_jars(ctx.attr.plugins + ctx.attr.deps)
539546
plugins = ctx.attr.plugins + _exported_plugins(deps = ctx.attr.deps)
540547
deps_artifacts = _deps_artifacts(toolchains, ctx.attr.deps + associates.targets)
@@ -558,6 +565,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
558565
deps_artifacts = deps_artifacts,
559566
annotation_processors = annotation_processors,
560567
ksp_annotation_processors = ksp_annotation_processors,
568+
ksp_options = ksp_options,
561569
transitive_runtime_jars = transitive_runtime_jars,
562570
plugins = plugins,
563571
compile_jar = compile_jar,
@@ -650,6 +658,7 @@ def _run_kt_java_builder_actions(
650658
deps_artifacts,
651659
annotation_processors,
652660
ksp_annotation_processors,
661+
ksp_options,
653662
transitive_runtime_jars,
654663
plugins,
655664
compile_jar,
@@ -699,6 +708,7 @@ def _run_kt_java_builder_actions(
699708
compile_deps = compile_deps,
700709
deps_artifacts = deps_artifacts,
701710
annotation_processors = ksp_annotation_processors,
711+
options = ksp_options,
702712
transitive_runtime_jars = transitive_runtime_jars,
703713
plugins = plugins,
704714
)
@@ -736,6 +746,7 @@ def _run_kt_java_builder_actions(
736746
compile_deps = compile_deps,
737747
deps_artifacts = deps_artifacts,
738748
annotation_processors = [],
749+
ksp_options = [],
739750
transitive_runtime_jars = transitive_runtime_jars,
740751
plugins = plugins,
741752
outputs = outputs,

kotlin/internal/jvm/impl.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,5 +437,5 @@ def kt_ksp_plugin_impl(ctx):
437437
# processors out of the public ABI.
438438
generates_api = True,
439439
),
440-
]),
440+
], options = ctx.attr.options),
441441
]

kotlin/internal/jvm/jvm.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ kt_jvm_library(
597597
doc = " The fully qualified class name that the Java compiler uses as an entry point to the annotation processor.",
598598
mandatory = True,
599599
),
600+
"options": attr.string_list(
601+
doc = "Options to be passed to the annotation processor. The syntax is key=value",
602+
),
600603
},
601604
implementation = _kt_ksp_plugin_impl,
602605
provides = [_KspPluginInfo],

kotlin/internal/jvm/plugins.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ def _targets_to_ksp_annotation_processors(targets):
4949
plugins.append(plugin.plugins)
5050
return depset(plugins)
5151

52+
def _targets_to_ksp_options(targets):
53+
options = []
54+
for t in targets:
55+
if _KspPluginInfo in t:
56+
options.extend(t[_KspPluginInfo].options)
57+
return depset(options)
58+
5259
def _targets_to_annotation_processors_java_plugin_info(targets):
5360
return [t[JavaPluginInfo] for t in targets if JavaPluginInfo in t]
5461

@@ -66,6 +73,7 @@ def _targets_to_transitive_runtime_jars(targets):
6673
mappers = struct(
6774
targets_to_annotation_processors = _targets_to_annotation_processors,
6875
targets_to_ksp_annotation_processors = _targets_to_ksp_annotation_processors,
76+
targets_to_ksp_options = _targets_to_ksp_options,
6977
targets_to_annotation_processors_java_plugin_info = _targets_to_annotation_processors_java_plugin_info,
7078
targets_to_transitive_runtime_jars = _targets_to_transitive_runtime_jars,
7179
kt_plugin_to_processor = _kt_plugin_to_processor,

src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class KotlinBuilder @Inject internal constructor(
8787
REDUCED_CLASSPATH_MODE("--reduced_classpath_mode"),
8888
INSTRUMENT_COVERAGE("--instrument_coverage"),
8989
KSP_GENERATED_JAVA_SRCJAR("--ksp_generated_java_srcjar"),
90+
KSP_OPTIONS("--ksp_options"),
9091
}
9192
}
9293

@@ -295,6 +296,9 @@ class KotlinBuilder @Inject internal constructor(
295296
addAllCompilerPluginClasspath(
296297
argMap.optional(KotlinBuilderFlags.COMPILER_PLUGIN_CLASS_PATH) ?: emptyList(),
297298
)
299+
addAllKspOptions(
300+
argMap.optional(KotlinBuilderFlags.KSP_OPTIONS) ?: emptyList(),
301+
)
298302

299303
argMap.optional(KotlinBuilderFlags.SOURCES)
300304
?.iterator()

src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/CompilationTask.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ internal fun JvmCompilationTask.kspArgs(
208208
flag(pair.first, value)
209209
}
210210
}
211+
212+
inputs.kspOptionsList.forEach { option ->
213+
flag("apoption", option)
214+
}
211215
}
212216
}
213217
}
@@ -281,6 +285,9 @@ private fun JvmCompilationTask.runKspPlugin(
281285
.flag("-d", directories.generatedClasses)
282286
.values(inputs.kotlinSourcesList)
283287
.values(inputs.javaSourcesList).list().let { args ->
288+
context.whenTracing {
289+
printLines("Ksp args:", args)
290+
}
284291
context.executeCompilerTask(
285292
args,
286293
compiler::compile,

src/main/protobuf/kotlin_model.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ message JvmCompilationTask {
158158
repeated string javac_flags = 15;
159159
// JDeps dependency artifacts
160160
repeated string deps_artifacts = 16;
161+
// Kotlin symbol processor options
162+
repeated string ksp_options = 17;
161163
}
162164

163165
CompilationTaskInfo info = 1;

0 commit comments

Comments
 (0)