• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Copyright (C) 2023 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17load("@rules_android//rules:common.bzl", _common = "common")
18load("@rules_android//rules:java.bzl", _java = "java")
19load(
20    "@rules_android//rules:processing_pipeline.bzl",
21    "ProviderInfo",
22    "processing_pipeline",
23)
24load("@rules_android//rules:utils.bzl", "utils")
25load(
26    "@rules_android//rules/android_library:impl.bzl",
27    "finalize",
28    _BASE_PROCESSORS = "PROCESSORS",
29)
30load("@rules_kotlin//kotlin:common.bzl", _kt_common = "common")
31load("@rules_kotlin//kotlin:compiler_opt.bzl", "merge_kotlincopts")
32load("@rules_kotlin//kotlin:jvm_compile.bzl", "kt_jvm_compile")
33load("@rules_kotlin//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", _kt_jvm_toolchains = "kt_jvm_toolchains")
34
35def _validations_processor(ctx, **_unused_sub_ctxs):
36    utils.check_for_failures(ctx.label, ctx.attr.deps, ctx.attr.exports)
37
38def _process_jvm(
39        ctx,
40        java_package,  # @unused
41        exceptions_ctx,  # @unused
42        resources_ctx,
43        idl_ctx,
44        db_ctx,
45        **_unused_sub_ctxs):
46    # Filter out disallowed sources.
47    srcs = ctx.files.srcs + idl_ctx.idl_java_srcs + db_ctx.java_srcs
48
49    # kt_jvm_compile expects deps that only carry CcInfo in runtime_deps
50    deps = [dep for dep in ctx.attr.deps if JavaInfo in dep] + idl_ctx.idl_deps
51    runtime_deps = [dep for dep in ctx.attr.deps if JavaInfo not in dep]
52
53    jvm_ctx = kt_jvm_compile(
54        ctx,
55        ctx.outputs.lib_jar,
56        # ctx.outputs.lib_src_jar,  # Implicitly determines file.
57        srcs = srcs,
58        common_srcs = ctx.files.common_srcs,
59        coverage_srcs = ctx.files.coverage_srcs,
60        deps = deps,
61        plugins = ctx.attr.plugins + db_ctx.java_plugins,
62        exports = ctx.attr.exports,
63        # As the JavaInfo constructor does not support attaching
64        # exported_plugins, for the purposes of propagation, the plugin is
65        # wrapped in a java_library.exported_plugins target and attached with
66        # export to this rule.
67        exported_plugins = ctx.attr.exported_plugins,
68        runtime_deps = runtime_deps,
69        r_java = resources_ctx.r_java,
70        javacopts = ctx.attr.javacopts + db_ctx.javac_opts,
71        kotlincopts = merge_kotlincopts(ctx),
72        neverlink = ctx.attr.neverlink,
73        testonly = ctx.attr.testonly,
74        android_lint_plugins = [],
75        android_lint_rules_jars = depset(),
76        manifest = getattr(ctx.file, "manifest", None),
77        merged_manifest = resources_ctx.merged_manifest,
78        resource_files = ctx.files.resource_files,
79        kt_toolchain = _kt_jvm_toolchains.get(ctx),
80        java_toolchain = _common.get_java_toolchain(ctx),
81        disable_lint_checks = [],
82        rule_family = _kt_common.RULE_FAMILY.ANDROID_LIBRARY,
83        annotation_processor_additional_outputs = (
84            db_ctx.java_annotation_processor_additional_outputs
85        ),
86        annotation_processor_additional_inputs = (
87            db_ctx.java_annotation_processor_additional_inputs
88        ),
89    )
90
91    java_info = jvm_ctx.java_info
92
93    return ProviderInfo(
94        name = "jvm_ctx",
95        value = struct(
96            java_info = java_info,
97            providers = [java_info],
98        ),
99    )
100
101def _process_coverage(ctx, **_unused_ctx):
102    return ProviderInfo(
103        name = "coverage_ctx",
104        value = struct(
105            providers = [
106                coverage_common.instrumented_files_info(
107                    ctx,
108                    source_attributes = ["srcs", "coverage_srcs"],
109                    dependency_attributes = ["assets", "deps", "exports"],
110                ),
111            ],
112        ),
113    )
114
115PROCESSORS = processing_pipeline.prepend(
116    processing_pipeline.replace(
117        _BASE_PROCESSORS,
118        JvmProcessor = _process_jvm,
119        CoverageProcessor = _process_coverage,
120    ),
121    ValidationsProcessor = _validations_processor,
122)
123
124_PROCESSING_PIPELINE = processing_pipeline.make_processing_pipeline(
125    processors = PROCESSORS,
126    finalize = finalize,
127)
128
129def impl(ctx):
130    java_package = _java.resolve_package_from_label(ctx.label, ctx.attr.custom_package)
131    return processing_pipeline.run(ctx, java_package, _PROCESSING_PIPELINE)
132