xref: /aosp_15_r20/external/dagger2/tools/bazel_compat.bzl (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1# Copyright (C) 202 The Dagger Authors.
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"""Macros for building with Bazel.
16"""
17
18load("//third_party/kotlin/build_extensions:rules.bzl", "kt_android_library")
19
20def compat_kt_android_library(name, **kwargs):
21    bazel_kt_android_library(name, kwargs)
22
23def bazel_kt_android_library(name, kwargs):
24    """A macro that wraps Bazel's kt_android_library.
25
26    This macro wraps Bazel's kt_android_library to output the jars files
27    in the expected locations (b/203519416). It also adds a dependency on
28    kotlin_stdlib if there are kotlin sources.
29
30    Args:
31      name: the name of the library.
32      kwargs: Additional arguments of the library.
33    """
34
35    # If there are any kotlin sources, add the kotlin_stdlib, otherwise
36    # java-only projects may be missing a required runtime dependency on it.
37    if any([src.endswith(".kt") for src in kwargs.get("srcs", [])]):
38        # Add the kotlin_stdlib, otherwise it will be missing from java-only projects.
39        # We use deps rather than exports because exports isn't picked up by the pom file.
40        # See https://github.com/google/dagger/issues/3119
41        required_deps = ["@maven//:org_jetbrains_kotlin_kotlin_stdlib"]
42        kwargs["deps"] = kwargs.get("deps", []) + required_deps
43
44    # TODO(b/203519416): Bazel's kt_android_library outputs its jars under a target
45    # suffixed with "_kt". Thus, we have to do a bit of name aliasing to ensure that
46    # the jars exist at the expected targets.
47    kt_android_library(
48        name = "{}_internal".format(name),
49        **kwargs
50    )
51
52    native.alias(
53        name = name,
54        actual = ":{}_internal_kt".format(name),
55    )
56
57    native.alias(
58        name = "lib{}.jar".format(name),
59        actual = ":{}_internal_kt.jar".format(name),
60    )
61
62    native.alias(
63        name = "lib{}-src.jar".format(name),
64        actual = ":{}_internal_kt-sources.jar".format(name),
65    )
66