blob: 99c4456c5415e2d69d30c8e628d35e809c54921d [file] [log] [blame]
Googler25a11312024-05-13 22:02:05 -07001load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
Fabian Meumertzheim449303e2024-12-06 00:33:25 -08002load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_setting")
Ivo List79c6e692024-10-11 02:22:16 -07003load("@rules_cc//cc:cc_library.bzl", "cc_library")
Googlerdab88d62023-05-22 10:09:34 -07004load(
Fabian Meumertzheim449303e2024-12-06 00:33:25 -08005 ":bootclasspath.bzl",
6 "bootclasspath",
7 "language_version_bootstrap_runtime",
8)
9load(
Googler72f72322023-05-25 07:48:30 -070010 ":default_java_toolchain.bzl",
Googlerdab88d62023-05-22 10:09:34 -070011 "DEFAULT_TOOLCHAIN_CONFIGURATION",
12 "PREBUILT_TOOLCHAIN_CONFIGURATION",
Googlerdab88d62023-05-22 10:09:34 -070013 "default_java_toolchain",
14 "java_runtime_files",
15)
16load(
Googler72f72322023-05-25 07:48:30 -070017 ":java_toolchain_alias.bzl",
Googlerdab88d62023-05-22 10:09:34 -070018 "java_host_runtime_alias",
19 "java_runtime_alias",
20 "java_runtime_version_alias",
21 "java_toolchain_alias",
22)
Fabian Meumertzheimc22454f2024-11-27 07:28:44 -080023load(":utf8_environment.bzl", "utf8_environment")
Googlerdab88d62023-05-22 10:09:34 -070024
25package(default_visibility = ["//visibility:public"])
26
27licenses(["notice"])
28
29filegroup(
30 name = "srcs",
31 srcs = glob(["**"]),
Googlerdab88d62023-05-22 10:09:34 -070032)
33
Googlerecf792b2023-06-05 06:37:03 -070034filegroup(
35 name = "bzl_srcs",
36 srcs = glob(["*.bzl"]),
37)
38
Fabian Meumertzheim449303e2024-12-06 00:33:25 -080039# If enabled, the bootclasspath for Java compilation will be extracted from a Java runtime matching
40# the version specified with `--java_language_version` rather than the runtime specified with
41# `--java_runtime_version`.
42bool_flag(
43 name = "incompatible_language_version_bootclasspath",
44 build_setting_default = False,
45 visibility = ["//visibility:private"],
46)
47
Fabian Meumertzheimea0bd912023-08-07 05:35:43 -070048# A single binary distribution of a JDK (e.g., OpenJDK 17 for Windows arm64) provides three
49# different types of toolchains from the perspective of Bazel:
50
51# The compilation toolchain, which provides the Java runtime used to execute the Java compiler, as
52# well as various helper tools and settings.
53#
54# Toolchains of this type typically have constraints on the execution platform so that their Java
55# runtime can run the compiler, but not on the target platform as Java compilation outputs are
56# platform independent.
57#
58# Obtain the associated JavaToolchainInfo via:
59# ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java
Googlerdab88d62023-05-22 10:09:34 -070060# TODO: migrate away from using @bazel_tools//tools/jdk:toolchain_type ?
61# toolchain_type(name = "toolchain_type")
62
Fabian Meumertzheimea0bd912023-08-07 05:35:43 -070063# The Java runtime that executable Java compilation outputs (e.g., java_binary with
64# create_executable = True) will run on.
65#
66# Toolchains of this type typically have constraints on the target platform so that the runtime's
67# native 'java' binary can be run there, but not on the execution platform as building an executable
68# Java target only requires copying or symlinking the runtime, which can be done on any platform.
69#
70# Obtain the associated JavaRuntimeInfo via:
71# ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"].java_runtime
Googlerdab88d62023-05-22 10:09:34 -070072# TODO: migrate away from using @bazel_tools//tools/jdk:runtime_toolchain_type ?
73# toolchain_type(name = "runtime_toolchain_type")
74
Fabian Meumertzheimea0bd912023-08-07 05:35:43 -070075# The Java runtime to extract the bootclasspath from that is then used to compile Java sources.
76#
77# As the bootclasspath is platform independent, toolchains of this type may have no constraints.
78# Purely as an optimization to prevent unnecessary fetches of remote runtimes for other
79# architectures, toolchains of this type may have constraints on the execution platform that match
80# those on the corresponding compilation toolchain.
81#
82# Toolchains of this type are only consumed internally by the bootclasspath rule and should not be
83# accessed from Starlark.
hvadehra4206c532024-12-03 02:29:13 -080084# TODO: migrate away from using @bazel_tools//tools/jdk:bootstrap_runtime_toolchain_type ?
85# toolchain_type(name = "bootstrap_runtime_toolchain_type")
Fabian Meumertzheimea0bd912023-08-07 05:35:43 -070086
Googlerdab88d62023-05-22 10:09:34 -070087# Points to toolchain[":runtime_toolchain_type"] (was :legacy_current_java_runtime)
88java_runtime_alias(name = "current_java_runtime")
89
90# Host configuration of ":current_java_runtime"
91java_host_runtime_alias(name = "current_host_java_runtime")
92
93# Points to toolchain[":toolchain_type"] (was :legacy_current_java_toolchain)
94java_toolchain_alias(name = "current_java_toolchain")
95
96# These individual jni_* targets are exposed for legacy reasons.
97# Most users should depend on :jni.
98
99java_runtime_files(
100 name = "jni_header",
101 srcs = ["include/jni.h"],
102)
103
104java_runtime_files(
105 name = "jni_md_header-darwin",
106 srcs = ["include/darwin/jni_md.h"],
107)
108
109java_runtime_files(
110 name = "jni_md_header-linux",
111 srcs = ["include/linux/jni_md.h"],
112)
113
114java_runtime_files(
115 name = "jni_md_header-windows",
116 srcs = ["include/win32/jni_md.h"],
117)
118
119java_runtime_files(
120 name = "jni_md_header-freebsd",
121 srcs = ["include/freebsd/jni_md.h"],
122)
123
124java_runtime_files(
125 name = "jni_md_header-openbsd",
126 srcs = ["include/openbsd/jni_md.h"],
127)
128
129# The Java native interface. Depend on this package if you #include <jni.h>.
130#
131# See test_jni in third_party/bazel/src/test/shell/bazel/bazel_java_test.sh for
132# an example of using Bazel to build a Java program that calls a C function.
133#
134# TODO(ilist): use //src:condition:linux when released in Bazel
135cc_library(
136 name = "jni",
137 hdrs = [":jni_header"] + select({
138 "@bazel_tools//src/conditions:darwin": [":jni_md_header-darwin"],
139 "@bazel_tools//src/conditions:freebsd": [":jni_md_header-freebsd"],
140 "@bazel_tools//src/conditions:linux_aarch64": [":jni_md_header-linux"],
141 "@bazel_tools//src/conditions:linux_mips64": [":jni_md_header-linux"],
142 "@bazel_tools//src/conditions:linux_ppc64le": [":jni_md_header-linux"],
143 "@bazel_tools//src/conditions:linux_riscv64": [":jni_md_header-linux"],
144 "@bazel_tools//src/conditions:linux_s390x": [":jni_md_header-linux"],
145 "@bazel_tools//src/conditions:linux_x86_64": [":jni_md_header-linux"],
146 "@bazel_tools//src/conditions:openbsd": [":jni_md_header-openbsd"],
147 "@bazel_tools//src/conditions:windows": [":jni_md_header-windows"],
148 "//conditions:default": [],
149 }),
150 includes = ["include"] + select({
151 "@bazel_tools//src/conditions:darwin": ["include/darwin"],
152 "@bazel_tools//src/conditions:freebsd": ["include/freebsd"],
153 "@bazel_tools//src/conditions:linux_aarch64": ["include/linux"],
Lazy Programer38cdd622024-07-17 00:33:48 -0700154 "@bazel_tools//src/conditions:linux_mips64": ["include/linux"],
Googlerdab88d62023-05-22 10:09:34 -0700155 "@bazel_tools//src/conditions:linux_ppc64le": ["include/linux"],
Lazy Programer38cdd622024-07-17 00:33:48 -0700156 "@bazel_tools//src/conditions:linux_riscv64": ["include/linux"],
Googlerdab88d62023-05-22 10:09:34 -0700157 "@bazel_tools//src/conditions:linux_s390x": ["include/linux"],
158 "@bazel_tools//src/conditions:linux_x86_64": ["include/linux"],
159 "@bazel_tools//src/conditions:openbsd": ["include/openbsd"],
160 "@bazel_tools//src/conditions:windows": ["include/win32"],
161 "//conditions:default": [],
162 }),
Googler72f72322023-05-25 07:48:30 -0700163 tags = ["nobuilder"],
Googlerdab88d62023-05-22 10:09:34 -0700164)
165
166[
167 (
168 alias(
169 name = "ijar_prebuilt_binary_%s" % OS,
170 actual = "@remote_java_tools_%s//:ijar_prebuilt_binary" % OS,
171 visibility = ["//visibility:private"],
172 ),
173 alias(
174 name = "prebuilt_singlejar_%s" % OS,
175 actual = "@remote_java_tools_%s//:prebuilt_singlejar" % OS,
176 visibility = ["//visibility:private"],
177 ),
Fabian Meumertzheimd5b3ecf2023-11-16 08:04:31 -0800178 alias(
Fabian Meumertzheim4f50c3b2024-09-04 10:13:51 -0700179 name = "prebuilt_one_version_%s" % OS,
180 actual = "@remote_java_tools_%s//:prebuilt_one_version" % OS,
181 visibility = ["//visibility:private"],
182 ),
183 alias(
Fabian Meumertzheimd5b3ecf2023-11-16 08:04:31 -0800184 name = "turbine_direct_graal_%s" % OS,
185 actual = "@remote_java_tools_%s//:turbine_direct_graal" % OS,
186 ),
Googlerdab88d62023-05-22 10:09:34 -0700187 )
188 for OS in [
189 "linux",
190 "darwin_x86_64",
191 "darwin_arm64",
192 "windows",
193 ]
194]
195
Googlerdab88d62023-05-22 10:09:34 -0700196alias(
197 name = "ijar",
198 actual = ":ijar_prebuilt_binary_or_cc_binary",
199)
200
201alias(
202 name = "ijar_prebuilt_binary_or_cc_binary",
203 actual = select({
204 "@bazel_tools//src/conditions:darwin_arm64": ":ijar_prebuilt_binary_darwin_arm64",
205 "@bazel_tools//src/conditions:darwin_x86_64": ":ijar_prebuilt_binary_darwin_x86_64",
206 "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux",
207 "@bazel_tools//src/conditions:windows": ":ijar_prebuilt_binary_windows",
208 "//conditions:default": "@remote_java_tools//:ijar_cc_binary",
209 }),
210)
211
212alias(
213 name = "ijar_prebuilt_binary",
214 actual = select({
215 "@bazel_tools//src/conditions:darwin_arm64": ":ijar_prebuilt_binary_darwin_arm64",
216 "@bazel_tools//src/conditions:darwin_x86_64": ":ijar_prebuilt_binary_darwin_x86_64",
217 "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux",
218 "@bazel_tools//src/conditions:windows": ":ijar_prebuilt_binary_windows",
219 }),
220)
221
Googlerdab88d62023-05-22 10:09:34 -0700222alias(
223 name = "singlejar",
224 actual = ":singlejar_prebuilt_or_cc_binary",
225)
226
227alias(
228 name = "singlejar_prebuilt_or_cc_binary",
229 actual = select({
230 "@bazel_tools//src/conditions:darwin_arm64": ":prebuilt_singlejar_darwin_arm64",
231 "@bazel_tools//src/conditions:darwin_x86_64": ":prebuilt_singlejar_darwin_x86_64",
232 "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux",
233 "@bazel_tools//src/conditions:windows": ":prebuilt_singlejar_windows",
234 "//conditions:default": "@remote_java_tools//:singlejar_cc_bin",
235 }),
236)
237
238alias(
239 name = "prebuilt_singlejar",
240 actual = select({
241 "@bazel_tools//src/conditions:darwin_arm64": ":prebuilt_singlejar_darwin_arm64",
242 "@bazel_tools//src/conditions:darwin_x86_64": ":prebuilt_singlejar_darwin_x86_64",
243 "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux",
244 "@bazel_tools//src/conditions:windows": ":prebuilt_singlejar_windows",
245 }),
246)
247
Fabian Meumertzheimd5b3ecf2023-11-16 08:04:31 -0800248alias(
Fabian Meumertzheim4f50c3b2024-09-04 10:13:51 -0700249 name = "one_version",
250 actual = ":one_version_prebuilt_or_cc_binary",
251)
252
253alias(
254 name = "one_version_prebuilt_or_cc_binary",
255 actual = select({
256 "@bazel_tools//src/conditions:darwin_arm64": ":prebuilt_one_version_darwin_arm64",
257 "@bazel_tools//src/conditions:darwin_x86_64": ":prebuilt_one_version_darwin_x86_64",
258 "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_one_version_linux",
259 "@bazel_tools//src/conditions:windows": ":prebuilt_one_version_windows",
260 "//conditions:default": "@remote_java_tools//:one_version_cc_bin",
261 }),
262)
263
264alias(
265 name = "prebuilt_one_version",
266 actual = select({
267 "@bazel_tools//src/conditions:darwin_arm64": ":prebuilt_one_version_darwin_arm64",
268 "@bazel_tools//src/conditions:darwin_x86_64": ":prebuilt_one_version_darwin_x86_64",
269 "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_one_version_linux",
270 "@bazel_tools//src/conditions:windows": ":prebuilt_one_version_windows",
271 }),
272)
273
274alias(
Fabian Meumertzheimd5b3ecf2023-11-16 08:04:31 -0800275 name = "turbine_direct",
276 actual = ":turbine_direct_graal_or_java",
277)
278
279alias(
280 name = "turbine_direct_graal_or_java",
281 actual = select({
282 "@bazel_tools//src/conditions:darwin_arm64": ":turbine_direct_graal_darwin_arm64",
283 "@bazel_tools//src/conditions:darwin_x86_64": ":turbine_direct_graal_darwin_x86_64",
284 "@bazel_tools//src/conditions:linux_x86_64": ":turbine_direct_graal_linux",
285 "@bazel_tools//src/conditions:windows": ":turbine_direct_graal_windows",
286 "//conditions:default": "@remote_java_tools//:TurbineDirect",
287 }),
288)
289
290alias(
291 name = "turbine_direct_graal",
292 actual = select({
293 "@bazel_tools//src/conditions:darwin_arm64": ":turbine_direct_graal_darwin_arm64",
294 "@bazel_tools//src/conditions:darwin_x86_64": ":turbine_direct_graal_darwin_x86_64",
295 "@bazel_tools//src/conditions:linux_x86_64": ":turbine_direct_graal_linux",
296 "@bazel_tools//src/conditions:windows": ":turbine_direct_graal_windows",
297 }),
298)
299
Fabian Meumertzheim449303e2024-12-06 00:33:25 -0800300string_setting(
301 name = "java_language_version",
302 build_setting_default = "",
303 visibility = ["//visibility:private"],
304)
305
306string_setting(
307 name = "java_runtime_version",
308 build_setting_default = "",
309 visibility = ["//visibility:private"],
310)
311
312language_version_bootstrap_runtime(
313 name = "language_version_bootstrap_runtime",
314 java_language_version = ":java_language_version",
315 java_runtime_version = ":java_runtime_version",
316 visibility = ["//visibility:private"],
317)
318
Fabian Meumertzheimc22454f2024-11-27 07:28:44 -0800319utf8_environment(
320 name = "utf8_environment",
321 visibility = ["//visibility:private"],
322)
323
Fabian Meumertzheim449303e2024-12-06 00:33:25 -0800324config_setting(
325 name = "incompatible_language_version_bootclasspath_enabled",
326 flag_values = {
327 ":incompatible_language_version_bootclasspath": "True",
328 },
329 visibility = ["//visibility:private"],
330)
331
Googlerdab88d62023-05-22 10:09:34 -0700332bootclasspath(
333 name = "platformclasspath",
334 src = "DumpPlatformClassPath.java",
Fabian Meumertzheimea0bd912023-08-07 05:35:43 -0700335 java_runtime_alias = ":current_java_runtime",
Fabian Meumertzheim449303e2024-12-06 00:33:25 -0800336 language_version_bootstrap_runtime = select({
337 ":incompatible_language_version_bootclasspath_enabled": ":language_version_bootstrap_runtime",
338 "//conditions:default": None,
339 }),
Googlerdab88d62023-05-22 10:09:34 -0700340)
341
342default_java_toolchain(
343 name = "toolchain",
344 configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,
345 toolchain_definition = False,
346)
347
348alias(
349 name = "remote_toolchain",
350 actual = ":toolchain",
351)
352
hvdfda9e4a2023-10-19 12:35:30 +0200353RELEASES = (8, 9, 10, 11, 17, 21)
Googlerdab88d62023-05-22 10:09:34 -0700354
355[
356 default_java_toolchain(
hvdfda9e4a2023-10-19 12:35:30 +0200357 name = ("toolchain_java%d" if release <= 11 else "toolchain_jdk_%d") % release,
Googlerdab88d62023-05-22 10:09:34 -0700358 configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,
359 source_version = "%s" % release,
360 target_version = "%s" % release,
361 )
362 for release in RELEASES
363]
364
Googlerdab88d62023-05-22 10:09:34 -0700365default_java_toolchain(
366 name = "prebuilt_toolchain",
367 configuration = PREBUILT_TOOLCHAIN_CONFIGURATION,
368 toolchain_definition = False,
369)
370
371# A JDK 11 for use as a --host_javabase.
372java_runtime_version_alias(
373 name = "remote_jdk11",
374 runtime_version = "remotejdk_11",
375 visibility = ["//visibility:public"],
376)
377
378java_runtime_version_alias(
Googlerdab88d62023-05-22 10:09:34 -0700379 name = "remotejdk_17",
380 runtime_version = "remotejdk_17",
381 visibility = ["//visibility:public"],
382)
Yun Pengf7107b42023-05-16 15:10:00 +0200383
384java_runtime_version_alias(
Benjamin Peterson35ba97c2023-09-19 11:05:28 -0700385 name = "remotejdk_21",
386 runtime_version = "remotejdk_21",
Yun Pengf7107b42023-05-16 15:10:00 +0200387 visibility = ["//visibility:public"],
388)
389
390java_runtime_version_alias(
391 name = "jdk_8",
392 runtime_version = "8",
393 visibility = ["//visibility:public"],
394)
Googler25a11312024-05-13 22:02:05 -0700395
396bzl_library(
397 name = "toolchain_utils",
398 srcs = ["toolchain_utils.bzl"],
399 visibility = ["//visibility:public"],
400 deps = ["//java/common"],
401)