blob: 01c82cb49c8d9fdca9800309c8909515d3ec82bf [file] [log] [blame]
Googler239b89a2024-01-03 08:48:59 -08001# Copyright 2023 The Bazel Authors. 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
Googler707d0652024-03-11 04:15:38 -070015"""Utility for restricting Java APIs."""
Googler239b89a2024-01-03 08:48:59 -080016
17load("@rules_java//java:defs.bzl", "java_library")
18
19# Drop in replacement for java_library that builds the library at Java language level 11.
20def java_11_library(**attrs):
21 javacopts = attrs.pop("javacopts", [])
22 java_library(
23 javacopts = javacopts + ["-source 11", "-target 11"],
24 **attrs
25 )
Googler707d0652024-03-11 04:15:38 -070026
27_java_language_version_8_transition = transition(
28 implementation = lambda settings, attr: {
29 "//command_line_option:java_language_version": "8",
30 },
31 inputs = [],
32 outputs = ["//command_line_option:java_language_version"],
33)
34
Zheng Wei Tanb532a462024-03-13 04:47:30 -070035def _transition_java_language_8_archive_impl(ctx):
Googler707d0652024-03-11 04:15:38 -070036 archive_zip = ctx.files.archive_zip[0]
37
38 outfile = ctx.actions.declare_file(ctx.label.name)
39
40 ctx.actions.run_shell(
41 inputs = [archive_zip],
42 outputs = [outfile],
43 command = "cp %s %s" % (archive_zip.path, outfile.path),
44 )
45 return [
46 DefaultInfo(
47 files = depset([outfile]),
48 ),
49 ]
50
Zheng Wei Tanb532a462024-03-13 04:47:30 -070051_transitioned_java_8_archive = rule(
52 implementation = _transition_java_language_8_archive_impl,
Googler707d0652024-03-11 04:15:38 -070053 attrs = {
54 "archive_zip": attr.label(
55 allow_files = True,
56 cfg = _java_language_version_8_transition,
57 mandatory = True,
58 ),
59 },
60)
61
62# Used to transition the zip file generated by release_archive to compile at Java language 8.
63def transition_java_language_8_archive(name, archive_zip, visibility):
Zheng Wei Tanb532a462024-03-13 04:47:30 -070064 _transitioned_java_8_archive(
Googler707d0652024-03-11 04:15:38 -070065 name = name,
66 archive_zip = archive_zip,
67 visibility = visibility,
68 )