xref: /aosp_15_r20/external/bazelbuild-rules_java/toolchains/jdk_build_file.bzl (revision abe8e1b943c923005d847f1e3cf6637de4ed1a1f)
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
15"""A templated BUILD file for Java repositories."""
16
17JDK_BUILD_TEMPLATE = """load("@rules_java//java:defs.bzl", "java_runtime")
18
19package(default_visibility = ["//visibility:public"])
20
21exports_files(["WORKSPACE", "BUILD.bazel"])
22
23filegroup(
24    name = "jre",
25    srcs = glob(
26        [
27            "jre/bin/**",
28            "jre/lib/**",
29        ],
30        allow_empty = True,
31        # In some configurations, Java browser plugin is considered harmful and
32        # common antivirus software blocks access to npjp2.dll interfering with Bazel,
33        # so do not include it in JRE on Windows.
34        exclude = ["jre/bin/plugin2/**"],
35    ),
36)
37
38filegroup(
39    name = "jdk-bin",
40    srcs = glob(
41        ["bin/**"],
42        # The JDK on Windows sometimes contains a directory called
43        # "%systemroot%", which is not a valid label.
44        exclude = ["**/*%*/**"],
45    ),
46)
47
48# This folder holds security policies.
49filegroup(
50    name = "jdk-conf",
51    srcs = glob(
52        ["conf/**"],
53        allow_empty = True,
54    ),
55)
56
57filegroup(
58    name = "jdk-include",
59    srcs = glob(
60        ["include/**"],
61        allow_empty = True,
62    ),
63)
64
65filegroup(
66    name = "jdk-lib",
67    srcs = glob(
68        ["lib/**", "release"],
69        allow_empty = True,
70        exclude = [
71            "lib/missioncontrol/**",
72            "lib/visualvm/**",
73        ],
74    ),
75)
76
77java_runtime(
78    name = "jdk",
79    srcs = [
80        ":jdk-bin",
81        ":jdk-conf",
82        ":jdk-include",
83        ":jdk-lib",
84        ":jre",
85    ],
86    # Provide the 'java` binary explicitly so that the correct path is used by
87    # Bazel even when the host platform differs from the execution platform.
88    # Exactly one of the two globs will be empty depending on the host platform.
89    # When --incompatible_disallow_empty_glob is enabled, each individual empty
90    # glob will fail without allow_empty = True, even if the overall result is
91    # non-empty.
92    java = glob(["bin/java.exe", "bin/java"], allow_empty = True)[0],
93    version = {RUNTIME_VERSION},
94)
95
96filegroup(
97    name = "jdk-jmods",
98    srcs = glob(
99        ["jmods/**"],
100        allow_empty = True,
101    ),
102)
103
104java_runtime(
105    name = "jdk-with-jmods",
106    srcs = [
107        ":jdk-bin",
108        ":jdk-conf",
109        ":jdk-include",
110        ":jdk-lib",
111        ":jdk-jmods",
112        ":jre",
113    ],
114    java = glob(["bin/java.exe", "bin/java"], allow_empty = True)[0],
115    version = {RUNTIME_VERSION},
116)
117"""
118