Skip to content

Commit

Permalink
Move kotlin_compiler_plugins attribute to jvm_common
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Nate Stedman authored and facebook-github-bot committed Sep 28, 2024
1 parent 0cb0eff commit 1f7b2de
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 64 deletions.
6 changes: 2 additions & 4 deletions decls/android_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []),
Expand Down Expand Up @@ -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 = """
Expand All @@ -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 = []),
Expand Down Expand Up @@ -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 = []),
Expand Down Expand Up @@ -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()
),
)
Expand Down
64 changes: 64 additions & 0 deletions decls/jvm_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -186,4 +249,5 @@ jvm_common = struct(
k2 = _k2,
incremental = _incremental,
plugins = _plugins,
kotlin_compiler_plugins = _kotlin_compiler_plugins,
)
62 changes: 2 additions & 60 deletions decls/kotlin_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""),
Expand All @@ -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() |
Expand Down Expand Up @@ -258,6 +200,7 @@ kotlin_test = prelude_rule(
"""),
} |
jvm_common.k2() |
jvm_common.kotlin_compiler_plugins() |
jvm_common.incremental() |
jvm_common.test_env() |
{
Expand All @@ -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),
Expand Down

0 comments on commit 1f7b2de

Please sign in to comment.