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