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

Add support for passing ksp options #1036

Open
wants to merge 2 commits 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
19 changes: 11 additions & 8 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ kt_javac_options(<a href="#kt_javac_options-name">name</a>, <a href="#kt_javac_o

## kt_jvm_binary

kt_jvm_binary(<a href="#kt_jvm_binary-name">name</a>, <a href="#kt_jvm_binary-associates">associates</a>, <a href="#kt_jvm_binary-data">data</a>, <a href="#kt_jvm_binary-deps">deps</a>, <a href="#kt_jvm_binary-javac_opts">javac_opts</a>, <a href="#kt_jvm_binary-jvm_flags">jvm_flags</a>, <a href="#kt_jvm_binary-kotlinc_opts">kotlinc_opts</a>, <a href="#kt_jvm_binary-main_class">main_class</a>,
<a href="#kt_jvm_binary-module_name">module_name</a>, <a href="#kt_jvm_binary-plugins">plugins</a>, <a href="#kt_jvm_binary-resource_jars">resource_jars</a>, <a href="#kt_jvm_binary-resource_strip_prefix">resource_strip_prefix</a>, <a href="#kt_jvm_binary-resources">resources</a>, <a href="#kt_jvm_binary-runtime_deps">runtime_deps</a>,
<a href="#kt_jvm_binary-srcs">srcs</a>)
kt_jvm_binary(<a href="#kt_jvm_binary-name">name</a>, <a href="#kt_jvm_binary-associates">associates</a>, <a href="#kt_jvm_binary-data">data</a>, <a href="#kt_jvm_binary-deps">deps</a>, <a href="#kt_jvm_binary-javac_opts">javac_opts</a>, <a href="#kt_jvm_binary-jvm_flags">jvm_flags</a>, <a href="#kt_jvm_binary-kotlinc_opts">kotlinc_opts</a>, <a href="#kt_jvm_binary-ksp_opts">ksp_opts</a>,
<a href="#kt_jvm_binary-main_class">main_class</a>, <a href="#kt_jvm_binary-module_name">module_name</a>, <a href="#kt_jvm_binary-plugins">plugins</a>, <a href="#kt_jvm_binary-resource_jars">resource_jars</a>, <a href="#kt_jvm_binary-resource_strip_prefix">resource_strip_prefix</a>, <a href="#kt_jvm_binary-resources">resources</a>,
<a href="#kt_jvm_binary-runtime_deps">runtime_deps</a>, <a href="#kt_jvm_binary-srcs">srcs</a>)


Builds a Java archive ("jar file"), plus a wrapper shell script with the same name as the rule. The wrapper
Expand All @@ -97,6 +97,7 @@ kt_jvm_binary(<a href="#kt_jvm_binary-name">name</a>, <a href="#kt_jvm_binary-as
|<a id="kt_jvm_binary-javac_opts"></a>javac_opts | Javac options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | None |
|<a id="kt_jvm_binary-jvm_flags"></a>jvm_flags | A list of flags to embed in the wrapper script generated for running this binary. Note: does not yet support make variable substitution. | List of strings | optional | [] |
|<a id="kt_jvm_binary-kotlinc_opts"></a>kotlinc_opts | Kotlinc options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | None |
|<a id="kt_jvm_binary-ksp_opts"></a>ksp_opts | KSP processor options to be used when compiling this target. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | {} |
|<a id="kt_jvm_binary-main_class"></a>main_class | Name of class with main() method to use as entry point. | String | required | |
|<a id="kt_jvm_binary-module_name"></a>module_name | The name of the module, if not provided the module name is derived from the label. --e.g., <code>//some/package/path:label_name</code> is translated to <code>some_package_path-label_name</code>. | String | optional | "" |
|<a id="kt_jvm_binary-plugins"></a>plugins | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
Expand Down Expand Up @@ -168,8 +169,8 @@ kt_jvm_import(<a href="#kt_jvm_import-name">name</a>, <a href="#kt_jvm_import-de
## kt_jvm_library

kt_jvm_library(<a href="#kt_jvm_library-name">name</a>, <a href="#kt_jvm_library-associates">associates</a>, <a href="#kt_jvm_library-data">data</a>, <a href="#kt_jvm_library-deps">deps</a>, <a href="#kt_jvm_library-exported_compiler_plugins">exported_compiler_plugins</a>, <a href="#kt_jvm_library-exports">exports</a>, <a href="#kt_jvm_library-javac_opts">javac_opts</a>,
<a href="#kt_jvm_library-kotlinc_opts">kotlinc_opts</a>, <a href="#kt_jvm_library-module_name">module_name</a>, <a href="#kt_jvm_library-neverlink">neverlink</a>, <a href="#kt_jvm_library-plugins">plugins</a>, <a href="#kt_jvm_library-resource_jars">resource_jars</a>, <a href="#kt_jvm_library-resource_strip_prefix">resource_strip_prefix</a>,
<a href="#kt_jvm_library-resources">resources</a>, <a href="#kt_jvm_library-runtime_deps">runtime_deps</a>, <a href="#kt_jvm_library-srcs">srcs</a>)
<a href="#kt_jvm_library-kotlinc_opts">kotlinc_opts</a>, <a href="#kt_jvm_library-ksp_opts">ksp_opts</a>, <a href="#kt_jvm_library-module_name">module_name</a>, <a href="#kt_jvm_library-neverlink">neverlink</a>, <a href="#kt_jvm_library-plugins">plugins</a>, <a href="#kt_jvm_library-resource_jars">resource_jars</a>,
<a href="#kt_jvm_library-resource_strip_prefix">resource_strip_prefix</a>, <a href="#kt_jvm_library-resources">resources</a>, <a href="#kt_jvm_library-runtime_deps">runtime_deps</a>, <a href="#kt_jvm_library-srcs">srcs</a>)


This rule compiles and links Kotlin and Java sources into a .jar file.
Expand All @@ -188,6 +189,7 @@ kt_jvm_library(<a href="#kt_jvm_library-name">name</a>, <a href="#kt_jvm_library
|<a id="kt_jvm_library-exports"></a>exports | Exported libraries.<br><br>Deps listed here will be made available to other rules, as if the parents explicitly depended on these deps. This is not true for regular (non-exported) deps. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
|<a id="kt_jvm_library-javac_opts"></a>javac_opts | Javac options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | None |
|<a id="kt_jvm_library-kotlinc_opts"></a>kotlinc_opts | Kotlinc options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | None |
|<a id="kt_jvm_library-ksp_opts"></a>ksp_opts | KSP processor options to be used when compiling this target. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | {} |
|<a id="kt_jvm_library-module_name"></a>module_name | The name of the module, if not provided the module name is derived from the label. --e.g., <code>//some/package/path:label_name</code> is translated to <code>some_package_path-label_name</code>. | String | optional | "" |
|<a id="kt_jvm_library-neverlink"></a>neverlink | If true only use this library for compilation and not at runtime. | Boolean | optional | False |
|<a id="kt_jvm_library-plugins"></a>plugins | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
Expand All @@ -202,9 +204,9 @@ kt_jvm_library(<a href="#kt_jvm_library-name">name</a>, <a href="#kt_jvm_library

## kt_jvm_test

kt_jvm_test(<a href="#kt_jvm_test-name">name</a>, <a href="#kt_jvm_test-associates">associates</a>, <a href="#kt_jvm_test-data">data</a>, <a href="#kt_jvm_test-deps">deps</a>, <a href="#kt_jvm_test-env">env</a>, <a href="#kt_jvm_test-javac_opts">javac_opts</a>, <a href="#kt_jvm_test-jvm_flags">jvm_flags</a>, <a href="#kt_jvm_test-kotlinc_opts">kotlinc_opts</a>, <a href="#kt_jvm_test-main_class">main_class</a>,
<a href="#kt_jvm_test-module_name">module_name</a>, <a href="#kt_jvm_test-plugins">plugins</a>, <a href="#kt_jvm_test-resource_jars">resource_jars</a>, <a href="#kt_jvm_test-resource_strip_prefix">resource_strip_prefix</a>, <a href="#kt_jvm_test-resources">resources</a>, <a href="#kt_jvm_test-runtime_deps">runtime_deps</a>, <a href="#kt_jvm_test-srcs">srcs</a>,
<a href="#kt_jvm_test-test_class">test_class</a>)
kt_jvm_test(<a href="#kt_jvm_test-name">name</a>, <a href="#kt_jvm_test-associates">associates</a>, <a href="#kt_jvm_test-data">data</a>, <a href="#kt_jvm_test-deps">deps</a>, <a href="#kt_jvm_test-env">env</a>, <a href="#kt_jvm_test-javac_opts">javac_opts</a>, <a href="#kt_jvm_test-jvm_flags">jvm_flags</a>, <a href="#kt_jvm_test-kotlinc_opts">kotlinc_opts</a>, <a href="#kt_jvm_test-ksp_opts">ksp_opts</a>,
<a href="#kt_jvm_test-main_class">main_class</a>, <a href="#kt_jvm_test-module_name">module_name</a>, <a href="#kt_jvm_test-plugins">plugins</a>, <a href="#kt_jvm_test-resource_jars">resource_jars</a>, <a href="#kt_jvm_test-resource_strip_prefix">resource_strip_prefix</a>, <a href="#kt_jvm_test-resources">resources</a>,
<a href="#kt_jvm_test-runtime_deps">runtime_deps</a>, <a href="#kt_jvm_test-srcs">srcs</a>, <a href="#kt_jvm_test-test_class">test_class</a>)


Setup a simple kotlin_test.
Expand All @@ -227,6 +229,7 @@ kt_jvm_test(<a href="#kt_jvm_test-name">name</a>, <a href="#kt_jvm_test-associat
|<a id="kt_jvm_test-javac_opts"></a>javac_opts | Javac options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | None |
|<a id="kt_jvm_test-jvm_flags"></a>jvm_flags | A list of flags to embed in the wrapper script generated for running this binary. Note: does not yet support make variable substitution. | List of strings | optional | [] |
|<a id="kt_jvm_test-kotlinc_opts"></a>kotlinc_opts | Kotlinc options to be used when compiling this target. These opts if provided will be used instead of the ones provided to the toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | None |
|<a id="kt_jvm_test-ksp_opts"></a>ksp_opts | KSP processor options to be used when compiling this target. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | {} |
|<a id="kt_jvm_test-main_class"></a>main_class | - | String | optional | "com.google.testing.junit.runner.BazelTestRunner" |
|<a id="kt_jvm_test-module_name"></a>module_name | The name of the module, if not provided the module name is derived from the label. --e.g., <code>//some/package/path:label_name</code> is translated to <code>some_package_path-label_name</code>. | String | optional | "" |
|<a id="kt_jvm_test-plugins"></a>plugins | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
Expand Down
4 changes: 4 additions & 0 deletions examples/ksp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ kt_jvm_library(
"*.kt",
"*.java",
]),
ksp_opts = {
"arg1": "value1",
"arg2": "value2",
},
plugins = [
"//:moshi-kotlin-codegen",
"//:autovalue",
Expand Down
2 changes: 2 additions & 0 deletions kotlin/internal/jvm/android.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _kt_android_artifact(
plugins = [],
associates = [],
kotlinc_opts = None,
ksp_opts = None,
javac_opts = None,
enable_data_binding = False,
tags = [],
Expand Down Expand Up @@ -65,6 +66,7 @@ def _kt_android_artifact(
testonly = kwargs.get("testonly", default = False),
visibility = ["//visibility:public"],
kotlinc_opts = kotlinc_opts,
ksp_opts = ksp_opts,
javac_opts = javac_opts,
tags = tags,
exec_properties = exec_properties,
Expand Down
7 changes: 7 additions & 0 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ def _run_kt_builder_action(
"""Creates a KotlinBuilder action invocation."""
kotlinc_options = ctx.attr.kotlinc_opts[KotlincOptions] if ctx.attr.kotlinc_opts else toolchains.kt.kotlinc_options
javac_options = ctx.attr.javac_opts[JavacOptions] if ctx.attr.javac_opts else toolchains.kt.javac_options
ksp_opts = ctx.attr.ksp_opts if ctx.attr.ksp_opts else None
args = _utils.init_args(ctx, rule_kind, associates.module_name, kotlinc_options)

for f, path in outputs.items():
Expand Down Expand Up @@ -459,6 +460,12 @@ def _run_kt_builder_action(
omit_if_empty = True,
)

if ksp_opts:
args.add_all(
"--ksp_opts",
_utils.dic_to_option_list(ksp_opts),
)

args.add("--build_kotlin", build_kotlin)

progress_message = "%s %%{label} { kt: %d, java: %d, srcjars: %d } for %s" % (
Expand Down
5 changes: 5 additions & 0 deletions kotlin/internal/jvm/jvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ _common_attr = utils.add_dicts(
providers = [_JavacOptions],
mandatory = False,
),
"ksp_opts": attr.string_dict(
doc = """KSP processor options to be used when compiling this target.""",
default = {},
mandatory = False,
),
},
)

Expand Down
8 changes: 8 additions & 0 deletions kotlin/internal/utils/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,18 @@ def _builder_workspace_name(ctx):
lbl = ctx.workspace_name
return lbl.replace("external/", "")

def _dic_to_option_list(dic):
"""Converts a dictionary to a list of options in the form of `key=value`"""
options = []
for key, value in dic.items():
options.append(key + "=" + value)
return options

utils = struct(
add_dicts = _add_dicts,
init_args = _init_builder_args,
restore_label = _restore_label,
derive_module_name = _derive_module_name,
builder_workspace_name = _builder_workspace_name,
dic_to_option_list = _dic_to_option_list,
)
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_OPTS("--ksp_opts"),
}
}

Expand Down Expand Up @@ -152,7 +153,7 @@ class KotlinBuilder @Inject internal constructor(
check(it.isNotBlank()) { "--kotlin_module_name should not be blank" }
}
addAllPassthroughFlags(argMap.optional(KotlinBuilderFlags.PASSTHROUGH_FLAGS) ?: emptyList())

addAllKspOpts(argMap.optional(KotlinBuilderFlags.KSP_OPTS) ?: emptyList())
argMap.optional(KotlinBuilderFlags.FRIEND_PATHS)?.let(::addAllFriendPaths)
toolchainInfoBuilder.commonBuilder.apiVersion =
argMap.mandatorySingle(KotlinBuilderFlags.API_VERSION)
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)
}
}

info.kspOptsList.forEach { option ->
flag("apoption", option)
}
}
}
}
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 @@ -82,6 +82,8 @@ message CompilationTaskInfo {
string strict_kotlin_deps = 10;
// Optimize classpath by removing dependencies not required for compilation
string reduced_classpath_mode = 11;
// KSP plugin options.
repeated string ksp_opts = 12;
}

// Nested messages not marked with stable could be refactored.
Expand Down