Skip to content

Commit 07eda23

Browse files
committed
further refactor and use native as a parameter to make it easier to override
1 parent 2df3e18 commit 07eda23

File tree

3 files changed

+68
-70
lines changed

3 files changed

+68
-70
lines changed

python/private/config_settings.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def construct_config_settings(name = None): # buildifier: disable=function-docs
182182
)
183183

184184
native.config_setting(
185-
name = "is_python_default",
185+
name = "is_python_version_unset",
186186
flag_values = {
187187
Label("//python/config_settings:python_version"): "",
188188
},

python/private/pip_config_settings.bzl

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ def pip_config_settings(
8282
target_platforms = [],
8383
name = None,
8484
visibility = None,
85-
alias_rule = None,
86-
config_setting_rule = None):
85+
native = native):
8786
"""Generate all of the pip config settings.
8887
8988
Args:
@@ -100,10 +99,9 @@ def pip_config_settings(
10099
constraint values for each condition.
101100
visibility (list[str], optional): The visibility to be passed to the
102101
exposed labels. All other labels will be private.
103-
alias_rule (rule): The alias rule to use for creating the
104-
objects. Can be overridden for unit tests reasons.
105-
config_setting_rule (rule): The config setting rule to use for creating the
106-
objects. Can be overridden for unit tests reasons.
102+
native (struct): The struct containing alias and config_setting rules
103+
to use for creating the objects. Can be overridden for unit tests
104+
reasons.
107105
"""
108106

109107
glibc_versions = [""] + glibc_versions
@@ -114,38 +112,25 @@ def pip_config_settings(
114112
for t in target_platforms
115113
]
116114

117-
alias_rule = alias_rule or native.alias
118-
config_setting_rule = config_setting_rule or _dist_config_setting
119-
120-
for version in ["default"] + python_versions:
121-
is_python = "is_python_{}".format(version)
122-
alias_rule(
115+
for python_version in [""] + python_versions:
116+
is_python = "is_python_{}".format(python_version or "version_unset")
117+
native.alias(
123118
name = is_python,
124119
actual = Label("//python/config_settings:" + is_python),
125120
visibility = visibility,
126121
)
127122

128-
for os, cpu in target_platforms:
129-
constraint_values = []
130-
suffix = ""
131-
if os:
132-
constraint_values.append("@platforms//os:" + os)
133-
suffix += "_" + os
134-
if cpu:
135-
constraint_values.append("@platforms//cpu:" + cpu)
136-
suffix += "_" + cpu
137-
138-
for python_version in [""] + python_versions:
139-
sdist = "cp{}_sdist".format(python_version) if python_version else "sdist"
140-
config_setting_rule(
141-
name = "is_{}{}".format(sdist, suffix),
142-
python_version = python_version,
143-
flag_values = {_flags.dist: ""},
144-
is_pip_whl = FLAGS.is_pip_whl_no,
145-
constraint_values = constraint_values,
146-
visibility = visibility,
147-
)
148-
_whl_config_settings(
123+
for os, cpu in target_platforms:
124+
constraint_values = []
125+
suffix = ""
126+
if os:
127+
constraint_values.append("@platforms//os:" + os)
128+
suffix += "_" + os
129+
if cpu:
130+
constraint_values.append("@platforms//cpu:" + cpu)
131+
suffix += "_" + cpu
132+
133+
_dist_config_settings(
149134
suffix = suffix,
150135
plat_flag_values = _plat_flag_values(
151136
os = os,
@@ -156,39 +141,45 @@ def pip_config_settings(
156141
),
157142
constraint_values = constraint_values,
158143
python_version = python_version,
159-
is_pip_whl = FLAGS.is_pip_whl_only,
144+
is_python = is_python,
160145
visibility = visibility,
161-
config_setting_rule = config_setting_rule,
146+
native = native,
162147
)
163148

164-
def _whl_config_settings(*, suffix, plat_flag_values, config_setting_rule, **kwargs):
165-
# With the following three we cover different per-version wheels
166-
python_version = kwargs.get("python_version")
167-
py = "cp{}_py".format(python_version) if python_version else "py"
168-
pycp = "cp{}_cp3x".format(python_version) if python_version else "cp3x"
169-
170-
flag_values = {
171-
_flags.dist: "",
172-
}
149+
def _dist_config_settings(*, suffix, plat_flag_values, **kwargs):
150+
flag_values = {_flags.dist: ""}
151+
152+
# First create an sdist, we will be building upon the flag values, which
153+
# will ensure that each sdist config setting is the least specialized of
154+
# all. However, we need at least one flag value to cover the case where we
155+
# have `sdist` for any platform, hence we have a non-empty `flag_values`
156+
# here.
157+
_dist_config_setting(
158+
name = "sdist{}".format(suffix),
159+
flag_values = flag_values,
160+
is_pip_whl = FLAGS.is_pip_whl_no,
161+
**kwargs
162+
)
173163

174-
for name, f in {
175-
"is_{}_none_any{}".format(py, suffix): _flags.whl_py2_py3,
176-
"is_{}3_none_any{}".format(py, suffix): _flags.whl_py3,
177-
"is_{}3_abi3_any{}".format(py, suffix): _flags.whl_py3_abi3,
178-
"is_{}_none_any{}".format(pycp, suffix): _flags.whl_pycp3x,
179-
"is_{}_abi3_any{}".format(pycp, suffix): _flags.whl_pycp3x_abi3,
180-
"is_{}_cp_any{}".format(pycp, suffix): _flags.whl_pycp3x_abicp,
181-
}.items():
164+
for name, f in [
165+
("py_none", _flags.whl_py2_py3),
166+
("py3_none", _flags.whl_py3),
167+
("py3_abi3", _flags.whl_py3_abi3),
168+
("cp3x_none", _flags.whl_pycp3x),
169+
("cp3x_abi3", _flags.whl_pycp3x_abi3),
170+
("cp3x_cp", _flags.whl_pycp3x_abicp),
171+
]:
182172
if f in flag_values:
183173
# This should never happen as all of the different whls should have
184174
# unique flag values.
185175
fail("BUG: the flag {} is attempted to be added twice to the list".format(f))
186176
else:
187177
flag_values[f] = ""
188178

189-
config_setting_rule(
190-
name = name,
179+
_dist_config_setting(
180+
name = "{}_any{}".format(name, suffix),
191181
flag_values = flag_values,
182+
is_pip_whl = FLAGS.is_pip_whl_only,
192183
**kwargs
193184
)
194185

@@ -197,24 +188,25 @@ def _whl_config_settings(*, suffix, plat_flag_values, config_setting_rule, **kwa
197188
for (suffix, flag_values) in plat_flag_values:
198189
flag_values = flag_values | generic_flag_values
199190

200-
for name, f in {
201-
"is_{}_none_{}".format(py, suffix): _flags.whl_plat,
202-
"is_{}3_none_{}".format(py, suffix): _flags.whl_plat_py3,
203-
"is_{}3_abi3_{}".format(py, suffix): _flags.whl_plat_py3_abi3,
204-
"is_{}_none_{}".format(pycp, suffix): _flags.whl_plat_pycp3x,
205-
"is_{}_abi3_{}".format(pycp, suffix): _flags.whl_plat_pycp3x_abi3,
206-
"is_{}_cp_{}".format(pycp, suffix): _flags.whl_plat_pycp3x_abicp,
207-
}.items():
191+
for name, f in [
192+
("py_none", _flags.whl_plat),
193+
("py3_none", _flags.whl_plat_py3),
194+
("py3_abi3", _flags.whl_plat_py3_abi3),
195+
("cp3x_none", _flags.whl_plat_pycp3x),
196+
("cp3x_abi3", _flags.whl_plat_pycp3x_abi3),
197+
("cp3x_cp", _flags.whl_plat_pycp3x_abicp),
198+
]:
208199
if f in flag_values:
209200
# This should never happen as all of the different whls should have
210201
# unique flag values.
211202
fail("BUG: the flag {} is attempted to be added twice to the list".format(f))
212203
else:
213204
flag_values[f] = ""
214205

215-
config_setting_rule(
216-
name = name,
206+
_dist_config_setting(
207+
name = "{}_{}".format(name, suffix),
217208
flag_values = flag_values,
209+
is_pip_whl = FLAGS.is_pip_whl_only,
218210
**kwargs
219211
)
220212

@@ -284,24 +276,28 @@ def _plat_flag_values(os, cpu, osx_versions, glibc_versions, muslc_versions):
284276

285277
return ret
286278

287-
def _dist_config_setting(*, name, is_pip_whl, python_version = "", **kwargs):
279+
def _dist_config_setting(*, name, is_pip_whl, is_python, python_version, native = native, **kwargs):
288280
"""A macro to create a target that matches is_pip_whl_auto and one more value.
289281
290282
Args:
291283
name: The name of the public target.
292284
is_pip_whl: The config setting to match in addition to
293285
`is_pip_whl_auto` when evaluating the config setting.
294-
python_version: The python version to match.
286+
is_python: The python version config_setting to match.
287+
python_version: The python version name.
288+
native (struct): The struct containing alias and config_setting rules
289+
to use for creating the objects. Can be overridden for unit tests
290+
reasons.
295291
**kwargs: The kwargs passed to the config_setting rule. Visibility of
296292
the main alias target is also taken from the kwargs.
297293
"""
294+
name = "is_cp{}_{}".format(python_version, name) if python_version else "is_{}".format(name)
298295
if python_version:
299296
_name = name.replace("is_cp{}".format(python_version), "_is")
300297
else:
301298
_name = "_" + name
302299

303300
# First match by the python version
304-
is_python = ":is_python_" + (python_version or "default")
305301
visibility = kwargs.get("visibility")
306302
native.alias(
307303
name = name,

tests/pip_hub_repository/render_pkg_aliases/render_pkg_aliases_test.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,10 @@ def _test_config_settings_exist(env):
926926
mock_rule = lambda name, **kwargs: available_config_settings.append(name)
927927
pip_config_settings(
928928
python_versions = ["3.11"],
929-
alias_rule = mock_rule,
930-
config_setting_rule = mock_rule,
929+
native = struct(
930+
alias = mock_rule,
931+
config_setting = mock_rule,
932+
),
931933
**kwargs
932934
)
933935

0 commit comments

Comments
 (0)