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