|
| 1 | +#! /usr/bin/env bash |
| 2 | +# |
| 3 | +# Only PyAV's own Cython->C extensions are compiled here; FFmpeg is downloaded |
| 4 | +# prebuilt for armv7l (see scripts/fetch-vendor.py). The pieces that make this |
| 5 | +# work fully emulation-free are: |
| 6 | +# |
| 7 | +# * zig as the cross C compiler ("zig cc -target arm-linux-gnueabihf"). |
| 8 | +# * CPython's native cross-build env vars (_PYTHON_HOST_PLATFORM and |
| 9 | +# _PYTHON_SYSCONFIGDATA_NAME) so sysconfig reports armv7l values without |
| 10 | +# ever running an armv7l binary. The target Python trees are copied out of |
| 11 | +# the official manylinux/musllinux armv7l images with `docker cp`, which |
| 12 | +# reads the image filesystem and does not execute any armv7l code. |
| 13 | +# |
| 14 | +# Produces the four wheels the old QEMU job built: |
| 15 | +# cp311-abi3 (Limited API, covers 3.11-3.13) x {manylinux, musllinux} |
| 16 | +# cp314t (free-threaded 3.14) x {manylinux, musllinux} |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 21 | +root="$(cd "$here/.." && pwd)" |
| 22 | +cd "$root" |
| 23 | + |
| 24 | +# --- configuration (override via env) ------------------------------------ |
| 25 | +DIST_DIR="${DIST_DIR:-$root/dist}" |
| 26 | +WORK_DIR="${WORK_DIR:-$root/build/armv7l-cross}" |
| 27 | + |
| 28 | +# Official images that ship matching target Python builds + sysconfigdata. |
| 29 | +MANYLINUX_IMAGE="${MANYLINUX_IMAGE:-quay.io/pypa/manylinux_2_31_armv7l}" |
| 30 | +MUSLLINUX_IMAGE="${MUSLLINUX_IMAGE:-quay.io/pypa/musllinux_1_2_armv7l}" |
| 31 | + |
| 32 | +# Pin the glibc the extension may require to match the manylinux policy. |
| 33 | +GLIBC_VER="${GLIBC_VER:-2.31}" |
| 34 | + |
| 35 | +# Host interpreters that drive each build. They must match the wheel's Python |
| 36 | +# version: 3.11 emits cp311-abi3, 3.14t emits cp314t. zig/auditwheel run from |
| 37 | +# TOOLS_PY (any 3.x with `ziglang` and `auditwheel` installed). |
| 38 | +HOST_PY311="${HOST_PY311:-python3.11}" |
| 39 | +HOST_PY314T="${HOST_PY314T:-python3.14t}" |
| 40 | +TOOLS_PY="${TOOLS_PY:-python3}" |
| 41 | + |
| 42 | +FFMPEG_CONFIG="${FFMPEG_CONFIG:-scripts/ffmpeg-latest.json}" |
| 43 | + |
| 44 | +BIN_DIR="$WORK_DIR/bin" |
| 45 | +mkdir -p "$DIST_DIR" "$BIN_DIR" "$WORK_DIR/py" "$WORK_DIR/vendor" "$WORK_DIR/raw" |
| 46 | + |
| 47 | +# --- zig compiler wrappers ------------------------------------------------ |
| 48 | +# setuptools invokes $CC / $LDSHARED as plain commands, so wrap "zig cc" in a |
| 49 | +# script. The wrapper also drops a few gcc-only flags the target sysconfigdata |
| 50 | +# may carry that clang (zig) rejects. The target triple comes from $ZIG_TARGET |
| 51 | +# so one wrapper serves every build. |
| 52 | +write_wrapper() { |
| 53 | + local path="$1" subcmd="$2" |
| 54 | + cat >"$path" <<EOF |
| 55 | +#! /usr/bin/env bash |
| 56 | +args=() |
| 57 | +for a in "\$@"; do |
| 58 | + case "\$a" in |
| 59 | + -fno-semantic-interposition|-fstack-clash-protection|-fcf-protection*|-mno-cet|-march=native|-mtune=native|--param=*|-fipa-pta) ;; |
| 60 | + *) args+=("\$a") ;; |
| 61 | + esac |
| 62 | +done |
| 63 | +exec "${TOOLS_PY}" -m ziglang ${subcmd} -target "\${ZIG_TARGET}" "\${args[@]}" |
| 64 | +EOF |
| 65 | + chmod +x "$path" |
| 66 | +} |
| 67 | +write_wrapper "$BIN_DIR/zig-cc" "cc" |
| 68 | +write_wrapper "$BIN_DIR/zig-cxx" "c++" |
| 69 | +cat >"$BIN_DIR/zig-ar" <<EOF |
| 70 | +#! /usr/bin/env bash |
| 71 | +exec "${TOOLS_PY}" -m ziglang ar "\$@" |
| 72 | +EOF |
| 73 | +chmod +x "$BIN_DIR/zig-ar" |
| 74 | + |
| 75 | +# --- copy target Python trees out of the armv7l images (no execution) ----- |
| 76 | +extract_pythons() { |
| 77 | + local libc="$1" image="$2" |
| 78 | + local dest="$WORK_DIR/py/$libc" |
| 79 | + if [[ -d "$dest/cp311-cp311" ]]; then |
| 80 | + echo "Reusing cached target pythons for $libc" |
| 81 | + return |
| 82 | + fi |
| 83 | + echo "Pulling $image and copying /opt/python ($libc)" |
| 84 | + docker pull --platform linux/arm/v7 "$image" |
| 85 | + local cid |
| 86 | + cid="$(docker create --platform linux/arm/v7 "$image")" |
| 87 | + mkdir -p "$dest" |
| 88 | + docker cp "$cid:/opt/python/." "$dest/" |
| 89 | + docker rm -f "$cid" >/dev/null |
| 90 | +} |
| 91 | + |
| 92 | +# --- build one wheel ------------------------------------------------------ |
| 93 | +# args: <libc> <host_py> <py_glob> <triple> <plat_tag> <vendor_platform> |
| 94 | +build_one() { |
| 95 | + local libc="$1" host_py="$2" py_glob="$3" triple="$4" plat="$5" vendor_plat="$6" |
| 96 | + |
| 97 | + local pytree |
| 98 | + pytree="$(ls -d "$WORK_DIR/py/$libc/"$py_glob 2>/dev/null | head -1 || true)" |
| 99 | + if [[ -z "$pytree" || ! -d "$pytree" ]]; then |
| 100 | + echo "ERROR: no target python matching '$py_glob' for $libc" >&2 |
| 101 | + echo " (check that the image still ships this build)" >&2 |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + |
| 105 | + local incdirs=( "$pytree"/include/python3.* ) |
| 106 | + local incdir="${incdirs[0]}" |
| 107 | + local scds=( "$pytree"/lib/python3.*/_sysconfigdata_*.py ) |
| 108 | + local scd_file="${scds[0]}" |
| 109 | + local scd_dir scd_name |
| 110 | + scd_dir="$(dirname "$scd_file")" |
| 111 | + scd_name="$(basename "$scd_file" .py)" |
| 112 | + |
| 113 | + echo "=== $plat / $(basename "$pytree") ===" |
| 114 | + echo " host python : $host_py" |
| 115 | + echo " zig target : $triple" |
| 116 | + echo " headers : $incdir" |
| 117 | + echo " sysconfig : $scd_name" |
| 118 | + |
| 119 | + # Fetch the matching prebuilt FFmpeg for armv7l. |
| 120 | + local vendor="$WORK_DIR/vendor/$vendor_plat" |
| 121 | + PYAV_VENDOR_PLATFORM="$vendor_plat" "$TOOLS_PY" scripts/fetch-vendor.py \ |
| 122 | + --config-file "$FFMPEG_CONFIG" "$vendor" |
| 123 | + |
| 124 | + local raw="$WORK_DIR/raw/$libc-$(basename "$pytree")" |
| 125 | + rm -rf "$raw"; mkdir -p "$raw" |
| 126 | + |
| 127 | + env \ |
| 128 | + ZIG_TARGET="$triple" \ |
| 129 | + CC="$BIN_DIR/zig-cc" \ |
| 130 | + CXX="$BIN_DIR/zig-cxx" \ |
| 131 | + AR="$BIN_DIR/zig-ar" \ |
| 132 | + LDSHARED="$BIN_DIR/zig-cc -shared" \ |
| 133 | + _PYTHON_HOST_PLATFORM="linux-armv7l" \ |
| 134 | + _PYTHON_SYSCONFIGDATA_NAME="$scd_name" \ |
| 135 | + PYTHONPATH="$scd_dir" \ |
| 136 | + CFLAGS="-I$incdir -Wno-error=incompatible-pointer-types" \ |
| 137 | + PKG_CONFIG_PATH="$vendor/lib/pkgconfig" \ |
| 138 | + LD_LIBRARY_PATH="$vendor/lib" \ |
| 139 | + "$host_py" -m pip wheel . --no-build-isolation --no-deps -w "$raw" |
| 140 | + |
| 141 | + # Bundle the FFmpeg shared libs and stamp the platform tag. auditwheel and |
| 142 | + # patchelf are arch-agnostic, so this works on the x86_64 host. |
| 143 | + LD_LIBRARY_PATH="$vendor/lib" "$TOOLS_PY" -m auditwheel repair \ |
| 144 | + --plat "$plat" -w "$DIST_DIR" "$raw"/*.whl |
| 145 | +} |
| 146 | + |
| 147 | +# --- run ------------------------------------------------------------------ |
| 148 | +extract_pythons glibc "$MANYLINUX_IMAGE" |
| 149 | +extract_pythons musl "$MUSLLINUX_IMAGE" |
| 150 | + |
| 151 | +build_one glibc "$HOST_PY311" "cp311-cp311" "arm-linux-gnueabihf.$GLIBC_VER" "manylinux_2_31_armv7l" "manylinux-armv7l" |
| 152 | +build_one musl "$HOST_PY311" "cp311-cp311" "arm-linux-musleabihf" "musllinux_1_2_armv7l" "musllinux-armv7l" |
| 153 | +build_one glibc "$HOST_PY314T" "cp314*-cp314t" "arm-linux-gnueabihf.$GLIBC_VER" "manylinux_2_31_armv7l" "manylinux-armv7l" |
| 154 | +build_one musl "$HOST_PY314T" "cp314*-cp314t" "arm-linux-musleabihf" "musllinux_1_2_armv7l" "musllinux-armv7l" |
| 155 | + |
| 156 | +echo |
| 157 | +echo "Built armv7l wheels:" |
| 158 | +ls -1 "$DIST_DIR"/*armv7l*.whl |
0 commit comments