Skip to content

Commit

Permalink
Reapply "[kt_compiler_plugin] Add kt_plugin_cfg for reusable and comp…
Browse files Browse the repository at this point in the history
…… …osable op…" (#1131)  (#1135)

* Reapply "[kt_compiler_plugin] Add kt_plugin_cfg for reusable and composable op…" (#1131)

This reverts commit 00ef838.

* Cleanup lint, etc
  • Loading branch information
restingbull committed Mar 27, 2024
1 parent d5b0308 commit 973e16d
Show file tree
Hide file tree
Showing 26 changed files with 899 additions and 104 deletions.
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ use_repo(maven, "kotlin_rules_maven", "unpinned_kotlin_rules_maven")
bazel_dep(name = "rules_pkg", version = "0.7.0")
bazel_dep(name = "stardoc", version = "0.5.6", repo_name = "io_bazel_stardoc")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")

bazel_dep(name = "rules_testing", version = "0.5.0", dev_dependency = True)
27 changes: 26 additions & 1 deletion docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ kt_compiler_plugin(<a href="#kt_compiler_plugin-name">name</a>, <a href="#kt_com
|<a id="kt_compiler_plugin-compile_phase"></a>compile_phase | Runs the compiler plugin during kotlin compilation. Known examples: <code>allopen</code>, <code>sam_with_reciever</code> | Boolean | optional | True |
|<a id="kt_compiler_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_compiler_plugin-id"></a>id | The ID of the plugin | String | required | |
|<a id="kt_compiler_plugin-options"></a>options | Dictionary of options to be passed to the plugin. Supports the following template values:<br><br>- <code>{generatedClasses}</code>: directory for generated class output - <code>{temp}</code>: temporary directory, discarded between invocations - <code>{generatedSources}</code>: directory for generated source output | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | {} |
|<a id="kt_compiler_plugin-options"></a>options | Dictionary of options to be passed to the plugin. Supports the following template values:<br><br>- <code>{generatedClasses}</code>: directory for generated class output - <code>{temp}</code>: temporary directory, discarded between invocations - <code>{generatedSources}</code>: directory for generated source output - <code>{classpath}</code> : replaced with a list of jars separated by the filesystem appropriate separator. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | {} |
|<a id="kt_compiler_plugin-stubs_phase"></a>stubs_phase | Runs the compiler plugin in kapt stub generation. | Boolean | optional | True |
|<a id="kt_compiler_plugin-target_embedded_compiler"></a>target_embedded_compiler | Plugin was compiled against the embeddable kotlin compiler. These plugins expect shaded kotlinc dependencies, and will fail when running against a non-embeddable compiler. | Boolean | optional | False |

Expand Down Expand Up @@ -482,6 +482,31 @@ kt_ksp_plugin(<a href="#kt_ksp_plugin-name">name</a>, <a href="#kt_ksp_plugin-de
|<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 | |


<a id="#kt_plugin_cfg"></a>

## kt_plugin_cfg

kt_plugin_cfg(<a href="#kt_plugin_cfg-name">name</a>, <a href="#kt_plugin_cfg-deps">deps</a>, <a href="#kt_plugin_cfg-options">options</a>, <a href="#kt_plugin_cfg-plugin">plugin</a>)

Configurations for kt_compiler_plugin, ksp_plugin, and java_plugin.
This allows setting options and dependencies independently from the initial plugin definition.


**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
|<a id="kt_plugin_cfg-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
|<a id="kt_plugin_cfg-deps"></a>deps | Dependencies for this configuration. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
|<a id="kt_plugin_cfg-options"></a>options | A dictionary of flag to values to be used as plugin configuration options. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> List of strings</a> | optional | {} |
|<a id="kt_plugin_cfg-plugin"></a>plugin | The plugin to associate with this configuration | <a href="https://bazel.build/concepts/labels">Label</a> | required | |


<a id="define_kt_toolchain"></a>

## define_kt_toolchain
Expand Down
72 changes: 71 additions & 1 deletion examples/ksp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ load("@rules_java//java:defs.bzl", "java_binary", "java_plugin")
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_ksp_plugin")
load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_compiler_plugin", "kt_ksp_plugin", "kt_plugin_cfg")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -81,3 +81,73 @@ build_test(
"//:coffee_app_deploy.jar",
],
)

kt_compiler_plugin(
name = "ksp",
compile_phase = True,
id = "com.google.devtools.ksp.symbol-processing",
options = {
"apclasspath": "{classpath}",
# projectBaseDir shouldn't matter because incremental is disabled
"projectBaseDir": "{temp}",
# Disable incremental mode
"incremental": "false",
# Directory where class files are written to. Files written to this directory are class
# files being written directly from the annotation processor, not Kotlinc
"classOutputDir": "{generatedClasses}",
# Directory where generated Java sources files are written to
"javaOutputDir": "{generatedSources}",
# Directory where generated Kotlin sources files are written to
"kotlinOutputDir": "{generatedSources}",
# Directory where META-INF data is written to. This might not be the most ideal place to
# write this. Maybe just directly to the classes directory?
"resourceOutputDir": "{generatedSources}",
# TODO(bencodes) Not sure what this directory is yet.
"kspOutputDir": "{temp}",
# Directory to write KSP caches. Shouldn't matter because incremental is disabled
"cachesDir": "{temp}",
# Include in compilation as an example. This should be processed in the stubs phase.
"withCompilation": "true",
# Set returnOkOnError to false because we want to fail the build if there are any errors
"returnOkOnError": "false",
"allWarningsAsErrors": "false",
},
deps = [
"@rules_kotlin//kotlin/compiler:symbol-processing-api",
"@rules_kotlin//kotlin/compiler:symbol-processing-cmdline",
],
)

kt_plugin_cfg(
name = "ksp_moshi",
options = {
},
plugin = ":ksp",
deps = [
"@maven//:com_squareup_moshi_moshi",
"@maven//:com_squareup_moshi_moshi_kotlin",
"@maven//:com_squareup_moshi_moshi_kotlin_codegen",
],
)

kt_jvm_library(
name = "raw_ksp_coffee_app_lib",
srcs = ["CoffeeAppModel.kt"],
plugins = [
"//:ksp",
"//:ksp_moshi",
],
deps = [
"@maven//:com_google_auto_service_auto_service_annotations",
"@maven//:com_google_auto_value_auto_value_annotations",
"@maven//:com_squareup_moshi_moshi",
"@maven//:com_squareup_moshi_moshi_kotlin",
],
)

build_test(
name = "raw_ksp_lib_test",
targets = [
"//:raw_ksp_coffee_app_lib",
],
)
6 changes: 6 additions & 0 deletions examples/ksp/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
2 changes: 2 additions & 0 deletions kotlin/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ load(
"//kotlin/internal/jvm:jvm.bzl",
_kt_compiler_plugin = "kt_compiler_plugin",
_kt_ksp_plugin = "kt_ksp_plugin",
_kt_plugin_cfg = "kt_plugin_cfg",
)

define_kt_toolchain = _define_kt_toolchain
Expand All @@ -20,3 +21,4 @@ kt_javac_options = _kt_javac_options
kt_kotlinc_options = _kt_kotlinc_options
kt_compiler_plugin = _kt_compiler_plugin
kt_ksp_plugin = _kt_ksp_plugin
kt_plugin_cfg = _kt_plugin_cfg
1 change: 1 addition & 0 deletions kotlin/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ bzl_library(
"//kotlin/internal/lint",
"//kotlin/internal/utils",
"//src/main/starlark",
"//src/main/starlark/core/options",
"@rules_java//java:rules",
],
)
21 changes: 0 additions & 21 deletions kotlin/internal/compiler_plugins.bzl

This file was deleted.

27 changes: 13 additions & 14 deletions kotlin/internal/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.#
load(
"//src/main/starlark/core/plugin:providers.bzl",
_KspPluginInfo = "KspPluginInfo",
_KtCompilerPluginInfo = "KtCompilerPluginInfo",
_KtCompilerPluginOption = "KtCompilerPluginOption",
_KtPluginConfiguration = "KtPluginConfiguration",
)

# The Kotlin Toolchain type.
TOOLCHAIN_TYPE = "%s" % Label("//kotlin/internal:kt_toolchain_type")
Expand Down Expand Up @@ -50,18 +57,10 @@ KtJsInfo = provider(
},
)

KtCompilerPluginInfo = provider(
fields = {
"plugin_jars": "List of plugin jars.",
"classpath": "The kotlin compiler plugin classpath.",
"stubs": "Run this plugin during kapt stub generation.",
"compile": "Run this plugin during koltinc compilation.",
"options": "List of plugin options, represented as structs with an id and a value field, to be passed to the compiler",
},
)
KtCompilerPluginInfo = _KtCompilerPluginInfo

KspPluginInfo = provider(
fields = {
"plugins": "List of JavaPLuginInfo providers for the plugins to run with KSP",
},
)
KspPluginInfo = _KspPluginInfo

KtCompilerPluginOption = _KtCompilerPluginOption

KtPluginConfiguration = _KtPluginConfiguration
Loading

0 comments on commit 973e16d

Please sign in to comment.