From 1f7b2de93b0a6b51d852bd3750101832732a4485 Mon Sep 17 00:00:00 2001 From: Nate Stedman Date: Sat, 28 Sep 2024 07:00:47 -0700 Subject: [PATCH] Move kotlin_compiler_plugins attribute to jvm_common Summary: - Makes future updates simpler. - All rules using this will now get the documentation that previously only one rule had. Reviewed By: IanChilds Differential Revision: D63538422 fbshipit-source-id: 8df54260f1a6cfba8c7d9eab14911b8462ea6236 --- decls/android_rules.bzl | 6 ++-- decls/jvm_common.bzl | 64 +++++++++++++++++++++++++++++++++++++++++ decls/kotlin_rules.bzl | 62 ++------------------------------------- 3 files changed, 68 insertions(+), 64 deletions(-) diff --git a/decls/android_rules.bzl b/decls/android_rules.bzl index 8975d89a3..98e56e0d1 100644 --- a/decls/android_rules.bzl +++ b/decls/android_rules.bzl @@ -122,7 +122,6 @@ android_aar = prelude_rule( "friend_paths": attrs.list(attrs.dep(), default = []), "java_version": attrs.option(attrs.string(), default = None), "javac": attrs.option(attrs.source(), default = None), - "kotlin_compiler_plugins": attrs.dict(key = attrs.source(), value = attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False), sorted = False, default = {}), "labels": attrs.list(attrs.string(), default = []), "language": attrs.option(attrs.enum(JvmLanguage), default = None), "licenses": attrs.list(attrs.source(), default = []), @@ -752,6 +751,7 @@ android_library = prelude_rule( jvm_common.source_only_abi_deps() | jvm_common.required_for_source_only_abi() | jvm_common.k2() | + jvm_common.kotlin_compiler_plugins() | jvm_common.incremental() | { "remove_classes": attrs.list(attrs.regex(), default = [], doc = """ @@ -766,7 +766,6 @@ android_library = prelude_rule( "friend_paths": attrs.list(attrs.dep(), default = []), "java_version": attrs.option(attrs.string(), default = None), "jar_postprocessor": attrs.option(attrs.exec_dep(), default = None), - "kotlin_compiler_plugins": attrs.dict(key = attrs.source(), value = attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False), sorted = False, default = {}), "labels": attrs.list(attrs.string(), default = []), "language": attrs.option(attrs.enum(JvmLanguage), default = None), "licenses": attrs.list(attrs.source(), default = []), @@ -1436,7 +1435,6 @@ robolectric_test = prelude_rule( "java_version": attrs.option(attrs.string(), default = None), "java": attrs.option(attrs.dep(), default = None), "javac": attrs.option(attrs.source(), default = None), - "kotlin_compiler_plugins": attrs.dict(key = attrs.source(), value = attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False), sorted = False, default = {}), "labels": attrs.list(attrs.string(), default = []), "language": attrs.option(attrs.enum(JvmLanguage), default = None), "licenses": attrs.list(attrs.source(), default = []), @@ -1476,7 +1474,7 @@ robolectric_test = prelude_rule( "used_as_dependency_deprecated_do_not_use": attrs.bool(default = False), "use_jvm_abi_gen": attrs.option(attrs.bool(), default = None), "vm_args": attrs.list(attrs.arg(), default = []), - } | jvm_common.k2() | jvm_common.incremental() | jvm_common.plugins() | + } | jvm_common.k2() | jvm_common.incremental() | jvm_common.plugins() | jvm_common.kotlin_compiler_plugins() | re_test_common.test_args() ), ) diff --git a/decls/jvm_common.bzl b/decls/jvm_common.bzl index e8b05ddac..9b3f6441b 100644 --- a/decls/jvm_common.bzl +++ b/decls/jvm_common.bzl @@ -172,6 +172,69 @@ def _plugins(): ), } +def _kotlin_compiler_plugins(): + return { + "kotlin_compiler_plugins": attrs.dict(key = attrs.source(), value = attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False), sorted = False, default = {}, doc = """ + Use this to specify [Kotlin compiler plugins](https://kotlinlang.org/docs/reference/compiler-plugins.html) to use when compiling this library. + This takes a map, with each entry specify one plugin. Entry's key is plugin source path, + and value is a map of plugin option key value pair. Unlike `extra_kotlinc_arguments`, + these can be *source paths*, not just strings. + + A special option value is + `__codegen_dir__`, in which case Buck will provide a default codegen folder's path as + option value instead. + E.g. + + ``` + + kotlin_compiler_plugins = { + "somePluginSourcePath": { + "plugin:somePluginId:somePluginOptionKey": "somePluginOptionValue", + "plugin:somePluginId:someDirectoryRelatedOptionKey": "__codegen_dir__", + }, + }, + + ``` + Each plugin source path will be prefixed with `-Xplugin=` and passed as extra + arguments to the compiler. Plugin options will be appended after its plugin with `-P`. + + A specific example is, if you want to use [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) + with `kotlin_library()`, you need to specify `kotlinx-serialization-compiler-plugin.jar` under `kotlin_compiler_plugins` and `kotlinx-serialization-runtime.jar` (which you may have to fetch from Maven) in your `deps`: + + ``` + + kotlin_library( + name = "example", + srcs = glob(["*.kt"]), + deps = [ + ":kotlinx-serialization-runtime", + ], + kotlin_compiler_plugins = { + # Likely copied from your $KOTLIN_HOME directory. + "kotlinx-serialization-compiler-plugin.jar": {}, + }, + ) + + prebuilt_jar( + name = "kotlinx-serialization-runtime", + binary_jar = ":kotlinx-serialization-runtime-0.10.0", + ) + + # Note you probably want to set + # maven_repo=http://jcenter.bintray.com/ in your .buckconfig until + # https://github.com/Kotlin/kotlinx.serialization/issues/64 + # is closed. + remote_file( + name = "kotlinx-serialization-runtime-0.10.0", + out = "kotlinx-serialization-runtime-0.10.0.jar", + url = "mvn:org.jetbrains.kotlinx:kotlinx-serialization-runtime:jar:0.10.0", + sha1 = "23d777a5282c1957c7ce35946374fff0adab114c" + ) + + ``` + """), + } + jvm_common = struct( test_env = _test_env, resources_arg = _resources_arg, @@ -186,4 +249,5 @@ jvm_common = struct( k2 = _k2, incremental = _incremental, plugins = _plugins, + kotlin_compiler_plugins = _kotlin_compiler_plugins, ) diff --git a/decls/kotlin_rules.bzl b/decls/kotlin_rules.bzl index 654c6994d..2eafac630 100644 --- a/decls/kotlin_rules.bzl +++ b/decls/kotlin_rules.bzl @@ -90,65 +90,6 @@ kotlin_library = prelude_rule( Rules (usually other `kotlin_library` rules) that are used to generate the classpath required to compile this `kotlin_library`. """), - "kotlin_compiler_plugins": attrs.dict(key = attrs.source(), value = attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False), sorted = False, default = {}, doc = """ - Use this to specify [Kotlin compiler plugins](https://kotlinlang.org/docs/reference/compiler-plugins.html) to use when compiling this library. - This takes a map, with each entry specify one plugin. Entry's key is plugin source path, - and value is a map of plugin option key value pair. Unlike `extra_kotlinc_arguments`, - these can be *source paths*, not just strings. - - A special option value is - `__codegen_dir__`, in which case Buck will provide a default codegen folder's path as - option value instead. - E.g. - - ``` - - kotlin_compiler_plugins = { - "somePluginSourcePath": { - "plugin:somePluginId:somePluginOptionKey": "somePluginOptionValue", - "plugin:somePluginId:someDirectoryRelatedOptionKey": "__codegen_dir__", - }, - }, - - ``` - Each plugin source path will be prefixed with `-Xplugin=` and passed as extra - arguments to the compiler. Plugin options will be appended after its plugin with `-P`. - - A specific example is, if you want to use [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) - with `kotlin_library()`, you need to specify `kotlinx-serialization-compiler-plugin.jar` under `kotlin_compiler_plugins` and `kotlinx-serialization-runtime.jar` (which you may have to fetch from Maven) in your `deps`: - - ``` - - kotlin_library( - name = "example", - srcs = glob(["*.kt"]), - deps = [ - ":kotlinx-serialization-runtime", - ], - kotlin_compiler_plugins = { - # Likely copied from your $KOTLIN_HOME directory. - "kotlinx-serialization-compiler-plugin.jar": {}, - }, - ) - - prebuilt_jar( - name = "kotlinx-serialization-runtime", - binary_jar = ":kotlinx-serialization-runtime-0.10.0", - ) - - # Note you probably want to set - # maven_repo=http://jcenter.bintray.com/ in your .buckconfig until - # https://github.com/Kotlin/kotlinx.serialization/issues/64 - # is closed. - remote_file( - name = "kotlinx-serialization-runtime-0.10.0", - out = "kotlinx-serialization-runtime-0.10.0.jar", - url = "mvn:org.jetbrains.kotlinx:kotlinx-serialization-runtime:jar:0.10.0", - sha1 = "23d777a5282c1957c7ce35946374fff0adab114c" - ) - - ``` - """), "extra_kotlinc_arguments": attrs.list(attrs.string(), default = [], doc = """ List of additional arguments to pass into the Kotlin compiler. """), @@ -169,6 +110,7 @@ kotlin_library = prelude_rule( jvm_common.provided_deps() | jvm_common.exported_provided_deps() | jvm_common.k2() | + jvm_common.kotlin_compiler_plugins() | jvm_common.incremental() | jvm_common.plugins() | buck.labels_arg() | @@ -258,6 +200,7 @@ kotlin_test = prelude_rule( """), } | jvm_common.k2() | + jvm_common.kotlin_compiler_plugins() | jvm_common.incremental() | jvm_common.test_env() | { @@ -279,7 +222,6 @@ kotlin_test = prelude_rule( "java_version": attrs.option(attrs.string(), default = None), "java": attrs.option(attrs.dep(), default = None), "javac": attrs.option(attrs.source(), default = None), - "kotlin_compiler_plugins": attrs.dict(key = attrs.source(), value = attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False), sorted = False, default = {}), "licenses": attrs.list(attrs.source(), default = []), "manifest_file": attrs.option(attrs.source(), default = None), "maven_coords": attrs.option(attrs.string(), default = None),