Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ examples/pip_parse/bazel-pip_parse
examples/pip_parse_vendored/bazel-pip_parse_vendored
examples/pip_repository_annotations/bazel-pip_repository_annotations
examples/py_proto_library/bazel-py_proto_library
examples/sdist_cross_compile/bazel-bin
examples/sdist_cross_compile/bazel-out
examples/sdist_cross_compile/bazel-sdist_cross_compile
examples/sdist_cross_compile/bazel-testlogs
gazelle/bazel-gazelle
gazelle/examples/bzlmod_build_file_generation/bazel-bin
gazelle/examples/bzlmod_build_file_generation/bazel-bzlmod_build_file_generation
Expand Down
16 changes: 16 additions & 0 deletions examples/sdist_cross_compile/.bazelrc
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
4 changes: 4 additions & 0 deletions examples/sdist_cross_compile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
user.bazelrc
MODULE.bazel.lock
bazel-remote-grpc.log
bazel-execution-log.binpb.zst
83 changes: 83 additions & 0 deletions examples/sdist_cross_compile/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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(":transitions.bzl", "cross_platform_files")

platform(
name = "linux_aarch64",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
"@llvm//constraints/libc:gnu.2.17",
],
)

py_binary(
name = "make_test_sdists",
srcs = ["make_test_sdists.py"],
)

# Self-contained tiny setuptools C-extension sdist generated at build time.
genrule(
name = "gen_tiny_setuptools_sdist",
outs = ["tiny_setuptools-0.1.0.tar.gz"],
cmd = "$(execpath :make_test_sdists) setuptools $@",
tools = [":make_test_sdists"],
)

py_sdist_wheel(
name = "tiny_setuptools.whldir",
sdist = ":gen_tiny_setuptools_sdist",
)

# Self-contained tiny meson-python C/C++17 sdist generated at build time.
genrule(
name = "gen_tiny_meson_sdist",
outs = ["tiny_meson-0.1.0.tar.gz"],
cmd = "$(execpath :make_test_sdists) meson $@",
tools = [":make_test_sdists"],
)

py_sdist_wheel(
name = "tiny_meson.whldir",
build_deps = [
"@sdist_build_deps//meson_python:pkg",
"@sdist_build_deps//ninja:data",
"@sdist_build_deps//ninja:pkg",
],
sdist = ":gen_tiny_meson_sdist",
)

# Cross-compiled setuptools C-extension wheel (linux_x86_64 -> linux_aarch64).
cross_platform_files(
name = "tiny_setuptools_aarch64",
target_platform = ":linux_aarch64",
deps = [":tiny_setuptools.whldir"],
)

# Cross-compiled meson-python C/C++17 wheel (linux_x86_64 -> linux_aarch64).
cross_platform_files(
name = "tiny_meson_aarch64",
target_platform = ":linux_aarch64",
deps = [":tiny_meson.whldir"],
)

py_test(
name = "native_sdist_test",
size = "small",
srcs = ["native_sdist_test.py"],
data = [
":tiny_meson.whldir",
":tiny_setuptools.whldir",
],
)

py_test(
name = "cross_compile_test",
size = "small",
srcs = ["cross_compile_test.py"],
data = [
":tiny_meson_aarch64",
":tiny_setuptools_aarch64",
],
)
50 changes: 50 additions & 0 deletions examples/sdist_cross_compile/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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",
)

# Build-time dependencies (meson-python, meson, ninja) fetched as prebuilt wheels.
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "sdist_build_deps",
python_version = "3.13",
requirements_lock = "//:requirements.txt",
)
use_repo(pip, "sdist_build_deps")
72 changes: 72 additions & 0 deletions examples/sdist_cross_compile/cross_compile_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import fnmatch
import os
import pathlib
import struct
import unittest
import zipfile

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 _check_elf_header(header: bytes, label: str) -> None:
magic = header[0:4]
ei_class = header[4]
ei_data = header[5]
(e_machine,) = struct.unpack("<H", header[18:20])
if magic != ELF_MAGIC:
raise AssertionError(f"{label} is not an ELF binary")
if ei_class != ELFCLASS64:
raise AssertionError(f"{label} is not 64-bit ELF")
if ei_data != ELFDATA2LSB:
raise AssertionError(f"{label} is not little-endian ELF")
if e_machine != EM_AARCH64:
raise AssertionError(
f"{label} has e_machine={e_machine}, expected EM_AARCH64 ({EM_AARCH64})"
)


class CrossCompileTest(unittest.TestCase):
def _assert_wheel_contains_aarch64_so(self, wheel_name: str, so_glob: str) -> None:
root = _find_runfiles_root()
matches = list(root.rglob(wheel_name))
self.assertTrue(matches, f"Expected wheel {wheel_name!r} under {root}")
with zipfile.ZipFile(matches[0]) as zf:
so_entries = [n for n in zf.namelist() if fnmatch.fnmatch(n, so_glob)]
self.assertTrue(
so_entries,
f"Expected entry matching {so_glob!r} in {matches[0]}, got {zf.namelist()}",
)
header = zf.read(so_entries[0])[:20]
_check_elf_header(header, f"{matches[0]}:{so_entries[0]}")

def test_tiny_setuptools_aarch64_elf(self):
self._assert_wheel_contains_aarch64_so(
"tiny_setuptools.sdist_build.whl",
"tiny_setuptools/_tiny_st.cpython-*-aarch64-linux-gnu.so",
)

def test_tiny_meson_c_aarch64_elf(self):
self._assert_wheel_contains_aarch64_so(
"tiny_meson.sdist_build.whl",
"tiny_meson/_tiny_c.cpython-*-aarch64-linux-gnu.so",
)

def test_tiny_meson_cpp_aarch64_elf(self):
self._assert_wheel_contains_aarch64_so(
"tiny_meson.sdist_build.whl",
"tiny_meson/_tiny_cpp.cpython-*-aarch64-linux-gnu.so",
)


if __name__ == "__main__":
unittest.main()
Loading
Loading