xref: /aosp_15_r20/external/bazelbuild-rules_android/rules/android_local_test/attrs.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1# Copyright 2020 The Bazel Authors. 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"""Attributes."""
16
17load(
18    "//rules:attrs.bzl",
19    _attrs = "attrs",
20)
21
22def make_attrs(additional_aspects = [], native_libs_transition = None):
23    return _attrs.add(
24        dict(
25            deps = attr.label_list(
26                allow_files = True,
27                allow_rules = [
28                    "aar_import",
29                    "android_library",
30                    "java_import",
31                    "java_library",
32                    "java_lite_proto_library",
33                ],
34                aspects = additional_aspects,
35                providers = [[CcInfo], [JavaInfo]],
36                doc = """
37                The list of libraries to be tested as well as additional libraries to be linked
38                in to the target.
39                All resources, assets and manifest files declared in Android rules in the transitive
40                closure of this attribute are made available in the test.
41
42                The list of allowed rules in `deps` are `aar_import`,
43                `android_library`, `java_import`, `java_library`,
44                and `java_lite_proto_library`.
45                """,
46                cfg = native_libs_transition,
47            ),
48            feature_flags = attr.label_keyed_string_dict(
49                doc = "This is a deprecated feature. Do not use it.",
50            ),
51            jvm_flags = attr.string_list(
52                doc = """
53                A list of flags to embed in the wrapper script generated for running this binary.
54                Subject to [$(location / execpath / rootpath)](https://docs.bazel.build/versions/main/be/make-variables.html#predefined_label_variables) and
55                ["Make variable"](https://docs.bazel.build/versions/main/be/make-variables.html) substitution, and
56                [Bourne shell tokenization](https://docs.bazel.build/versions/main/be/common-definitions.html#sh-tokenization).
57
58                The wrapper script for a Java binary includes a CLASSPATH definition
59                (to find all the dependent jars) and invokes the right Java interpreter.
60                The command line generated by the wrapper script includes the name of
61                the main class followed by a `"$@"` so you can pass along other
62                arguments after the classname.  However, arguments intended for parsing
63                by the JVM must be specified _before_ the classname on the command
64                line.  The contents of `jvm_flags` are added to the wrapper
65                script before the classname is listed.
66
67                Note that this attribute has _no effect_ on `*_deploy.jar`
68                outputs.
69                """,
70            ),
71            manifest_values = attr.string_dict(
72                doc = """
73                A dictionary of values to be overridden in the manifest. Any instance of ${name} in the
74                manifest will be replaced with the value corresponding to name in this dictionary.
75                `applicationId`, `versionCode`, `versionName`,
76                `minSdkVersion`, `targetSdkVersion` and
77                `maxSdkVersion` will also override the corresponding attributes
78                of the manifest and
79                uses-sdk tags. `packageName` will be ignored and will be set from either
80                `applicationId` if
81                specified or the package in the manifest.
82                It is not necessary to have a manifest on the rule in order to use manifest_values.
83                """,
84            ),
85            nocompress_extensions = attr.string_list(
86                doc = "A list of file extensions to leave uncompressed in the resource apk.",
87            ),
88            resources = attr.label_list(
89                allow_files = True,
90                doc = """
91                A list of data files to include in a Java jar.
92
93                If resources are specified, they will be bundled in the jar along with the usual
94                `.class` files produced by compilation. The location of the resources inside
95                of the jar file is determined by the project structure. Bazel first looks for Maven's
96                [standard directory layout](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html),
97                (a "src" directory followed by a "resources" directory grandchild). If that is not
98                found, Bazel then looks for the topmost directory named "java" or "javatests" (so, for
99                example, if a resource is at `<workspace root>/x/java/y/java/z`, the
100                path of the resource will be `y/java/z`. This heuristic cannot be overridden.
101
102                Resources may be source files or generated files.
103
104                """,
105            ),
106            runtime_deps = attr.label_list(
107                allow_files = True,
108                doc = """
109                Libraries to make available to the final binary or test at runtime only.
110                Like ordinary `deps`, these will appear on the runtime classpath, but unlike
111                them, not on the compile-time classpath. Dependencies needed only at runtime should be
112                listed here. Dependency-analysis tools should ignore targets that appear in both
113                `runtime_deps` and `deps`.
114                """,
115                # TODO(timpeut): verify we can require JavaInfo
116                # providers = [JavaInfo],
117                cfg = native_libs_transition,
118            ),
119            srcs = attr.label_list(
120                # TODO(timpeut): order independent
121                # TODO(timpeut): direct compile time input
122                allow_files = [".java", ".srcjar", ".properties", ".xmb"],
123                doc = """
124                The list of source files that are processed to create the target.
125                Required except in special case described below.
126
127                `srcs` files of type `.java` are compiled.
128                _For readability's sake_, it is not good to put the name of a
129                generated `.java` source file into the `srcs`.
130                Instead, put the depended-on rule name in the `srcs`, as
131                described below.
132
133                `srcs` files of type `.srcjar` are unpacked and
134                compiled. (This is useful if you need to generate a set of .java files with
135                a genrule or build extension.)
136
137                All other files are ignored, as long as
138                there is at least one file of a file type described above. Otherwise an
139                error is raised.
140
141                The `srcs` attribute is required and cannot be empty, unless
142                `runtime_deps` is specified.
143                """,
144            ),
145            stamp = _attrs.tristate.create(
146                default = _attrs.tristate.no,
147                doc = """
148                Whether to encode build information into the binary. Possible values:
149
150                - `stamp = 1`: Always stamp the build information into the binary, even in
151                  [`--nostamp`](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. **This
152                  setting should be avoided**, since it potentially kills remote caching for the
153                  binary and any downstream actions that depend on it.
154
155                - `stamp = 0`: Always replace build information by constant values. This
156                  gives good build result caching.
157
158                - `stamp = -1`: Embedding of build information is controlled by the
159                  [`--[no]stamp`](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
160
161                Stamped binaries are _not_ rebuilt unless their dependencies change.
162                """,
163            ),
164            resource_configuration_filters = attr.string_list(
165                doc = "A list of resource configuration filters, such as 'en' " +
166                      "that will limit the resources in the apk to only the " +
167                      "ones in the 'en' configuration.",
168            ),
169            densities = attr.string_list(
170                doc = "Densities to filter for when building the apk. A " +
171                      "corresponding compatible-screens section will also be " +
172                      "added to the manifest if it does not already contain a " +
173                      "superset listing.",
174            ),
175            robolectric_properties_file = attr.string(
176                doc = "The classpath to robolectric-deps.properties file.",
177                default = "${JAVA_RUNFILES}/robolectric/bazel/robolectric-deps.properties",
178            ),
179            test_class = attr.string(
180                doc = """
181                The Java class to be loaded by the test runner.
182
183                This attribute specifies the name of a Java class to be run by
184                this test. It is rare to need to set this. If this argument is omitted, the Java class
185                whose name corresponds to the `name` of this
186                `android_local_test` rule will be used.
187                The test class needs to be annotated with `org.junit.runner.RunWith`.
188                """,
189            ),
190            _runfiles_root_prefix = attr.label(
191                doc = """
192                A directory prefix that ends with a slash.
193
194                This attribute is appended to ${JAVA_RUNFILES} when the root of path to the runfile
195                resources is not directly under ${JAVA_RUNFILES}.
196                """,
197                default = "//rules/flags:runfiles_root_prefix",
198            ),
199            _flags = attr.label(
200                default = "//rules/flags",
201            ),
202            _java_toolchain = attr.label(
203                default = Label("//tools/jdk:current_java_toolchain"),
204            ),
205            _current_java_runtime = attr.label(
206                default = Label("//tools/jdk:current_java_runtime"),
207                providers = [java_common.JavaRuntimeInfo],
208            ),
209            _implicit_classpath = attr.label_list(
210                default = [
211                    Label("//tools/android:android_jar"),
212                ],
213            ),
214        ),
215        _attrs.COMPILATION,
216        _attrs.DATA_CONTEXT,
217        _attrs.AUTOMATIC_EXEC_GROUPS_ENABLED,
218    )
219
220ATTRS = make_attrs()
221