xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/kotlin/jvm_library.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"""Kotlin kt_jvm_library rule."""
16
17load("//bazel:stubs.bzl", "register_extension_info")
18load("//:visibility.bzl", "RULES_KOTLIN")
19load("@bazel_skylib//lib:dicts.bzl", "dicts")
20load("//bazel:stubs.bzl", "lint_actions")
21load("//bazel:stubs.bzl", "LINT_REGISTRY")
22load("//bazel:stubs.bzl", "registry_checks_for_package")
23load(":jvm_library.internal.bzl", "kt_jvm_library_helper")
24
25visibility(RULES_KOTLIN)
26
27def kt_jvm_library(
28        name,
29        srcs = None,
30        common_srcs = None,
31        data = None,
32        exports = None,
33        deps = None,
34        runtime_deps = None,
35        proguard_specs = None,
36        plugins = None,
37        exported_plugins = None,
38        resources = None,
39        tags = None,
40        javacopts = None,
41        custom_kotlincopts = None,
42        disable_lint_checks = None,
43        transitive_configs = None,
44        **kwargs):
45    """This rule compiles Kotlin (and Java) sources into a Jar file.
46
47    Most Java-like libraries
48    and binaries can depend on this rule, and this rule can in turn depend on Kotlin and
49    Java libraries. This rule supports a subset of attributes supported by `java_library`.
50    In addition to documentation provided as part of this rule, please also refer to their
51    documentation as part of `java_library`.
52
53    Args:
54      name: Name of the target.
55      srcs: A list of sources to compile.
56      common_srcs: A list of common sources to compile for multi-platform projects.
57      data: A list of data dependencies.
58      exports: A list of targets to export to rules that depend on this one.
59      deps: A list of dependencies. NOTE: kt_library targets cannot be added here (yet).
60      runtime_deps: Libraries to make available to the final binary or test at runtime only.
61      proguard_specs: Proguard specifications to go along with this library.
62      plugins: Java annotation processors to run at compile-time.
63      exported_plugins: https://bazel.build/reference/be/java#java_plugin rules to export to direct
64        dependencies.
65      resources: A list of data files to include in the Jar, see
66        https://bazel.build/reference/be/java#java_library.resources.
67      tags: A list of string tags passed to generated targets.
68      testonly: Whether this target is intended only for tests.
69      javacopts: Additional flags to pass to javac if used.
70      custom_kotlincopts: Additional flags to pass to Kotlin compiler.
71      disable_lint_checks: A list of AndroidLint checks to be skipped.
72      transitive_configs:  Blaze feature flags (if any) on which this target depends.
73      deprecation: Standard attribute, see
74        https://bazel.build/reference/be/common-definitions#common.deprecation.
75      features: Features enabled.
76      **kwargs: Other keyword arguments.
77    """
78    srcs = srcs or []
79    common_srcs = common_srcs or []
80    data = data or []
81    exports = exports or []
82    deps = deps or []
83    runtime_deps = runtime_deps or []
84    plugins = plugins or []
85    exported_plugins = exported_plugins or []
86    proguard_specs = proguard_specs or []
87    resources = resources or []
88
89    # Helps go/build_cleaner to identify the targets generated by the macro.
90    tags = (tags or []) + ["kt_jvm_library"]
91
92    javacopts = javacopts or []
93    disable_lint_checks = disable_lint_checks or []
94
95    kt_jvm_library_helper(
96        name = name,
97        srcs = srcs,
98        common_srcs = common_srcs,
99        deps = deps,
100        exports = exports,
101        runtime_deps = runtime_deps,
102        plugins = plugins,
103        exported_plugins = exported_plugins,
104        resources = resources,
105        javacopts = javacopts,
106        custom_kotlincopts = custom_kotlincopts,
107        proguard_specs = proguard_specs,
108        data = data,
109        disable_lint_checks = disable_lint_checks,
110        tags = tags,
111        transitive_configs = transitive_configs,
112        **dicts.add(
113            kwargs,
114            {
115                # Dictionary necessary to set private attributes.
116                "$android_lint_baseline_file": lint_actions.get_android_lint_baseline_file(native.package_name()),
117                "$android_lint_plugins": registry_checks_for_package(LINT_REGISTRY, native.package_name()),
118            },
119        )
120    )
121
122register_extension_info(
123    extension = kt_jvm_library,
124    label_regex_for_dep = "{extension_name}",
125)
126