15
15
"""This module is for implementing PEP508 environment definition.
16
16
"""
17
17
18
+ load ("//python/private:version.bzl" , "version" )
19
+
20
+ _DEFAULT = "//conditions:default"
21
+
22
+ # Here we store the aliases in the platform so that the users can specify any valid target in
23
+ # there.
24
+ _cpu_aliases = {
25
+ "arm" : "aarch32" ,
26
+ "arm64" : "aarch64" ,
27
+ }
28
+ _os_aliases = {
29
+ "macos" : "osx" ,
30
+ }
31
+
18
32
# See https://stackoverflow.com/a/45125525
19
33
platform_machine_aliases = {
20
34
# These pairs mean the same hardware, but different values may be used
@@ -59,7 +73,7 @@ platform_machine_select_map = {
59
73
"@platforms//cpu:x86_64" : "x86_64" ,
60
74
# The value is empty string if it cannot be determined:
61
75
# https://docs.python.org/3/library/platform.html#platform.machine
62
- "//conditions:default" : "" ,
76
+ _DEFAULT : "" ,
63
77
}
64
78
65
79
# Platform system returns results from the `uname` call.
@@ -73,17 +87,17 @@ _platform_system_values = {
73
87
"linux" : "Linux" ,
74
88
"netbsd" : "NetBSD" ,
75
89
"openbsd" : "OpenBSD" ,
76
- "osx" : "Darwin" ,
90
+ "osx" : "Darwin" , # NOTE: macos is an alias to osx, we handle it through _os_aliases
77
91
"windows" : "Windows" ,
78
92
}
79
93
80
94
platform_system_select_map = {
81
- "@platforms//os:{}" .format (bazel_os ): py_system
82
- for bazel_os , py_system in _platform_system_values .items ()
95
+ "@platforms//os:{}" .format (os ): value
96
+ for os , value in _platform_system_values .items ()
83
97
} | {
84
98
# The value is empty string if it cannot be determined:
85
99
# https://docs.python.org/3/library/platform.html#platform.machine
86
- "//conditions:default" : "" ,
100
+ _DEFAULT : "" ,
87
101
}
88
102
89
103
# The copy of SO [answer](https://stackoverflow.com/a/13874620) containing
@@ -123,72 +137,78 @@ _sys_platform_values = {
123
137
"ios" : "ios" ,
124
138
"linux" : "linux" ,
125
139
"openbsd" : "openbsd" ,
126
- "osx" : "darwin" ,
140
+ "osx" : "darwin" , # NOTE: macos is an alias to osx, we handle it through _os_aliases
127
141
"wasi" : "wasi" ,
128
142
"windows" : "win32" ,
129
143
}
130
144
131
145
sys_platform_select_map = {
132
- "@platforms//os:{}" .format (bazel_os ): py_platform
133
- for bazel_os , py_platform in _sys_platform_values .items ()
146
+ # These values are decided by the sys.platform docs.
147
+ "@platforms//os:{}" .format (os ): value
148
+ for os , value in _sys_platform_values .items ()
134
149
} | {
135
150
# For lack of a better option, use empty string. No standard doc/spec
136
151
# about sys_platform value.
137
- "//conditions:default" : "" ,
152
+ _DEFAULT : "" ,
138
153
}
139
154
140
155
# The "java" value is documented, but with Jython defunct,
141
156
# shouldn't occur in practice.
142
157
# The os.name value is technically a property of the runtime, not the
143
158
# targetted runtime OS, but the distinction shouldn't matter if
144
159
# things are properly configured.
145
- _os_name_values = {
146
- "linux" : "posix" ,
147
- "osx" : "posix" ,
148
- "windows" : "nt" ,
149
- }
150
-
151
160
os_name_select_map = {
152
- "@platforms//os:{}" .format (bazel_os ): py_os
153
- for bazel_os , py_os in _os_name_values .items ()
154
- } | {
155
- "//conditions:default" : "posix" ,
161
+ "@platforms//os:windows" : "nt" ,
162
+ _DEFAULT : "posix" ,
156
163
}
157
164
158
- def env (target_platform , * , extra = None ):
165
+ def _set_default (env , env_key , m , key ):
166
+ """Set the default value in the env if it is not already set."""
167
+ default = m .get (key , m [_DEFAULT ])
168
+ env .setdefault (env_key , default )
169
+
170
+ def env (* , env = None , os , arch , python_version = "" , extra = None ):
159
171
"""Return an env target platform
160
172
161
173
NOTE: This is for use during the loading phase. For the analysis phase,
162
174
`env_marker_setting()` constructs the env dict.
163
175
164
176
Args:
165
- target_platform: {type}`str` the target platform identifier, e.g.
166
- `cp33_linux_aarch64`
177
+ env: {type}`str` the environment.
178
+ os: {type}`str` the OS name.
179
+ arch: {type}`str` the CPU name.
180
+ python_version: {type}`str` the full python version.
167
181
extra: {type}`str` the extra value to be added into the env.
168
182
169
183
Returns:
170
184
A dict that can be used as `env` in the marker evaluation.
171
185
"""
172
- env = create_env ()
186
+ env = env or {}
187
+ env = env | create_env ()
173
188
if extra != None :
174
189
env ["extra" ] = extra
175
190
176
- if target_platform .abi :
177
- minor_version , _ , micro_version = target_platform .abi [3 :].partition ("." )
178
- micro_version = micro_version or "0"
179
- env = env | {
180
- "implementation_version" : "3.{}.{}" .format (minor_version , micro_version ),
181
- "python_full_version" : "3.{}.{}" .format (minor_version , micro_version ),
182
- "python_version" : "3.{}" .format (minor_version ),
183
- }
184
- if target_platform .os and target_platform .arch :
185
- os = target_platform .os
191
+ if python_version :
192
+ v = version .parse (python_version )
193
+ major = v .release [0 ]
194
+ minor = v .release [1 ]
195
+ micro = v .release [2 ] if len (v .release ) > 2 else 0
186
196
env = env | {
187
- "os_name" : _os_name_values .get (os , "" ),
188
- "platform_machine" : target_platform .arch ,
189
- "platform_system" : _platform_system_values .get (os , "" ),
190
- "sys_platform" : _sys_platform_values .get (os , "" ),
197
+ "implementation_version" : "{}.{}.{}" .format (major , minor , micro ),
198
+ "python_full_version" : "{}.{}.{}" .format (major , minor , micro ),
199
+ "python_version" : "{}.{}" .format (major , minor ),
191
200
}
201
+
202
+ if os :
203
+ os = "@platforms//os:{}" .format (_os_aliases .get (os , os ))
204
+ _set_default (env , "os_name" , os_name_select_map , os )
205
+ _set_default (env , "platform_system" , platform_system_select_map , os )
206
+ _set_default (env , "sys_platform" , sys_platform_select_map , os )
207
+
208
+ if arch :
209
+ arch = "@platforms//cpu:{}" .format (_cpu_aliases .get (arch , arch ))
210
+ _set_default (env , "platform_machine" , platform_machine_select_map , arch )
211
+
192
212
set_missing_env_defaults (env )
193
213
194
214
return env
0 commit comments