xref: /aosp_15_r20/external/bazelbuild-rules_java/toolchains/BUILD (revision abe8e1b943c923005d847f1e3cf6637de4ed1a1f)
1load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2load("@rules_cc//cc:defs.bzl", "cc_library")
3load(
4    ":default_java_toolchain.bzl",
5    "DEFAULT_TOOLCHAIN_CONFIGURATION",
6    "PREBUILT_TOOLCHAIN_CONFIGURATION",
7    "bootclasspath",
8    "default_java_toolchain",
9    "java_runtime_files",
10)
11load(
12    ":java_toolchain_alias.bzl",
13    "java_host_runtime_alias",
14    "java_runtime_alias",
15    "java_runtime_version_alias",
16    "java_toolchain_alias",
17)
18
19package(default_visibility = ["//visibility:public"])
20
21licenses(["notice"])
22
23filegroup(
24    name = "srcs",
25    srcs = glob(["**"]),
26)
27
28filegroup(
29    name = "bzl_srcs",
30    srcs = glob(["*.bzl"]),
31)
32
33# A single binary distribution of a JDK (e.g., OpenJDK 17 for Windows arm64) provides three
34# different types of toolchains from the perspective of Bazel:
35
36# The compilation toolchain, which provides the Java runtime used to execute the Java compiler, as
37# well as various helper tools and settings.
38#
39# Toolchains of this type typically have constraints on the execution platform so that their Java
40# runtime can run the compiler, but not on the target platform as Java compilation outputs are
41# platform independent.
42#
43# Obtain the associated JavaToolchainInfo via:
44#   ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java
45# TODO: migrate away from using @bazel_tools//tools/jdk:toolchain_type ?
46# toolchain_type(name = "toolchain_type")
47
48# The Java runtime that executable Java compilation outputs (e.g., java_binary with
49# create_executable = True) will run on.
50#
51# Toolchains of this type typically have constraints on the target platform so that the runtime's
52# native 'java' binary can be run there, but not on the execution platform as building an executable
53# Java target only requires copying or symlinking the runtime, which can be done on any platform.
54#
55# Obtain the associated JavaRuntimeInfo via:
56#   ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"].java_runtime
57# TODO: migrate away from using @bazel_tools//tools/jdk:runtime_toolchain_type ?
58# toolchain_type(name = "runtime_toolchain_type")
59
60# The Java runtime to extract the bootclasspath from that is then used to compile Java sources.
61#
62# As the bootclasspath is platform independent, toolchains of this type may have no constraints.
63# Purely as an optimization to prevent unnecessary fetches of remote runtimes for other
64# architectures, toolchains of this type may have constraints on the execution platform that match
65# those on the corresponding compilation toolchain.
66#
67# Toolchains of this type are only consumed internally by the bootclasspath rule and should not be
68# accessed from Starlark.
69# TODO: migrate away from using @bazel_tools//tools/jdk:bootstrap_runtime_toolchain_type ?
70# toolchain_type(name = "bootstrap_runtime_toolchain_type")
71
72# Points to toolchain[":runtime_toolchain_type"] (was :legacy_current_java_runtime)
73java_runtime_alias(name = "current_java_runtime")
74
75# Host configuration of ":current_java_runtime"
76java_host_runtime_alias(name = "current_host_java_runtime")
77
78# Points to toolchain[":toolchain_type"] (was :legacy_current_java_toolchain)
79java_toolchain_alias(name = "current_java_toolchain")
80
81# These individual jni_* targets are exposed for legacy reasons.
82# Most users should depend on :jni.
83
84java_runtime_files(
85    name = "jni_header",
86    srcs = ["include/jni.h"],
87)
88
89java_runtime_files(
90    name = "jni_md_header-darwin",
91    srcs = ["include/darwin/jni_md.h"],
92)
93
94java_runtime_files(
95    name = "jni_md_header-linux",
96    srcs = ["include/linux/jni_md.h"],
97)
98
99java_runtime_files(
100    name = "jni_md_header-windows",
101    srcs = ["include/win32/jni_md.h"],
102)
103
104java_runtime_files(
105    name = "jni_md_header-freebsd",
106    srcs = ["include/freebsd/jni_md.h"],
107)
108
109java_runtime_files(
110    name = "jni_md_header-openbsd",
111    srcs = ["include/openbsd/jni_md.h"],
112)
113
114# The Java native interface. Depend on this package if you #include <jni.h>.
115#
116# See test_jni in third_party/bazel/src/test/shell/bazel/bazel_java_test.sh for
117# an example of using Bazel to build a Java program that calls a C function.
118#
119# TODO(ilist): use //src:condition:linux when released in Bazel
120cc_library(
121    name = "jni",
122    hdrs = [":jni_header"] + select({
123        "@bazel_tools//src/conditions:darwin": [":jni_md_header-darwin"],
124        "@bazel_tools//src/conditions:freebsd": [":jni_md_header-freebsd"],
125        "@bazel_tools//src/conditions:linux_aarch64": [":jni_md_header-linux"],
126        "@bazel_tools//src/conditions:linux_mips64": [":jni_md_header-linux"],
127        "@bazel_tools//src/conditions:linux_ppc64le": [":jni_md_header-linux"],
128        "@bazel_tools//src/conditions:linux_riscv64": [":jni_md_header-linux"],
129        "@bazel_tools//src/conditions:linux_s390x": [":jni_md_header-linux"],
130        "@bazel_tools//src/conditions:linux_x86_64": [":jni_md_header-linux"],
131        "@bazel_tools//src/conditions:openbsd": [":jni_md_header-openbsd"],
132        "@bazel_tools//src/conditions:windows": [":jni_md_header-windows"],
133        "//conditions:default": [],
134    }),
135    includes = ["include"] + select({
136        "@bazel_tools//src/conditions:darwin": ["include/darwin"],
137        "@bazel_tools//src/conditions:freebsd": ["include/freebsd"],
138        "@bazel_tools//src/conditions:linux_aarch64": ["include/linux"],
139        "@bazel_tools//src/conditions:linux_mips64": [":include/linux"],
140        "@bazel_tools//src/conditions:linux_ppc64le": ["include/linux"],
141        "@bazel_tools//src/conditions:linux_riscv64": [":include/linux"],
142        "@bazel_tools//src/conditions:linux_s390x": ["include/linux"],
143        "@bazel_tools//src/conditions:linux_x86_64": ["include/linux"],
144        "@bazel_tools//src/conditions:openbsd": ["include/openbsd"],
145        "@bazel_tools//src/conditions:windows": ["include/win32"],
146        "//conditions:default": [],
147    }),
148    tags = ["nobuilder"],
149)
150
151[
152    (
153        alias(
154            name = "ijar_prebuilt_binary_%s" % OS,
155            actual = "@remote_java_tools_%s//:ijar_prebuilt_binary" % OS,
156            visibility = ["//visibility:private"],
157        ),
158        alias(
159            name = "prebuilt_singlejar_%s" % OS,
160            actual = "@remote_java_tools_%s//:prebuilt_singlejar" % OS,
161            visibility = ["//visibility:private"],
162        ),
163        alias(
164            name = "turbine_direct_graal_%s" % OS,
165            actual = "@remote_java_tools_%s//:turbine_direct_graal" % OS,
166        ),
167    )
168    for OS in [
169        "linux",
170        "darwin_x86_64",
171        "darwin_arm64",
172        "windows",
173    ]
174]
175
176alias(
177    name = "ijar",
178    actual = ":ijar_prebuilt_binary_or_cc_binary",
179)
180
181alias(
182    name = "ijar_prebuilt_binary_or_cc_binary",
183    actual = select({
184        "@bazel_tools//src/conditions:darwin_arm64": ":ijar_prebuilt_binary_darwin_arm64",
185        "@bazel_tools//src/conditions:darwin_x86_64": ":ijar_prebuilt_binary_darwin_x86_64",
186        "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux",
187        "@bazel_tools//src/conditions:windows": ":ijar_prebuilt_binary_windows",
188        "//conditions:default": "@remote_java_tools//:ijar_cc_binary",
189    }),
190)
191
192alias(
193    name = "ijar_prebuilt_binary",
194    actual = select({
195        "@bazel_tools//src/conditions:darwin_arm64": ":ijar_prebuilt_binary_darwin_arm64",
196        "@bazel_tools//src/conditions:darwin_x86_64": ":ijar_prebuilt_binary_darwin_x86_64",
197        "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux",
198        "@bazel_tools//src/conditions:windows": ":ijar_prebuilt_binary_windows",
199    }),
200)
201
202alias(
203    name = "singlejar",
204    actual = ":singlejar_prebuilt_or_cc_binary",
205)
206
207alias(
208    name = "singlejar_prebuilt_or_cc_binary",
209    actual = select({
210        "@bazel_tools//src/conditions:darwin_arm64": ":prebuilt_singlejar_darwin_arm64",
211        "@bazel_tools//src/conditions:darwin_x86_64": ":prebuilt_singlejar_darwin_x86_64",
212        "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux",
213        "@bazel_tools//src/conditions:windows": ":prebuilt_singlejar_windows",
214        "//conditions:default": "@remote_java_tools//:singlejar_cc_bin",
215    }),
216)
217
218alias(
219    name = "prebuilt_singlejar",
220    actual = select({
221        "@bazel_tools//src/conditions:darwin_arm64": ":prebuilt_singlejar_darwin_arm64",
222        "@bazel_tools//src/conditions:darwin_x86_64": ":prebuilt_singlejar_darwin_x86_64",
223        "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux",
224        "@bazel_tools//src/conditions:windows": ":prebuilt_singlejar_windows",
225    }),
226)
227
228alias(
229    name = "turbine_direct",
230    actual = ":turbine_direct_graal_or_java",
231)
232
233alias(
234    name = "turbine_direct_graal_or_java",
235    actual = select({
236        "@bazel_tools//src/conditions:darwin_arm64": ":turbine_direct_graal_darwin_arm64",
237        "@bazel_tools//src/conditions:darwin_x86_64": ":turbine_direct_graal_darwin_x86_64",
238        "@bazel_tools//src/conditions:linux_x86_64": ":turbine_direct_graal_linux",
239        "@bazel_tools//src/conditions:windows": ":turbine_direct_graal_windows",
240        "//conditions:default": "@remote_java_tools//:TurbineDirect",
241    }),
242)
243
244alias(
245    name = "turbine_direct_graal",
246    actual = select({
247        "@bazel_tools//src/conditions:darwin_arm64": ":turbine_direct_graal_darwin_arm64",
248        "@bazel_tools//src/conditions:darwin_x86_64": ":turbine_direct_graal_darwin_x86_64",
249        "@bazel_tools//src/conditions:linux_x86_64": ":turbine_direct_graal_linux",
250        "@bazel_tools//src/conditions:windows": ":turbine_direct_graal_windows",
251    }),
252)
253
254bootclasspath(
255    name = "platformclasspath",
256    src = "DumpPlatformClassPath.java",
257    java_runtime_alias = ":current_java_runtime",
258)
259
260default_java_toolchain(
261    name = "toolchain",
262    configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,
263    toolchain_definition = False,
264)
265
266alias(
267    name = "remote_toolchain",
268    actual = ":toolchain",
269)
270
271RELEASES = (8, 9, 10, 11, 17, 21)
272
273[
274    default_java_toolchain(
275        name = ("toolchain_java%d" if release <= 11 else "toolchain_jdk_%d") % release,
276        configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,
277        source_version = "%s" % release,
278        target_version = "%s" % release,
279    )
280    for release in RELEASES
281]
282
283default_java_toolchain(
284    name = "prebuilt_toolchain",
285    configuration = PREBUILT_TOOLCHAIN_CONFIGURATION,
286    toolchain_definition = False,
287)
288
289# A JDK 11 for use as a --host_javabase.
290java_runtime_version_alias(
291    name = "remote_jdk11",
292    runtime_version = "remotejdk_11",
293    visibility = ["//visibility:public"],
294)
295
296java_runtime_version_alias(
297    name = "remotejdk_15",
298    runtime_version = "remotejdk_15",
299    visibility = ["//visibility:public"],
300)
301
302java_runtime_version_alias(
303    name = "remotejdk_16",
304    runtime_version = "remotejdk_16",
305    visibility = ["//visibility:public"],
306)
307
308java_runtime_version_alias(
309    name = "remotejdk_17",
310    runtime_version = "remotejdk_17",
311    visibility = ["//visibility:public"],
312)
313
314java_runtime_version_alias(
315    name = "remotejdk_21",
316    runtime_version = "remotejdk_21",
317    visibility = ["//visibility:public"],
318)
319
320java_runtime_version_alias(
321    name = "jdk_8",
322    runtime_version = "8",
323    visibility = ["//visibility:public"],
324)
325
326bzl_library(
327    name = "toolchain_utils",
328    srcs = ["toolchain_utils.bzl"],
329    visibility = ["//visibility:public"],
330    deps = ["//java/common"],
331)
332