1""" 2Copyright (C) 2023 The Android Open Source Project 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15""" 16 17load("@rules_kotlin//kotlin:compiler_opt.bzl", "kt_compiler_opt") 18load("@rules_kotlin//kotlin:rules.bzl", _kt_jvm_library = "kt_jvm_library") 19load("//build/bazel/rules/java:java_resources.bzl", "java_resources") 20load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition_attrs") 21 22def make_kt_compiler_opt( 23 name, 24 kotlincflags = None): 25 custom_kotlincopts = None 26 if kotlincflags != None: 27 ktcopts_name = name + "_kotlincopts" 28 kt_compiler_opt( 29 name = ktcopts_name, 30 opts = kotlincflags, 31 ) 32 custom_kotlincopts = [":" + ktcopts_name] 33 34 return custom_kotlincopts 35 36# TODO(b/277801336): document these attributes. 37def kt_jvm_library( 38 name, 39 deps = None, 40 resources = None, 41 resource_strip_prefix = None, 42 kotlincflags = None, 43 java_version = None, 44 sdk_version = None, 45 javacopts = [], 46 errorprone_force_enable = None, 47 tags = [], 48 target_compatible_with = [], 49 visibility = None, 50 **kwargs): 51 """Bazel macro wrapping for kt_jvm_library 52 53 Attributes: 54 errorprone_force_enable: set this to true to always run Error Prone 55 on this target (overriding the value of environment variable 56 RUN_ERROR_PRONE). Error Prone can be force disabled for an individual 57 module by adding the "-XepDisableAllChecks" flag to javacopts 58 """ 59 if resource_strip_prefix != None: 60 kt_res_jar_name = name + "__kt_res_jar" 61 62 java_resources( 63 name = kt_res_jar_name, 64 resources = resources, 65 resource_strip_prefix = resource_strip_prefix, 66 ) 67 68 deps = deps + [":" + kt_res_jar_name] 69 70 custom_kotlincopts = make_kt_compiler_opt(name, kotlincflags) 71 72 opts = javacopts 73 if errorprone_force_enable == None: 74 # TODO (b/227504307) temporarily disable errorprone until environment variable is handled 75 opts = opts + ["-XepDisableAllChecks"] 76 77 lib_name = name + "_private" 78 _kt_jvm_library( 79 name = lib_name, 80 deps = deps, 81 custom_kotlincopts = custom_kotlincopts, 82 javacopts = opts, 83 tags = tags + ["manual"], 84 target_compatible_with = target_compatible_with, 85 visibility = ["//visibility:private"], 86 **kwargs 87 ) 88 89 kt_jvm_library_sdk_transition( 90 name = name, 91 sdk_version = sdk_version, 92 java_version = java_version, 93 exports = lib_name, 94 tags = tags, 95 target_compatible_with = target_compatible_with, 96 visibility = visibility, 97 ) 98 99# The list of providers to forward was determined using cquery on one 100# of the example targets listed under EXAMPLE_WRAPPER_TARGETS at 101# //build/bazel/ci/target_lists.sh. It may not be exhaustive. A unit 102# test ensures that the wrapper's providers and the wrapped rule's do 103# match. 104def _kt_jvm_library_sdk_transition_impl(ctx): 105 return [ 106 ctx.attr.exports[0][JavaInfo], 107 ctx.attr.exports[0][InstrumentedFilesInfo], 108 ctx.attr.exports[0][ProguardSpecProvider], 109 ctx.attr.exports[0][OutputGroupInfo], 110 ctx.attr.exports[0][DefaultInfo], 111 ] 112 113kt_jvm_library_sdk_transition = rule( 114 implementation = _kt_jvm_library_sdk_transition_impl, 115 attrs = sdk_transition_attrs, 116 provides = [JavaInfo], 117) 118