xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/kotlin/compiler_plugin.bzl (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
1# Copyright 2022 Google LLC. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the License);
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""A rule for declaring and passing kotlinc plugins."""
16
17load("//:visibility.bzl", "RULES_KOTLIN")
18
19visibility(RULES_KOTLIN)
20
21KtCompilerPluginInfo, _make_kt_compiler_plugin_info = provider(
22    doc = "Info for running a plugin that directly registers itself to kotlinc extension points",
23    fields = dict(
24        plugin_id = "string",
25        jar = "File",
26        args = "list[string]",
27    ),
28    init = fail,
29)
30
31def _kt_compiler_plugin_impl(ctx):
32
33    return [
34        JavaPluginInfo(
35            runtime_deps = [],
36            processor_class = None,
37        ),
38        _make_kt_compiler_plugin_info(
39            plugin_id = ctx.attr.plugin_id,
40            jar = ctx.file.jar or ctx.attr.jar[JavaInfo].output_jar,
41            args = [
42                "plugin:%s:%s=%s" % (ctx.attr.plugin_id, k, v)
43                for (k, v) in ctx.attr.args.items()
44            ],
45        ),
46    ]
47
48kt_compiler_plugin = rule(
49    implementation = _kt_compiler_plugin_impl,
50    attrs = dict(
51        plugin_id = attr.string(
52            doc = "ID used to register this plugin with kotlinc",
53            mandatory = True,
54        ),
55        jar = attr.label(
56            doc = "JAR that provides the plugin implementation",
57            mandatory = True,
58            allow_single_file = [".jar"],
59            cfg = "exec",
60        ),
61        args = attr.string_dict(
62            doc = """Args to pass to the plugin
63
64            The rule impl will format key-value pairs for the koltinc
65            CLI. All plugin invocations will receive the same args.
66            """,
67            default = {},
68        ),
69    ),
70    provides = [
71        JavaPluginInfo,  # Allow this rule to be passed to java rules
72        KtCompilerPluginInfo,
73    ],
74)
75