22
33load ("@bazel_skylib//rules:common_settings.bzl" , "BuildSettingInfo" )
44load ("//python/private:toolchain_types.bzl" , "TARGET_TOOLCHAIN_TYPE" )
5- load (
6- ":pep508_env.bzl" ,
7- "env_aliases" ,
8- "os_name_select_map" ,
9- "platform_machine_select_map" ,
10- "platform_system_select_map" ,
11- "sys_platform_select_map" ,
12- )
5+ load (":env_marker_info.bzl" , "EnvMarkerInfo" )
6+ load (":pep508_env.bzl" , "create_env" , "set_missing_env_defaults" )
137load (":pep508_evaluate.bzl" , "evaluate" )
148
159# Use capitals to hint its not an actual boolean type.
@@ -39,72 +33,37 @@ def env_marker_setting(*, name, expression, **kwargs):
3933 _env_marker_setting (
4034 name = name ,
4135 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- }),
5036 ** kwargs
5137 )
5238
5339def _env_marker_setting_impl (ctx ):
54- env = {}
40+ env = create_env ()
41+ env .update (
42+ ctx .attr ._env_marker_config_flag [EnvMarkerInfo ].env ,
43+ )
5544
5645 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" ] = ""
105-
106- env .update (env_aliases ())
10746
47+ if "python_version" not in env :
48+ if runtime .interpreter_version_info :
49+ version_info = runtime .interpreter_version_info
50+ env ["python_version" ] = "{major}.{minor}" .format (
51+ major = version_info .major ,
52+ minor = version_info .minor ,
53+ )
54+ full_version = _format_full_version (version_info )
55+ env ["python_full_version" ] = full_version
56+ env ["implementation_version" ] = full_version
57+ else :
58+ env ["python_version" ] = _get_flag (ctx .attr ._python_version_major_minor_flag )
59+ full_version = _get_flag (ctx .attr ._python_full_version_flag )
60+ env ["python_full_version" ] = full_version
61+ env ["implementation_version" ] = full_version
62+
63+ if "implementation_name" not in env and runtime .implementation_name :
64+ env ["implementation_name" ] = runtime .implementation_name
65+
66+ set_missing_env_defaults (env )
10867 if evaluate (ctx .attr .expression , env = env ):
10968 value = _ENV_MARKER_TRUE
11069 else :
@@ -125,14 +84,9 @@ for the specification of behavior.
12584 mandatory = True ,
12685 doc = "Environment marker expression to evaluate." ,
12786 ),
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 ]],
87+ "_env_marker_config_flag" : attr .label (
88+ default = "//python/config_settings:pip_env_marker_config" ,
89+ providers = [EnvMarkerInfo ],
13690 ),
13791 "_python_full_version_flag" : attr .label (
13892 default = "//python/config_settings:python_version" ,
0 commit comments