Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ksp: support ksp options #1103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ kt_kotlinc_options(<a href="#kt_kotlinc_options-name">name</a>, <a href="#kt_kot

## kt_ksp_plugin

kt_ksp_plugin(<a href="#kt_ksp_plugin-name">name</a>, <a href="#kt_ksp_plugin-deps">deps</a>, <a href="#kt_ksp_plugin-processor_class">processor_class</a>)
kt_ksp_plugin(<a href="#kt_ksp_plugin-name">name</a>, <a href="#kt_ksp_plugin-deps">deps</a>, <a href="#kt_ksp_plugin-options">options</a>, <a href="#kt_ksp_plugin-processor_class">processor_class</a>)


Define a KSP plugin for the Kotlin compiler to run. The plugin can then be referenced in the `plugins` attribute
Expand Down Expand Up @@ -478,6 +478,7 @@ kt_ksp_plugin(<a href="#kt_ksp_plugin-name">name</a>, <a href="#kt_ksp_plugin-de
| :------------- | :------------- | :------------- | :------------- | :------------- |
|<a id="kt_ksp_plugin-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
|<a id="kt_ksp_plugin-deps"></a>deps | The list of libraries to be added to the compiler's plugin classpath | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
|<a id="kt_ksp_plugin-options"></a>options | Dictionary of options to be passed to the ksp plugin. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | {} |
|<a id="kt_ksp_plugin-processor_class"></a>processor_class | The fully qualified class name that the Java compiler uses as an entry point to the annotation processor. | String | required | |


Expand Down
1 change: 1 addition & 0 deletions kotlin/internal/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ KtCompilerPluginInfo = provider(
KspPluginInfo = provider(
fields = {
"plugins": "List of JavaPLuginInfo providers for the plugins to run with KSP",
"options": "List of plugin options, represented as string with key=value field, to be passed to the ksp compiler",
},
)
11 changes: 11 additions & 0 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def _run_kapt_builder_actions(
compile_deps = compile_deps,
deps_artifacts = deps_artifacts,
annotation_processors = annotation_processors,
ksp_options = [],
transitive_runtime_jars = transitive_runtime_jars,
plugins = plugins,
outputs = {
Expand All @@ -343,6 +344,7 @@ def _run_ksp_builder_actions(
compile_deps,
deps_artifacts,
annotation_processors,
options,
transitive_runtime_jars,
plugins):
"""Runs KSP using the KotlinBuilder tool
Expand All @@ -362,6 +364,7 @@ def _run_ksp_builder_actions(
compile_deps = compile_deps,
deps_artifacts = deps_artifacts,
annotation_processors = annotation_processors,
ksp_options = options,
transitive_runtime_jars = transitive_runtime_jars,
plugins = plugins,
outputs = {
Expand All @@ -383,6 +386,7 @@ def _run_kt_builder_action(
compile_deps,
deps_artifacts,
annotation_processors,
ksp_options,
transitive_runtime_jars,
plugins,
outputs,
Expand All @@ -408,6 +412,7 @@ def _run_kt_builder_action(
args.add_all("--deps_artifacts", deps_artifacts, omit_if_empty = True)
args.add_all("--kotlin_friend_paths", associates.jars, map_each = _associate_utils.flatten_jars)
args.add("--instrument_coverage", ctx.coverage_instrumented())
args.add_all("--ksp_options", ksp_options, omit_if_empty = True)

# Collect and prepare plugin descriptor for the worker.
args.add_all(
Expand Down Expand Up @@ -535,6 +540,8 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
)
annotation_processors = _plugin_mappers.targets_to_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
ksp_annotation_processors = _plugin_mappers.targets_to_ksp_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
ksp_options = _plugin_mappers.targets_to_ksp_options(ctx.attr.plugins + ctx.attr.deps).to_list()

transitive_runtime_jars = _plugin_mappers.targets_to_transitive_runtime_jars(ctx.attr.plugins + ctx.attr.deps)
plugins = ctx.attr.plugins + _exported_plugins(deps = ctx.attr.deps)
deps_artifacts = _deps_artifacts(toolchains, ctx.attr.deps + associates.targets)
Expand All @@ -558,6 +565,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
deps_artifacts = deps_artifacts,
annotation_processors = annotation_processors,
ksp_annotation_processors = ksp_annotation_processors,
ksp_options = ksp_options,
transitive_runtime_jars = transitive_runtime_jars,
plugins = plugins,
compile_jar = compile_jar,
Expand Down Expand Up @@ -650,6 +658,7 @@ def _run_kt_java_builder_actions(
deps_artifacts,
annotation_processors,
ksp_annotation_processors,
ksp_options,
transitive_runtime_jars,
plugins,
compile_jar,
Expand Down Expand Up @@ -699,6 +708,7 @@ def _run_kt_java_builder_actions(
compile_deps = compile_deps,
deps_artifacts = deps_artifacts,
annotation_processors = ksp_annotation_processors,
options = ksp_options,
transitive_runtime_jars = transitive_runtime_jars,
plugins = plugins,
)
Expand Down Expand Up @@ -736,6 +746,7 @@ def _run_kt_java_builder_actions(
compile_deps = compile_deps,
deps_artifacts = deps_artifacts,
annotation_processors = [],
ksp_options = [],
transitive_runtime_jars = transitive_runtime_jars,
plugins = plugins,
outputs = outputs,
Expand Down
7 changes: 6 additions & 1 deletion kotlin/internal/jvm/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ def kt_compiler_plugin_impl(ctx):
def kt_ksp_plugin_impl(ctx):
info = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
classpath = depset(info.runtime_output_jars, transitive = [info.transitive_runtime_jars])
options = []
for (k, v) in ctx.attr.options.items():
if "=" in k:
fail("kt_compiler_plugin options keys cannot contain the = symbol")
options.append("%s=%s" % (k, v))

return [
DefaultInfo(files = classpath),
Expand All @@ -437,5 +442,5 @@ def kt_ksp_plugin_impl(ctx):
# processors out of the public ABI.
generates_api = True,
),
]),
], options = options),
]
6 changes: 6 additions & 0 deletions kotlin/internal/jvm/jvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,12 @@ kt_jvm_library(
doc = " The fully qualified class name that the Java compiler uses as an entry point to the annotation processor.",
mandatory = True,
),
"options": attr.string_dict(
doc = """\
Dictionary of options to be passed to the ksp plugin.
""",
default = {},
),
},
implementation = _kt_ksp_plugin_impl,
provides = [_KspPluginInfo],
Expand Down
8 changes: 8 additions & 0 deletions kotlin/internal/jvm/plugins.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def _targets_to_ksp_annotation_processors(targets):
plugins.append(plugin.plugins)
return depset(plugins)

def _targets_to_ksp_options(targets):
options = []
for t in targets:
if _KspPluginInfo in t:
options.extend(t[_KspPluginInfo].options)
return depset(options)

def _targets_to_annotation_processors_java_plugin_info(targets):
return [t[JavaPluginInfo] for t in targets if JavaPluginInfo in t]

Expand All @@ -66,6 +73,7 @@ def _targets_to_transitive_runtime_jars(targets):
mappers = struct(
targets_to_annotation_processors = _targets_to_annotation_processors,
targets_to_ksp_annotation_processors = _targets_to_ksp_annotation_processors,
targets_to_ksp_options = _targets_to_ksp_options,
targets_to_annotation_processors_java_plugin_info = _targets_to_annotation_processors_java_plugin_info,
targets_to_transitive_runtime_jars = _targets_to_transitive_runtime_jars,
kt_plugin_to_processor = _kt_plugin_to_processor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class KotlinBuilder @Inject internal constructor(
REDUCED_CLASSPATH_MODE("--reduced_classpath_mode"),
INSTRUMENT_COVERAGE("--instrument_coverage"),
KSP_GENERATED_JAVA_SRCJAR("--ksp_generated_java_srcjar"),
KSP_OPTIONS("--ksp_options"),
}
}

Expand Down Expand Up @@ -295,6 +296,9 @@ class KotlinBuilder @Inject internal constructor(
addAllCompilerPluginClasspath(
argMap.optional(KotlinBuilderFlags.COMPILER_PLUGIN_CLASS_PATH) ?: emptyList(),
)
addAllKspOptions(
argMap.optional(KotlinBuilderFlags.KSP_OPTIONS) ?: emptyList(),
)

argMap.optional(KotlinBuilderFlags.SOURCES)
?.iterator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ internal fun JvmCompilationTask.kspArgs(
flag(pair.first, value)
}
}

inputs.kspOptionsList.forEach { option ->
flag("apoption", option)
}
}
}
}
Expand Down Expand Up @@ -281,6 +285,9 @@ private fun JvmCompilationTask.runKspPlugin(
.flag("-d", directories.generatedClasses)
.values(inputs.kotlinSourcesList)
.values(inputs.javaSourcesList).list().let { args ->
context.whenTracing {
printLines("Ksp args:", args)
}
context.executeCompilerTask(
args,
compiler::compile,
Expand Down
2 changes: 2 additions & 0 deletions src/main/protobuf/kotlin_model.proto
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ message JvmCompilationTask {
repeated string javac_flags = 15;
// JDeps dependency artifacts
repeated string deps_artifacts = 16;
// Kotlin symbol processor options
repeated string ksp_options = 17;
}

CompilationTaskInfo info = 1;
Expand Down