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