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