Skip to content

Commit

Permalink
revert the select changes from #1743
Browse files Browse the repository at this point in the history
  • Loading branch information
aignas committed Feb 12, 2024
1 parent c8e51cb commit d6e357e
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 32 deletions.
80 changes: 57 additions & 23 deletions python/pip_install/private/generate_whl_library_build_bazel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Generate the BUILD.bazel contents for a repo defined by a whl_library."""

load("//python/config_settings:config_settings.bzl", "VERSION_FLAG_VALUES")
load(
"//python/private:labels.bzl",
"DATA_LABEL",
Expand Down Expand Up @@ -101,6 +102,8 @@ alias(
)
"""

_python_version = Label("//python/config_settings:python_version")

def _plat_label(plat):
if plat.startswith("@//"):
return "@@" + str(Label("//:BUILD.bazel")).partition("//")[0].strip("@") + plat.strip("@")
Expand Down Expand Up @@ -141,49 +144,76 @@ def _render_list_and_select(deps, deps_by_platform, tmpl):
return "{} + {}".format(deps, deps_by_platform)

def _render_config_settings(dependencies_by_platform):
plats = []
loads = []
additional_content = []
for p in dependencies_by_platform:
# p can be one of the following formats:
# * @platforms//os:{value}
# * @platforms//cpu:{value}
# * @//python/config_settings:is_python_3.{minor_version}
# * @//python/config_settings:is_python_3.{minor_version}_{os}_{cpu}
# * @//python/config_settings:is_python_3.{minor_version}_{os}_any
# * @//python/config_settings:is_python_3.{minor_version}_any_{cpu}
# * {os}_{cpu}
# * cp3{minor_version}_{os}_{cpu}
if p.startswith("@"):
continue

os, _, arch = p.partition("_")
abi, _, tail = p.partition("_")
if not abi[2:].startswith("3"):
tail = p
abi = ""
os, _, arch = tail.partition("_")
os = "" if os == "any" else os
arch = "" if arch == "any" else arch

# TODO @aignas 2024-02-03: simplify
plats.append((os, arch))
constraint_values = []
if os:
constraint_values.append("@platforms//os:{}".format(os))
if arch:
constraint_values.append("@platforms//cpu:{}".format(arch))

if not plats:
return None

additional_content = []
for (os, arch) in plats:
constraint_values = [
"@platforms//os:{}".format(os),
"@platforms//cpu:{}".format(arch),
]
if abi == "":
if not os or not arch:
fail("BUG: both os and arch should be set in this case")

additional_content.append(
"""\
additional_content.append(
"""\
config_setting(
name = "is_{name}",
constraint_values = {values},
visibility = ["//visibility:private"],
)""".format(
name = "{}_{}".format(os, arch),
values = render.indent(render.list(sorted([str(Label(c)) for c in constraint_values]))).strip(),
),
name = "{}_{}".format(os, arch),
values = render.indent(render.list(sorted([str(Label(c)) for c in constraint_values]))).strip(),
),
)
continue

if not loads:
loads.append(
"""load("@@{rules_python}//python/config_settings:config_settings.bzl", "is_python_config_setting")""".format(
rules_python=_python_version.workspace_name,
)
)

minor_version = int(abi[len("cp3"):])
minor = "3.{}".format(minor_version)
text = render.is_python_config_setting(
name = "is_{}".format(p),
flag_values = {
str(_python_version): minor,
},
match_extra = {
"is_{}".format(p).replace(abi, x): {str(_python_version): x}
for x in VERSION_FLAG_VALUES[minor]
},
constraint_values = constraint_values,
visibility = ["//visibility:private"],
)
additional_content.append(text)

if not additional_content:
return None, None

return "\n\n".join(additional_content)
return loads, "\n\n".join(additional_content)

def generate_whl_library_build_bazel(
*,
Expand Down Expand Up @@ -292,8 +322,12 @@ def generate_whl_library_build_bazel(
"""load("@bazel_skylib//rules:copy_file.bzl", "copy_file")""",
]

config_settings_content = _render_config_settings(dependencies_by_platform)
loads_, config_settings_content = _render_config_settings(dependencies_by_platform)
if config_settings_content:
for line in loads_:
if line not in loads:
loads.append(line)

additional_content.append(config_settings_content)

lib_dependencies = _render_list_and_select(
Expand Down
77 changes: 68 additions & 9 deletions tests/pip_install/whl_library/generate_build_bazel_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def _test_dep_selects(env):
want = """\
load("@rules_python//python:defs.bzl", "py_library", "py_binary")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@//python/config_settings:config_settings.bzl", "is_python_config_setting")
package(default_visibility = ["//visibility:public"])
Expand All @@ -119,12 +120,12 @@ filegroup(
"@pypi_foo//:whl",
] + select(
{
"@//python/config_settings:is_python_3.10_linux_ppc": ["@pypi_py310_linux_ppc_dep//:whl"],
"@//python/config_settings:is_python_3.9": ["@pypi_py39_dep//:whl"],
"@//python/config_settings:is_python_3.9_aarch64": ["@pypi_py39_arm_dep//:whl"],
"@//python/config_settings:is_python_3.9_linux": ["@pypi_py39_linux_dep//:whl"],
"@platforms//cpu:aarch64": ["@pypi_arm_dep//:whl"],
"@platforms//os:windows": ["@pypi_win_dep//:whl"],
":is_cp310_linux_ppc": ["@pypi_@pypi_py310_linux_ppc_dep//:whl//:whl"],
":is_cp39_any_aarch64": ["@pypi_@pypi_py39_arm_dep//:whl//:whl"],
":is_cp39_linux_any": ["@pypi_@pypi_py39_linux_dep//:whl//:whl"],
":is_linux_x86_64": ["@pypi_linux_intel_dep//:whl"],
"//conditions:default": [],
},
Expand Down Expand Up @@ -153,12 +154,12 @@ py_library(
"@pypi_foo//:pkg",
] + select(
{
"@//python/config_settings:is_python_3.10_linux_ppc": ["@pypi_py310_linux_ppc_dep//:pkg"],
"@//python/config_settings:is_python_3.9": ["@pypi_py39_dep//:pkg"],
"@//python/config_settings:is_python_3.9_aarch64": ["@pypi_py39_arm_dep//:pkg"],
"@//python/config_settings:is_python_3.9_linux": ["@pypi_py39_linux_dep//:pkg"],
"@platforms//cpu:aarch64": ["@pypi_arm_dep//:pkg"],
"@platforms//os:windows": ["@pypi_win_dep//:pkg"],
":is_cp310_linux_ppc": ["@pypi_@pypi_py310_linux_ppc_dep//:whl//:pkg"],
":is_cp39_any_aarch64": ["@pypi_@pypi_py39_arm_dep//:whl//:pkg"],
":is_cp39_linux_any": ["@pypi_@pypi_py39_linux_dep//:whl//:pkg"],
":is_linux_x86_64": ["@pypi_linux_intel_dep//:pkg"],
"//conditions:default": [],
},
Expand All @@ -177,6 +178,64 @@ alias(
actual = "_whl",
)
is_python_config_setting(
name = "is_cp310_linux_ppc",
flag_values = {
"@//python/config_settings:python_version": "3.10",
},
match_extra = {
"is_3.10.2_linux_ppc": {"@//python/config_settings:python_version": "3.10.2"},
"is_3.10.4_linux_ppc": {"@//python/config_settings:python_version": "3.10.4"},
"is_3.10.6_linux_ppc": {"@//python/config_settings:python_version": "3.10.6"},
"is_3.10.8_linux_ppc": {"@//python/config_settings:python_version": "3.10.8"},
"is_3.10.9_linux_ppc": {"@//python/config_settings:python_version": "3.10.9"},
"is_3.10.11_linux_ppc": {"@//python/config_settings:python_version": "3.10.11"},
"is_3.10.12_linux_ppc": {"@//python/config_settings:python_version": "3.10.12"},
"is_3.10.13_linux_ppc": {"@//python/config_settings:python_version": "3.10.13"},
},
visibility = ["//visibility:private"],
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:ppc",
],
)
is_python_config_setting(
name = "is_cp39_any_aarch64",
flag_values = {
"@//python/config_settings:python_version": "3.9",
},
match_extra = {
"is_3.9.10_any_aarch64": {"@//python/config_settings:python_version": "3.9.10"},
"is_3.9.12_any_aarch64": {"@//python/config_settings:python_version": "3.9.12"},
"is_3.9.13_any_aarch64": {"@//python/config_settings:python_version": "3.9.13"},
"is_3.9.15_any_aarch64": {"@//python/config_settings:python_version": "3.9.15"},
"is_3.9.16_any_aarch64": {"@//python/config_settings:python_version": "3.9.16"},
"is_3.9.17_any_aarch64": {"@//python/config_settings:python_version": "3.9.17"},
"is_3.9.18_any_aarch64": {"@//python/config_settings:python_version": "3.9.18"},
},
visibility = ["//visibility:private"],
constraint_values = ["@platforms//cpu:aarch64"],
)
is_python_config_setting(
name = "is_cp39_linux_any",
flag_values = {
"@//python/config_settings:python_version": "3.9",
},
match_extra = {
"is_3.9.10_linux_any": {"@//python/config_settings:python_version": "3.9.10"},
"is_3.9.12_linux_any": {"@//python/config_settings:python_version": "3.9.12"},
"is_3.9.13_linux_any": {"@//python/config_settings:python_version": "3.9.13"},
"is_3.9.15_linux_any": {"@//python/config_settings:python_version": "3.9.15"},
"is_3.9.16_linux_any": {"@//python/config_settings:python_version": "3.9.16"},
"is_3.9.17_linux_any": {"@//python/config_settings:python_version": "3.9.17"},
"is_3.9.18_linux_any": {"@//python/config_settings:python_version": "3.9.18"},
},
visibility = ["//visibility:private"],
constraint_values = ["@platforms//os:linux"],
)
config_setting(
name = "is_linux_x86_64",
constraint_values = [
Expand All @@ -191,12 +250,12 @@ config_setting(
whl_name = "foo.whl",
dependencies = ["foo", "bar-baz"],
dependencies_by_platform = {
"@//python/config_settings:is_python_3.10_linux_ppc": ["py310_linux_ppc_dep"],
"@//python/config_settings:is_python_3.9": ["py39_dep"],
"@//python/config_settings:is_python_3.9_aarch64": ["py39_arm_dep"],
"@//python/config_settings:is_python_3.9_linux": ["py39_linux_dep"],
"@platforms//cpu:aarch64": ["arm_dep"],
"@platforms//os:windows": ["win_dep"],
"cp310_linux_ppc": ["@pypi_py310_linux_ppc_dep//:whl"],
"cp39_any_aarch64": ["@pypi_py39_arm_dep//:whl"],
"cp39_linux_any": ["@pypi_py39_linux_dep//:whl"],
"linux_x86_64": ["linux_intel_dep"],
},
data_exclude = [],
Expand Down

0 comments on commit d6e357e

Please sign in to comment.