1# Copyright (C) 2023 The Android Open Source Project 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"""Macro wrapping the java_library for bp2build. """ 16 17load( 18 "@rules_java//java:defs.bzl", 19 _java_library = "java_library", 20) 21load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition_attrs") 22 23_sharded_java_library = experimental_java_library_export_do_not_use.sharded_java_library( 24 default_shard_size = 0, 25) 26 27# TODO(b/277801336): document these attributes. 28def java_library( 29 name = "", 30 srcs = [], 31 deps = [], 32 javacopts = [], 33 sdk_version = None, 34 java_version = None, 35 errorprone_force_enable = None, 36 tags = [], 37 target_compatible_with = [], 38 visibility = None, 39 javac_shard_size = 0, 40 **kwargs): 41 """ java_library macro wrapper that handles custom attrs needed in AOSP 42 43 Args: 44 errorprone_force_enable: set this to true to always run Error Prone 45 on this target (overriding the value of environment variable 46 RUN_ERROR_PRONE). Error Prone can be force disabled for an individual 47 module by adding the "-XepDisableAllChecks" flag to javacopts 48 """ 49 lib_name = name + "_private" 50 51 opts = javacopts 52 if errorprone_force_enable == None: 53 # TODO (b/227504307) temporarily disable errorprone until environment variable is handled 54 opts = opts + ["-XepDisableAllChecks"] 55 56 args = { 57 "name": lib_name, 58 "srcs": srcs, 59 "deps": deps, 60 "javacopts": opts, 61 "tags": tags + ["manual"], 62 "target_compatible_with": target_compatible_with, 63 "visibility": ["//visibility:private"], 64 } 65 args.update(kwargs) 66 if javac_shard_size > 0: 67 args["experimental_javac_shard_size"] = javac_shard_size 68 _sharded_java_library(**args) 69 else: 70 _java_library(**args) 71 72 java_library_sdk_transition( 73 name = name, 74 sdk_version = sdk_version, 75 java_version = java_version, 76 exports = lib_name, 77 tags = tags, 78 target_compatible_with = target_compatible_with, 79 visibility = visibility, 80 ) 81 82# The list of providers to forward was determined using cquery on one 83# of the example targets listed under EXAMPLE_WRAPPER_TARGETS at 84# //build/bazel/ci/target_lists.sh. It may not be exhaustive. A unit 85# test ensures that the wrapper's providers and the wrapped rule's do 86# match. 87def _java_library_sdk_transition_impl(ctx): 88 return [ 89 ctx.attr.exports[0][JavaInfo], 90 ctx.attr.exports[0][InstrumentedFilesInfo], 91 ctx.attr.exports[0][ProguardSpecProvider], 92 ctx.attr.exports[0][OutputGroupInfo], 93 ctx.attr.exports[0][DefaultInfo], 94 ] 95 96java_library_sdk_transition = rule( 97 implementation = _java_library_sdk_transition_impl, 98 attrs = sdk_transition_attrs, 99 provides = [JavaInfo], 100) 101