xref: /aosp_15_r20/external/bazelbuild-rules_android/rules/android_library/attrs.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1*9e965d6fSRomain Jobredeaux# Copyright 2018 The Bazel Authors. All rights reserved.
2*9e965d6fSRomain Jobredeaux#
3*9e965d6fSRomain Jobredeaux# Licensed under the Apache License, Version 2.0 (the "License");
4*9e965d6fSRomain Jobredeaux# you may not use this file except in compliance with the License.
5*9e965d6fSRomain Jobredeaux# You may obtain a copy of the License at
6*9e965d6fSRomain Jobredeaux#
7*9e965d6fSRomain Jobredeaux#    http://www.apache.org/licenses/LICENSE-2.0
8*9e965d6fSRomain Jobredeaux#
9*9e965d6fSRomain Jobredeaux# Unless required by applicable law or agreed to in writing, software
10*9e965d6fSRomain Jobredeaux# distributed under the License is distributed on an "AS IS" BASIS,
11*9e965d6fSRomain Jobredeaux# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9e965d6fSRomain Jobredeaux# See the License for the specific language governing permissions and
13*9e965d6fSRomain Jobredeaux# limitations under the License.
14*9e965d6fSRomain Jobredeaux
15*9e965d6fSRomain Jobredeaux"""Attributes."""
16*9e965d6fSRomain Jobredeaux
17*9e965d6fSRomain Jobredeauxload(
18*9e965d6fSRomain Jobredeaux    "//rules:attrs.bzl",
19*9e965d6fSRomain Jobredeaux    _attrs = "attrs",
20*9e965d6fSRomain Jobredeaux)
21*9e965d6fSRomain Jobredeauxload("//rules:providers.bzl", "StarlarkApkInfo")
22*9e965d6fSRomain Jobredeaux
23*9e965d6fSRomain JobredeauxATTRS = _attrs.add(
24*9e965d6fSRomain Jobredeaux    dict(
25*9e965d6fSRomain Jobredeaux        baseline_profiles = attr.label_list(
26*9e965d6fSRomain Jobredeaux            allow_files = [".txt"],
27*9e965d6fSRomain Jobredeaux            doc = (
28*9e965d6fSRomain Jobredeaux                "The list of baseline profiles. They provide a way for developers " +
29*9e965d6fSRomain Jobredeaux                "to provide profile rules and could be used at installation time to " +
30*9e965d6fSRomain Jobredeaux                "speed up app startup and reduce jank. See " +
31*9e965d6fSRomain Jobredeaux                "https://developer.android.com/topic/performance/baselineprofiles/overview for " +
32*9e965d6fSRomain Jobredeaux                "more details."
33*9e965d6fSRomain Jobredeaux            ),
34*9e965d6fSRomain Jobredeaux        ),
35*9e965d6fSRomain Jobredeaux        deps = attr.label_list(
36*9e965d6fSRomain Jobredeaux            providers = [
37*9e965d6fSRomain Jobredeaux                [CcInfo],
38*9e965d6fSRomain Jobredeaux                [JavaInfo],
39*9e965d6fSRomain Jobredeaux            ],
40*9e965d6fSRomain Jobredeaux            doc = (
41*9e965d6fSRomain Jobredeaux                "The list of other libraries to link against. Permitted library types " +
42*9e965d6fSRomain Jobredeaux                "are: `android_library`, `java_library` with `android` constraint and " +
43*9e965d6fSRomain Jobredeaux                "`cc_library` wrapping or producing `.so` native libraries for the " +
44*9e965d6fSRomain Jobredeaux                "Android target platform."
45*9e965d6fSRomain Jobredeaux            ),
46*9e965d6fSRomain Jobredeaux        ),
47*9e965d6fSRomain Jobredeaux        enable_data_binding = attr.bool(
48*9e965d6fSRomain Jobredeaux            default = False,
49*9e965d6fSRomain Jobredeaux            doc = (
50*9e965d6fSRomain Jobredeaux                "If true, this rule processes [data binding]" +
51*9e965d6fSRomain Jobredeaux                "(https://developer.android.com/topic/libraries/data-binding) " +
52*9e965d6fSRomain Jobredeaux                "expressions in layout resources included through the [resource_files]" +
53*9e965d6fSRomain Jobredeaux                "(https://docs.bazel.build/versions/main/be/android.html#android_binary.resource_files) " +
54*9e965d6fSRomain Jobredeaux                "attribute. Without this setting, data binding expressions produce build " +
55*9e965d6fSRomain Jobredeaux                "failures. To build an Android app with data binding, you must also do the following:" +
56*9e965d6fSRomain Jobredeaux                "\n\n1. Set this attribute for all Android rules that transitively depend on " +
57*9e965d6fSRomain Jobredeaux                "this one. This is because dependers inherit the rule's data binding " +
58*9e965d6fSRomain Jobredeaux                "expressions through resource merging. So they also need to build with " +
59*9e965d6fSRomain Jobredeaux                "data binding to parse those expressions." +
60*9e965d6fSRomain Jobredeaux                "\n\n2. Add a `deps =` entry for the data binding runtime library to all targets " +
61*9e965d6fSRomain Jobredeaux                "that set this attribute. The location of this library depends on your depot setup."
62*9e965d6fSRomain Jobredeaux            ),
63*9e965d6fSRomain Jobredeaux        ),
64*9e965d6fSRomain Jobredeaux        exported_plugins = attr.label_list(
65*9e965d6fSRomain Jobredeaux            providers = [
66*9e965d6fSRomain Jobredeaux                [JavaPluginInfo],
67*9e965d6fSRomain Jobredeaux            ],
68*9e965d6fSRomain Jobredeaux            cfg = "exec",
69*9e965d6fSRomain Jobredeaux            doc = (
70*9e965d6fSRomain Jobredeaux                "The list of [java_plugin](https://docs.bazel.build/versions/main/be/java.html#java_plugin)s " +
71*9e965d6fSRomain Jobredeaux                "(e.g. annotation processors) to export to libraries that directly depend on this library. " +
72*9e965d6fSRomain Jobredeaux                "The specified list of `java_plugin`s will be applied to any library which directly depends on " +
73*9e965d6fSRomain Jobredeaux                "this library, just as if that library had explicitly declared these labels in " +
74*9e965d6fSRomain Jobredeaux                "[plugins](#android_library-plugins)."
75*9e965d6fSRomain Jobredeaux            ),
76*9e965d6fSRomain Jobredeaux        ),
77*9e965d6fSRomain Jobredeaux        exports = attr.label_list(
78*9e965d6fSRomain Jobredeaux            providers = [
79*9e965d6fSRomain Jobredeaux                [CcInfo],
80*9e965d6fSRomain Jobredeaux                [JavaInfo],
81*9e965d6fSRomain Jobredeaux            ],
82*9e965d6fSRomain Jobredeaux            doc = (
83*9e965d6fSRomain Jobredeaux                "The closure of all rules reached via `exports` attributes are considered " +
84*9e965d6fSRomain Jobredeaux                "direct dependencies of any rule that directly depends on the target with " +
85*9e965d6fSRomain Jobredeaux                "`exports`. The `exports` are not direct deps of the rule they belong to."
86*9e965d6fSRomain Jobredeaux            ),
87*9e965d6fSRomain Jobredeaux        ),
88*9e965d6fSRomain Jobredeaux        exports_manifest = _attrs.tristate.create(
89*9e965d6fSRomain Jobredeaux            default = _attrs.tristate.no,
90*9e965d6fSRomain Jobredeaux            doc = (
91*9e965d6fSRomain Jobredeaux                "Whether to export manifest entries to `android_binary` targets that " +
92*9e965d6fSRomain Jobredeaux                "depend on this target. `uses-permissions` attributes are never exported."
93*9e965d6fSRomain Jobredeaux            ),
94*9e965d6fSRomain Jobredeaux        ),
95*9e965d6fSRomain Jobredeaux        idl_import_root = attr.string(
96*9e965d6fSRomain Jobredeaux            doc = (
97*9e965d6fSRomain Jobredeaux                "Package-relative path to the root of the java package tree containing idl " +
98*9e965d6fSRomain Jobredeaux                "sources included in this library. This path will be used as the import root " +
99*9e965d6fSRomain Jobredeaux                "when processing idl sources that depend on this library." +
100*9e965d6fSRomain Jobredeaux                "\n\n" +
101*9e965d6fSRomain Jobredeaux                "When `idl_import_root` is specified, both `idl_parcelables` and `idl_srcs` must " +
102*9e965d6fSRomain Jobredeaux                "be at the path specified by the java package of the object they represent " +
103*9e965d6fSRomain Jobredeaux                "under `idl_import_root`. When `idl_import_root` is not specified, both " +
104*9e965d6fSRomain Jobredeaux                "`idl_parcelables` and `idl_srcs` must be at the path specified by their " +
105*9e965d6fSRomain Jobredeaux                "package under a Java root. " +
106*9e965d6fSRomain Jobredeaux                "See [examples](#examples)"
107*9e965d6fSRomain Jobredeaux            ),
108*9e965d6fSRomain Jobredeaux        ),
109*9e965d6fSRomain Jobredeaux        idl_parcelables = attr.label_list(
110*9e965d6fSRomain Jobredeaux            allow_files = [".aidl"],
111*9e965d6fSRomain Jobredeaux            doc = (
112*9e965d6fSRomain Jobredeaux                "List of Android IDL definitions to supply as imports. These files will " +
113*9e965d6fSRomain Jobredeaux                "be made available as imports for any `android_library` target that depends " +
114*9e965d6fSRomain Jobredeaux                "on this library, directly or via its transitive closure, but will not be " +
115*9e965d6fSRomain Jobredeaux                "translated to Java or compiled. Only `.aidl` files that correspond directly " +
116*9e965d6fSRomain Jobredeaux                "to `.java` sources in this library should be included (e.g., custom " +
117*9e965d6fSRomain Jobredeaux                "implementations of Parcelable), otherwise `idl_srcs` should be used." +
118*9e965d6fSRomain Jobredeaux                "\n\n" +
119*9e965d6fSRomain Jobredeaux                "These files must be placed appropriately for the aidl compiler to find " +
120*9e965d6fSRomain Jobredeaux                "them. See the description of [idl_import_root](#android_library-idl_import_root) " +
121*9e965d6fSRomain Jobredeaux                "for information about what this means."
122*9e965d6fSRomain Jobredeaux            ),
123*9e965d6fSRomain Jobredeaux        ),
124*9e965d6fSRomain Jobredeaux        idl_preprocessed = attr.label_list(
125*9e965d6fSRomain Jobredeaux            allow_files = [".aidl"],
126*9e965d6fSRomain Jobredeaux            doc = (
127*9e965d6fSRomain Jobredeaux                "List of preprocessed Android IDL definitions to supply as imports. These " +
128*9e965d6fSRomain Jobredeaux                "files will be made available as imports for any `android_library` target " +
129*9e965d6fSRomain Jobredeaux                "that depends on this library, directly or via its transitive closure, but " +
130*9e965d6fSRomain Jobredeaux                "will not be translated to Java or compiled. Only preprocessed `.aidl` " +
131*9e965d6fSRomain Jobredeaux                "files that correspond directly to `.java` sources in this library should " +
132*9e965d6fSRomain Jobredeaux                "be included (e.g., custom implementations of Parcelable), otherwise use " +
133*9e965d6fSRomain Jobredeaux                "`idl_srcs` for Android IDL definitions that need to be translated to Java " +
134*9e965d6fSRomain Jobredeaux                "interfaces and use `idl_parcelable` for non-preprocessed AIDL files."
135*9e965d6fSRomain Jobredeaux            ),
136*9e965d6fSRomain Jobredeaux        ),
137*9e965d6fSRomain Jobredeaux        idl_srcs = attr.label_list(
138*9e965d6fSRomain Jobredeaux            allow_files = [".aidl"],
139*9e965d6fSRomain Jobredeaux            doc = (
140*9e965d6fSRomain Jobredeaux                "List of Android IDL definitions to translate to Java interfaces. After " +
141*9e965d6fSRomain Jobredeaux                "the Java interfaces are generated, they will be compiled together with " +
142*9e965d6fSRomain Jobredeaux                "the contents of `srcs`. These files will be made available as imports " +
143*9e965d6fSRomain Jobredeaux                "for any `android_library` target that depends on this library, directly " +
144*9e965d6fSRomain Jobredeaux                "or via its transitive closure." +
145*9e965d6fSRomain Jobredeaux                "\n\n" +
146*9e965d6fSRomain Jobredeaux                "These files must be placed appropriately for the aidl compiler to find " +
147*9e965d6fSRomain Jobredeaux                "them. See the description of [idl_import_root](#android_library-idl_import_root) " +
148*9e965d6fSRomain Jobredeaux                "for information about what this means."
149*9e965d6fSRomain Jobredeaux            ),
150*9e965d6fSRomain Jobredeaux        ),
151*9e965d6fSRomain Jobredeaux        idl_uses_aosp_compiler = attr.bool(
152*9e965d6fSRomain Jobredeaux            default = False,
153*9e965d6fSRomain Jobredeaux            doc = (
154*9e965d6fSRomain Jobredeaux                "Use the upstream AOSP compiler to generate Java files out of `idl_srcs`." +
155*9e965d6fSRomain Jobredeaux                "The upstream AOSP compiler provides several new language features that the " +
156*9e965d6fSRomain Jobredeaux                "Google3-only compiler doesn't provide. For example: structured parcelables, " +
157*9e965d6fSRomain Jobredeaux                "unions, enums, nested type declarations, constant expressions, annotations, " +
158*9e965d6fSRomain Jobredeaux                "and more. " +
159*9e965d6fSRomain Jobredeaux                "See [AIDL Doc](https://source.android.com/docs/core/architecture/aidl/overview) " +
160*9e965d6fSRomain Jobredeaux                "for more details. " +
161*9e965d6fSRomain Jobredeaux                "Note: the use of the AOSP compiler in google3 is restricted due to performance " +
162*9e965d6fSRomain Jobredeaux                "considerations. This should not be broadly used unless these features are " +
163*9e965d6fSRomain Jobredeaux                "strictly required."
164*9e965d6fSRomain Jobredeaux            ),
165*9e965d6fSRomain Jobredeaux        ),
166*9e965d6fSRomain Jobredeaux        idlopts = attr.string_list(
167*9e965d6fSRomain Jobredeaux            mandatory = False,
168*9e965d6fSRomain Jobredeaux            allow_empty = True,
169*9e965d6fSRomain Jobredeaux            default = [],
170*9e965d6fSRomain Jobredeaux            doc = (
171*9e965d6fSRomain Jobredeaux                "Add these flags to the AIDL compiler command."
172*9e965d6fSRomain Jobredeaux            ),
173*9e965d6fSRomain Jobredeaux        ),
174*9e965d6fSRomain Jobredeaux        neverlink = attr.bool(
175*9e965d6fSRomain Jobredeaux            default = False,
176*9e965d6fSRomain Jobredeaux            doc = (
177*9e965d6fSRomain Jobredeaux                "Only use this library for compilation and not at runtime. The outputs " +
178*9e965d6fSRomain Jobredeaux                "of a rule marked as neverlink will not be used in `.apk` creation. " +
179*9e965d6fSRomain Jobredeaux                "Useful if the library will be provided by the runtime environment during execution."
180*9e965d6fSRomain Jobredeaux            ),
181*9e965d6fSRomain Jobredeaux        ),
182*9e965d6fSRomain Jobredeaux        proguard_specs = attr.label_list(
183*9e965d6fSRomain Jobredeaux            allow_files = True,
184*9e965d6fSRomain Jobredeaux            doc = (
185*9e965d6fSRomain Jobredeaux                "Files to be used as Proguard specification. These will describe the set " +
186*9e965d6fSRomain Jobredeaux                "of specifications to be used by Proguard. If specified, they will be " +
187*9e965d6fSRomain Jobredeaux                "added to any `android_binary` target depending on this library. The " +
188*9e965d6fSRomain Jobredeaux                "files included here must only have idempotent rules, namely -dontnote, " +
189*9e965d6fSRomain Jobredeaux                "-dontwarn, assumenosideeffects, and rules that start with -keep. Other " +
190*9e965d6fSRomain Jobredeaux                "options can only appear in `android_binary`'s proguard_specs, to " +
191*9e965d6fSRomain Jobredeaux                "ensure non-tautological merges."
192*9e965d6fSRomain Jobredeaux            ),
193*9e965d6fSRomain Jobredeaux        ),
194*9e965d6fSRomain Jobredeaux        resource_apks = attr.label_list(
195*9e965d6fSRomain Jobredeaux            allow_rules = ["apk_import"],
196*9e965d6fSRomain Jobredeaux            providers = [
197*9e965d6fSRomain Jobredeaux                [StarlarkApkInfo],
198*9e965d6fSRomain Jobredeaux            ],
199*9e965d6fSRomain Jobredeaux            doc = (
200*9e965d6fSRomain Jobredeaux                "List of resource only apks to link against."
201*9e965d6fSRomain Jobredeaux            ),
202*9e965d6fSRomain Jobredeaux        ),
203*9e965d6fSRomain Jobredeaux        srcs = attr.label_list(
204*9e965d6fSRomain Jobredeaux            allow_files = [".java", ".srcjar"],
205*9e965d6fSRomain Jobredeaux            doc = (
206*9e965d6fSRomain Jobredeaux                "The list of `.java` or `.srcjar` files that are processed to create the " +
207*9e965d6fSRomain Jobredeaux                "target. `srcs` files of type `.java` are compiled. For *readability's " +
208*9e965d6fSRomain Jobredeaux                "sake*, it is not good to put the name of a generated `.java` source " +
209*9e965d6fSRomain Jobredeaux                "file into the `srcs`. Instead, put the depended-on rule name in the `srcs`, " +
210*9e965d6fSRomain Jobredeaux                "as described below." +
211*9e965d6fSRomain Jobredeaux                "\n\n" +
212*9e965d6fSRomain Jobredeaux                "`srcs` files of type `.srcjar` are unpacked and compiled. (This is useful " +
213*9e965d6fSRomain Jobredeaux                "if you need to generate a set of `.java` files with a genrule or build extension.)"
214*9e965d6fSRomain Jobredeaux            ),
215*9e965d6fSRomain Jobredeaux        ),
216*9e965d6fSRomain Jobredeaux        # TODO(b/127517031): Remove these entries once fixed.
217*9e965d6fSRomain Jobredeaux        _defined_assets = attr.bool(default = False),
218*9e965d6fSRomain Jobredeaux        _defined_assets_dir = attr.bool(default = False),
219*9e965d6fSRomain Jobredeaux        _defined_idl_import_root = attr.bool(default = False),
220*9e965d6fSRomain Jobredeaux        _defined_idl_parcelables = attr.bool(default = False),
221*9e965d6fSRomain Jobredeaux        _defined_idl_srcs = attr.bool(default = False),
222*9e965d6fSRomain Jobredeaux        _defined_local_resources = attr.bool(default = False),
223*9e965d6fSRomain Jobredeaux        _java_toolchain = attr.label(
224*9e965d6fSRomain Jobredeaux            default = Label("//tools/jdk:toolchain_android_only"),
225*9e965d6fSRomain Jobredeaux        ),
226*9e965d6fSRomain Jobredeaux        # TODO(str): Remove when fully migrated to android_instrumentation_test
227*9e965d6fSRomain Jobredeaux        _android_test_migration = attr.bool(default = False),
228*9e965d6fSRomain Jobredeaux        _flags = attr.label(
229*9e965d6fSRomain Jobredeaux            default = "//rules/flags",
230*9e965d6fSRomain Jobredeaux        ),
231*9e965d6fSRomain Jobredeaux        _package_name = attr.string(),  # for sending the package name to the outputs callback
232*9e965d6fSRomain Jobredeaux    ),
233*9e965d6fSRomain Jobredeaux    _attrs.COMPILATION,
234*9e965d6fSRomain Jobredeaux    _attrs.DATA_CONTEXT,
235*9e965d6fSRomain Jobredeaux    _attrs.ANDROID_TOOLCHAIN_ATTRS,
236*9e965d6fSRomain Jobredeaux    _attrs.AUTOMATIC_EXEC_GROUPS_ENABLED,
237*9e965d6fSRomain Jobredeaux)
238