1515"""This module is for implementing PEP508 environment definition.
1616"""
1717
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+
1832# See https://stackoverflow.com/a/45125525
1933platform_machine_aliases = {
2034 # These pairs mean the same hardware, but different values may be used
@@ -59,7 +73,7 @@ platform_machine_select_map = {
5973 "@platforms//cpu:x86_64" : "x86_64" ,
6074 # The value is empty string if it cannot be determined:
6175 # https://docs.python.org/3/library/platform.html#platform.machine
62- "//conditions:default" : "" ,
76+ _DEFAULT : "" ,
6377}
6478
6579# Platform system returns results from the `uname` call.
@@ -73,7 +87,7 @@ _platform_system_values = {
7387 "linux" : "Linux" ,
7488 "netbsd" : "NetBSD" ,
7589 "openbsd" : "OpenBSD" ,
76- "osx" : "Darwin" ,
90+ "osx" : "Darwin" , # NOTE: macos is an alias to osx, we handle it through _os_aliases
7791 "windows" : "Windows" ,
7892}
7993
@@ -83,7 +97,7 @@ platform_system_select_map = {
8397} | {
8498 # The value is empty string if it cannot be determined:
8599 # https://docs.python.org/3/library/platform.html#platform.machine
86- "//conditions:default" : "" ,
100+ _DEFAULT : "" ,
87101}
88102
89103# The copy of SO [answer](https://stackoverflow.com/a/13874620) containing
@@ -123,72 +137,78 @@ _sys_platform_values = {
123137 "ios" : "ios" ,
124138 "linux" : "linux" ,
125139 "openbsd" : "openbsd" ,
126- "osx" : "darwin" ,
140+ "osx" : "darwin" , # NOTE: macos is an alias to osx, we handle it through _os_aliases
127141 "wasi" : "wasi" ,
128142 "windows" : "win32" ,
129143}
130144
131145sys_platform_select_map = {
146+ # These values are decided by the sys.platform docs.
132147 "@platforms//os:{}" .format (bazel_os ): py_platform
133148 for bazel_os , py_platform in _sys_platform_values .items ()
134149} | {
135150 # For lack of a better option, use empty string. No standard doc/spec
136151 # about sys_platform value.
137- "//conditions:default" : "" ,
152+ _DEFAULT : "" ,
138153}
139154
140155# The "java" value is documented, but with Jython defunct,
141156# shouldn't occur in practice.
142157# The os.name value is technically a property of the runtime, not the
143158# targetted runtime OS, but the distinction shouldn't matter if
144159# things are properly configured.
145- _os_name_values = {
146- "linux" : "posix" ,
147- "osx" : "posix" ,
148- "windows" : "nt" ,
149- }
150-
151160os_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" ,
156163}
157164
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 ):
159171 """Return an env target platform
160172
161173 NOTE: This is for use during the loading phase. For the analysis phase,
162174 `env_marker_setting()` constructs the env dict.
163175
164176 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.
167181 extra: {type}`str` the extra value to be added into the env.
168182
169183 Returns:
170184 A dict that can be used as `env` in the marker evaluation.
171185 """
172- env = create_env ()
186+ env = env or {}
187+ env = env | create_env ()
173188 if extra != None :
174189 env ["extra" ] = extra
175190
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
186196 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 ),
191200 }
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+
192212 set_missing_env_defaults (env )
193213
194214 return env
0 commit comments