From 742038e32a35cda3fe4bcc0b3ce789c7a22ef9c3 Mon Sep 17 00:00:00 2001 From: idanakav Date: Fri, 29 Sep 2023 11:00:38 -0700 Subject: [PATCH 1/2] Add support for passing ksp options This commit adds the support to pass options to KSP processors, currently it the support is added on a target level only. Eventually the options are passed through the kotlinc: `-P google.devtools.ksp.symbol-processing:apoption=key=value` --- examples/ksp/BUILD | 4 ++++ kotlin/internal/jvm/android.bzl | 2 ++ kotlin/internal/jvm/compile.bzl | 7 +++++++ kotlin/internal/jvm/jvm.bzl | 5 +++++ kotlin/internal/utils/utils.bzl | 8 ++++++++ .../kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt | 3 ++- .../io/bazel/kotlin/builder/tasks/jvm/CompilationTask.kt | 4 ++++ src/main/protobuf/kotlin_model.proto | 2 ++ 8 files changed, 34 insertions(+), 1 deletion(-) diff --git a/examples/ksp/BUILD b/examples/ksp/BUILD index d6c194b32..b8b6a3800 100644 --- a/examples/ksp/BUILD +++ b/examples/ksp/BUILD @@ -43,6 +43,10 @@ kt_jvm_library( "*.kt", "*.java", ]), + ksp_opts = { + "arg1": "value1", + "arg2": "value2", + }, plugins = [ "//:moshi-kotlin-codegen", "//:autovalue", diff --git a/kotlin/internal/jvm/android.bzl b/kotlin/internal/jvm/android.bzl index b43f3c686..6a38f063d 100644 --- a/kotlin/internal/jvm/android.bzl +++ b/kotlin/internal/jvm/android.bzl @@ -31,6 +31,7 @@ def _kt_android_artifact( plugins = [], associates = [], kotlinc_opts = None, + ksp_opts = None, javac_opts = None, enable_data_binding = False, tags = [], @@ -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, diff --git a/kotlin/internal/jvm/compile.bzl b/kotlin/internal/jvm/compile.bzl index cc692e96e..3921f4a48 100644 --- a/kotlin/internal/jvm/compile.bzl +++ b/kotlin/internal/jvm/compile.bzl @@ -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(): @@ -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" % ( diff --git a/kotlin/internal/jvm/jvm.bzl b/kotlin/internal/jvm/jvm.bzl index ed3029c35..9746b1390 100644 --- a/kotlin/internal/jvm/jvm.bzl +++ b/kotlin/internal/jvm/jvm.bzl @@ -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, + ), }, ) diff --git a/kotlin/internal/utils/utils.bzl b/kotlin/internal/utils/utils.bzl index 09581637f..703e7dd3b 100644 --- a/kotlin/internal/utils/utils.bzl +++ b/kotlin/internal/utils/utils.bzl @@ -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, ) diff --git a/src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt b/src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt index d2dd9d483..7af9992ff 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt @@ -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"), } } @@ -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) diff --git a/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/CompilationTask.kt b/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/CompilationTask.kt index ebc836178..f3f228ffe 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/CompilationTask.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/CompilationTask.kt @@ -208,6 +208,10 @@ internal fun JvmCompilationTask.kspArgs( flag(pair.first, value) } } + + info.kspOptsList.forEach { option -> + flag("apoption", option) + } } } } diff --git a/src/main/protobuf/kotlin_model.proto b/src/main/protobuf/kotlin_model.proto index 2a3b5146b..75b8618ca 100644 --- a/src/main/protobuf/kotlin_model.proto +++ b/src/main/protobuf/kotlin_model.proto @@ -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. From e1d78d7a34bb8f731a8f520a08b3eccfdda68cb4 Mon Sep 17 00:00:00 2001 From: idanakav Date: Fri, 29 Sep 2023 11:17:43 -0700 Subject: [PATCH 2/2] Update docs --- docs/kotlin.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/kotlin.md b/docs/kotlin.md index 5eadb3734..1f992c0dc 100755 --- a/docs/kotlin.md +++ b/docs/kotlin.md @@ -72,9 +72,9 @@ kt_javac_options(name, name, associates, data, deps, javac_opts, jvm_flags, kotlinc_opts, main_class, - module_name, plugins, resource_jars, resource_strip_prefix, resources, runtime_deps, - srcs) +kt_jvm_binary(name, associates, data, deps, javac_opts, jvm_flags, kotlinc_opts, ksp_opts, + main_class, module_name, plugins, resource_jars, resource_strip_prefix, resources, + runtime_deps, srcs) Builds a Java archive ("jar file"), plus a wrapper shell script with the same name as the rule. The wrapper @@ -97,6 +97,7 @@ kt_jvm_binary(name, 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. | Label | optional | None | |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 | [] | |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. | Label | optional | None | +|ksp_opts | KSP processor options to be used when compiling this target. | Dictionary: String -> String | optional | {} | |main_class | Name of class with main() method to use as entry point. | String | required | | |module_name | The name of the module, if not provided the module name is derived from the label. --e.g., //some/package/path:label_name is translated to some_package_path-label_name. | String | optional | "" | |plugins | - | List of labels | optional | [] | @@ -168,8 +169,8 @@ kt_jvm_import(name, name, associates, data, deps, exported_compiler_plugins, exports, javac_opts, - kotlinc_opts, module_name, neverlink, plugins, resource_jars, resource_strip_prefix, - resources, runtime_deps, srcs) + kotlinc_opts, ksp_opts, module_name, neverlink, plugins, resource_jars, + resource_strip_prefix, resources, runtime_deps, srcs) This rule compiles and links Kotlin and Java sources into a .jar file. @@ -188,6 +189,7 @@ kt_jvm_library(name, exports | Exported libraries.

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. | List of labels | optional | [] | |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. | Label | optional | None | |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. | Label | optional | None | +|ksp_opts | KSP processor options to be used when compiling this target. | Dictionary: String -> String | optional | {} | |module_name | The name of the module, if not provided the module name is derived from the label. --e.g., //some/package/path:label_name is translated to some_package_path-label_name. | String | optional | "" | |neverlink | If true only use this library for compilation and not at runtime. | Boolean | optional | False | |plugins | - | List of labels | optional | [] | @@ -202,9 +204,9 @@ kt_jvm_library(name, name, associates, data, deps, env, javac_opts, jvm_flags, kotlinc_opts, main_class, - module_name, plugins, resource_jars, resource_strip_prefix, resources, runtime_deps, srcs, - test_class) +kt_jvm_test(name, associates, data, deps, env, javac_opts, jvm_flags, kotlinc_opts, ksp_opts, + main_class, module_name, plugins, resource_jars, resource_strip_prefix, resources, + runtime_deps, srcs, test_class) Setup a simple kotlin_test. @@ -227,6 +229,7 @@ kt_jvm_test(name, 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. | Label | optional | None | |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 | [] | |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. | Label | optional | None | +|ksp_opts | KSP processor options to be used when compiling this target. | Dictionary: String -> String | optional | {} | |main_class | - | String | optional | "com.google.testing.junit.runner.BazelTestRunner" | |module_name | The name of the module, if not provided the module name is derived from the label. --e.g., //some/package/path:label_name is translated to some_package_path-label_name. | String | optional | "" | |plugins | - | List of labels | optional | [] |