• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_import rule."""
16
17load(":common.bzl", "common")
18load(":traverse_exports.bzl", "kt_traverse_exports")
19load("//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", "kt_jvm_toolchains")
20load("//toolchains/kotlin_jvm:java_toolchains.bzl", "java_toolchains")
21load("@bazel_skylib//lib:dicts.bzl", "dicts")
22load(":compiler_plugin.bzl", "KtCompilerPluginInfo")
23load("//:visibility.bzl", "RULES_KOTLIN")
24
25def _kt_jvm_import_impl(ctx):
26    kt_jvm_toolchain = kt_jvm_toolchains.get(ctx)
27
28    runtime_deps_java_infos = []
29    for runtime_dep in ctx.attr.runtime_deps:
30        if JavaInfo in runtime_dep:
31            runtime_deps_java_infos.append(runtime_dep[JavaInfo])
32        elif CcInfo not in runtime_dep:
33            fail("Unexpected runtime dependency (must provide JavaInfo or CcInfo): %" % runtime_dep.label)
34
35    result = common.kt_jvm_import(
36        ctx,
37        kt_toolchain = kt_jvm_toolchain,
38        jars = ctx.files.jars,
39        srcjar = ctx.file.srcjar,
40        deps = common.collect_providers(JavaInfo, ctx.attr.deps),
41        runtime_deps = runtime_deps_java_infos,
42        neverlink = ctx.attr.neverlink,
43        java_toolchain = java_toolchains.get(ctx),
44        deps_checker = ctx.executable._deps_checker,
45    )
46
47    # Collect runfiles from deps unless neverlink
48    runfiles = None
49    if not ctx.attr.neverlink:
50        transitive_runfiles = []
51        for p in common.collect_providers(DefaultInfo, ctx.attr.deps):
52            transitive_runfiles.append(p.data_runfiles.files)
53            transitive_runfiles.append(p.default_runfiles.files)
54        runfiles = ctx.runfiles(
55            files = ctx.files.jars,
56            transitive_files = depset(transitive = transitive_runfiles),
57            collect_default = True,  # handles data attribute
58        )
59
60    return [
61        result.java_info,
62        ProguardSpecProvider(common.collect_proguard_specs(
63            ctx,
64            ctx.files.proguard_specs,
65            ctx.attr.deps,
66            kt_jvm_toolchain.proguard_whitelister,
67        )),
68        OutputGroupInfo(_validation = depset(result.validations)),
69        DefaultInfo(runfiles = runfiles),  # rule doesn't build any files
70    ]
71
72_KT_JVM_IMPORT_ATTRS = dicts.add(
73    java_toolchains.attrs,
74    kt_jvm_toolchains.attrs,
75    deps = attr.label_list(
76        aspects = [kt_traverse_exports.aspect],
77        providers = [
78            # Each provider-set expands on allow_rules
79            [JavaInfo],  # We allow android rule deps to make importing android JARs easier.
80        ],
81        doc = """The list of libraries this library directly depends on at compile-time. For Java
82                 and Kotlin libraries listed, the Jars they build as well as the transitive closure
83                 of their `deps` and `exports` will be on the compile-time classpath for this rule;
84                 also, the transitive closure of their `deps`, `runtime_deps`, and `exports` will be
85                 on the runtime classpath (excluding dependencies only depended on as `neverlink`).
86
87                 Note on strict_deps: any Java type explicitly or implicitly referred to in `srcs`
88                 must be included here. This is a stronger requirement than what is enforced for
89                 `java_library`. Any build failures resulting from this requirement will include the
90                 missing dependencies and a command to fix the rule.""",
91    ),
92    exported_plugins = attr.label_list(
93        providers = [[KtCompilerPluginInfo]],
94        cfg = "exec",
95        doc = """JVM plugins to export to users.
96
97
98                 Every plugin listed will run during compliations that depend on this target, as
99                 if it were listed directly in that target's `plugins` attribute. `java_*` targets
100                 will not run kotlinc plugins""",
101    ),
102    jars = attr.label_list(
103        allow_files = common.JAR_FILE_TYPE,
104        allow_empty = False,
105        doc = """The list of Java and/or Kotlin JAR files provided to targets that depend on this
106                 target (required).  Currently only a single Jar is supported.""",
107    ),
108    neverlink = attr.bool(
109        default = False,
110        doc = """Only use this library for compilation and not at runtime.""",
111    ),
112    proguard_specs = attr.label_list(
113        allow_files = True,
114        doc = """Proguard specifications to go along with this library.""",
115    ),
116    runtime_deps = attr.label_list(
117        providers = [
118            # Each provider-set expands on allow_rules
119            [JavaInfo],
120            [CcInfo],  # for JNI / native dependencies
121        ],
122        aspects = [kt_traverse_exports.aspect],
123        doc = """Runtime-only dependencies.""",
124    ),
125    srcjar = attr.label(
126        allow_single_file = common.SRCJAR_FILE_TYPES,
127        doc = """A JAR file that contains source code for the compiled JAR files.""",
128    ),
129    _deps_checker = attr.label(
130        default = "@bazel_tools//tools/android:aar_import_deps_checker",
131        executable = True,
132        cfg = "exec",
133    ),
134)
135
136kt_jvm_import = rule(
137    attrs = _KT_JVM_IMPORT_ATTRS,
138    fragments = ["java"],
139    provides = [JavaInfo],
140    implementation = _kt_jvm_import_impl,
141    toolchains = [kt_jvm_toolchains.type],
142    doc = """Allows the use of precompiled Kotlin `.jar` files as deps of `kt_*` targets.
143
144             Prefer this rule to `java_import` for Kotlin Jars. Most Java-like libraries
145             and binaries can depend on this rule, and this rule can in turn depend on Kotlin and
146             Java libraries. This rule supports a subset of attributes supported by `java_import`.
147
148             In addition to documentation provided as part of this rule, please also refer to their
149             documentation as part of `java_import`.
150          """,
151)
152