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