forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattributes.bzl
More file actions
245 lines (220 loc) · 7.67 KB
/
Copy pathattributes.bzl
File metadata and controls
245 lines (220 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Attributes for Python rules."""
load("//python/private:reexports.bzl", "BuiltinPyInfo")
load(":common.bzl", "union_attrs")
load(":providers.bzl", "PyInfo")
load(":py_internal.bzl", "py_internal")
load(
":semantics.bzl",
"DEPS_ATTR_ALLOW_RULES",
"SRCS_ATTR_ALLOW_FILES",
)
# TODO: Load CcInfo from rules_cc
_CcInfo = CcInfo
_PackageSpecificationInfo = getattr(py_internal, "PackageSpecificationInfo", None)
_STAMP_VALUES = [-1, 0, 1]
def create_stamp_attr(**kwargs):
return {"stamp": attr.int(values = _STAMP_VALUES, **kwargs)}
def create_srcs_attr(*, mandatory):
return {
"srcs": attr.label_list(
# Google builds change the set of allowed files.
allow_files = SRCS_ATTR_ALLOW_FILES,
mandatory = mandatory,
# Necessary for --compile_one_dependency to work.
flags = ["DIRECT_COMPILE_TIME_INPUT"],
),
}
SRCS_VERSION_ALL_VALUES = ["PY2", "PY2ONLY", "PY2AND3", "PY3", "PY3ONLY"]
SRCS_VERSION_NON_CONVERSION_VALUES = ["PY2AND3", "PY2ONLY", "PY3ONLY"]
def create_srcs_version_attr(values):
return {
"srcs_version": attr.string(
default = "PY2AND3",
values = values,
),
}
def copy_common_binary_kwargs(kwargs):
return {
key: kwargs[key]
for key in BINARY_ATTR_NAMES
if key in kwargs
}
def copy_common_test_kwargs(kwargs):
return {
key: kwargs[key]
for key in TEST_ATTR_NAMES
if key in kwargs
}
CC_TOOLCHAIN = {
# NOTE: The `cc_helper.find_cpp_toolchain()` function expects the attribute
# name to be this name.
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
}
# The common "data" attribute definition.
DATA_ATTRS = {
# NOTE: The "flags" attribute is deprecated, but there isn't an alternative
# way to specify that constraints should be ignored.
"data": attr.label_list(
allow_files = True,
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
),
}
def _create_native_rules_allowlist_attrs():
if py_internal:
# The fragment and name are validated when configuration_field is called
default = configuration_field(
fragment = "py",
name = "native_rules_allowlist",
)
# A None provider isn't allowed
providers = [_PackageSpecificationInfo]
else:
default = None
providers = []
return {
"_native_rules_allowlist": attr.label(
default = default,
providers = providers,
),
}
NATIVE_RULES_ALLOWLIST_ATTRS = _create_native_rules_allowlist_attrs()
# Attributes common to all rules.
COMMON_ATTRS = union_attrs(
DATA_ATTRS,
NATIVE_RULES_ALLOWLIST_ATTRS,
{
# NOTE: This attribute is deprecated and slated for removal.
"distribs": attr.string_list(),
# TODO(b/148103851): This attribute is deprecated and slated for
# removal.
# NOTE: The license attribute is missing in some Java integration tests,
# so fallback to a regular string_list for that case.
# buildifier: disable=attr-license
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
},
allow_none = True,
)
# Attributes common to rules accepting Python sources and deps.
PY_SRCS_ATTRS = union_attrs(
{
"deps": attr.label_list(
providers = [
[PyInfo],
[_CcInfo],
[BuiltinPyInfo],
],
# TODO(b/228692666): Google-specific; remove these allowances once
# the depot is cleaned up.
allow_rules = DEPS_ATTR_ALLOW_RULES,
),
# Required attribute, but details vary by rule.
# Use create_srcs_attr to create one.
"srcs": None,
# NOTE: In Google, this attribute is deprecated, and can only
# effectively be PY3 or PY3ONLY. Externally, with Bazel, this attribute
# has a separate story.
# Required attribute, but the details vary by rule.
# Use create_srcs_version_attr to create one.
"srcs_version": None,
},
allow_none = True,
)
# Attributes specific to Python executable-equivalent rules. Such rules may not
# accept Python sources (e.g. some packaged-version of a py_test/py_binary), but
# still accept Python source-agnostic settings.
AGNOSTIC_EXECUTABLE_ATTRS = union_attrs(
DATA_ATTRS,
{
"env": attr.string_dict(
doc = """\
Dictionary of strings; optional; values are subject to `$(location)` and "Make
variable" substitution.
Specifies additional environment variables to set when the target is executed by
`test` or `run`.
""",
),
# The value is required, but varies by rule and/or rule type. Use
# create_stamp_attr to create one.
"stamp": None,
},
allow_none = True,
)
# Attributes specific to Python test-equivalent executable rules. Such rules may
# not accept Python sources (e.g. some packaged-version of a py_test/py_binary),
# but still accept Python source-agnostic settings.
AGNOSTIC_TEST_ATTRS = union_attrs(
AGNOSTIC_EXECUTABLE_ATTRS,
# Tests have stamping disabled by default.
create_stamp_attr(default = 0),
{
"env_inherit": attr.string_list(
doc = """\
List of strings; optional
Specifies additional environment variables to inherit from the external
environment when the test is executed by bazel test.
""",
),
# TODO(b/176993122): Remove when Bazel automatically knows to run on darwin.
"_apple_constraints": attr.label_list(
default = [
"@platforms//os:ios",
"@platforms//os:macos",
"@platforms//os:tvos",
"@platforms//os:visionos",
"@platforms//os:watchos",
],
),
},
)
# Attributes specific to Python binary-equivalent executable rules. Such rules may
# not accept Python sources (e.g. some packaged-version of a py_test/py_binary),
# but still accept Python source-agnostic settings.
AGNOSTIC_BINARY_ATTRS = union_attrs(
AGNOSTIC_EXECUTABLE_ATTRS,
create_stamp_attr(default = -1),
)
# Attribute names common to all Python rules
COMMON_ATTR_NAMES = [
"compatible_with",
"deprecation",
"distribs", # NOTE: Currently common to all rules, but slated for removal
"exec_compatible_with",
"exec_properties",
"features",
"restricted_to",
"tags",
"target_compatible_with",
# NOTE: The testonly attribute requires careful handling: None/unset means
# to use the `package(default_testonly`) value, which isn't observable
# during the loading phase.
"testonly",
"toolchains",
"visibility",
] + COMMON_ATTRS.keys()
# Attribute names common to all test=True rules
TEST_ATTR_NAMES = COMMON_ATTR_NAMES + [
"args",
"size",
"timeout",
"flaky",
"shard_count",
"local",
] + AGNOSTIC_TEST_ATTRS.keys()
# Attribute names common to all executable=True rules
BINARY_ATTR_NAMES = COMMON_ATTR_NAMES + [
"args",
"output_licenses", # NOTE: Common to all rules, but slated for removal
] + AGNOSTIC_BINARY_ATTRS.keys()