8000 normalize the platform_machine during evaluation · aignas/rules_python@a17aa55 · GitHub
[go: up one dir, main page]

Skip to content

Commit a17aa55

Browse files
committed
normalize the platform_machine during evaluation
1 parent 0585c0d commit a17aa55

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

python/private/pypi/pep508_env.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
"""This module is for implementing PEP508 environment definition.
1616
"""
1717

18+
# See https://stackoverflow.com/questions/45125516/possible-values-for-uname-m
1819
_platform_machine_values = {
19-
"aarch64": "arm64",
20+
"aarch64": "aarch64",
2021
"ppc": "ppc",
2122
"ppc64le": "ppc64le",
23+
"riscv64": "riscv64",
2224
"s390x": "s390x",
2325
"x86_32": "i386",
2426
"x86_64": "x86_64",
@@ -73,7 +75,7 @@ def env(target_platform, *, extra = None):
7375
arch = target_platform.arch
7476
env = env | {
7577
"os_name": _os_name_values.get(os, ""),
76-
"platform_machine": "aarch64" if (os, arch) == ("linux", "aarch64") else _platform_machine_values.get(arch, ""),
78+
"platform_machine": _platform_machine_values.get(arch, ""),
7779
"platform_system": _platform_system_values.get(os, ""),
7880
"sys_platform": _sys_platform_values.get(os, ""),
7981
}

python/private/pypi/pep508_evaluate.bzl

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ _NON_VERSION_VAR_NAMES = [
5656
_AND = "and"
5757
_OR = "or"
5858
_NOT = "not"
59+
_VALUE_ALIASES = {
60+
"platform_machine": {
61+
# These pairs mean the same hardware, but different values may be used
62+
# on different host platforms.
63+
"amd64": "x86_64",
64+
"arm64": "aarch64",
65+
"i386": "x86_32",
66+
"i686": "x86_32",
67+
},
68+
}
5969

6070
def tokenize(marker):
6171
"""Tokenize the input string.
@@ -123,13 +133,17 @@ def tokenize(marker):
123133

124134
return fail("BUG: failed to process the marker in allocated cycles: {}".format(marker))
125135

126-
def evaluate(marker, *, env, strict = True, **kwargs):
136+
def evaluate(marker, *, env, strict = True, value_aliases = _VALUE_ALIASES, **kwargs):
127137
"""Evaluate the marker against a given env.
128138
129139
Args:
130-
marker: {type}`str`: The string marker to evaluate.
131-
env: {type}`dict`: The environment to evaluate the marker against.
132-
strict: {type}`bool`: A setting to not fail on missing values in the env.
140+
marker: {type}`str` The string marker to evaluate.
141+
env: {type}`dict` The environment to evaluate the marker against.
142+
strict: {type}`bool` A setting to not fail on missing values in the env.
143+
value_aliases: {type}`dict` The value normalization to do for certain
144+
fields to ensure that `aarch64` evaluation in the `platform_machine`
145+
works the same way irrespective if the marker uses `arm64` or
146+
`aarch64` value in the expression.
133147
**kwargs: Extra kwargs to be passed to the expression evaluator.
134148
135149
Returns:
@@ -142,7 +156,7 @@ def evaluate(marker, *, env, strict = True, **kwargs):
142156
if not tokens:
143157
break
144158

145-
tokens = ast.parse(env = env, tokens = tokens, strict = strict)
159+
tokens = ast.parse(env = env, tokens = tokens, strict = strict, value_aliases = value_aliases)
146160

147161
if not tokens:
148162
return ast.value()
@@ -236,7 +250,7 @@ def _new_expr(
236250
)
237251
return self
238252

239-
def _parse(self, *, env, tokens, strict = False):
253+
def _parse(self, *, env, tokens, strict = False, value_aliases = {}):
240254
"""The parse function takes the consumed tokens and returns the remaining."""
241255
token, remaining = tokens[0], tokens[1:]
242256

@@ -251,7 +265,7 @@ def _parse(self, *, env, tokens, strict = False):
251265
elif token == _NOT:
252266
expr = _not_expr(self)
253267
else:
254-
expr = marker_expr(env = env, strict = strict, *tokens[:3])
268+
expr = marker_expr(env = env, strict = strict, value_aliases = value_aliases, *tokens[:3])
255269
remaining = tokens[3:]
256270

257271
_append(self, expr)
@@ -277,7 +291,7 @@ def _value(self):
277291

278292
fail("BUG: invalid state: {}".format(self.tree))
279293

280-
def marker_expr(left, op, right, *, env, strict = True):
294+
def marker_expr(left, op, right, *, env, strict = True, value_aliases = {}):
281295
"""Evaluate a marker expression
282296
283297
Args:
@@ -288,6 +302,7 @@ def marker_expr(left, op, right, *, env, strict = True):
288302
in the environment, otherwise returns the original expression.
289303
env: {type}`dict[str, str]` the `env` to substitute `env` identifiers in
290304
the `<left> <op> <right>` expression.
305+
value_aliases: the value normalization used for certain fields.
291306
292307
Returns:
293308
{type}`bool` if the expression evaluation result or {type}`str` if the expression
@@ -300,11 +315,21 @@ def marker_expr(left, op, right, *, env, strict = True):
300315
var_name = right
301316
right = env[right]
302317
left = left.strip("\"")
318+
319+
# On Windows, Linux, OSX different values may mean the same hardware,
320+
# e.g. Python on Windows returns arm64, but on Linux returns aarch64.
321+
# e.g. Python on Windows returns amd64, but on Linux returns x86_64.
322+
#
323+
# The following normalizes the values
324+
left = value_aliases.get(var_name, {}).get(left, left)
303325
else:
304326
var_name = left
305327
left = env[left]
306328
right = right.strip("\"")
307329

330+
# See the note above on normalization
331+
right = value_aliases.get(var_name, {}).get(right, right)
332+
308333
if var_name in _NON_VERSION_VAR_NAMES:
309334
return _env_expr(left, op, right)
310335
elif var_name.endswith("_version"):

0 commit comments

Comments
 (0)
0