From 3dbb72fc319b344df9ff4bfabc4e8793b15ad08f Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Sat, 7 Nov 2026 20:59:09 -0500 Subject: [PATCH] Build nasm once for manylinux and musllinux cibuildwheel builds both Linux x86_64 variants in separate containers, so each built nasm from source. Build a static nasm on the runner instead and put it on the containers' PATH. build-ffmpeg.py uses a nasm from PATH only when it reports the version pinned in pkg.py. --- .github/workflows/build-ffmpeg.yml | 6 ++++ .gitignore | 1 + scripts/build-ffmpeg.py | 19 +++++++++++ scripts/build-nasm.py | 55 ++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 scripts/build-nasm.py diff --git a/.github/workflows/build-ffmpeg.yml b/.github/workflows/build-ffmpeg.yml index 14161a77..03e2912c 100644 --- a/.github/workflows/build-ffmpeg.yml +++ b/.github/workflows/build-ffmpeg.yml @@ -94,6 +94,10 @@ jobs: if: runner.os == 'macOS' run: | brew unlink gettext libidn2 libpng libtiff libunistring libx11 libxcb libxdmcp + # Build nasm once for both the manylinux and musllinux containers + - name: Build nasm + if: runner.os == 'Linux' && matrix.arch == 'x86_64' + run: python scripts/build-nasm.py tools/bin - uses: msys2/setup-msys2@v2 if: runner.os == 'Windows' with: @@ -110,6 +114,8 @@ jobs: CIBW_BEFORE_BUILD: python scripts/build-ffmpeg.py /tmp/vendor CIBW_BEFORE_BUILD_WINDOWS: python scripts\build-ffmpeg.py C:\cibw\vendor CIBW_BUILD: cp312-* + # cibuildwheel copies the project to /project in each container + CIBW_ENVIRONMENT_LINUX: PATH=/project/tools/bin:$PATH CIBW_REPAIR_WHEEL_COMMAND_LINUX: LD_LIBRARY_PATH=/tmp/vendor/lib:$LD_LIBRARY_PATH auditwheel repair --exclude libmvec.so.1 --exclude libmvec-2.so --exclude libmvec.so --exclude libmvec -w {dest_dir} {wheel} CIBW_REPAIR_WHEEL_COMMAND_WINDOWS: delvewheel repair --add-path C:\cibw\vendor\bin -w {dest_dir} {wheel} CIBW_TEST_COMMAND: python -c "import dummy" diff --git a/.gitignore b/.gitignore index 40d00997..9ca20aef 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /dist /output /source +/tools diff --git a/scripts/build-ffmpeg.py b/scripts/build-ffmpeg.py index 1083042c..a47ac240 100644 --- a/scripts/build-ffmpeg.py +++ b/scripts/build-ffmpeg.py @@ -82,6 +82,23 @@ def make_tarball_name() -> str: else: return "ffmpeg-unknown" +def nasm_on_path_is_pinned() -> bool: + """True if a nasm on PATH reports the version pinned in pkg.py. + + CI builds one static nasm for the Linux containers (scripts/build-nasm.py). + Any other nasm is ignored so builds always use the pinned version. + """ + nasm = shutil.which("nasm") + if nasm is None: + return False + pinned = nasm_package.source_url.split("/")[-1].removeprefix("nasm-") + pinned = pinned.split(".tar")[0] + out = subprocess.run([nasm, "-v"], capture_output=True, text=True).stdout + found = out.split()[2] if len(out.split()) > 2 else "" + print(f"nasm on PATH: {nasm} ({found}), pinned: {pinned}") + return found == pinned + + def main(): parser = argparse.ArgumentParser("build-ffmpeg") parser.add_argument("destination") @@ -132,6 +149,8 @@ def main(): tools = ["gcc", "g++", "curl", "ld", "nasm", "pkg-config"] for tool in tools: run(["where", tool]) + elif nasm_on_path_is_pinned(): + available_tools.add("nasm") if plat == "Linux" and (is_musllinux or shutil.which("xxd") is None): with log_group("install system packages"): diff --git a/scripts/build-nasm.py b/scripts/build-nasm.py new file mode 100644 index 00000000..343903c0 --- /dev/null +++ b/scripts/build-nasm.py @@ -0,0 +1,55 @@ +"""Build a static nasm binary once on the CI host. + +On the x86_64 Linux runner, cibuildwheel builds both manylinux and musllinux +in separate containers. Without this, each container would build nasm from +source. A statically linked nasm runs under both glibc and musl, so we build it +here and put it on the containers' PATH (see the workflow). build-ffmpeg.py then +uses it, because it reports the version pinned in pkg.py. +""" + +import argparse +import os +import platform +import shutil +import subprocess +import tarfile +import tempfile + +from pkg import nasm_package + + +def main() -> None: + parser = argparse.ArgumentParser("build-nasm") + parser.add_argument("destination", help="directory to put the nasm binary in") + args = parser.parse_args() + + dest_dir = os.path.abspath(args.destination) + tarball = os.path.abspath( + os.path.join("source", nasm_package.source_url.split("/")[-1]) + ) + + env = os.environ.copy() + if platform.system() == "Linux": + env["LDFLAGS"] = "-static" + + with tempfile.TemporaryDirectory() as tmp: + with tarfile.open(tarball) as tar: + tar.extractall(tmp, filter="data") + (src_dir,) = [os.path.join(tmp, name) for name in os.listdir(tmp)] + + # --without-zlib uses nasm's bundled zlib, so no system libz.a is needed + subprocess.run( + ["./configure", "--without-zlib"], cwd=src_dir, env=env, check=True + ) + subprocess.run( + ["make", f"-j{os.cpu_count()}", "nasm"], cwd=src_dir, env=env, check=True + ) + + os.makedirs(dest_dir, exist_ok=True) + shutil.copy2(os.path.join(src_dir, "nasm"), os.path.join(dest_dir, "nasm")) + + subprocess.run([os.path.join(dest_dir, "nasm"), "-v"], check=True) + + +if __name__ == "__main__": + main()