8000 internal: copy Starlark rule implementation from Bazel (#1418) · geaden/rules_python@3a57e4a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a57e4a

Browse files
authored
internal: copy Starlark rule implementation from Bazel (bazel-contrib#1418)
This is a copy of the Starlark implementation of the Python rules from Bazel. This code isn't loaded and won't work as-is. Modifications to make it work will be made in subsequent changes. It's almost pristine; changes are made to satisfy the buildifier check. Work towards bazel-contrib#1069
1 parent abc3c9f commit 3a57e4a

17 files changed

+3018
-0
lines changed

python/private/common/attributes.bzl

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
# Copyright 2022 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+
"""Attributes for Python rules."""
15+
16+
load(":common/cc/cc_info.bzl", _CcInfo = "CcInfo")
17+
load(":common/python/common.bzl", "union_attrs")
18+
load(":common/python/providers.bzl", "PyInfo")
19+
load(
20+
":common/python/semantics.bzl",
21+
"DEPS_ATTR_ALLOW_RULES",
22+
"PLATFORMS_LOCATION",
23+
"SRCS_ATTR_ALLOW_FILES",
24+
"TOOLS_REPO",
25+
)
26+
27+
PackageSpecificationInfo = _builtins.toplevel.PackageSpecificationInfo
28+
29+
_STAMP_VALUES = [-1, 0, 1]
30+
31+
def create_stamp_attr(**kwargs):
32+
return {"stamp": attr.int(values = _STAMP_VALUES, **kwargs)}
33+
34+
def create_srcs_attr(*, mandatory):
35+
return {
36+
"srcs": attr.label_list(
37+
# Google builds change the set of allowed files.
38+
allow_files = SRCS_ATTR_ALLOW_FILES,
39+
mandatory = mandatory,
40+
# Necessary for --compile_one_dependency to work.
41+
flags = ["DIRECT_COMPILE_TIME_INPUT"],
42+
),
43+
}
44+
45+
SRCS_VERSION_ALL_VALUES = ["PY2", "PY2ONLY", "PY2AND3", "PY3", "PY3ONLY"]
46+
SRCS_VERSION_NON_CONVERSION_VALUES = ["PY2AND3", "PY2ONLY", "PY3ONLY"]
47+
48+
def create_srcs_version_attr(values):
49+
return {
50+
"srcs_version": attr.string(
51+
default = "PY2AND3",
52+
values = values,
53+
),
54+
}
55+
56+
def copy_common_binary_kwargs(kwargs):
57+
return {
58+
key: kwargs[key]
59+
for key in BINARY_ATTR_NAMES
60+
if key in kwargs
61+
}
62+
63+
def copy_common_test_kwargs(kwargs):
64+
return {
65+
key: kwargs[key]
66+
for key in TEST_ATTR_NAMES
67+
if key in kwargs
68+
}
69+
70+
CC_TOOLCHAIN = {
71+
# NOTE: The `cc_helper.find_cpp_toolchain()` function expects the attribute
72+
# name to be this name.
73+
"_cc_toolchain": attr.label(default = "@" + TOOLS_REPO + "//tools/cpp:current_cc_toolchain"),
74+
}
75+
76+
# The common "data" attribute definition.
77+
DATA_ATTRS = {
78+
# NOTE: The "flags" attribute is deprecated, but there isn't an alternative
79+
# way to specify that constraints should be ignored.
80+
"data": attr.label_list(
81+
allow_files = True,
82+
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
83+
),
84+
}
85+
86+
NATIVE_RULES_ALLOWLIST_ATTRS = {
87+
"_native_rules_allowlist": attr.label(
88+
default = configuration_field(
89+
fragment = "py",
90+
name = "native_rules_allowlist",
91+
),
92+
providers = [PackageSpecificationInfo],
93+
),
94+
}
95+
96+
# Attributes common to all rules.
97+
COMMON_ATTRS = union_attrs(
98+
DATA_ATTRS,
99+
NATIVE_RULES_ALLOWLIST_ATTRS,
100+
{
101+
# NOTE: This attribute is deprecated and slated for removal.
102+
"distribs": attr.string_list(),
103+
# TODO(b/148103851): This attribute is deprecated and slated for
104+
# removal.
105+
# NOTE: The license attribute is missing in some Java integration tests,
106+
# so fallback to a regular string_list for that case.
107+
# buildifier: disable=attr-license
108+
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
109+
},
110+
allow_none = True,
111+
)
112+
113+
# Attributes common to rules accepting Python sources and deps.
114+
PY_SRCS_ATTRS = union_attrs(
115+
{
116+
"deps": attr.label_list(
117+
providers = [[PyInfo], [_CcInfo]],
118+
# TODO(b/228692666): Google-specific; remove these allowances once
119+
# the depot is cleaned up.
120+
allow_rules = DEPS_ATTR_ALLOW_RULES,
121+
),
122+
# Required attribute, but details vary by rule.
123+
# Use create_srcs_attr to create one.
124+
"srcs": None,
125+
# NOTE: In Google, this attribute is deprecated, and can only
126+
# effectively be PY3 or PY3ONLY. Externally, with Bazel, this attribute
127+
# has a separate story.
128+
# Required attribute, but the details vary by rule.
129+
# Use create_srcs_version_attr to create one.
130+
"srcs_version": None,
131+
},
132+
allow_none = True,
133+
)
134+
135+
# Attributes specific to Python executable-equivalent rules. Such rules may not
136+
# accept Python sources (e.g. some packaged-version of a py_test/py_binary), but
137+
# still accept Python source-agnostic settings.
138+
AGNOSTIC_EXECUTABLE_ATTRS = union_attrs(
139+
DATA_ATTRS,
140+
{
141+
"env": attr.string_dict(
142+
doc = """\
143+
Dictionary of strings; optional; values are subject to `$(location)` and "Make
144+
variable" substitution.
145+
146+
Specifies additional environment variables to set when the target is executed by
147+
`test` or `run`.
148+
""",
149+
),
150+
# The value is required, but varies by rule and/or rule type. Use
151+
# create_stamp_attr to create one.
152+
"stamp": None,
153+
},
154+
allow_none = True,
155+
)
156+
157+
# Attributes specific to Python test-equivalent executable rules. Such rules may
158+
# not accept Python sources (e.g. some packaged-version of a py_test/py_binary),
159+
# but still accept Python source-agnostic settings.
160+
AGNOSTIC_TEST_ATTRS = union_attrs(
161+
AGNOSTIC_EXECUTABLE_ATTRS,
162+
# Tests have stamping disabled by default.
163+
create_stamp_attr(default = 0),
164+
{
165+
"env_inherit": attr.string_list(
166+
doc = """\
167+
List of strings; optional
168+
169+
Specifies additional environment variables to inherit from the external
170+
environment when the test is executed by bazel test.
171+
""",
172+
),
173+
# TODO(b/176993122): Remove when Bazel automatically knows to run on darwin.
174+
"_apple_constraints": attr.label_list(
175+
default = [
176+
PLATFORMS_LOCATION + "/os:ios",
177+
PLATFORMS_LOCATION + "/os:macos",
178+
PLATFORMS_LOCATION + "/os:tvos",
179+
PLATFORMS_LOCATION + "/os:visionos",
180+
PLATFORMS_LOCATION + "/os:watchos",
181+
],
182+
),
183+
},
F41A 184+
)
185+
186+
# Attributes specific to Python binary-equivalent executable rules. Such rules may
187+
# not accept Python sources (e.g. some packaged-version of a py_test/py_binary),
188+
# but still accept Python source-agnostic settings.
189+
AGNOSTIC_BINARY_ATTRS = union_attrs(
190+
AGNOSTIC_EXECUTABLE_ATTRS,
191+
create_stamp_attr(default = -1),
192+
)
193+
194+
# Attribute names common to all Python rules
195+
COMMON_ATTR_NAMES = [
196+
"compatible_with",
197+
"deprecation",
198+
"distribs", # NOTE: Currently common to all rules, but slated for removal
199+
"exec_compatible_with",
200+
"exec_properties",
201+
"features",
202+
"restricted_to",
203+
"tags",
204+
"target_compatible_with",
205+
# NOTE: The testonly attribute requires careful handling: None/unset means
206+
# to use the `package(default_testonly`) value, which isn't observable
207+
# during the loading phase.
208+
"testonly",
209+
"toolchains",
210+
"visibility",
211+
] + COMMON_ATTRS.keys()
212+
213+
# Attribute names common to all test=True rules
214+
TEST_ATTR_NAMES = COMMON_ATTR_NAMES + [
215+
"args",
216+
"size",
217+
"timeout",
218+
"flaky",
219+
"shard_count",
220+
"local",
221+
] + AGNOSTIC_TEST_ATTRS.keys()
222+
223+
# Attribute names common to all executable=True rules
224+
BINARY_ATTR_NAMES = COMMON_ATTR_NAMES + [
225+
"args",
226+
"output_licenses", # NOTE: Common to all rules, but slated for removal
227+
] + AGNOSTIC_BINARY_ATTRS.keys()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2022 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+
"""Attributes specific to the Bazel implementation of the Python rules."""
15+
16+
IMPORTS_ATTRS = {
17+
"imports": attr.string_list(
18+
doc = """
19+
List of import directories to be added to the PYTHONPATH.
20+
21+
Subject to "Make variable" substitution. These import directories will be added
22+
for this rule and all rules that depend on it (note: not the rules this rule
23+
depends on. Each directory will be added to `PYTHONPATH` by `py_binary` rules
24+
that depend on this rule. The strings are repo-runfiles-root relative,
25+
26+
Absolute paths (paths that start with `/`) and paths that references a path
27+
above the execution root are not allowed and will result in an error.
28+
""",
29+
),
30+
}

0 commit comments

Comments
 (0)
0