xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/kotlin/compiler_plugin_export.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"""Convenience macro for getting `kt_compiler_plugin` into uncooperative rules.
16
17Some targets (Z), where we would like to export a plugin (Y), use rules that don't
18support `exported_plugins` (e.g android_library). To solve this, we create a dummy
19target (X) that has `X.exported_plugins = [Y]`, and then set `Z.exports = [Y]`.
20This creates a chain of exports for `kt_traverse_exports` to follow when discovering
21`kt_compiler_plugin`s.
22"""
23
24load(":compiler_plugin.bzl", "kt_compiler_plugin")
25load(":jvm_import.bzl", "kt_jvm_import")
26
27def _kt_compiler_plugin_export(
28        name,
29        visibility = [],
30        compatible_with = [],
31        **kwargs):
32    if not name.endswith("_plugin_export"):
33        fail()
34
35    basename = name.replace("_plugin_export", "")
36
37    kt_jvm_import(
38        name = name,
39        exported_plugins = [basename + "_plugin"],
40        jars = [basename + "_empty_jar"],
41                compatible_with = compatible_with,
42        visibility = visibility,
43        neverlink = True,  # Don't link kotlin stdlib into Java users
44    )
45
46    kt_compiler_plugin(
47        name = basename + "_plugin",
48        visibility = visibility,
49        compatible_with = compatible_with,
50        **kwargs
51    )
52
53    native.genrule(
54        name = basename + "_empty_jar",
55        visibility = visibility,
56        outs = [name + "_empty.jar"],
57        cmd = """$(location @bazel_tools//tools/zip:zipper) c $@ "assets/_empty=" """,
58        compatible_with = compatible_with,
59        tools = ["@bazel_tools//tools/zip:zipper"],
60    )
61
62kt_compiler_plugin_export = _kt_compiler_plugin_export
63