Skip to content

Commit e65c973

Browse files
committed
source values from select and flags
1 parent e7ea75b commit e65c973

File tree

3 files changed

+123
-23
lines changed

3 files changed

+123
-23
lines changed

python/config_settings/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,21 @@ string_flag(
220220
define_pypi_internal_flags(
221221
name = "define_pypi_internal_flags",
222222
)
223+
224+
# todo: have single --pypi_env_config flag instead? Under the hood, it
225+
# could be driven more more discrete flags, which could be considered an
226+
# implementation detail of our particular impl.
227+
# A label is used to allow users to use select() to affect the value.
228+
# A target providing BuildSettingInfo is used to allow users to define
229+
# a flag as the target, which lets them CLI-override or transition it easily.
230+
label_flag(
231+
name = "pip_platform_release_config",
232+
build_setting_default = ":_pip_platform_release_default_config",
233+
)
234+
235+
# docs: points to a label that provides BuildSettingInfo for the value.
236+
# A label and BuildSettingInfo are used for the same reason as platform_release
237+
label_flag(
238+
name = "pip_platform_version_config",
239+
build_setting_default = ":_pip_platform_version_default_config",
240+
)

python/private/pypi/dependency_specifier_flag.bzl

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
22
load(":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?
46
def _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+
70101
pypa_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
),

python/private/pypi/flags.bzl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ def define_pypi_internal_flags(name):
8181
name = "_internal_pip_whl",
8282
visibility = ["//visibility:public"],
8383
)
84+
_platform_release_config(
85+
name = "_pip_platform_release_default_config",
86+
value = select({
87+
"//conditions:osx": "USE_OSX_VERSION_FLAG",
88+
"//conditions:default": "",
89+
}),
90+
)
91+
_platform_version_config(
92+
name = "_pip_platform_version_default_config",
93+
value = select({
94+
"//conditions:osx": "USE_OSX_VERSION_FLAG",
95+
"//conditions:default": "",
96+
}),
97+
)
8498

8599
def _allow_wheels_flag_impl(ctx):
86100
input = ctx.attr._setting[BuildSettingInfo].value
@@ -97,3 +111,35 @@ This rule allows us to greatly reduce the number of config setting targets at no
97111
if we are duplicating some of the functionality of the `native.config_setting`.
98112
""",
99113
)
114+
115+
def _platform_release_config_impl(ctx):
116+
value = ctx.attr.value
117+
if value == "USE_OSX_VERSION_FLAG":
118+
value = ctx.attr._osx_version[BuildSettingInfo].value
119+
120+
return [BuildSettingInfo(value = ctx.attr.value)]
121+
122+
# This value is loosely some version-value looking thing, but the format
123+
# varies depending on the OS.
124+
_platform_release_config = rule(
125+
implementation = _platform_release_config_impl,
126+
attrs = {
127+
"value": attr.string(),
128+
"_osx_version": attr.label(
129+
default = "//python/config_settings:pip_whl_osx_version",
130+
),
131+
},
132+
)
133+
134+
def _platform_version_config_impl(ctx):
135+
value = ctx.attr.value
136+
return [BuildSettingInfo(value = ctx.attr.value)]
137+
138+
# Despite its name, this "version" value is not a simple version value.
139+
# It's a more detailed, arbitrary, description the OS gives about itself.
140+
_platform_version_config = rule(
141+
implementation = _platform_version_config_impl,
142+
attrs = {
143+
"value": attr.string(),
144+
},
145+
)

0 commit comments

Comments
 (0)