Ivo List | e68b1e3 | 2020-11-12 08:28:55 -0800 | [diff] [blame] | 1 | # Copyright 2020 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 | |
| 17 | _DEFAULT_JAVACOPTS = [ |
| 18 | "-XDskipDuplicateBridges=true", |
| 19 | "-XDcompilePolicy=simple", |
| 20 | "-g", |
| 21 | "-parameters", |
| 22 | ] |
| 23 | |
| 24 | # JVM options, without patching java.compiler and jdk.compiler modules. |
| 25 | JDK9_JVM_OPTS = [ |
| 26 | # Allow JavaBuilder to access internal javac APIs. |
| 27 | "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", |
| 28 | "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", |
| 29 | "--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED", |
| 30 | "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", |
| 31 | "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED", |
| 32 | "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", |
| 33 | "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", |
| 34 | "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", |
| 35 | |
| 36 | # 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 | |
| 43 | # java_toolchain parameters without specifying javac, java.compiler, |
| 44 | # jdk.compiler module, and jvm_opts |
| 45 | _BASE_TOOLCHAIN_CONFIGURATION = dict( |
| 46 | forcibly_disable_header_compilation = False, |
| 47 | genclass = ["//:GenClass"], |
| 48 | header_compiler = ["//:TurbineDirect"], |
| 49 | header_compiler_direct = ["//:TurbineDirect"], |
| 50 | ijar = ["//:ijar"], |
| 51 | javabuilder = ["//:JavaBuilder"], |
| 52 | javac_supports_workers = True, |
| 53 | jacocorunner = "//:jacoco_coverage_runner_filegroup", |
| 54 | jvm_opts = JDK9_JVM_OPTS, |
| 55 | misc = _DEFAULT_JAVACOPTS, |
| 56 | singlejar = ["//:singlejar"], |
| 57 | # Code to enumerate target JVM boot classpath uses host JVM. Because |
| 58 | # java_runtime-s are involved, its implementation is in @bazel_tools. |
| 59 | bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"], |
| 60 | source_version = "8", |
| 61 | target_version = "8", |
| 62 | ) |
| 63 | |
| 64 | JVM8_TOOLCHAIN_CONFIGURATION = dict( |
ilist | 3fb0f21 | 2020-11-17 10:37:15 -0800 | [diff] [blame] | 65 | tools = ["//:javac_jar"], |
Ivo List | e68b1e3 | 2020-11-12 08:28:55 -0800 | [diff] [blame] | 66 | jvm_opts = ["-Xbootclasspath/p:$(location //:javac_jar)"], |
| 67 | ) |
| 68 | |
| 69 | JAVABUILDER_TOOLCHAIN_CONFIGURATION = dict( |
Ivo List | e68b1e3 | 2020-11-12 08:28:55 -0800 | [diff] [blame] | 70 | jvm_opts = [ |
| 71 | # In JDK9 we have seen a ~30% slow down in JavaBuilder performance when using |
| 72 | # G1 collector and having compact strings enabled. |
| 73 | "-XX:+UseParallelOldGC", |
| 74 | "-XX:-CompactStrings", |
| 75 | # override the javac in the JDK. |
| 76 | "--patch-module=java.compiler=$(location //:java_compiler_jar)", |
| 77 | "--patch-module=jdk.compiler=$(location //:jdk_compiler_jar)", |
| 78 | ] + JDK9_JVM_OPTS, |
| 79 | tools = [ |
| 80 | "//:java_compiler_jar", |
| 81 | "//:jdk_compiler_jar", |
| 82 | ], |
| 83 | ) |
| 84 | |
| 85 | # The 'vanilla' toolchain is an unsupported alternative to the default. |
| 86 | # |
| 87 | # It does not provide any of the following features: |
| 88 | # * Error Prone |
| 89 | # * Strict Java Deps |
| 90 | # * Header Compilation |
| 91 | # * Reduced Classpath Optimization |
| 92 | # |
| 93 | # It uses the version of internal javac from the `--host_javabase` JDK instead |
| 94 | # of providing a javac. Internal javac may not be source- or bug-compatible with |
| 95 | # the javac that is provided with other toolchains. |
| 96 | # |
| 97 | # However it does allow using a wider range of `--host_javabase`s, including |
| 98 | # versions newer than the current JDK. |
| 99 | VANILLA_TOOLCHAIN_CONFIGURATION = dict( |
| 100 | forcibly_disable_header_compilation = True, |
| 101 | javabuilder = ["//:VanillaJavaBuilder"], |
| 102 | jvm_opts = [], |
| 103 | ) |
| 104 | |
| 105 | # The new toolchain is using all the pre-built tools, including |
| 106 | # singlejar and ijar, even on remote execution. This toolchain |
| 107 | # should be used only when host and execution platform are the |
| 108 | # same, otherwise the binaries will not work on the execution |
| 109 | # platform. |
| 110 | PREBUILT_TOOLCHAIN_CONFIGURATION = dict( |
Ivo List | e68b1e3 | 2020-11-12 08:28:55 -0800 | [diff] [blame] | 111 | jvm_opts = [ |
| 112 | # In JDK9 we have seen a ~30% slow down in JavaBuilder performance when using |
| 113 | # G1 collector and having compact strings enabled. |
| 114 | "-XX:+UseParallelOldGC", |
| 115 | "-XX:-CompactStrings", |
| 116 | # override the javac in the JDK. |
| 117 | "--patch-module=java.compiler=$(location //:java_compiler_jar)", |
| 118 | "--patch-module=jdk.compiler=$(location //:jdk_compiler_jar)", |
| 119 | ] + JDK9_JVM_OPTS, |
| 120 | tools = [ |
| 121 | "//:java_compiler_jar", |
| 122 | "//:jdk_compiler_jar", |
| 123 | ], |
| 124 | ijar = ["//:ijar_prebuilt_binary"], |
| 125 | singlejar = ["//:prebuilt_singlejar"], |
| 126 | ) |
| 127 | |
| 128 | _LABEL_LISTS = [ |
| 129 | "bootclasspath", |
Ivo List | e68b1e3 | 2020-11-12 08:28:55 -0800 | [diff] [blame] | 130 | "javac", |
| 131 | "tools", |
| 132 | "javabuilder", |
| 133 | "singlejar", |
| 134 | "genclass", |
| 135 | "resourcejar", |
| 136 | "ijar", |
| 137 | "header_compiler", |
| 138 | "header_compiler_direct", |
| 139 | "package_configuration", |
| 140 | ] |
| 141 | |
| 142 | _LABELS = [ |
| 143 | "timezone_data", |
| 144 | "oneversion", |
| 145 | "oneversion_whitelist", |
| 146 | "jacocorunner", |
| 147 | "proguard_allowlister", |
| 148 | "java_runtime", |
| 149 | ] |
| 150 | |
| 151 | # Converts values to labels, so that they are resolved relative to this java_tools repository |
| 152 | def _to_label(k, v): |
| 153 | if k in _LABELS and type(v) != type(Label("//a")): |
| 154 | return Label(v) |
| 155 | if k in _LABEL_LISTS and type(v) == type([Label("//a")]): |
| 156 | return [Label(label) if type(label) == type("") else label for label in v] |
| 157 | return v |
| 158 | |
| 159 | # Makes labels in jvm_opts absolute, that is replaces " //:" with " @repo//:". |
| 160 | def _format_jvm_opts(toolchain_args, repo): |
| 161 | jvm_opts = toolchain_args["jvm_opts"] |
| 162 | if [opt for opt in jvm_opts if opt.find(" :") >= 0] != []: |
| 163 | fail("Relative labels are not supported in jvm_opts parameter.") |
| 164 | jvm_opts = [opt.replace(" //:", " @{repo}//:").format(repo = repo) for opt in jvm_opts] |
| 165 | return dict(toolchain_args, jvm_opts = jvm_opts) |
| 166 | |
| 167 | def java_toolchain_default(name, configuration = dict(), **kwargs): |
| 168 | """Defines a java_toolchain with appropriate defaults for Bazel.""" |
| 169 | |
| 170 | toolchain_args = dict(_BASE_TOOLCHAIN_CONFIGURATION) |
| 171 | toolchain_args.update(configuration) |
| 172 | toolchain_args.update(kwargs) |
| 173 | toolchain_args = {k: _to_label(k, v) for k, v in toolchain_args.items()} |
| 174 | toolchain_args = _format_jvm_opts(toolchain_args, Label("//x").workspace_name) |
| 175 | native.java_toolchain( |
| 176 | name = name, |
| 177 | **toolchain_args |
| 178 | ) |