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"""kt_jvm_compile_stubs""" 16 17load("//:visibility.bzl", "RULES_KOTLIN") 18load("//kotlin:common.bzl", "common") 19load("//kotlin:jvm_compile.bzl", "kt_jvm_compile") 20load("//kotlin:traverse_exports.bzl", "kt_traverse_exports") 21load("//kotlin/common/testing:analysis.bzl", "kt_analysis") 22load("//kotlin/common/testing:testing_rules.bzl", "kt_testing_rules") 23load("//toolchains/kotlin_jvm:java_toolchains.bzl", "java_toolchains") 24load("//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", "kt_jvm_toolchains") 25load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 26 27visibility(RULES_KOTLIN) 28 29_kt_jvm_compile_stub_rule = rule( 30 implementation = lambda ctx: _kt_jvm_compile_stub_rule_impl(ctx), 31 attrs = dict( 32 srcs = attr.label_list( 33 allow_files = True, 34 ), 35 common_srcs = attr.label_list( 36 allow_files = True, 37 ), 38 deps = attr.label_list( 39 aspects = [kt_traverse_exports.aspect], 40 providers = [JavaInfo], 41 ), 42 exports = attr.label_list( 43 aspects = [kt_traverse_exports.aspect], 44 providers = [JavaInfo], 45 ), 46 rule_family = attr.int( 47 default = common.RULE_FAMILY.UNKNOWN, 48 ), 49 r_java = attr.label( 50 providers = [JavaInfo], 51 ), 52 _java_toolchain = attr.label( 53 default = Label( 54 "@bazel_tools//tools/jdk:current_java_toolchain", 55 ), 56 ), 57 ), 58 fragments = ["java"], 59 outputs = dict( 60 jar = "lib%{name}.jar", 61 ), 62 toolchains = [kt_jvm_toolchains.type, "@bazel_tools//tools/jdk:toolchain_type"], 63) 64 65def _kt_jvm_compile_stub_rule_impl(ctx): 66 # As additional capabilites need to be tested, this rule should support 67 # additional fields/attributes. 68 result = kt_jvm_compile( 69 ctx, 70 output = ctx.outputs.jar, 71 srcs = ctx.files.srcs, 72 common_srcs = ctx.files.common_srcs, 73 deps = ctx.attr.deps, 74 plugins = [], 75 exported_plugins = [], 76 runtime_deps = [], 77 exports = ctx.attr.exports, 78 javacopts = [], 79 kotlincopts = [], 80 neverlink = False, 81 testonly = False, 82 android_lint_plugins = [], 83 manifest = None, 84 merged_manifest = None, 85 resource_files = [], 86 rule_family = ctx.attr.rule_family, 87 kt_toolchain = kt_jvm_toolchains.get(ctx), 88 java_toolchain = java_toolchains.get(ctx), 89 disable_lint_checks = [], 90 r_java = ctx.attr.r_java[JavaInfo] if ctx.attr.r_java else None, 91 ) 92 return [result.java_info] 93 94_kt_jvm_compile_stub_analysis_test = analysistest.make( 95 impl = lambda ctx: _kt_jvm_compile_stub_analysis_test_impl(ctx), 96 attrs = dict( 97 expected_kotlinc_classpath_names = attr.string_list(default = kt_analysis.DEFAULT_LIST), 98 ), 99) 100 101def _kt_jvm_compile_stub_analysis_test_impl(ctx): 102 kt_analysis.check_endswith_test(ctx) 103 104 env = analysistest.begin(ctx) 105 106 actions = analysistest.target_actions(env) 107 kotlinc_action = kt_analysis.get_action(actions, "Kt2JavaCompile") 108 109 asserts.true( 110 env, 111 JavaInfo in ctx.attr.target_under_test, 112 "Did not produce JavaInfo provider.", 113 ) 114 115 if ctx.attr.expected_kotlinc_classpath_names != kt_analysis.DEFAULT_LIST: 116 kotlinc_classpath = kt_analysis.get_arg(kotlinc_action, "-cp", style = "next").split(":") 117 asserts.equals( 118 env, 119 ctx.attr.expected_kotlinc_classpath_names, 120 [file.rsplit("/", 1)[1] for file in kotlinc_classpath], 121 ) 122 123 return analysistest.end(env) 124 125kt_jvm_compile_stubs = struct( 126 rule = kt_testing_rules.wrap_for_analysis(_kt_jvm_compile_stub_rule), 127 analysis_test = _kt_jvm_compile_stub_analysis_test, 128) 129