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"""Compile method that can compile kotlin or java sources""" 16 17load(":common.bzl", "common") 18load(":compiler_plugin.bzl", "KtCompilerPluginInfo") 19load(":traverse_exports.bzl", "kt_traverse_exports") 20load("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") 21 22_RULE_FAMILY = common.RULE_FAMILY 23 24def kt_jvm_compile( 25 ctx, 26 output, 27 srcs, 28 common_srcs, 29 deps, 30 plugins, 31 runtime_deps, 32 exports, 33 javacopts, 34 kotlincopts, 35 neverlink, 36 testonly, 37 android_lint_plugins, 38 resource_files, 39 exported_plugins, 40 manifest = None, 41 merged_manifest = None, 42 classpath_resources = [], 43 kt_toolchain = None, 44 java_toolchain = None, 45 android_lint_rules_jars = depset(), 46 disable_lint_checks = [], 47 r_java = None, 48 output_srcjar = None, 49 rule_family = _RULE_FAMILY.UNKNOWN, 50 annotation_processor_additional_outputs = [], 51 annotation_processor_additional_inputs = [], 52 coverage_srcs = [], 53 **_kwargs): 54 """ 55 The Kotlin JVM Compile method. 56 57 Args: 58 ctx: The context. 59 output: A File. The output jar. 60 srcs: List of Files. The Kotlin and Java sources. 61 common_srcs: List of common source files. 62 deps: List of targets. A list of dependencies. 63 plugins: List of targets. A list of jvm plugins. 64 runtime_deps: List of targets. A list of runtime deps. 65 exports: List of targets. A list of exports. 66 javacopts: List of strings. A list of Java compile options. 67 kotlincopts: List of strings. A list of Kotlin compile options. 68 neverlink: A bool. Signifies whether the target is only used for compile-time. 69 testonly: A bool. Signifies whether the target is only used for testing only. 70 android_lint_plugins: List of targets. An list of android lint plugins to 71 execute as a part of linting. 72 resource_files: List of Files. The list of Android Resource files. 73 exported_plugins: List of exported javac/kotlinc plugins 74 manifest: A File. The raw Android manifest. Optional. 75 merged_manifest: A File. The merged Android manifest. Optional. 76 classpath_resources: List of Files. The list of classpath resources (kt_jvm_library only). 77 kt_toolchain: The Kotlin toolchain. 78 java_toolchain: The Java toolchain. 79 android_lint_rules_jars: Depset of Files. Standalone Android Lint rule Jar artifacts. 80 disable_lint_checks: Whether to disable link checks. 81 NOTE: This field should only be used when the provider is not produced 82 by a target. For example, the JavaInfo created for the Android R.java 83 within an android_library rule. 84 r_java: A JavaInfo provider. The JavaInfo provider for the Android R.java 85 which is both depended on and propagated as an export. 86 NOTE: This field accepts a JavaInfo, but should only be used for the 87 Android R.java within an android_library rule. 88 output_srcjar: Target output file for generated source jar. Default filename used if None. 89 rule_family: The family of the rule calling this function. Element of common.RULE_FAMILY. 90 May be used to enable/disable some features. 91 annotation_processor_additional_outputs: sequence of Files. A list of 92 files produced by an annotation processor. 93 annotation_processor_additional_inputs: sequence of Files. A list of 94 files consumed by an annotation processor. 95 coverage_srcs: Files to use as the basis when computing code coverage. These are typically 96 handwritten files that were inputs to generated `srcs`. Should be disjoint with `srcs`. 97 **_kwargs: Unused kwargs so that parameters are easy to add and remove. 98 99 Returns: 100 A struct that carries the following fields: java_info and validations. 101 """ 102 if rule_family != _RULE_FAMILY.ANDROID_LIBRARY and not (srcs + common_srcs + exports): 103 # Demands either of the following to be present. 104 # - Source-type artifacts, srcs or common_srcs, including an empty 105 # tree-artifact (a directory) or a srcjar without jar entries. 106 # - Exporting targets, exports. It is typically used by a library author 107 # to publish one user-facing target with direct exposure to its 108 # dependent libraries. 109 fail("Expected one of (srcs, common_srcs, exports) is not empty for kotlin/jvm_compile on target: {}".format(ctx.label)) 110 111 if classpath_resources and rule_family != _RULE_FAMILY.JVM_LIBRARY: 112 fail("resources attribute only allowed for jvm libraries") 113 114 if type(java_toolchain) != "JavaToolchainInfo": 115 # Allow passing either a target or a provider until all callers are updated 116 java_toolchain = java_toolchain[java_common.JavaToolchainInfo] 117 118 java_infos = [] 119 120 # The r_java field only support Android resources Jar files. For now, verify 121 # that the name of the jar matches "_resources.jar". This check does not to 122 # prevent malicious use, the intent is to prevent accidental usage. 123 r_java_infos = [] 124 if r_java: 125 for jar in r_java.outputs.jars: 126 if not jar.class_jar.path.endswith("_resources.jar"): 127 fail("Error, illegal dependency provided for r_java. This " + 128 "only supports Android resource Jar files, " + 129 "'*_resources.jar'.") 130 r_java_infos.append(r_java) 131 132 # Skip deps validation check for any android_library target with no kotlin sources: b/239721906 133 has_kt_srcs = any([common.is_kt_src(src) for src in srcs]) 134 if rule_family != _RULE_FAMILY.ANDROID_LIBRARY or has_kt_srcs: 135 kt_traverse_exports.expand_forbidden_deps(deps + runtime_deps + exports) 136 137 for dep in deps: 138 if JavaInfo in dep: 139 java_infos.append(dep[JavaInfo]) 140 else: 141 fail("Unexpected dependency (must provide JavaInfo): %s" % dep.label) 142 143 if kotlincopts != None and "-Werror" in kotlincopts: 144 fail("Flag -Werror is not permitted") 145 146 return common.kt_jvm_library( 147 ctx, 148 classpath_resources = classpath_resources, 149 common_srcs = common_srcs, 150 coverage_srcs = coverage_srcs, 151 deps = r_java_infos + java_infos, 152 disable_lint_checks = disable_lint_checks, 153 exported_plugins = [e[JavaPluginInfo] for e in exported_plugins if (JavaPluginInfo in e)], 154 # Not all exported targets contain a JavaInfo (e.g. some only have CcInfo) 155 exports = r_java_infos + [e[JavaInfo] for e in exports if JavaInfo in e], 156 friend_jars = kt_traverse_exports.expand_friend_jars(deps, root = ctx), 157 java_toolchain = java_toolchain, 158 javacopts = javacopts, 159 kotlincopts = kotlincopts, 160 compile_jdeps = kt_traverse_exports.expand_direct_jdeps(deps), 161 kt_toolchain = kt_toolchain, 162 manifest = manifest, 163 merged_manifest = merged_manifest, 164 native_libraries = [p[CcInfo] for p in deps + runtime_deps + exports if CcInfo in p], 165 neverlink = neverlink, 166 output = output, 167 output_srcjar = output_srcjar, 168 plugins = common.kt_plugins_map( 169 android_lint_singlejar_plugins = android_lint_rules_jars, 170 android_lint_libjar_plugin_infos = [p[JavaInfo] for p in android_lint_plugins], 171 java_plugin_infos = [ 172 plugin[JavaPluginInfo] 173 for plugin in plugins 174 if (JavaPluginInfo in plugin) 175 ], 176 kt_compiler_plugin_infos = 177 kt_traverse_exports.expand_compiler_plugins(deps).to_list() + [ 178 plugin[KtCompilerPluginInfo] 179 for plugin in plugins 180 if (KtCompilerPluginInfo in plugin) 181 ], 182 ), 183 resource_files = resource_files, 184 runtime_deps = [d[JavaInfo] for d in runtime_deps if JavaInfo in d], 185 srcs = srcs, 186 testonly = testonly, 187 rule_family = rule_family, 188 annotation_processor_additional_outputs = annotation_processor_additional_outputs, 189 annotation_processor_additional_inputs = annotation_processor_additional_inputs, 190 ) 191