-
-
Notifications
You must be signed in to change notification settings - Fork 574
/
Copy pathpep508_env.bzl
109 lines (96 loc) · 3.37 KB
/
pep508_env.bzl
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
# Copyright 2025 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.
"""This module is for implementing PEP508 environment definition.
"""
_platform_machine_values = {
"aarch64": "arm64",
"ppc": "ppc64le",
"s390x": "s390x",
"x86_32": "i386",
"x86_64": "x86_64",
}
_platform_system_values = {
"linux": "Linux",
"osx": "Darwin",
"windows": "Windows",
}
_sys_platform_values = {
"linux": "posix",
"osx": "darwin",
"windows": "win32",
}
_os_name_values = {
"linux": "posix",
"osx": "posix",
"windows": "nt",
}
def env(target_platform, *, extra = None):
"""Return an env target platform
Args:
target_platform: {type}`str` the target platform identifier, e.g.
`cp33_linux_aarch64`
extra: {type}`str` the extra value to be added into the env.
Returns:
A dict that can be used as `env` in the marker evaluation.
"""
# TODO @aignas 2025-02-13: consider moving this into config settings.
env = {"extra": extra} if extra != None else {}
env = env | {
"implementation_name": "cpython",
"platform_python_implementation": "CPython",
"platform_release": "",
"platform_version": "",
}
if target_platform.abi:
minor_version, _, micro_version = target_platform.abi[3:].partition(".")
micro_version = micro_version or "0"
env = env | {
"implementation_version": "3.{}.{}".format(minor_version, micro_version),
"python_full_version": "3.{}.{}".format(minor_version, micro_version),
"python_version": "3.{}".format(minor_version),
}
if target_platform.os and target_platform.arch:
os = target_platform.os
arch = target_platform.arch
env = env | {
"os_name": _os_name_values.get(os, ""),
"platform_machine": "aarch64" if (os, arch) == ("linux", "aarch64") else _platform_machine_values.get(arch, ""),
"platform_system": _platform_system_values.get(os, ""),
"sys_platform": _sys_platform_values.get(os, ""),
}
# This is split by topic
return env
def _platform(*, abi = None, os = None, arch = None):
return struct(
abi = abi,
os = os,
arch = arch,
)
def platform_from_str(p, python_version):
"""Return a platform from a string.
Args:
p: {type}`str` the actual string.
python_version: {type}`str` the python version to add to platform if needed.
Returns:
A struct that is returned by the `_platform` function.
"""
if p.startswith("cp"):
abi, _, p = p.partition("_")
elif python_version:
major, _, tail = python_version.partition(".")
abi = "cp{}{}".format(major, tail)
else:
abi = None
os, _, arch = p.partition("_")
return _platform(abi = abi, os = os or None, arch = arch or None)