Googler | 239b89a | 2024-01-03 08:48:59 -0800 | [diff] [blame] | 1 | # 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 | |
Googler | 707d065 | 2024-03-11 04:15:38 -0700 | [diff] [blame] | 15 | """Utility for restricting Java APIs.""" |
Googler | 239b89a | 2024-01-03 08:48:59 -0800 | [diff] [blame] | 16 | |
| 17 | load("@rules_java//java:defs.bzl", "java_library") |
| 18 | |
| 19 | # Drop in replacement for java_library that builds the library at Java language level 11. |
| 20 | def java_11_library(**attrs): |
| 21 | javacopts = attrs.pop("javacopts", []) |
| 22 | java_library( |
| 23 | javacopts = javacopts + ["-source 11", "-target 11"], |
| 24 | **attrs |
| 25 | ) |
Googler | 707d065 | 2024-03-11 04:15:38 -0700 | [diff] [blame] | 26 | |
| 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 Tan | b532a46 | 2024-03-13 04:47:30 -0700 | [diff] [blame] | 35 | def _transition_java_language_8_archive_impl(ctx): |
Googler | 707d065 | 2024-03-11 04:15:38 -0700 | [diff] [blame] | 36 | 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 Tan | b532a46 | 2024-03-13 04:47:30 -0700 | [diff] [blame] | 51 | _transitioned_java_8_archive = rule( |
| 52 | implementation = _transition_java_language_8_archive_impl, |
Googler | 707d065 | 2024-03-11 04:15:38 -0700 | [diff] [blame] | 53 | 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. |
| 63 | def transition_java_language_8_archive(name, archive_zip, visibility): |
Zheng Wei Tan | b532a46 | 2024-03-13 04:47:30 -0700 | [diff] [blame] | 64 | _transitioned_java_8_archive( |
Googler | 707d065 | 2024-03-11 04:15:38 -0700 | [diff] [blame] | 65 | name = name, |
| 66 | archive_zip = archive_zip, |
| 67 | visibility = visibility, |
| 68 | ) |