1# Copyright 2022 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"""Rules for defining default_java_toolchain""" 16 17load("//java/toolchains:java_toolchain.bzl", "java_toolchain") 18load(":bootclasspath.bzl", _bootclasspath = "bootclasspath") 19 20# JVM options, without patching java.compiler and jdk.compiler modules. 21BASE_JDK9_JVM_OPTS = [ 22 # Allow JavaBuilder to access internal javac APIs. 23 "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", 24 "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED", 25 "--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED", 26 "--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED", 27 "--add-exports=jdk.compiler/com.sun.tools.javac.resources=ALL-UNNAMED", 28 "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", 29 "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", 30 "--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", 31 "--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED", 32 "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", 33 "--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", 34 35 # quiet warnings from com.google.protobuf.UnsafeUtil, 36 # see: https://github.com/google/protobuf/issues/3781 37 # and: https://github.com/bazelbuild/bazel/issues/5599 38 "--add-opens=java.base/java.nio=ALL-UNNAMED", 39 "--add-opens=java.base/java.lang=ALL-UNNAMED", 40 41 # TODO(b/64485048): Disable this option in persistent worker mode only. 42 # Disable symlinks resolution cache since symlinks in exec root change 43 "-Dsun.io.useCanonCaches=false", 44 45 # Compact strings make JavaBuilder slightly slower. 46 "-XX:-CompactStrings", 47 48 # Since https://bugs.openjdk.org/browse/JDK-8153723, JVM logging goes to stdout. This 49 # makes it go to stderr instead. 50 "-Xlog:disable", 51 "-Xlog:all=warning:stderr:uptime,level,tags", 52] 53 54JDK9_JVM_OPTS = BASE_JDK9_JVM_OPTS 55 56DEFAULT_JAVACOPTS = [ 57 "-XDskipDuplicateBridges=true", 58 "-XDcompilePolicy=simple", 59 "--should-stop=ifError=FLOW", # See b/27049950, https://github.com/google/error-prone/issues/4595 60 "-g", 61 "-parameters", 62 # https://github.com/bazelbuild/bazel/issues/15219 63 "-Xep:ReturnValueIgnored:OFF", 64 # https://github.com/bazelbuild/bazel/issues/16996 65 "-Xep:IgnoredPureGetter:OFF", 66 "-Xep:EmptyTopLevelDeclaration:OFF", 67 "-Xep:LenientFormatStringValidation:OFF", 68 "-Xep:ReturnMissingNullable:OFF", 69 "-Xep:UseCorrectAssertInTests:OFF", 70] 71 72# If this is changed, the docs for "{,tool_}java_language_version" also 73# need to be updated in the Bazel user manual 74_DEFAULT_JAVA_LANGUAGE_VERSION = "11" 75 76# Default java_toolchain parameters 77_BASE_TOOLCHAIN_CONFIGURATION = dict( 78 forcibly_disable_header_compilation = False, 79 genclass = [Label("@remote_java_tools//:GenClass")], 80 header_compiler = [Label("@remote_java_tools//:TurbineDirect")], 81 header_compiler_direct = [Label("//toolchains:turbine_direct")], 82 ijar = [Label("//toolchains:ijar")], 83 javabuilder = [Label("@remote_java_tools//:JavaBuilder")], 84 javac_supports_workers = True, 85 jacocorunner = Label("@remote_java_tools//:jacoco_coverage_runner_filegroup"), 86 jvm_opts = BASE_JDK9_JVM_OPTS, 87 turbine_jvm_opts = [ 88 # Turbine is not a worker and parallel GC is faster for short-lived programs. 89 "-XX:+UseParallelGC", 90 ], 91 misc = DEFAULT_JAVACOPTS, 92 singlejar = [Label("//toolchains:singlejar")], 93 # Code to enumerate target JVM boot classpath uses host JVM. Because 94 # java_runtime-s are involved, its implementation is in @bazel_tools. 95 bootclasspath = [Label("//toolchains:platformclasspath")], 96 source_version = _DEFAULT_JAVA_LANGUAGE_VERSION, 97 target_version = _DEFAULT_JAVA_LANGUAGE_VERSION, 98 reduced_classpath_incompatible_processors = [ 99 "dagger.hilt.processor.internal.root.RootProcessor", # see b/21307381 100 ], 101 java_runtime = Label("//toolchains:remotejdk_21"), 102 oneversion = Label("//toolchains:one_version"), 103) 104 105DEFAULT_TOOLCHAIN_CONFIGURATION = _BASE_TOOLCHAIN_CONFIGURATION 106 107# The 'vanilla' toolchain is an unsupported alternative to the default. 108# 109# It does not provide any of the following features: 110# * Error Prone 111# * Strict Java Deps 112# * Reduced Classpath Optimization 113# 114# It uses the version of internal javac from the `--host_javabase` JDK instead 115# of providing a javac. Internal javac may not be source- or bug-compatible with 116# the javac that is provided with other toolchains. 117# 118# However it does allow using a wider range of `--host_javabase`s, including 119# versions newer than the current JDK. 120VANILLA_TOOLCHAIN_CONFIGURATION = dict( 121 javabuilder = [Label("@remote_java_tools//:VanillaJavaBuilder")], 122 jvm_opts = [], 123 java_runtime = None, 124) 125 126# The new toolchain is using all the pre-built tools, including 127# singlejar and ijar, even on remote execution. This toolchain 128# should be used only when host and execution platform are the 129# same, otherwise the binaries will not work on the execution 130# platform. 131PREBUILT_TOOLCHAIN_CONFIGURATION = dict( 132 ijar = [Label("//toolchains:ijar_prebuilt_binary")], 133 singlejar = [Label("//toolchains:prebuilt_singlejar")], 134 oneversion = Label("//toolchains:prebuilt_one_version"), 135) 136 137# The new toolchain is using all the tools from sources. 138NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict( 139 ijar = [Label("@remote_java_tools//:ijar_cc_binary")], 140 singlejar = [Label("@remote_java_tools//:singlejar_cc_bin")], 141 header_compiler_direct = [Label("@remote_java_tools//:TurbineDirect")], 142 oneversion = Label("@remote_java_tools//:one_version_cc_bin"), 143) 144 145def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, exec_compatible_with = [], target_compatible_with = [], **kwargs): 146 """Defines a remote java_toolchain with appropriate defaults for Bazel. 147 148 Args: 149 name: The name of the toolchain 150 configuration: Toolchain configuration 151 toolchain_definition: Whether to define toolchain target and its config setting 152 exec_compatible_with: A list of constraint values that must be 153 satisifed for the exec platform. 154 target_compatible_with: A list of constraint values that must be 155 satisifed for the target platform. 156 **kwargs: More arguments for the java_toolchain target 157 """ 158 159 toolchain_args = dict(_BASE_TOOLCHAIN_CONFIGURATION) 160 toolchain_args.update(configuration) 161 toolchain_args.update(kwargs) 162 java_toolchain( 163 name = name, 164 **toolchain_args 165 ) 166 if toolchain_definition: 167 source_version = toolchain_args["source_version"] 168 if source_version == _DEFAULT_JAVA_LANGUAGE_VERSION: 169 native.config_setting( 170 name = name + "_default_version_setting", 171 values = {"java_language_version": ""}, 172 visibility = ["//visibility:private"], 173 ) 174 native.toolchain( 175 name = name + "_default_definition", 176 toolchain_type = Label("@bazel_tools//tools/jdk:toolchain_type"), 177 target_settings = [name + "_default_version_setting"], 178 toolchain = name, 179 exec_compatible_with = exec_compatible_with, 180 target_compatible_with = target_compatible_with, 181 ) 182 183 native.config_setting( 184 name = name + "_version_setting", 185 values = {"java_language_version": source_version}, 186 visibility = ["//visibility:private"], 187 ) 188 native.toolchain( 189 name = name + "_definition", 190 toolchain_type = Label("@bazel_tools//tools/jdk:toolchain_type"), 191 target_settings = [name + "_version_setting"], 192 toolchain = name, 193 exec_compatible_with = exec_compatible_with, 194 target_compatible_with = target_compatible_with, 195 ) 196 197def java_runtime_files(name, srcs): 198 """Copies the given sources out of the current Java runtime.""" 199 200 native.filegroup( 201 name = name, 202 srcs = srcs, 203 tags = ["manual"], 204 ) 205 for src in srcs: 206 native.genrule( 207 name = "gen_%s" % src, 208 srcs = [Label("//toolchains:current_java_runtime")], 209 toolchains = [Label("//toolchains:current_java_runtime")], 210 cmd = "cp $(JAVABASE)/%s $@" % src, 211 outs = [src], 212 tags = ["manual"], 213 ) 214 215bootclasspath = _bootclasspath 216