• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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", "sdk_transition_attrs")
22
23# TODO(b/277801336): document these attributes.
24def java_library(
25        name = "",
26        srcs = [],
27        deps = [],
28        javacopts = [],
29        sdk_version = None,
30        java_version = None,
31        tags = [],
32        target_compatible_with = [],
33        visibility = None,
34        **kwargs):
35    lib_name = name + "_private"
36
37    #    Disable the error prone check of HashtableContains by default. See https://errorprone.info/bugpattern/HashtableContains
38    #    HashtableContains error is reported when compiling //external/bouncycastle:bouncycastle-bcpkix-unbundled
39    opts = ["-Xep:HashtableContains:OFF"] + javacopts
40
41    _java_library(
42        name = lib_name,
43        srcs = srcs,
44        deps = deps,
45        javacopts = opts,
46        tags = tags + ["manual"],
47        target_compatible_with = target_compatible_with,
48        visibility = ["//visibility:private"],
49        **kwargs
50    )
51
52    java_library_sdk_transition(
53        name = name,
54        sdk_version = sdk_version,
55        java_version = java_version,
56        exports = lib_name,
57        tags = tags,
58        target_compatible_with = target_compatible_with,
59        visibility = ["//visibility:public"],
60    )
61
62# The list of providers to forward was determined using cquery on one
63# of the example targets listed under EXAMPLE_WRAPPER_TARGETS at
64# //build/bazel/ci/target_lists.sh. It may not be exhaustive. A unit
65# test ensures that the wrapper's providers and the wrapped rule's do
66# match.
67def _java_library_sdk_transition_impl(ctx):
68    return [
69        ctx.attr.exports[0][JavaInfo],
70        ctx.attr.exports[0][InstrumentedFilesInfo],
71        ctx.attr.exports[0][ProguardSpecProvider],
72        ctx.attr.exports[0][OutputGroupInfo],
73        ctx.attr.exports[0][DefaultInfo],
74    ]
75
76java_library_sdk_transition = rule(
77    implementation = _java_library_sdk_transition_impl,
78    attrs = sdk_transition_attrs,
79    provides = [JavaInfo],
80)
81