Repository navigation
feat(pypi): build and cross-compile sdists in build actions #4589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rsartor-cmd
wants to merge
10
commits into
main
Choose a base branch
from
sdist
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bc9a0d6
feat(pypi): add experimental_action_install mode for wheels
jvolkman 929414c
feat(pypi): add PyInstalledWheelInfo, py_wheel_headers, py_wheel_tran…
jvolkman c677164
feat(pypi): add py_sdist_wheel rule for building sdists in actions
jvolkman cfefe3d
feat(pypi): install action-built wheels in py_wheel_library
jvolkman 8589886
feat(pypi): build sdists in actions when experimental_action_install …
jvolkman 403a204
Use hermetic-llvm for cross-compiling toolchains.
rsartor-cmd 825be2e
Add adjustments for hermetic_cc_toolchain.
rsartor-cmd f461aff
Fix hermeticity bug.
rsartor-cmd 432de03
Add test cases for sdist, both regular and cross-compiling.
rsartor-cmd e603b4d
Merge branch 'main' into sdist
rsartor-cmd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| common --enable_bzlmod | ||
| common --enable_platform_specific_config | ||
| common --incompatible_python_disallow_native_rules | ||
|
|
||
| test --test_output=errors | ||
|
|
||
| # BuildBuddy's managed cloud pool only has Linux executors; disable remote | ||
| # execution on macOS and Windows while keeping BES and Remote Cache. | ||
| common:macos --remote_executor= | ||
| common:macos --extra_execution_platforms= | ||
| common:windows --remote_executor= | ||
| common:windows --extra_execution_platforms= | ||
|
|
||
| import %workspace%/../../buildbuddy.bazelrc | ||
| try-import %workspace%/../../user.bazelrc | ||
| try-import %workspace%/user.bazelrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| user.bazelrc | ||
| bazel-remote-grpc.log | ||
| bazel-execution-log.binpb.zst | ||
| /bazel-* | ||
| /bazel-bin | ||
| /bazel-genfiles | ||
| /bazel-out | ||
| /bazel-testlogs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| load("@rules_python//python:py_binary.bzl", "py_binary") | ||
| load("@rules_python//python:py_test.bzl", "py_test") | ||
| load("@rules_python//python/private/pypi:py_sdist_wheel.bzl", "py_sdist_wheel") # buildifier: disable=bzl-visibility | ||
| load("@rules_python//python/private/pypi:py_wheel_library.bzl", "py_wheel_library") # buildifier: disable=bzl-visibility | ||
| load(":transitions.bzl", "cross_platform_files") | ||
|
|
||
| platform( | ||
| name = "linux_aarch64", | ||
| constraint_values = [ | ||
| "@platforms//os:linux", | ||
| "@platforms//cpu:aarch64", | ||
| "@llvm//constraints/libc:gnu.2.17", | ||
| ], | ||
| ) | ||
|
|
||
| # Self-contained tiny meson-python C/C++ sdist generated at build time (no | ||
| # checked-in binary archives) to exercise meson-python + meson + ninja + | ||
| # meson-cross.ini cross-compilation quickly. | ||
| py_binary( | ||
| name = "make_meson_sdist", | ||
| srcs = ["make_meson_sdist.py"], | ||
| ) | ||
|
|
||
| genrule( | ||
| name = "gen_tiny_meson_sdist", | ||
| outs = ["tiny_meson-0.1.0.tar.gz"], | ||
| cmd = "$(execpath :make_meson_sdist) $@", | ||
| tools = [":make_meson_sdist"], | ||
| ) | ||
|
|
||
| py_sdist_wheel( | ||
| name = "tiny_meson.whldir", | ||
| build_deps = [ | ||
| "@sdist_deps//meson_python:pkg", | ||
| "@sdist_deps//ninja:pkg", | ||
| ], | ||
| sdist = ":gen_tiny_meson_sdist", | ||
| ) | ||
|
|
||
| py_wheel_library( | ||
| name = "tiny_meson_wheel_lib", | ||
| site_paths = ["tiny_meson"], | ||
| wheel = ":tiny_meson.whldir", | ||
| ) | ||
|
|
||
| # Simple setuptools C-extension sdist from PyPI. | ||
| cross_platform_files( | ||
| name = "markupsafe_aarch64", | ||
| target_platform = ":linux_aarch64", | ||
| deps = ["@sdist_deps//markupsafe"], | ||
| ) | ||
|
|
||
| # Pure-Python sdist from PyPI (with a setuptools-scm build-time dependency). | ||
| cross_platform_files( | ||
| name = "tabulate_aarch64", | ||
| target_platform = ":linux_aarch64", | ||
| deps = ["@sdist_deps//tabulate"], | ||
| ) | ||
|
|
||
| # In-repo meson-python C/C++17 sdist built directly via py_sdist_wheel. | ||
| cross_platform_files( | ||
| name = "tiny_meson_aarch64", | ||
| target_platform = ":linux_aarch64", | ||
| deps = [":tiny_meson_wheel_lib"], | ||
| ) | ||
|
|
||
| # Exercises pip.parse + pip.whl_mods(sdist_build_deps = ...) with a real PyPI | ||
| # meson-python + pybind11 C++17 sdist. | ||
| cross_platform_files( | ||
| name = "contourpy_aarch64", | ||
| target_platform = ":linux_aarch64", | ||
| deps = ["@sdist_deps//contourpy"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "native_sdist_test", | ||
| size = "small", | ||
| srcs = ["native_sdist_test.py"], | ||
| deps = [ | ||
| ":tiny_meson_wheel_lib", | ||
| "@sdist_deps//contourpy", | ||
| "@sdist_deps//markupsafe", | ||
| "@sdist_deps//tabulate", | ||
| ], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "cross_compile_test", | ||
| size = "small", | ||
| srcs = ["cross_compile_test.py"], | ||
| data = [ | ||
| ":contourpy_aarch64", | ||
| ":markupsafe_aarch64", | ||
| ":tabulate_aarch64", | ||
| ":tiny_meson_aarch64", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| module( | ||
| name = "example_sdist_cross_compile", | ||
| version = "0.0.0", | ||
| ) | ||
|
|
||
| bazel_dep(name = "platforms", version = "1.1.0") | ||
| bazel_dep(name = "rules_cc", version = "0.2.25") | ||
| bazel_dep(name = "rules_python", version = "0.0.0") | ||
| local_path_override( | ||
| module_name = "rules_python", | ||
| path = "../..", | ||
| ) | ||
|
|
||
| # Hermetic LLVM C/C++ cross-compilation toolchain (linux_x86_64 -> linux_aarch64). | ||
| bazel_dep(name = "llvm", version = "0.8.27") | ||
| register_toolchains("@llvm//toolchain:all") | ||
|
|
||
| # BuildBuddy RBE platform and Ubuntu 24.04 GCC toolchain (used with --config=buildbuddy_*). | ||
| bazel_dep(name = "toolchains_buildbuddy", version = "0.0.4", dev_dependency = True) | ||
| buildbuddy = use_extension( | ||
| "@toolchains_buildbuddy//:extensions.bzl", | ||
| "buildbuddy", | ||
| dev_dependency = True, | ||
| ) | ||
| buildbuddy.platform(buildbuddy_container_image = "UBUNTU24_04_IMAGE") | ||
|
|
||
| rules_python_config = use_extension( | ||
| "@rules_python//python/extensions:config.bzl", | ||
| "config", | ||
| ) | ||
| rules_python_config.explicit_init_py(default = True) | ||
|
|
||
| python = use_extension("@rules_python//python/extensions:python.bzl", "python") | ||
| python.defaults( | ||
| python_version = "3.13", | ||
| ) | ||
| python.toolchain( | ||
| python_version = "3.13", | ||
| ) | ||
|
|
||
| pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") | ||
| # Exercise pip.parse + pip.whl_mods(sdist_build_deps = ...) end-to-end with | ||
| # contourpy, a lightweight PyPI C++17 sdist built via meson-python + pybind11. | ||
| pip.whl_mods( | ||
| hub_name = "sdist_whl_mods", | ||
| sdist_build_deps = [ | ||
| "@sdist_deps//meson_python:pkg", | ||
| "@sdist_deps//ninja:pkg", | ||
| "@sdist_deps//pybind11:pkg", | ||
| ], | ||
| whl_name = "contourpy", | ||
| ) | ||
| pip.parse( | ||
| experimental_action_install = True, | ||
| hub_name = "sdist_deps", | ||
| python_version = "3.13", | ||
| requirements_lock = "//:requirements.txt", | ||
| target_platforms = [ | ||
| "linux_x86_64", | ||
| "linux_aarch64", | ||
| ], | ||
| whl_modifications = { | ||
| "@sdist_whl_mods//:contourpy.json": "contourpy", | ||
| }, | ||
| ) | ||
| use_repo(pip, "sdist_deps", "sdist_whl_mods") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import os | ||
| import pathlib | ||
| import struct | ||
| import unittest | ||
|
|
||
| ELF_MAGIC = b"\x7fELF" | ||
| ELFCLASS64 = 2 | ||
| ELFDATA2LSB = 1 | ||
| EM_AARCH64 = 183 | ||
|
|
||
|
|
||
| def _find_runfiles_root() -> pathlib.Path: | ||
| runfiles_dir = os.environ.get("TEST_SRCDIR") or os.environ.get("RUNFILES_DIR") | ||
| if not runfiles_dir: | ||
| raise AssertionError("Neither TEST_SRCDIR nor RUNFILES_DIR is set") | ||
| return pathlib.Path(runfiles_dir) | ||
|
|
||
|
|
||
| def _read_elf_header(path: pathlib.Path) -> tuple[bytes, int, int, int]: | ||
| with path.open("rb") as f: | ||
| header = f.read(20) | ||
| magic = header[0:4] | ||
| ei_class = header[4] | ||
| ei_data = header[5] | ||
| # e_machine is at offset 18 (uint16, little-endian for ELFDATA2LSB). | ||
| (e_machine,) = struct.unpack("<H", header[18:20]) | ||
| return magic, ei_class, ei_data, e_machine | ||
|
|
||
|
|
||
| class CrossCompileTest(unittest.TestCase): | ||
| def _assert_aarch64_so(self, pattern: str) -> None: | ||
| root = _find_runfiles_root() | ||
| matches = list(root.rglob(pattern)) | ||
| self.assertTrue( | ||
| matches, | ||
| f"Expected aarch64 extension matching {pattern!r} under {root}", | ||
| ) | ||
| so_path = matches[0] | ||
| magic, ei_class, ei_data, e_machine = _read_elf_header(so_path) | ||
| self.assertEqual(magic, ELF_MAGIC, f"{so_path} is not an ELF binary") | ||
| self.assertEqual(ei_class, ELFCLASS64, f"{so_path} is not 64-bit ELF") | ||
| self.assertEqual(ei_data, ELFDATA2LSB, f"{so_path} is not little-endian ELF") | ||
| self.assertEqual( | ||
| e_machine, | ||
| EM_AARCH64, | ||
| f"{so_path} has e_machine={e_machine}, expected EM_AARCH64 ({EM_AARCH64})", | ||
| ) | ||
|
|
||
| def test_markupsafe_aarch64_elf(self): | ||
| self._assert_aarch64_so("_speedups.cpython-*-aarch64-linux-gnu.so") | ||
|
|
||
| def test_tiny_meson_c_aarch64_elf(self): | ||
| self._assert_aarch64_so("_tiny_c.cpython-*-aarch64-linux-gnu.so") | ||
|
|
||
| def test_tiny_meson_cpp_aarch64_elf(self): | ||
| self._assert_aarch64_so("_tiny_cpp.cpython-*-aarch64-linux-gnu.so") | ||
|
|
||
| def test_contourpy_aarch64_elf(self): | ||
| # Exercises pip.parse + pip.whl_mods with a PyPI meson-python C++ sdist. | ||
| self._assert_aarch64_so("_contourpy.cpython-*-aarch64-linux-gnu.so") | ||
|
|
||
| def test_tabulate_aarch64_present(self): | ||
| root = _find_runfiles_root() | ||
| matches = list(root.rglob("tabulate/__init__.py")) | ||
| self.assertTrue( | ||
| matches, | ||
| f"Expected tabulate/__init__.py in cross-compiled outputs under {root}", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow directory symlinks when locating installed outputs.
cross_platform_filesforwards installed-wheel directory artifacts into this test’s runfiles. Bazel permits directory symlinks there, but Python 3.13rglob()does not recurse through them by default. These searches can report missing extensions and packages even when cross-compilation succeeds. (raw.githubusercontent.com)Pass
recurse_symlinks=Trueto both searches. The example already selects Python 3.13, which supports this argument. (docs.python.org)Also applies to: 64-64