• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# Description:
16#    Macros for building with Bazel.
17
18load("//third_party/kotlin/build_extensions:rules.bzl", "kt_android_library")
19load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
20
21def compat_kt_android_library(name, **kwargs):
22    bazel_kt_android_library(name, kwargs)
23
24def compat_kt_jvm_library(name, **kwargs):
25    bazel_kt_jvm_library(name, kwargs)
26
27def bazel_kt_android_library(name, kwargs):
28    """A macro that wraps Bazel's kt_android_library.
29
30    This macro wraps Bazel's kt_android_library to output the jars files
31    in the expected locations (b/203519416). It also adds a dependency on
32    kotlin_stdlib if there are kotlin sources.
33
34    Args:
35      name: the name of the library.
36      kwargs: Additional arguments of the library.
37    """
38
39    # If there are any kotlin sources, add the kotlin_stdlib, otherwise
40    # java-only projects may be missing a required runtime dependency on it.
41    if any([src.endswith(".kt") for src in kwargs.get("srcs", [])]):
42        # Add the kotlin_stdlib, otherwise it will be missing from java-only projects.
43        # We use deps rather than exports because exports isn't picked up by the pom file.
44        # See https://github.com/google/dagger/issues/3119
45        required_deps = ["@maven//:org_jetbrains_kotlin_kotlin_stdlib"]
46        kwargs["deps"] = kwargs.get("deps", []) + required_deps
47
48    # TODO(b/203519416): Bazel's kt_android_library outputs its jars under a target
49    # suffixed with "_kt". Thus, we have to do a bit of name aliasing to ensure that
50    # the jars exist at the expected targets.
51    kt_android_library(
52        name = "{}_internal".format(name),
53        **kwargs
54    )
55
56    native.alias(
57        name = name,
58        actual = ":{}_internal_kt".format(name),
59    )
60
61    native.alias(
62        name = "lib{}.jar".format(name),
63        actual = ":{}_internal_kt.jar".format(name),
64    )
65
66    native.alias(
67        name = "lib{}-src.jar".format(name),
68        actual = ":{}_internal_kt-sources.jar".format(name),
69    )
70
71def bazel_kt_jvm_library(name, kwargs):
72    """A macro that wraps Bazel's kt_jvm_library.
73
74    This macro wraps Bazel's kt_jvm_library to output the jars files
75    in the expected locations (https://github.com/bazelbuild/rules_kotlin/issues/324).
76
77    Args:
78      name: the name of the library.
79      kwargs: Additional arguments of the library.
80    """
81
82    kt_jvm_library(
83        name = name,
84        **kwargs
85    )
86
87    native.alias(
88        name = "lib{}-src.jar".format(name),
89        actual = ":{}-sources.jar".format(name),
90    )
91