@@ -78,11 +78,10 @@ def is_python_config_setting(name, *, python_version, match_any = None, **kwargs
78
78
Args:
79
79
name: name for the target that will be created to be used in select statements.
80
80
python_version: The python_version to be passed in the `flag_values` in the `config_setting`.
81
- match_any: The labels that should be used for matching the extra versions instead of creating
82
- them on the fly. This will be passed to `config_setting_group.match_any`. This can be
83
- either None, which will create config settings necessary to match the `python_version` value,
84
- a list of 'config_setting' labels passed to bazel-skylib's `config_setting_group` `match_any`
85
- attribute.
81
+ match_any: The labels that should be used for matching the extra
82
+ versions, which get passed to `config_setting_group.match_any`. If
83
+ empty or None, this macro will create config settings necessary to
84
+ match the `python_version` value.
86
85
**kwargs: extra kwargs passed to the `config_setting`.
87
86
"""
88
87
if python_version not in name :
@@ -96,24 +95,30 @@ def is_python_config_setting(name, *, python_version, match_any = None, **kwargs
96
95
}
97
96
98
97
python_versions = VERSION_FLAG_VALUES [python_version ]
99
- if len (python_versions ) == 1 :
98
+ if len (python_versions ) == 1 and not match_any :
100
99
native .config_setting (
101
100
name = name ,
102
101
flag_values = flag_values ,
103
102
** kwargs
104
103
)
105
104
return
106
105
107
- if match_any :
108
- create_config_settings = {"_" + name : flag_values }
109
- else :
106
+ if not match_any :
110
107
create_config_settings = {
111
108
"_{}" .format (name ).replace (python_version , version ): {_PYTHON_VERSION_FLAG : version }
112
109
for version in python_versions
113
110
}
114
111
match_any = list (create_config_settings .keys ())
112
+ else :
113
+ setting_name = "_" + name if len (python_versions ) > 1 else name
114
+ create_config_settings = {setting_name : flag_values }
115
+ if setting_name not in match_any :
116
+ # Here we expect the user to supply us with a name of the label that
117
+ # would be created by this function. This needs to be the case because
118
+ # the VERSION_FLAG_VALUES values contain the all of the versions that
119
+ # have to be matched.
120
+ fail ("'{}' must be present in 'match_any'" .format (setting_name ))
115
121
116
- # Create all of the necessary config setting values for the config_setting_group
117
122
for name_ , flag_values_ in create_config_settings .items ():
118
123
native .config_setting (
119
124
name = name_ ,
@@ -170,6 +175,12 @@ def construct_config_settings(name = None): # buildifier: disable=function-docs
170
175
is_python_config_setting (
171
176
name = "is_python_{}" .format (version ),
172
177
python_version = version ,
178
+ # NOTE @aignas 2024-03-16: if we don't have this, then we may create duplicate
179
+ # targets. I think this should be improved, but I haven't figured out how yet
180
+ # and I ran out of time.
181
+ #
182
+ # The fact that this parameter is needed here, suggests that we may need to
183
+ # think of a better abstraction here.
173
184
match_any = match_any ,
174
185
visibility = ["//visibility:public" ],
175
186
)
0 commit comments