blob: 14479d46b61486f46e308c099554268e1c04e5be [file] [log] [blame]
cushon5c16ab232017-12-19 16:18:49 -08001# Copyright 2017 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"""Bazel rules for creating Java toolchains."""
16
lberki7eb9ea12018-09-07 04:01:48 -070017JDK8_JVM_OPTS = [
iliste0f2e252021-01-11 04:54:19 -080018 "-Xbootclasspath/p:$(location @remote_java_tools//:javac_jar)",
iirina22d375b2019-01-21 04:44:29 -080019]
20
Ivo List62022712020-11-27 07:24:36 -080021# JVM options, without patching java.compiler and jdk.compiler modules.
22BASE_JDK9_JVM_OPTS = [
iirina22d375b2019-01-21 04:44:29 -080023 # Allow JavaBuilder to access internal javac APIs.
24 "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
iirina22d375b2019-01-21 04:44:29 -080025 "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
cushon74f35d42021-04-05 11:20:04 -070026 "--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
cushon74f35d42021-04-05 11:20:04 -070027 "--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
David Ostrovskye2ed2fd2021-04-15 01:17:40 -070028 "--add-exports=jdk.compiler/com.sun.tools.javac.resources=ALL-UNNAMED",
iirina22d375b2019-01-21 04:44:29 -080029 "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
30 "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
cushon74f35d42021-04-05 11:20:04 -070031 "--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
32 "--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
iirina22d375b2019-01-21 04:44:29 -080033 "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
cushon43c7b972022-01-21 02:23:03 -080034 "--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
iirina22d375b2019-01-21 04:44:29 -080035
iirina22d375b2019-01-21 04:44:29 -080036 # quiet warnings from com.google.protobuf.UnsafeUtil,
37 # see: https://github.com/google/protobuf/issues/3781
38 # and: https://github.com/bazelbuild/bazel/issues/5599
39 "--add-opens=java.base/java.nio=ALL-UNNAMED",
40 "--add-opens=java.base/java.lang=ALL-UNNAMED",
41]
42
Ivo List62022712020-11-27 07:24:36 -080043JDK9_JVM_OPTS = BASE_JDK9_JVM_OPTS + [
44 # override the javac in the JDK.
45 "--patch-module=java.compiler=$(location @remote_java_tools//:java_compiler_jar)",
46 "--patch-module=jdk.compiler=$(location @remote_java_tools//:jdk_compiler_jar)",
47]
48
cushon5c16ab232017-12-19 16:18:49 -080049DEFAULT_JAVACOPTS = [
cushonb7c02482018-04-11 09:47:51 -070050 "-XDskipDuplicateBridges=true",
ilist8cec2892020-10-23 01:59:15 -070051 "-XDcompilePolicy=simple",
cushonb7c02482018-04-11 09:47:51 -070052 "-g",
53 "-parameters",
pcloudyfd44fd22022-01-19 06:59:37 -080054 # https://github.com/bazelbuild/java_tools/issues/51#issuecomment-927940699
55 "-XepOpt:ReturnValueIgnored:ObjectMethods=false",
cushon5c16ab232017-12-19 16:18:49 -080056]
57
Ivo List62022712020-11-27 07:24:36 -080058# java_toolchain parameters without specifying javac, java.compiler,
59# jdk.compiler module, and jvm_opts
60_BASE_TOOLCHAIN_CONFIGURATION = dict(
61 forcibly_disable_header_compilation = False,
62 genclass = ["@remote_java_tools//:GenClass"],
63 header_compiler = ["@remote_java_tools//:TurbineDirect"],
64 header_compiler_direct = ["@remote_java_tools//:TurbineDirect"],
65 ijar = ["@bazel_tools//tools/jdk:ijar"],
66 javabuilder = ["@remote_java_tools//:JavaBuilder"],
67 javac_supports_workers = True,
68 jacocorunner = "@remote_java_tools//:jacoco_coverage_runner_filegroup",
69 jvm_opts = BASE_JDK9_JVM_OPTS,
70 misc = DEFAULT_JAVACOPTS,
71 singlejar = ["@bazel_tools//tools/jdk:singlejar"],
72 # Code to enumerate target JVM boot classpath uses host JVM. Because
73 # java_runtime-s are involved, its implementation is in @bazel_tools.
74 bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"],
75 source_version = "8",
76 target_version = "8",
Googler6c73a2e2021-10-18 15:11:07 -070077 reduced_classpath_incompatible_processors = [
78 "dagger.hilt.processor.internal.root.RootProcessor", # see b/21307381
79 ],
Ivo List62022712020-11-27 07:24:36 -080080)
iirina22d375b2019-01-21 04:44:29 -080081
Ivo List62022712020-11-27 07:24:36 -080082JVM8_TOOLCHAIN_CONFIGURATION = dict(
83 tools = ["@remote_java_tools//:javac_jar"],
84 jvm_opts = ["-Xbootclasspath/p:$(location @remote_java_tools//:javac_jar)"],
iliste0f2e252021-01-11 04:54:19 -080085 java_runtime = "@bazel_tools//tools/jdk:jdk_8",
Ivo List62022712020-11-27 07:24:36 -080086)
87
88DEFAULT_TOOLCHAIN_CONFIGURATION = dict(
89 jvm_opts = [
Benjamin Petersond1f6b012020-12-03 23:40:31 -080090 # Compact strings make JavaBuilder slightly slower.
Ivo List62022712020-11-27 07:24:36 -080091 "-XX:-CompactStrings",
92 ] + JDK9_JVM_OPTS,
Benjamin Petersond1f6b012020-12-03 23:40:31 -080093 turbine_jvm_opts = [
94 # Turbine is not a worker and parallel GC is faster for short-lived programs.
95 "-XX:+UseParallelOldGC",
96 ],
Ivo List62022712020-11-27 07:24:36 -080097 tools = [
98 "@remote_java_tools//:java_compiler_jar",
99 "@remote_java_tools//:jdk_compiler_jar",
100 ],
Ivo Listec29e282020-12-16 12:30:12 -0800101 java_runtime = "@bazel_tools//tools/jdk:remote_jdk11",
Ivo List62022712020-11-27 07:24:36 -0800102)
103
104# The 'vanilla' toolchain is an unsupported alternative to the default.
105#
106# It does not provide any of the following features:
107# * Error Prone
108# * Strict Java Deps
Ivo List62022712020-11-27 07:24:36 -0800109# * Reduced Classpath Optimization
110#
111# It uses the version of internal javac from the `--host_javabase` JDK instead
112# of providing a javac. Internal javac may not be source- or bug-compatible with
113# the javac that is provided with other toolchains.
114#
115# However it does allow using a wider range of `--host_javabase`s, including
116# versions newer than the current JDK.
117VANILLA_TOOLCHAIN_CONFIGURATION = dict(
Ivo List62022712020-11-27 07:24:36 -0800118 javabuilder = ["@remote_java_tools//:VanillaJavaBuilder"],
119 jvm_opts = [],
120)
121
122# The new toolchain is using all the pre-built tools, including
123# singlejar and ijar, even on remote execution. This toolchain
124# should be used only when host and execution platform are the
125# same, otherwise the binaries will not work on the execution
126# platform.
127PREBUILT_TOOLCHAIN_CONFIGURATION = dict(
128 jvm_opts = [
Benjamin Petersond1f6b012020-12-03 23:40:31 -0800129 # Compact strings make JavaBuilder slightly slower.
Ivo List62022712020-11-27 07:24:36 -0800130 "-XX:-CompactStrings",
131 ] + JDK9_JVM_OPTS,
Benjamin Petersond1f6b012020-12-03 23:40:31 -0800132 turbine_jvm_opts = [
133 # Turbine is not a worker and parallel GC is faster for short-lived programs.
134 "-XX:+UseParallelOldGC",
135 ],
Ivo List62022712020-11-27 07:24:36 -0800136 tools = [
137 "@remote_java_tools//:java_compiler_jar",
138 "@remote_java_tools//:jdk_compiler_jar",
139 ],
Ivo List9fba7c5c2020-12-17 11:59:38 -0800140 ijar = ["@bazel_tools//tools/jdk:ijar_prebuilt_binary"],
141 singlejar = ["@bazel_tools//tools/jdk:prebuilt_singlejar"],
Ivo Listec29e282020-12-16 12:30:12 -0800142 java_runtime = "@bazel_tools//tools/jdk:remote_jdk11",
Ivo Listbf32cb82020-12-10 08:28:17 -0800143)
144
145# The new toolchain is using all the tools from sources.
146NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict(
147 jvm_opts = [
148 # Compact strings make JavaBuilder slightly slower.
149 "-XX:-CompactStrings",
150 ] + JDK9_JVM_OPTS,
151 turbine_jvm_opts = [
152 # Turbine is not a worker and parallel GC is faster for short-lived programs.
153 "-XX:+UseParallelOldGC",
154 ],
155 tools = [
156 "@remote_java_tools//:java_compiler_jar",
157 "@remote_java_tools//:jdk_compiler_jar",
158 ],
Ivo List9fba7c5c2020-12-17 11:59:38 -0800159 ijar = ["@remote_java_tools//:ijar_cc_binary"],
160 singlejar = ["@remote_java_tools//:singlejar_cc_bin"],
Ivo Listec29e282020-12-16 12:30:12 -0800161 java_runtime = "@bazel_tools//tools/jdk:remote_jdk11",
Ivo List62022712020-11-27 07:24:36 -0800162)
163
Ivo Listf5b6abc2020-12-16 23:50:56 -0800164def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, **kwargs):
iirina22d375b2019-01-21 04:44:29 -0800165 """Defines a remote java_toolchain with appropriate defaults for Bazel."""
166
Ivo List62022712020-11-27 07:24:36 -0800167 toolchain_args = dict(_BASE_TOOLCHAIN_CONFIGURATION)
168 toolchain_args.update(configuration)
iirina22d375b2019-01-21 04:44:29 -0800169 toolchain_args.update(kwargs)
Ivo Listba8d4042020-12-17 07:23:30 -0800170 native.java_toolchain(
iirina22d375b2019-01-21 04:44:29 -0800171 name = name,
172 **toolchain_args
173 )
Ivo Listf5b6abc2020-12-16 23:50:56 -0800174 if toolchain_definition:
175 native.config_setting(
176 name = name + "_version_setting",
177 values = {"java_language_version": toolchain_args["source_version"]},
178 visibility = ["//visibility:private"],
179 )
180 native.toolchain(
181 name = name + "_definition",
182 toolchain_type = "@bazel_tools//tools/jdk:toolchain_type",
183 target_settings = [name + "_version_setting"],
184 toolchain = name,
185 )
Ivo List37330652020-12-09 09:39:02 -0800186
cushon30c601d2018-08-08 09:09:08 -0700187def java_runtime_files(name, srcs):
188 """Copies the given sources out of the current Java runtime."""
189
190 native.filegroup(
191 name = name,
192 srcs = srcs,
193 )
194 for src in srcs:
195 native.genrule(
196 name = "gen_%s" % src,
cushon262ab772018-09-10 02:04:32 -0700197 srcs = ["@bazel_tools//tools/jdk:current_java_runtime"],
198 toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
cushon30c601d2018-08-08 09:09:08 -0700199 cmd = "cp $(JAVABASE)/%s $@" % src,
200 outs = [src],
201 )
cushon999354b2018-09-21 00:31:42 -0700202
cparsons36c70a62019-06-07 09:31:14 -0700203def _bootclasspath_impl(ctx):
cushon999354b2018-09-21 00:31:42 -0700204 host_javabase = ctx.attr.host_javabase[java_common.JavaRuntimeInfo]
205
cushon73972b32018-10-16 17:30:32 -0700206 # explicitly list output files instead of using TreeArtifact to work around
207 # https://github.com/bazelbuild/bazel/issues/6203
208 classes = [
209 "DumpPlatformClassPath.class",
cushon73972b32018-10-16 17:30:32 -0700210 ]
211
212 class_outputs = [
213 ctx.actions.declare_file("%s_classes/%s" % (ctx.label.name, clazz))
214 for clazz in classes
215 ]
cushon999354b2018-09-21 00:31:42 -0700216
217 args = ctx.actions.args()
218 args.add("-source")
219 args.add("8")
220 args.add("-target")
221 args.add("8")
222 args.add("-Xlint:-options")
223 args.add("-cp")
224 args.add("%s/lib/tools.jar" % host_javabase.java_home)
225 args.add("-d")
cushon73972b32018-10-16 17:30:32 -0700226 args.add(class_outputs[0].dirname)
cushon999354b2018-09-21 00:31:42 -0700227 args.add(ctx.file.src)
228
229 ctx.actions.run(
230 executable = "%s/bin/javac" % host_javabase.java_home,
Nick Korostelev120ea6c2021-03-03 05:02:02 -0800231 mnemonic = "JavaToolchainCompileClasses",
cushon999354b2018-09-21 00:31:42 -0700232 inputs = [ctx.file.src] + ctx.files.host_javabase,
cushon73972b32018-10-16 17:30:32 -0700233 outputs = class_outputs,
cushon999354b2018-09-21 00:31:42 -0700234 arguments = [args],
235 )
236
cparsons36c70a62019-06-07 09:31:14 -0700237 bootclasspath = ctx.outputs.output_jar
cushon999354b2018-09-21 00:31:42 -0700238
cushon73972b32018-10-16 17:30:32 -0700239 inputs = class_outputs + ctx.files.host_javabase
cushon999354b2018-09-21 00:31:42 -0700240
241 args = ctx.actions.args()
242 args.add("-XX:+IgnoreUnrecognizedVMOptions")
cushon74f35d42021-04-05 11:20:04 -0700243 args.add("--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED")
cushon999354b2018-09-21 00:31:42 -0700244 args.add("--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED")
cushon74f35d42021-04-05 11:20:04 -0700245 args.add("--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED")
cushon999354b2018-09-21 00:31:42 -0700246 args.add_joined(
247 "-cp",
cushon73972b32018-10-16 17:30:32 -0700248 [class_outputs[0].dirname, "%s/lib/tools.jar" % host_javabase.java_home],
cushon999354b2018-09-21 00:31:42 -0700249 join_with = ctx.configuration.host_path_separator,
250 )
251 args.add("DumpPlatformClassPath")
cushon999354b2018-09-21 00:31:42 -0700252 args.add(bootclasspath)
253
254 if ctx.attr.target_javabase:
255 inputs.extend(ctx.files.target_javabase)
256 args.add(ctx.attr.target_javabase[java_common.JavaRuntimeInfo].java_home)
257
258 ctx.actions.run(
259 executable = str(host_javabase.java_executable_exec_path),
Nick Korostelev120ea6c2021-03-03 05:02:02 -0800260 mnemonic = "JavaToolchainCompileBootClasspath",
cushon999354b2018-09-21 00:31:42 -0700261 inputs = inputs,
262 outputs = [bootclasspath],
263 arguments = [args],
264 )
cparsons36c70a62019-06-07 09:31:14 -0700265 return [
266 DefaultInfo(files = depset([bootclasspath])),
267 OutputGroupInfo(jar = [bootclasspath]),
268 ]
cushon999354b2018-09-21 00:31:42 -0700269
cparsons36c70a62019-06-07 09:31:14 -0700270_bootclasspath = rule(
271 implementation = _bootclasspath_impl,
cushon999354b2018-09-21 00:31:42 -0700272 attrs = {
273 "host_javabase": attr.label(
274 cfg = "host",
275 providers = [java_common.JavaRuntimeInfo],
276 ),
cushon999354b2018-09-21 00:31:42 -0700277 "src": attr.label(
278 cfg = "host",
279 allow_single_file = True,
280 ),
281 "target_javabase": attr.label(
282 providers = [java_common.JavaRuntimeInfo],
283 ),
cparsons36c70a62019-06-07 09:31:14 -0700284 "output_jar": attr.output(mandatory = True),
cushon999354b2018-09-21 00:31:42 -0700285 },
cushon999354b2018-09-21 00:31:42 -0700286)
cparsons36c70a62019-06-07 09:31:14 -0700287
288def bootclasspath(name, **kwargs):
289 _bootclasspath(
290 name = name,
291 output_jar = name + ".jar",
292 **kwargs
293 )