22
33load ("@bazel_skylib//rules:common_settings.bzl" , "BuildSettingInfo" )
44load ("//python/private:toolchain_types.bzl" , "TARGET_TOOLCHAIN_TYPE" )
5+ load (":env_marker_info.bzl" , "EnvMarkerInfo" )
56load (
67 ":pep508_env.bzl" ,
78 "env_aliases" ,
8- "os_name_select_map" ,
9- "platform_machine_select_map" ,
10- "platform_system_select_map" ,
11- "sys_platform_select_map" ,
129)
1310load (":pep508_evaluate.bzl" , "evaluate" )
1411
@@ -39,69 +36,52 @@ def env_marker_setting(*, name, expression, **kwargs):
3936 _env_marker_setting (
4037 name = name ,
4138 expression = expression ,
42- os_name = select (os_name_select_map ),
43- sys_platform = select (sys_platform_select_map ),
44- platform_machine = select (platform_machine_select_map ),
45- platform_system = select (platform_system_select_map ),
46- platform_release = select ({
47- "@platforms//os:osx" : "USE_OSX_VERSION_FLAG" ,
48- "//conditions:default" : "" ,
49- }),
5039 ** kwargs
5140 )
5241
5342def _env_marker_setting_impl (ctx ):
5443 env = {}
44+ env .update (
45+ ctx .attr ._env_marker_config_flag [EnvMarkerInfo ].env ,
46+ )
5547
5648 runtime = ctx .toolchains [TARGET_TOOLCHAIN_TYPE ].py3_runtime
57- if runtime .interpreter_version_info :
58- version_info = runtime .interpreter_version_info
59- env ["python_version" ] = "{major}.{minor}" .format (
60- major = version_info .major ,
61- minor = version_info .minor ,
62- )
63- full_version = _format_full_version (version_info )
64- env ["python_full_version" ] = full_version
65- env ["implementation_version" ] = full_version
66- else :
67- env ["python_version" ] = _get_flag (ctx .attr ._python_version_major_minor_flag )
68- full_version = _get_flag (ctx .attr ._python_full_version_flag )
69- env ["python_full_version" ] = full_version
70- env ["implementation_version" ] = full_version
71-
72- # We assume cpython if the toolchain doesn't specify because it's most
73- # likely to be true.
74- env ["implementation_name" ] = runtime .implementation_name or "cpython"
75- env ["os_name" ] = ctx .attr .os_name
76- env ["sys_platform" ] = ctx .attr .sys_platform
77- env ["platform_machine" ] = ctx .attr .platform_machine
78-
79- # The `platform_python_implementation` marker value is supposed to come
80- # from `platform.python_implementation()`, however, PEP 421 introduced
81- # `sys.implementation.name` and the `implementation_name` env marker to
82- # replace it. Per the platform.python_implementation docs, there's now
83- # essentially just two possible "registered" values: CPython or PyPy.
84- # Rather than add a field to the toolchain, we just special case the value
85- # from `sys.implementation.name` to handle the two documented values.
86- platform_python_impl = runtime .implementation_name
87- if platform_python_impl == "cpython" :
88- platform_python_impl = "CPython"
89- elif platform_python_impl == "pypy" :
90- platform_python_impl = "PyPy"
91- env ["platform_python_implementation" ] = platform_python_impl
92-
93- # NOTE: Platform release for Android will be Android version:
94- # https://peps.python.org/pep-0738/#platform
95- # Similar for iOS:
96- # https://peps.python.org/pep-0730/#platform
97- platform_release = ctx .attr .platform_release
98- if platform_release == "USE_OSX_VERSION_FLAG" :
99- platform_release = _get_flag (ctx .attr ._pip_whl_osx_version_flag )
100- env ["platform_release" ] = platform_release
101- env ["platform_system" ] = ctx .attr .platform_system
102-
103- # For lack of a better option, just use an empty string for now.
104- env ["platform_version" ] = ""
49+
50+ if "python_version" not in env :
51+ if runtime .interpreter_version_info :
52+ version_info = runtime .interpreter_version_info
53+ env ["python_version" ] = "{major}.{minor}" .format (
54+ major = version_info .major ,
55+ minor = version_info .minor ,
56+ )
57+ full_version = _format_full_version (version_info )
58+ env ["python_full_version" ] = full_version
59+ env ["implementation_version" ] = full_version
60+ else :
61+ env ["python_version" ] = _get_flag (ctx .attr ._python_version_major_minor_flag )
62+ full_version = _get_flag (ctx .attr ._python_full_version_flag )
63+ env ["python_full_version" ] = full_version
64+ env ["implementation_version" ] = full_version
65+
66+ if "implementation_name" not in env :
67+ # We assume cpython if the toolchain doesn't specify because it's most
68+ # likely to be true.
69+ env ["implementation_name" ] = runtime .implementation_name or "cpython"
70+
71+ if "platform_python_implementation" not in env :
72+ # The `platform_python_implementation` marker value is supposed to come
73+ # from `platform.python_implementation()`, however, PEP 421 introduced
74+ # `sys.implementation.name` and the `implementation_name` env marker to
75+ # replace it. Per the platform.python_implementation docs, there's now
76+ # essentially just two possible "registered" values: CPython or PyPy.
77+ # Rather than add a field to the toolchain, we just special case the value
78+ # from `sys.implementation.name` to handle the two documented values.
79+ platform_python_impl = runtime .implementation_name
80+ if platform_python_impl == "cpython" :
81+ platform_python_impl = "CPython"
82+ elif platform_python_impl == "pypy" :
83+ platform_python_impl = "PyPy"
84+ env ["platform_python_implementation" ] = platform_python_impl
10585
10686 env .update (env_aliases ())
10787
@@ -125,14 +105,9 @@ for the specification of behavior.
125105 mandatory = True ,
126106 doc = "Environment marker expression to evaluate." ,
127107 ),
128- "os_name" : attr .string (),
129- "platform_machine" : attr .string (),
130- "platform_release" : attr .string (),
131- "platform_system" : attr .string (),
132- "sys_platform" : attr .string (),
133- "_pip_whl_osx_version_flag" : attr .label (
134- default = "//python/config_settings:pip_whl_osx_version" ,
135- providers = [[BuildSettingInfo ], [config_common .FeatureFlagInfo ]],
108+ "_env_marker_config_flag" : attr .label (
109+ default = "//python/config_settings:pip_env_marker_config" ,
110+ providers = [EnvMarkerInfo ],
136111 ),
137112 "_python_full_version_flag" : attr .label (
138113 default = "//python/config_settings:python_version" ,
0 commit comments