@@ -92,12 +92,14 @@ py_library(
92
92
"""
93
93
94
94
def _plat_label (plat ):
95
+ if plat .endswith ("default" ):
96
+ return plat
95
97
if plat .startswith ("@//" ):
96
98
return "@@" + str (Label ("//:BUILD.bazel" )).partition ("//" )[0 ].strip ("@" ) + plat .strip ("@" )
97
99
elif plat .startswith ("@" ):
98
100
return str (Label (plat ))
99
101
else :
100
- return ":is_" + plat
102
+ return ":is_" + plat . replace ( "cp3" , "python_3." )
101
103
102
104
def _render_list_and_select (deps , deps_by_platform , tmpl ):
103
105
deps = render .list ([tmpl .format (d ) for d in sorted (deps )])
@@ -115,14 +117,7 @@ def _render_list_and_select(deps, deps_by_platform, tmpl):
115
117
116
118
# Add the default, which means that we will be just using the dependencies in
117
119
# `deps` for platforms that are not handled in a special way by the packages
118
- #
119
- # FIXME @aignas 2024-01-24: This currently works as expected only if the default
120
- # value of the @rules_python//python/config_settings:python_version is set in
121
- # the `.bazelrc`. If it is unset, then the we don't get the expected behaviour
122
- # in cases where we are using a simple `py_binary` using the default toolchain
123
- # without forcing any transitions. If the `python_version` config setting is set
124
- # via .bazelrc, then everything works correctly.
125
- deps_by_platform ["//conditions:default" ] = []
120
+ deps_by_platform .setdefault ("//conditions:default" , [])
126
121
deps_by_platform = render .select (deps_by_platform , value_repr = render .list )
127
122
128
123
if deps == "[]" :
@@ -131,81 +126,63 @@ def _render_list_and_select(deps, deps_by_platform, tmpl):
131
126
return "{} + {}" .format (deps , deps_by_platform )
132
127
133
128
def _render_config_settings (dependencies_by_platform ):
134
- py_version_by_os_arch = {}
129
+ loads = []
130
+ additional_content = []
135
131
for p in dependencies_by_platform :
136
132
# p can be one of the following formats:
133
+ # * //conditions:default
137
134
# * @platforms//os:{value}
138
135
# * @platforms//cpu:{value}
139
136
# * @//python/config_settings:is_python_3.{minor_version}
140
137
# * {os}_{cpu}
141
138
# * cp3{minor_version}_{os}_{cpu}
142
- if p .startswith ("@" ):
139
+ if p .startswith ("@" ) or p . endswith ( "default" ) :
143
140
continue
144
141
145
142
abi , _ , tail = p .partition ("_" )
146
143
if not abi .startswith ("cp" ):
147
144
tail = p
148
145
abi = ""
146
+
149
147
os , _ , arch = tail .partition ("_" )
150
148
os = "" if os == "anyos" else os
151
149
arch = "" if arch == "anyarch" else arch
152
150
153
- py_version_by_os_arch .setdefault ((os , arch ), []).append (abi )
154
-
155
- if not py_version_by_os_arch :
156
- return None , None
157
-
158
- loads = []
159
- additional_content = []
160
- for (os , arch ), abis in py_version_by_os_arch .items ():
161
151
constraint_values = []
162
- if os :
163
- constraint_values .append ("@platforms//os:{}" .format (os ))
164
152
if arch :
165
153
constraint_values .append ("@platforms//cpu:{}" .format (arch ))
154
+ if os :
155
+ constraint_values .append ("@platforms//os:{}" .format (os ))
166
156
167
- os_arch = (os or "anyos" ) + "_" + (arch or "anyarch" )
168
- additional_content .append (
169
- """\
170
- config_setting(
157
+ constraint_values_str = render .indent (render .list (constraint_values )).lstrip ()
158
+
159
+ if abi :
160
+ if not loads :
161
+ loads .append ("""load("@rules_python//python/config_settings:config_settings.bzl", "is_python_config_setting")""" )
162
+
163
+ additional_content .append (
164
+ """\
165
+ is_python_config_setting(
171
166
name = "is_{name}",
172
- constraint_values = {values},
167
+ python_version = "3.{minor_version}",
168
+ constraint_values = {constraint_values},
173
169
visibility = ["//visibility:private"],
174
170
)""" .format (
175
- name = os_arch ,
176
- values = render .indent (render .list (sorted ([str (Label (c )) for c in constraint_values ]))).strip (),
177
- ),
178
- )
179
-
180
- if abis == ["" ]:
181
- if not os or not arch :
182
- fail ("BUG: both os and arch should be set in this case" )
183
- continue
184
-
185
- for abi in abis :
186
- if not loads :
187
- loads .append ("""load("@bazel_skylib//lib:selects.bzl", "selects")""" )
188
- minor_version = int (abi [len ("cp3" ):])
189
- setting = "@@{rules_python}//python/config_settings:is_python_3.{version}" .format (
190
- rules_python = str (Label ("//:BUILD.bazel" )).partition ("//" )[0 ].strip ("@" ),
191
- version = minor_version ,
171
+ name = p .replace ("cp3" , "python_3." ),
172
+ minor_version = abi [len ("cp3" ):],
173
+ constraint_values = constraint_values_str ,
174
+ ),
192
175
)
193
- settings = [
194
- ":is_" + os_arch ,
195
- setting ,
196
- ]
197
-
198
- plat = "{}_{}" .format (abi , os_arch )
199
-
176
+ else :
200
177
additional_content .append (
201
178
"""\
202
- selects.config_setting_group (
203
- name = "{name}",
204
- match_all = {values },
179
+ config_setting (
180
+ name = "is_ {name}",
181
+ constraint_values = {constraint_values },
205
182
visibility = ["//visibility:private"],
206
183
)""" .format (
207
- name = _plat_label ( plat ). lstrip ( ": " ),
208
- values = render . indent ( render . list ( sorted ( settings ))). strip () ,
184
+ name = p . replace ( "cp3" , "python_3. " ),
185
+ constraint_values = constraint_values_str ,
209
186
),
210
187
)
211
188
@@ -379,7 +356,7 @@ def generate_whl_library_build_bazel(
379
356
contents = "\n " .join (
380
357
[
381
358
_BUILD_TEMPLATE .format (
382
- loads = "\n " .join (loads ),
359
+ loads = "\n " .join (sorted ( loads ) ),
383
360
py_library_label = py_library_label ,
384
361
dependencies = render .indent (lib_dependencies , " " * 4 ).lstrip (),
385
362
whl_file_deps = render .indent (whl_file_deps , " " * 4 ).lstrip (),
0 commit comments