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