|
| 1 | +#! /usr/bin/env bash |
| 2 | +# |
| 3 | +# Cross-compile PyAV's armv7l (32-bit ARM) wheels with zig on an x86_64 host, |
| 4 | +# without QEMU. Only PyAV's Cython->C extensions are compiled; FFmpeg is fetched |
| 5 | +# prebuilt for armv7l. zig is the cross compiler, and CPython's cross-build env |
| 6 | +# vars (_PYTHON_HOST_PLATFORM / _PYTHON_SYSCONFIGDATA_NAME) make sysconfig report |
| 7 | +# armv7l without running any armv7l code. The target Python trees + system libs |
| 8 | +# are copied out of the manylinux armv7l image with `docker cp` (no execution). |
| 9 | +# |
| 10 | +# Builds two manylinux (glibc) wheels: cp311-abi3 (covers 3.11-3.13) and cp314t. |
| 11 | +# musllinux armv7l is skipped: the musl FFmpeg needs system libs (libdrm, |
| 12 | +# libxcb*, libbz2) the musllinux image doesn't ship for auditwheel to bundle. |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 17 | +root="$(cd "$here/.." && pwd)" |
| 18 | +cd "$root" |
| 19 | + |
| 20 | +# Host interpreters that drive each build (must match the wheel's Python: 3.11 |
| 21 | +# emits cp311-abi3, 3.14t emits cp314t). zig/auditwheel run from TOOLS_PY. |
| 22 | +HOST_PY311="${HOST_PY311:-python3.11}" |
| 23 | +HOST_PY314T="${HOST_PY314T:-python3.14t}" |
| 24 | +TOOLS_PY="${TOOLS_PY:-python3}" |
| 25 | + |
| 26 | +DIST_DIR="${DIST_DIR:-$root/dist}" |
| 27 | +WORK_DIR="${WORK_DIR:-$root/build/armv7l-cross}" |
| 28 | +IMAGE="quay.io/pypa/manylinux_2_31_armv7l" |
| 29 | +TRIPLE="arm-linux-gnueabihf.2.31" # pin glibc to the manylinux_2_31 policy |
| 30 | +PLAT="manylinux_2_31_armv7l" |
| 31 | +FFMPEG_CONFIG="scripts/ffmpeg-latest.json" |
| 32 | + |
| 33 | +PY_DIR="$WORK_DIR/py" |
| 34 | +BIN_DIR="$WORK_DIR/bin" |
| 35 | +mkdir -p "$DIST_DIR" "$BIN_DIR" "$PY_DIR" |
| 36 | + |
| 37 | +# --- zig compiler wrappers ------------------------------------------------ |
| 38 | +# setuptools invokes $CC / $LDSHARED as plain commands. Resolve the zig binary |
| 39 | +# once and call it directly (not "python -m ziglang"): the build sets PYTHONPATH |
| 40 | +# to the target stdlib, which would crash a per-compile Python. The wrappers also |
| 41 | +# drop a few gcc-only flags clang (zig) rejects. |
| 42 | +ZIG_BIN="$("$TOOLS_PY" -c 'import os, ziglang; print(os.path.join(os.path.dirname(ziglang.__file__), "zig"))')" |
| 43 | + |
| 44 | +write_cc() { |
| 45 | + cat >"$1" <<EOF |
| 46 | +#! /usr/bin/env bash |
| 47 | +args=() |
| 48 | +for a in "\$@"; do |
| 49 | + case "\$a" in |
| 50 | + -fno-semantic-interposition|-fstack-clash-protection|-fcf-protection*|-mno-cet|-march=native|-mtune=native|--param=*|-fipa-pta) ;; |
| 51 | + *) args+=("\$a") ;; |
| 52 | + esac |
| 53 | +done |
| 54 | +exec "$ZIG_BIN" $2 -target "$TRIPLE" "\${args[@]}" |
| 55 | +EOF |
| 56 | + chmod +x "$1" |
| 57 | +} |
| 58 | +write_cc "$BIN_DIR/zig-cc" cc |
| 59 | +write_cc "$BIN_DIR/zig-cxx" c++ |
| 60 | +printf '#! /usr/bin/env bash\nexec "%s" ar "$@"\n' "$ZIG_BIN" >"$BIN_DIR/zig-ar" |
| 61 | +chmod +x "$BIN_DIR/zig-ar" |
| 62 | + |
| 63 | +# --- copy the target Python trees + system libs out of the image ---------- |
| 64 | +extract_image() { |
| 65 | + if [[ -d "$PY_DIR/_internal" ]]; then |
| 66 | + return # already extracted |
| 67 | + fi |
| 68 | + echo "Pulling $IMAGE" |
| 69 | + docker pull --platform linux/arm/v7 "$IMAGE" |
| 70 | + local cid |
| 71 | + cid="$(docker create --platform linux/arm/v7 "$IMAGE")" |
| 72 | + # /opt/python/<tag> are symlinks into /opt/_internal/<tree>; copy both. Also |
| 73 | + # grab the armhf system libs (libxcb, ...) FFmpeg needs so auditwheel can |
| 74 | + # bundle them. docker cp reads the filesystem; it runs no armv7l code. |
| 75 | + docker cp "$cid:/opt/python" "$PY_DIR" |
| 76 | + docker cp "$cid:/opt/_internal" "$PY_DIR" |
| 77 | + docker cp "$cid:/usr/lib/arm-linux-gnueabihf" "$PY_DIR/syslib" |
| 78 | + docker rm -f "$cid" >/dev/null |
| 79 | +} |
| 80 | + |
| 81 | +# --- build + repair one wheel --------------------------------------------- |
| 82 | +# args: <host_py> <python-tag-glob> |
| 83 | +build_one() { |
| 84 | + local host_py="$1" py_glob="$2" |
| 85 | + |
| 86 | + # Resolve /opt/python/<tag> symlink to its real tree under /opt/_internal. |
| 87 | + local link |
| 88 | + link="$(find "$PY_DIR/python" -maxdepth 1 -name "$py_glob" | head -1)" |
| 89 | + [[ -n "$link" ]] || { echo "no python matching '$py_glob':" >&2; ls "$PY_DIR/python" >&2; exit 1; } |
| 90 | + local pytree="$PY_DIR/_internal/$(basename "$(readlink "$link")")" |
| 91 | + |
| 92 | + local inc=( "$pytree"/include/python3.* ) |
| 93 | + local scd=( "$pytree"/lib/python3.*/_sysconfigdata_*.py ) |
| 94 | + echo "=== building for $(basename "$pytree") (host $host_py) ===" |
| 95 | + |
| 96 | + # FFmpeg's pkg-config files bake in prefix=/tmp/vendor, so extract there. |
| 97 | + rm -rf /tmp/vendor |
| 98 | + PYAV_VENDOR_PLATFORM=manylinux-armv7l "$TOOLS_PY" scripts/fetch-vendor.py \ |
| 99 | + --config-file "$FFMPEG_CONFIG" /tmp/vendor |
| 100 | + |
| 101 | + local raw="$WORK_DIR/raw/$(basename "$pytree")" |
| 102 | + rm -rf "$raw"; mkdir -p "$raw" |
| 103 | + |
| 104 | + # The _PYTHON_* vars make sysconfig report armv7l; zig does the compiling. |
| 105 | + env \ |
| 106 | + CC="$BIN_DIR/zig-cc" CXX="$BIN_DIR/zig-cxx" AR="$BIN_DIR/zig-ar" \ |
| 107 | + LDSHARED="$BIN_DIR/zig-cc -shared" \ |
| 108 | + _PYTHON_HOST_PLATFORM=linux-armv7l \ |
| 109 | + _PYTHON_SYSCONFIGDATA_NAME="$(basename "${scd[0]}" .py)" \ |
| 110 | + PYTHONPATH="$(dirname "${scd[0]}")" \ |
| 111 | + CFLAGS="-I${inc[0]} -Wno-error=incompatible-pointer-types" \ |
| 112 | + PKG_CONFIG_PATH=/tmp/vendor/lib/pkgconfig \ |
| 113 | + LD_LIBRARY_PATH=/tmp/vendor/lib \ |
| 114 | + "$host_py" -m pip wheel . --no-build-isolation --no-deps -w "$raw" |
| 115 | + |
| 116 | + # Bundle FFmpeg + its armhf system deps and stamp the platform tag. The shim |
| 117 | + # lets auditwheel accept the armv7l --plat from an x86_64 host; --ldpaths |
| 118 | + # points it at the target's libs instead of the host search path. |
| 119 | + "$TOOLS_PY" scripts/auditwheel-cross.py armv7l glibc repair \ |
| 120 | + --ldpaths "/tmp/vendor/lib:$PY_DIR/syslib" \ |
| 121 | + --plat "$PLAT" -w "$DIST_DIR" "$raw"/*.whl |
| 122 | +} |
| 123 | + |
| 124 | +extract_image |
| 125 | +build_one "$HOST_PY311" "cp311-cp311" |
| 126 | +build_one "$HOST_PY314T" "cp314*-cp314t" |
| 127 | + |
| 128 | +echo |
| 129 | +echo "Built armv7l wheels:" |
| 130 | +ls -1 "$DIST_DIR"/*armv7l*.whl |
0 commit comments