Skip to content

Commit 7858cd8

Browse files
committed
cleanup the config settings implementation and mark the types as internal
1 parent 2783415 commit 7858cd8

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

python/config_settings/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2+
load("//python:versions.bzl", "MINOR_MAPPING", "TOOL_VERSIONS")
23
load(
34
"//python/private:flags.bzl",
45
"BootstrapImplFlag",
@@ -27,6 +28,8 @@ filegroup(
2728

2829
construct_config_settings(
2930
name = "construct_config_settings",
31+
minor_mapping = MINOR_MAPPING,
32+
versions = TOOL_VERSIONS.keys(),
3033
)
3134

3235
string_flag(

python/private/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ bzl_library(
7878
name = "config_settings_bzl",
7979
srcs = ["config_settings.bzl"],
8080
deps = [
81-
"//python:versions_bzl",
81+
":semver_bzl",
8282
"@bazel_skylib//lib:selects",
83+
"@bazel_skylib//rules:common_settings",
8384
],
8485
)
8586

python/private/config_settings.bzl

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,25 @@
1717

1818
load("@bazel_skylib//lib:selects.bzl", "selects")
1919
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
20-
load("//python:versions.bzl", "MINOR_MAPPING", "TOOL_VERSIONS")
2120
load(":semver.bzl", "semver")
2221

2322
_PYTHON_VERSION_FLAG = Label("//python/config_settings:python_version")
2423
_PYTHON_MAJOR_MINOR_VERSION_FLAG = Label("//python/config_settings:_python_major_minor_version")
2524

26-
def construct_config_settings(name = None): # buildifier: disable=function-docstring
25+
def construct_config_settings(*, name, versions, minor_mapping): # buildifier: disable=function-docstring
2726
"""Create a 'python_version' config flag and construct all config settings used in rules_python.
2827
2928
This mainly includes the targets that are used in the toolchain and pip hub
3029
repositories that only match on the 'python_version' flag values.
3130
3231
Args:
33-
name(str): A dummy name value that is no-op for now.
32+
name: {type}`str` A dummy name value that is no-op for now.
33+
versions: {type}`list[str]` A list of versions to build constraint settings for.
34+
minor_mapping: {type}`dict[str, str]` A mapping from `X.Y` to `X.Y.Z` python versions.
3435
"""
36+
_ = name # @unused
3537
_python_version_flag(
36-
name = "python_version",
38+
name = _PYTHON_VERSION_FLAG.name,
3739
# TODO: The default here should somehow match the MODULE config. Until
3840
# then, use the empty string to indicate an unknown version. This
3941
# also prevents version-unaware targets from inadvertently matching
@@ -42,7 +44,7 @@ def construct_config_settings(name = None): # buildifier: disable=function-docs
4244
visibility = ["//visibility:public"],
4345
)
4446

45-
_python_major_minor(
47+
_python_version_major_minor_flag(
4648
name = _PYTHON_MAJOR_MINOR_VERSION_FLAG.name,
4749
build_setting_default = "",
4850
visibility = ["//visibility:public"],
@@ -54,20 +56,12 @@ def construct_config_settings(name = None): # buildifier: disable=function-docs
5456
visibility = ["//visibility:public"],
5557
)
5658

57-
# This matches the raw flag value, e.g. --//python/config_settings:python_version=3.8
58-
# It's private because matching the concept of e.g. "3.8" value is done
59-
# using the `is_python_X.Y` config setting group, which is aware of the
60-
# minor versions that could match instead.
61-
for minor in range(20):
62-
native.config_setting(
63-
name = "is_python_3.{}".format(minor),
64-
flag_values = {_PYTHON_MAJOR_MINOR_VERSION_FLAG: "3.{}".format(minor)},
65-
visibility = ["//visibility:public"],
66-
)
67-
68-
for version in TOOL_VERSIONS.keys():
59+
minor_versions = {}
60+
for version in versions:
6961
minor_version, _, _ = version.rpartition(".")
70-
if MINOR_MAPPING[minor_version] != version:
62+
minor_versions[minor_version] = None
63+
64+
if minor_mapping[minor_version] != version:
7165
native.config_setting(
7266
name = "is_python_{}".format(version),
7367
flag_values = {":python_version": version},
@@ -101,6 +95,17 @@ def construct_config_settings(name = None): # buildifier: disable=function-docs
10195
visibility = ["//visibility:public"],
10296
)
10397

98+
# This matches the raw flag value, e.g. --//python/config_settings:python_version=3.8
99+
# It's private because matching the concept of e.g. "3.8" value is done
100+
# using the `is_python_X.Y` config setting group, which is aware of the
101+
# minor versions that could match instead.
102+
for minor in minor_versions:
103+
native.config_setting(
104+
name = "is_python_{}".format(minor),
105+
flag_values = {_PYTHON_MAJOR_MINOR_VERSION_FLAG: minor},
106+
visibility = ["//visibility:public"],
107+
)
108+
104109
def _python_version_flag_impl(ctx):
105110
value = ctx.build_setting_value
106111
return [
@@ -121,7 +126,7 @@ _python_version_flag = rule(
121126
attrs = {},
122127
)
123128

124-
def _python_major_minor_impl(ctx):
129+
def _python_version_major_minor_flag_impl(ctx):
125130
input = ctx.attr._python_version_flag[config_common.FeatureFlagInfo].value
126131
if input:
127132
version = semver(input)
@@ -131,8 +136,8 @@ def _python_major_minor_impl(ctx):
131136

132137
return [config_common.FeatureFlagInfo(value = value)]
133138

134-
_python_major_minor = rule(
135-
implementation = _python_major_minor_impl,
139+
_python_version_major_minor_flag = rule(
140+
implementation = _python_version_major_minor_flag_impl,
136141
build_setting = config.string(flag = False),
137142
attrs = {
138143
"_python_version_flag": attr.label(

0 commit comments

Comments
 (0)