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("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") 18load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") 19load("//bazel:stubs.bzl", "select_java_language_level") 20load(":kotlinc_flags.bzl", "kotlinc_flags") 21 22# Work around to toolchains in Google3. 23# buildifier: disable=provider-params 24KtJvmToolchainInfo = provider() 25 26KT_VERSION = "v1_9_10" 27 28# TODO: Remove this alias. Why are we letting people read this? 29KT_LANG_VERSION = kotlinc_flags.KT_LANG_VERSION 30 31# Kotlin JVM toolchain type label 32_TYPE = Label("//toolchains/kotlin_jvm:kt_jvm_toolchain_type") 33 34def _opt_for_test(val, getter): 35 return getter(val) if val else None 36 37def _kt_jvm_toolchain_impl(ctx): 38 profiling_filter = ctx.attr.profiling_filter[BuildSettingInfo].value 39 kotlinc_define_flags = kotlinc_flags.read_define_flags(ctx) 40 41 kt_jvm_toolchain = dict( 42 # go/keep-sorted start 43 android_java8_apis_desugared = ctx.attr.android_java8_apis_desugared, 44 android_lint_config = ctx.file.android_lint_config, 45 android_lint_runner = ctx.attr.android_lint_runner[DefaultInfo].files_to_run, 46 build_marker = ctx.file.build_marker, 47 coverage_instrumenter = ctx.attr.coverage_instrumenter[DefaultInfo].files_to_run, 48 coverage_runtime = _opt_for_test(ctx.attr.coverage_runtime, lambda x: x[JavaInfo]), 49 genclass = ctx.file.genclass, 50 header_gen_tool = _opt_for_test(ctx.attr.header_gen_tool, lambda x: x[DefaultInfo].files_to_run), 51 is_profiling_enabled = lambda label: profiling_filter and (profiling_filter in str(label)), 52 java_language_version = ctx.attr.java_language_version, 53 java_runtime = ctx.attr.java_runtime, 54 jvm_abi_gen_plugin = ctx.file.jvm_abi_gen_plugin, 55 kotlin_compiler = ctx.attr.kotlin_compiler[DefaultInfo].files_to_run, 56 kotlin_language_version = ctx.attr.kotlin_language_version, 57 kotlin_libs = [x[JavaInfo] for x in ctx.attr.kotlin_libs], 58 kotlin_sdk_libraries = ctx.attr.kotlin_sdk_libraries, 59 kotlinc_cli_flags = ctx.attr.kotlinc_cli_flags + kotlinc_define_flags, 60 kotlinc_ide_flags = ctx.attr.kotlinc_ide_flags + kotlinc_define_flags, 61 kt_codegen_java_runtime = ctx.attr.kt_codegen_java_runtime, 62 proguard_whitelister = ctx.attr.proguard_whitelister[DefaultInfo].files_to_run, 63 source_jar_zipper = ctx.file.source_jar_zipper, 64 toolchain_type = None if ctx.attr.toolchain_type == None else str(ctx.attr.toolchain_type.label), 65 # go/keep-sorted end 66 ) 67 return [ 68 platform_common.ToolchainInfo(**kt_jvm_toolchain), 69 KtJvmToolchainInfo(**kt_jvm_toolchain), 70 ] 71 72kt_jvm_toolchain = rule( 73 attrs = dict( 74 android_java8_apis_desugared = attr.bool( 75 # Reflects a select in build rules. 76 doc = "Whether Java 8 API desugaring is enabled", 77 mandatory = True, 78 ), 79 android_lint_config = attr.label( 80 cfg = "exec", 81 allow_single_file = [".xml"], 82 ), 83 android_lint_runner = attr.label( 84 default = "//bazel:stub_tool", 85 executable = True, 86 cfg = "exec", 87 ), 88 build_marker = attr.label( 89 default = "//tools:build_marker", 90 allow_single_file = [".jar"], 91 ), 92 coverage_instrumenter = attr.label( 93 default = "//tools/coverage:offline_instrument", 94 cfg = "exec", 95 executable = True, 96 ), 97 coverage_runtime = attr.label( 98 default = "@maven//:org_jacoco_org_jacoco_agent", 99 ), 100 genclass = attr.label( 101 default = "@bazel_tools//tools/jdk:GenClass_deploy.jar", 102 cfg = "exec", 103 allow_single_file = True, 104 ), 105 header_gen_tool = attr.label( 106 executable = True, 107 allow_single_file = True, 108 cfg = "exec", 109 ), 110 java_language_version = attr.string( 111 default = "11", 112 ), 113 java_runtime = attr.label( 114 default = "@bazel_tools//tools/jdk:current_java_runtime", 115 cfg = "exec", 116 allow_files = True, 117 ), 118 jvm_abi_gen_plugin = attr.label( 119 default = "@kotlinc//:jvm_abi_gen_plugin", 120 cfg = "exec", 121 allow_single_file = [".jar"], 122 ), 123 kotlin_compiler = attr.label( 124 default = "@kotlinc//:kotlin_compiler", 125 cfg = "exec", 126 executable = True, 127 ), 128 kotlin_language_version = attr.string( 129 default = KT_LANG_VERSION, 130 ), 131 kotlin_libs = attr.label_list( 132 doc = "The libraries required during all Kotlin builds.", 133 default = [ 134 "@kotlinc//:kotlin_stdlib", 135 "@kotlinc//:annotations", 136 ], 137 cfg = "target", 138 ), 139 kotlin_sdk_libraries = attr.label_list( 140 doc = "The libraries required to resolve Kotlin code in an IDE.", 141 default = [ 142 "@kotlinc//:kotlin_reflect", 143 "@kotlinc//:kotlin_stdlib", 144 "@kotlinc//:kotlin_test_not_testonly", 145 ], 146 cfg = "target", 147 ), 148 kotlinc_cli_flags = attr.string_list( 149 doc = "The static flags to pass to CLI kotlinc invocations", 150 default = kotlinc_flags.CLI_FLAGS, 151 ), 152 kotlinc_ide_flags = attr.string_list( 153 doc = "The static flags to pass to IDE kotlinc invocations", 154 default = kotlinc_flags.IDE_FLAGS, 155 ), 156 kt_codegen_java_runtime = attr.label( 157 cfg = "exec", 158 ), 159 profiling_filter = attr.label( 160 default = "//toolchains/kotlin_jvm:profiling_filter", 161 providers = [BuildSettingInfo], 162 ), 163 proguard_whitelister = attr.label( 164 default = "@bazel_tools//tools/jdk:proguard_whitelister", 165 cfg = "exec", 166 ), 167 runtime = attr.label_list( 168 # This attribute has a "magic" name recognized by the native DexArchiveAspect 169 # (b/78647825). Must list all implicit runtime deps here, this is not limited 170 # to Kotlin runtime libs. 171 default = [ 172 "@kotlinc//:kotlin_stdlib", 173 "@kotlinc//:annotations", 174 ], 175 cfg = "target", 176 doc = "The Kotlin runtime libraries grouped into one attribute.", 177 ), 178 source_jar_zipper = attr.label( 179 default = "//tools/bin:source_jar_zipper_binary", 180 cfg = "exec", 181 allow_single_file = [".jar"], 182 ), 183 toolchain_type = attr.label(), 184 ), 185 provides = [platform_common.ToolchainInfo], 186 implementation = _kt_jvm_toolchain_impl, 187) 188 189def _declare( 190 toolchain_type = _TYPE, 191 **kwargs): 192 kt_jvm_toolchain( 193 android_java8_apis_desugared = select({ 194 "//conditions:default": False, 195 }), 196 # The JVM bytecode version to output 197 # https://kotlinlang.org/docs/compiler-reference.html#jvm-target-version 198 toolchain_type = toolchain_type, 199 **kwargs 200 ) 201 202_ATTRS = dict( 203 _toolchain = attr.label( 204 # TODO: Delete this attr when fixed. 205 doc = "Magic attribute name for DexArchiveAspect (b/78647825)", 206 default = "//toolchains/kotlin_jvm:kt_jvm_toolchain_linux_sts_jdk", 207 ), 208) 209 210kt_jvm_toolchains = struct( 211 name = _TYPE.name, 212 get = lambda ctx: ctx.toolchains[_TYPE], 213 type = str(_TYPE), 214 declare = _declare, 215 attrs = _ATTRS, 216) 217