1+ load ("@bazel_skylib//rules:common_settings.bzl" , "BuildSettingInfo" )
12load ("//python/private:toolchain_types.bzl" , "TARGET_TOOLCHAIN_TYPE" )
23load (":pep508_evaluate.bzl" , "evaluate" )
34
5+ def depspec_flag (** kwargs ):
6+ pypa_dependency_specification (
7+ # todo: copied from pep508_env.bzl
8+ os_name = select ({
9+ # The "java" value is documented, but with Jython defunct,
10+ # shouldn't occur in practice.
11+ # The osname value is technically a property of the runtime, not the
12+ # targetted OS at runtime, but the distinction shouldn't matter in
13+ # practice.
14+ "@platforms//os:windows" : "nt" ,
15+ "//conditions:default" : "posix" ,
16+ }),
17+ # todo: copied from pep508_env.bzl
18+ sys_platform = select ({
19+ "@platforms//os:windows" : "win32" ,
20+ "@platforms//os:linux" : "linux" ,
21+ "@platforms//os:osx" : "darwin" ,
22+ # todo: what does spec say unknown value is?
23+ "//conditions:default" : "" ,
24+ }),
25+ # todo: copied from pep508_env.bzl
26+ # todo: pep508_env and evaluate have an "aliases" thing that needs
27+ # to be incorporated
28+ # todo: there are many more cpus. Unfortunately, it doesn't look like
29+ # the value is directly accessible to starlark. It might be possible to
30+ # get it via CcToolchain.cpu though.
31+ platform_machine = select ({
32+ "@platforms//cpu:x86_64" : "x86_64" ,
33+ "@platforms//cpu:aarch64" : "aarch64" ,
34+ # todo: what does spec say unknown value is?
35+ "//conditions:default" : "" ,
36+ }),
37+ # todo: copied from pep508_env.bzl
38+ platform_system = select ({
39+ "@platforms//os:windows" : "Windows" ,
40+ "@platforms//os:linux" : "Linux" ,
41+ "@platforms//os:osx" : "Darwin" ,
42+ # todo: what does spec say unknown value is?
43+ "//conditions:default" : "" ,
44+ }),
45+ ** kwargs
46+ )
47+
448# todo: maybe put all the env into a single target and have a
549# PyPiEnvMarkersInfo provider? Have --pypi_env=//some:target?
650def _impl (ctx ):
@@ -15,8 +59,8 @@ def _impl(ctx):
1559 env ["python_full_version" ] = full_version
1660 env ["implementation_version" ] = full_version
1761 else :
18- env ["python_version" ] = get_flag (ctx .attr ._python_version )
19- full_version = get_flag (ctx .attr ._python_full_version )
62+ env ["python_version" ] = _get_flag (ctx .attr ._python_version )
63+ full_version = _get_flag (ctx .attr ._python_full_version )
2064 env ["python_full_version" ] = full_version
2165 env ["implementation_version" ] = full_version
2266
@@ -40,76 +84,16 @@ def _impl(ctx):
4084 env ["platform_system" ] = ctx .attr .platform_system
4185 env ["platform_version" ] = ctx .attr ._platform_version_config_flag [BuildSettingInfo ].value
4286
43- if evalute (ctx .attr .expression , env ):
87+ if evaluate (ctx .attr .expression , env ):
4488 value = "yes"
4589 else :
4690 value = "no"
4791 return [config_common .FeatureFlagInfo (value = value )]
4892
49- # Adapted from spec code at:
50- # https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers
51- def format_full_info (info ):
52- kind = info .releaselevel
53- if kind == "final" :
54- kind = ""
55- serial = ""
56- else :
57- kind = kind [0 ] if kind else ""
58- serial = str (info .serial ) if info .serial else ""
59-
60- return "{v.major}.{v.minor}.{v.micro}{kind}{serial}" .format (
61- v = version_info ,
62- kind = kind ,
63- serial = serial ,
64- )
65- return version
66-
67- def pypa_dep_spec (** kwargs ):
68- pypa_dependency_specification (
69- # todo: copied from pep508_env.bzl
70- os_name = select ({
71- # The "java" value is documented, but with Jython defunct,
72- # shouldn't occur in practice.
73- # The osname value is technically a property of the runtime, not the
74- # targetted OS at runtime, but the distinction shouldn't matter in
75- # practice.
76- "@//platforms/os:windows" : "nt" ,
77- "//conditions:default" : "posix" ,
78- }),
79- # todo: copied from pep508_env.bzl
80- sys_platform = select ({
81- "@//platforms/os:windows" : "win32" ,
82- "@//platforms/os:linux" : "linux" ,
83- "@//platforms/os:osx" : "darwin" ,
84- # todo: what does spec say unknown value is?
85- "//conditions:default" : "" ,
86- }),
87- # todo: copied from pep508_env.bzl
88- # todo: pep508_env and evaluate have an "aliases" thing that needs
89- # to be incorporated
90- # todo: there are many more cpus. Unfortunately, it doesn't look like
91- # the value is directly accessible to starlark. It might be possible to
92- # get it via CcToolchain.cpu though.
93- platform_machine = select ({
94- "@platforms//cpu:x86_64" : "x86_64" ,
95- "@platforms//cpu:aarch64" : "aarch64" ,
96- # todo: what does spec say unknown value is?
97- "//conditions:default" : "" ,
98- }),
99- # todo: copied from pep508_env.bzl
100- platform_system = select ({
101- "@//platforms/os:windows" : "Windows" ,
102- "@//platforms/os:linux" : "Linux" ,
103- "@//platforms/os:osx" : "Darwin" ,
104- # todo: what does spec say unknown value is?
105- "//conditions:default" : "" ,
106- }),
107- )
108-
10993pypa_dependency_specification = rule (
11094 implementation = _impl ,
11195 attrs = {
112- "expression" : attt .string (),
96+ "expression" : attr .string (),
11397 "os_name" : attr .string (),
11498 "sys_platform" : attr .string (),
11599 "platform_machine" : attr .string (),
@@ -121,7 +105,7 @@ pypa_dependency_specification = rule(
121105 default = "//python/config_settings:pip_platform_version_config" ,
122106 ),
123107 "_python_version_flag" : attr .label (
124- default = "//python/config_settings:_python_version_major_minor " ,
108+ default = "//python/config_settings:python_version_major_minor " ,
125109 ),
126110 "_python_full_version_flag" : attr .label (
127111 default = "//python/config_settings:python_version" ,
@@ -133,6 +117,23 @@ pypa_dependency_specification = rule(
133117 ],
134118)
135119
120+ # Adapted from spec code at:
121+ # https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers
122+ def format_full_version (info ):
123+ kind = info .releaselevel
124+ if kind == "final" :
125+ kind = ""
126+ serial = ""
127+ else :
128+ kind = kind [0 ] if kind else ""
129+ serial = str (info .serial ) if info .serial else ""
130+
131+ return "{v.major}.{v.minor}.{v.micro}{kind}{serial}" .format (
132+ v = info ,
133+ kind = kind ,
134+ serial = serial ,
135+ )
136+
136137def _get_flag (t ):
137138 if config_common .FeatureFlagInfo in t :
138139 return t [config_common .FeatureFlagInfo ].value
0 commit comments