1# Copyright 2022 Google LLC. 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"""Kotlin toolchain.""" 16 17load("//bazel:stubs.bzl", "select_java_language_level") 18load("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") 19 20# Work around to toolchains in Google3. 21KtJvmToolchainInfo = provider() 22 23KT_VERSION = "v1_8_10" 24 25KT_LANG_VERSION = "1.8" 26 27# Kotlin JVM toolchain type label 28_TYPE = Label("//toolchains/kotlin_jvm:kt_jvm_toolchain_type") 29 30def _kotlinc_common_flags(ctx, other_flags): 31 """Returns kotlinc flags to use in all compilations.""" 32 args = [ 33 # We're supplying JDK in bootclasspath explicitly instead 34 "-no-jdk", 35 36 # stdlib included in merged_deps 37 "-no-stdlib", 38 39 # The bytecode format to emit 40 "-jvm-target", 41 ctx.attr.jvm_target, 42 43 # Emit bytecode with parameter names 44 "-java-parameters", 45 46 # Allow default method declarations, akin to what javac emits (b/110378321). 47 "-Xjvm-default=all", 48 49 # Trust JSR305 nullness type qualifier nicknames the same as @Nonnull/@Nullable 50 # (see https://kotlinlang.org/docs/reference/java-interop.html#jsr-305-support) 51 "-Xjsr305=strict", 52 53 # Trust go/JSpecify nullness annotations 54 # (see https://kotlinlang.org/docs/whatsnew1520.html#support-for-jspecify-nullness-annotations) 55 "-Xjspecify-annotations=strict", 56 57 # Trust annotations on type arguments, etc. 58 # (see https://kotlinlang.org/docs/java-interop.html#annotating-type-arguments-and-type-parameters) 59 "-Xtype-enhancement-improvements-strict-mode", 60 61 # TODO: Remove this as the default setting (probably Kotlin 1.7) 62 "-Xenhance-type-parameter-types-to-def-not-null=true", 63 64 # Explicitly set language version so we can update compiler separate from language version 65 "-language-version", 66 ctx.attr.kotlin_language_version, 67 68 # Enable type annotations in the JVM bytecode (b/170647926) 69 "-Xemit-jvm-type-annotations", 70 71 # TODO: Temporarily disable 1.5's sam wrapper conversion 72 "-Xsam-conversions=class", 73 74 # We don't want people to use experimental APIs, but if they do, we want them to use @OptIn 75 "-opt-in=kotlin.RequiresOptIn", 76 77 # Don't complain when using old builds or release candidate builds 78 "-Xskip-prerelease-check", 79 80 # Allows a no source files to create an empty jar. 81 "-Xallow-no-source-files", 82 83 # TODO: Remove this flag 84 "-Xuse-old-innerclasses-logic", 85 86 # TODO: Remove this flag 87 "-Xno-source-debug-extension", 88 ] + other_flags 89 90 # --define=extra_kt_jvm_opts is for overriding from command line. 91 # (Last wins in repeated --define=foo= use, so use --define=bar= instead.) 92 extra_kt_jvm_opts = ctx.var.get("extra_kt_jvm_opts", default = None) 93 if extra_kt_jvm_opts: 94 args.extend([o for o in extra_kt_jvm_opts.split(" ") if o]) 95 return args 96 97def _kotlinc_ide_flags(ctx): 98 return _kotlinc_common_flags(ctx, other_flags = []) 99 100def _kotlinc_cli_flags(ctx): 101 return _kotlinc_common_flags(ctx, other_flags = [ 102 # Silence all warning-level diagnostics 103 "-nowarn", 104 ]) 105 106def _kt_jvm_toolchain_impl(ctx): 107 kt_jvm_toolchain = dict( 108 android_java8_apis_desugared = ctx.attr.android_java8_apis_desugared, 109 android_lint_config = ctx.file.android_lint_config, 110 android_lint_runner = ctx.attr.android_lint_runner[DefaultInfo].files_to_run, 111 build_marker = ctx.file.build_marker, 112 coverage_instrumenter = ctx.attr.coverage_instrumenter[DefaultInfo].files_to_run, 113 # Don't require JavaInfo provider for integration test convenience. 114 coverage_runtime = ctx.attr.coverage_runtime[JavaInfo] if JavaInfo in ctx.attr.coverage_runtime else None, 115 genclass = ctx.file.genclass, 116 header_gen_tool = ctx.attr.header_gen_tool[DefaultInfo].files_to_run if ctx.attr.header_gen_tool else None, 117 jar_tool = ctx.attr.jar_tool[DefaultInfo].files_to_run, 118 java_language_version = ctx.attr.java_language_version, 119 java_runtime = ctx.attr.java_runtime, 120 jvm_abi_gen_plugin = ctx.file.jvm_abi_gen_plugin, 121 jvm_target = ctx.attr.jvm_target, 122 kotlin_annotation_processing = ctx.file.kotlin_annotation_processing, 123 kotlin_compiler = ctx.attr.kotlin_compiler[DefaultInfo].files_to_run, 124 kotlin_language_version = ctx.attr.kotlin_language_version, 125 kotlin_libs = [JavaInfo(compile_jar = jar, output_jar = jar) for jar in ctx.files.kotlin_libs], 126 kotlin_sdk_libraries = ctx.attr.kotlin_sdk_libraries, 127 kotlinc_cli_flags = _kotlinc_cli_flags(ctx), 128 kotlinc_ide_flags = _kotlinc_ide_flags(ctx), 129 proguard_whitelister = ctx.attr.proguard_whitelister[DefaultInfo].files_to_run, 130 source_jar_zipper = ctx.file.source_jar_zipper, 131 turbine = ctx.file.turbine, 132 turbine_direct = ctx.file.turbine_direct if ctx.attr.enable_turbine_direct else None, 133 turbine_jsa = ctx.file.turbine_jsa, 134 turbine_java_runtime = ctx.attr.turbine_java_runtime, 135 ) 136 return [ 137 platform_common.ToolchainInfo(**kt_jvm_toolchain), 138 KtJvmToolchainInfo(**kt_jvm_toolchain), 139 ] 140 141kt_jvm_toolchain = rule( 142 attrs = dict( 143 android_java8_apis_desugared = attr.bool( 144 # Reflects a select in build rules. 145 doc = "Whether Java 8 API desugaring is enabled", 146 mandatory = True, 147 ), 148 android_lint_config = attr.label( 149 cfg = "exec", 150 allow_single_file = [".xml"], 151 ), 152 android_lint_runner = attr.label( 153 default = "//bazel:stub_tool", 154 executable = True, 155 cfg = "exec", 156 ), 157 build_marker = attr.label( 158 default = "//tools:build_marker", 159 allow_single_file = [".jar"], 160 ), 161 coverage_instrumenter = attr.label( 162 default = "//tools/coverage:offline_instrument", 163 cfg = "exec", 164 executable = True, 165 ), 166 coverage_runtime = attr.label( 167 default = "@maven//:org_jacoco_org_jacoco_agent", 168 ), 169 enable_turbine_direct = attr.bool( 170 # If disabled, the value of turbine_direct will be ignored. 171 # Starlark doesn't allow None to override default-valued attributes: 172 default = True, 173 ), 174 genclass = attr.label( 175 default = "@bazel_tools//tools/jdk:GenClass_deploy.jar", 176 cfg = "exec", 177 allow_single_file = True, 178 ), 179 header_gen_tool = attr.label( 180 executable = True, 181 allow_single_file = True, 182 cfg = "exec", 183 ), 184 jar_tool = attr.label( 185 default = "@bazel_tools//tools/jdk:jar", 186 executable = True, 187 allow_files = True, 188 cfg = "exec", 189 ), 190 java_language_version = attr.string( 191 default = "11", 192 ), 193 java_runtime = attr.label( 194 default = "@bazel_tools//tools/jdk:current_java_runtime", 195 cfg = "exec", 196 allow_files = True, 197 ), 198 jvm_abi_gen_plugin = attr.label( 199 default = "@kotlinc//:jvm_abi_gen_plugin", 200 cfg = "exec", 201 allow_single_file = [".jar"], 202 ), 203 jvm_target = attr.string( 204 doc = "The value to pass as -jvm-target, indicating the bytecode format to emit.", 205 ), 206 kotlin_annotation_processing = attr.label( 207 default = "@kotlinc//:kotlin_annotation_processing", 208 cfg = "exec", 209 allow_single_file = True, 210 ), 211 kotlin_compiler = attr.label( 212 default = "@kotlinc//:kotlin_compiler", 213 cfg = "exec", 214 executable = True, 215 ), 216 kotlin_language_version = attr.string( 217 default = KT_LANG_VERSION, 218 ), 219 kotlin_libs = attr.label_list( 220 doc = "The libraries required during all Kotlin builds.", 221 default = [ 222 "@kotlinc//:kotlin_stdlib", 223 "@kotlinc//:annotations", 224 ], 225 allow_files = [".jar"], 226 cfg = "target", 227 ), 228 kotlin_sdk_libraries = attr.label_list( 229 doc = "The libraries required to resolve Kotlin code in an IDE.", 230 default = [ 231 "@kotlinc//:kotlin_reflect", 232 "@kotlinc//:kotlin_stdlib", 233 "@kotlinc//:kotlin_test_not_testonly", 234 ], 235 cfg = "target", 236 ), 237 proguard_whitelister = attr.label( 238 default = "@bazel_tools//tools/jdk:proguard_whitelister", 239 cfg = "exec", 240 ), 241 runtime = attr.label_list( 242 # This attribute has a "magic" name recognized by the native DexArchiveAspect 243 # (b/78647825). Must list all implicit runtime deps here, this is not limited 244 # to Kotlin runtime libs. 245 default = [ 246 "@kotlinc//:kotlin_stdlib", 247 "@kotlinc//:annotations", 248 ], 249 cfg = "target", 250 doc = "The Kotlin runtime libraries grouped into one attribute.", 251 ), 252 source_jar_zipper = attr.label( 253 default = "//tools/bin:source_jar_zipper_binary", 254 cfg = "exec", 255 allow_single_file = [".jar"], 256 ), 257 turbine = attr.label( 258 default = "@bazel_tools//tools/jdk:turbine_direct", 259 cfg = "exec", 260 allow_single_file = True, 261 ), 262 turbine_direct = attr.label( 263 executable = True, 264 cfg = "exec", 265 allow_single_file = True, 266 ), 267 turbine_jsa = attr.label( 268 cfg = "exec", 269 allow_single_file = True, 270 ), 271 turbine_java_runtime = attr.label( 272 cfg = "exec", 273 ), 274 ), 275 provides = [platform_common.ToolchainInfo], 276 implementation = _kt_jvm_toolchain_impl, 277) 278 279def _declare(**kwargs): 280 kt_jvm_toolchain( 281 android_java8_apis_desugared = select({ 282 "//conditions:default": False, 283 }), 284 # The JVM bytecode version to output 285 # https://kotlinlang.org/docs/compiler-reference.html#jvm-target-version 286 jvm_target = "11", 287 **kwargs 288 ) 289 290_ATTRS = dict( 291 _toolchain = attr.label( 292 # TODO: Delete this attr when fixed. 293 doc = "Magic attribute name for DexArchiveAspect (b/78647825)", 294 default = "//toolchains/kotlin_jvm:kt_jvm_toolchain_impl", 295 ), 296) 297 298kt_jvm_toolchains = struct( 299 name = _TYPE.name, 300 get = lambda ctx: ctx.toolchains[_TYPE], 301 type = str(_TYPE), 302 declare = _declare, 303 attrs = _ATTRS, 304) 305