11load ("//python/private:toolchain_types.bzl" , "TARGET_TOOLCHAIN_TYPE" )
22load (":pep508_evaluate.bzl" , "evaluate" )
33
4+ # todo: maybe put all the env into a single target and have a
5+ # PyPiEnvMarkersInfo provider? Have --pypi_env=//some:target?
46def _impl (ctx ):
7+ # todo: should unify with pep508_env.bzl
58 env = {}
69
710 runtime = ctx .toolchains [TARGET_TOOLCHAIN_TYPE ].py3_runtime
@@ -20,28 +23,16 @@ def _impl(ctx):
2023 # We assume cpython if the toolchain doesn't specify because it's most
2124 # likely to be true.
2225 env ["implementation_name" ] = runtime .implementation_name or "cpython"
23-
24- # todo: map from os constraint
25- env ["os_name" ] = struct ()
26-
27- # todo: map from os constraint
28- env ["sys_platform" ] = struct ()
29-
30- # todo: map from cpu flag (x86_64, etc)
31- env ["platform_machine" ] = struct ()
26+ env ["os_name" ] = ctx .attr .os_name
27+ env ["sys_platform" ] = ctx .attr .sys_platform
28+ env ["platform_machine" ] = ctx .attr .platform_machine
3229
3330 # todo: add PyRuntimeInfo.platform_python_implementation
3431 # The values are slightly different to implementation_name
3532 env ["platform_python_implementation" ] = runtime .implementation_name
36-
37- # todo: add flag to carry this
38- env ["platform_release" ] = struct ()
39-
40- # todo: map from os constraint
41- env ["platform_system" ] = struct ()
42-
43- # todo: add flag to carry this
44- env ["platform_version" ] = struct ()
33+ env ["platform_release" ] = ctx .attr ._platform_release_config_flag [BuildSettingInfo ].value
34+ env ["platform_system" ] = ctx .attr .platform_system
35+ env ["platform_version" ] = ctx .attr ._platform_version_config_flag [BuildSettingInfo ].value
4536
4637 if evalute (ctx .attr .expression , env ):
4738 value = "yes"
@@ -67,15 +58,60 @@ def format_full_info(info):
6758 )
6859 return version
6960
61+ def pypa_dep_spec (** kwargs ):
62+ pypa_dependency_specification (
63+ # todo: copied from pep508_env.bzl
64+ os_name = select ({
65+ # The "java" value is documented, but with Jython defunct,
66+ # shouldn't occur in practice.
67+ # The osname value is technically a property of the runtime, not the
68+ # targetted OS at runtime, but the distinction shouldn't matter in
69+ # practice.
70+ "@//platforms/os:windows" : "nt" ,
71+ "//conditions:default" : "posix" ,
72+ }),
73+ # todo: copied from pep508_env.bzl
74+ sys_platform = select ({
75+ "@//platforms/os:windows" : "win32" ,
76+ "@//platforms/os:linux" : "linux" ,
77+ "@//platforms/os:osx" : "darwin" ,
78+ # todo: what does spec say unknown value is?
79+ "//conditions:default" : "" ,
80+ }),
81+ # todo: copied from pep508_env.bzl
82+ # todo: there are many more cpus. Unfortunately, it doesn't look like
83+ # the value is directly accessible to starlark. It might be possible to
84+ # get it via CcToolchain.cpu though.
85+ platform_machine = select ({
86+ "@platforms//cpu:x86_64" : "x86_64" ,
87+ "@platforms//cpu:aarch64" : "aarch64" ,
88+ # todo: what does spec say unknown value is?
89+ "//conditions:default" : "" ,
90+ }),
91+ # todo: copied from pep508_env.bzl
92+ platform_system = select ({
93+ "@//platforms/os:windows" : "Windows" ,
94+ "@//platforms/os:linux" : "Linux" ,
95+ "@//platforms/os:osx" : "Darwin" ,
96+ # todo: what does spec say unknown value is?
97+ "//conditions:default" : "" ,
98+ }),
99+ )
100+
70101pypa_dependency_specification = rule (
71102 implementation = _impl ,
72103 attrs = {
73104 "expression" : attt .string (),
74- "_os_name" : attr .label (),
75- "_sys_platform_flag" : attr .label (),
76- "_platform_release_flag" : attr .label (),
77- "_platform_system_flag" : attr .label (),
78- "_platform_version_flag" : attr .label (),
105+ "os_name" : attr .string (),
106+ "sys_platform" : attr .string (),
107+ "platform_machine" : attr .string (),
108+ "platform_system" : attr .string (),
109+ "_platform_release_config_flag" : attr .label (
110+ default = "//python/config_settings:pip_platform_release_config" ,
111+ ),
112+ "_platform_version_config_flag" : attr .label (
113+ default = "//python/config_settings:pip_platform_version_config" ,
114+ ),
79115 "_python_version_flag" : attr .label (
80116 default = "//python/config_settings:_python_version_major_minor" ,
81117 ),
0 commit comments