Skip to content

Commit 52e3202

Browse files
committed
Address most PR comments
1 parent b367461 commit 52e3202

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed

python/config_settings/BUILD.bazel

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ filegroup(
99
visibility = ["//python:__pkg__"],
1010
)
1111

12-
construct_config_settings()
12+
construct_config_settings(
13+
name = "construct_config_settings",
14+
)

python/config_settings/config_settings.bzl

+10-32
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,16 @@
1515
"""This module is used to construct the config settings in the BUILD file in this same package.
1616
"""
1717

18-
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
19-
load("//python/private:config_settings.bzl", _VERSION_FLAG_VALUES = "VERSION_FLAG_VALUES", _is_python_config_setting = "is_python_config_setting")
18+
load(
19+
"//python/private:config_settings.bzl",
20+
_construct_config_settings = "construct_config_settings",
21+
_is_python_config_setting = "is_python_config_setting",
22+
)
2023

21-
VERSION_FLAG_VALUES = _VERSION_FLAG_VALUES
24+
# This is exposed only for cases where the pip hub repo needs to use this rule
25+
# to define hub-repo scoped config_settings for platform specific wheel
26+
# support.
2227
is_python_config_setting = _is_python_config_setting
2328

24-
def construct_config_settings(name = None): # buildifier: disable=function-docstring
25-
string_flag(
26-
name = "python_version",
27-
# TODO: The default here should somehow match the MODULE config. Until
28-
# then, use the empty string to indicate an unknown version. This
29-
# also prevents version-unaware targets from inadvertently matching
30-
# a select condition when they shouldn't.
31-
build_setting_default = "",
32-
values = [""] + VERSION_FLAG_VALUES.keys(),
33-
visibility = ["//visibility:public"],
34-
)
35-
36-
for version, matching_versions in VERSION_FLAG_VALUES.items():
37-
match_any = None
38-
if len(matching_versions) > 1:
39-
match_any = [
40-
# Use the internal labels created by this macro in order to handle matching
41-
# 3.8 config value if using the 3.8 version from MINOR_MAPPING with generating
42-
# fewer targets overall.
43-
("_is_python_{}" if len(VERSION_FLAG_VALUES[v]) > 1 else "is_python_{}").format(v)
44-
for v in matching_versions
45-
]
46-
47-
is_python_config_setting(
48-
name = "is_python_{}".format(version),
49-
python_version = version,
50-
match_any = match_any,
51-
visibility = ["//visibility:public"],
52-
)
29+
# This is exposed for usage in rules_python only.
30+
construct_config_settings = _construct_config_settings

python/private/config_settings.bzl

+49-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
load("@bazel_skylib//lib:selects.bzl", "selects")
19+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
1920
load("//python:versions.bzl", "MINOR_MAPPING", "TOOL_VERSIONS")
2021

2122
_PYTHON_VERSION_FLAG = str(Label("//python/config_settings:python_version"))
@@ -36,8 +37,9 @@ def _flag_values(python_versions):
3637
python_versions: list of strings; all X.Y.Z python versions
3738
3839
Returns:
39-
A map with config settings as keys and values are all of the python_version flag
40-
values that that match the given 'key' specification. For example:
40+
A `map[str, list[str]]`. Each key is a python_version flag value. Each value
41+
is a list of the python_version flag values that should match when for the
42+
`key`. For example:
4143
```
4244
"3.8" -> ["3.8", "3.8.1", "3.8.2", ..., "3.8.19"] # All 3.8 versions
4345
"3.8.2" -> ["3.8.2"] # Only 3.8.2
@@ -67,7 +69,7 @@ def _flag_values(python_versions):
6769

6870
VERSION_FLAG_VALUES = _flag_values(TOOL_VERSIONS.keys())
6971

70-
def is_python_config_setting(name, *, python_version = None, match_any = None, **kwargs):
72+
def is_python_config_setting(name, *, python_version, match_any = None, **kwargs):
7173
"""Create a config setting for matching 'python_version' configuration flag.
7274
7375
This function is mainly intended for internal use within the `whl_library` and `pip_parse`
@@ -81,19 +83,19 @@ def is_python_config_setting(name, *, python_version = None, match_any = None, *
8183
either None, which will create config settings necessary to match the `python_version` value,
8284
a list of 'config_setting' labels passed to bazel-skylib's `config_setting_group` `match_any`
8385
attribute.
84-
**kwargs: extra kwargs passed to the `config_setting`
86+
**kwargs: extra kwargs passed to the `config_setting`.
8587
"""
86-
visibility = kwargs.pop("visibility", [])
87-
88-
flag_values = {
89-
_PYTHON_VERSION_FLAG: python_version,
90-
}
9188
if python_version not in name:
9289
fail("The name '{}' must have the python version '{}' in it".format(name, python_version))
9390

9491
if python_version not in VERSION_FLAG_VALUES:
9592
fail("The 'python_version' must be known to 'rules_python', choose from the values: {}".format(VERSION_FLAG_VALUES.keys()))
9693

94+
flag_values = {
95+
_PYTHON_VERSION_FLAG: python_version,
96+
}
97+
visibility = kwargs.pop("visibility", [])
98+
9799
python_versions = VERSION_FLAG_VALUES[python_version]
98100
if len(python_versions) == 1 and not match_any:
99101
native.config_setting(
@@ -141,3 +143,41 @@ def is_python_config_setting(name, *, python_version = None, match_any = None, *
141143
actual = "_{}_group".format(name),
142144
visibility = visibility,
143145
)
146+
147+
def construct_config_settings(name = None): # buildifier: disable=function-docstring
148+
"""Create a 'python_version' config flag and construct all config settings used in rules_python.
149+
150+
This mainly includes the targets that are used in the toolchain and pip hub
151+
repositories that only match on the 'python_version' flag values.
152+
153+
Args:
154+
name(str): A dummy name value that is no-op for now.
155+
"""
156+
string_flag(
157+
name = "python_version",
158+
# TODO: The default here should somehow match the MODULE config. Until
159+
# then, use the empty string to indicate an unknown version. This
160+
# also prevents version-unaware targets from inadvertently matching
161+
# a select condition when they shouldn't.
162+
build_setting_default = "",
163+
values = [""] + VERSION_FLAG_VALUES.keys(),
164+
visibility = ["//visibility:public"],
165+
)
166+
167+
for version, matching_versions in VERSION_FLAG_VALUES.items():
168+
match_any = None
169+
if len(matching_versions) > 1:
170+
match_any = [
171+
# Use the internal labels created by this macro in order to handle matching
172+
# 3.8 config value if using the 3.8 version from MINOR_MAPPING with generating
173+
# fewer targets overall.
174+
("_is_python_{}" if len(VERSION_FLAG_VALUES[v]) > 1 else "is_python_{}").format(v)
175+
for v in matching_versions
176+
]
177+
178+
is_python_config_setting(
179+
name = "is_python_{}".format(version),
180+
python_version = version,
181+
match_any = match_any,
182+
visibility = ["//visibility:public"],
183+
)

0 commit comments

Comments
 (0)