From 673f2a6be0678ea172e1a7aec60f370ad104939d Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 20 Oct 2026 18:11:41 +0200 Subject: [PATCH] Bundle FFprobe for macOS in release v0.1.3 --- AGENTS.md | 8 +- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 4 +- crates/pptx-reporter/ffprobe-pins.json | 10 ++ docs/portable-quickstart.txt | 28 ++++ docs/releases.md | 60 +++++++-- rust-release.toml | 10 +- scripts/build_macos_ffprobe.py | 176 +++++++++++++++++++++++++ scripts/ffprobe-macos-cc.py | 18 +++ scripts/macos_binary.py | 47 +++++++ scripts/prepare-release.py | 12 +- tests/test_macos_ffprobe.py | 122 +++++++++++++++++ tests/test_prepare_release.py | 12 +- 14 files changed, 478 insertions(+), 33 deletions(-) create mode 100644 docs/portable-quickstart.txt create mode 100644 scripts/build_macos_ffprobe.py create mode 100644 scripts/ffprobe-macos-cc.py create mode 100644 scripts/macos_binary.py create mode 100644 tests/test_macos_ffprobe.py diff --git a/AGENTS.md b/AGENTS.md index 46e6efd..5fbde09 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -32,12 +32,14 @@ with quick-start examples and links to detailed documentation in `docs/`. releases or uploads release binaries. Cross and `rust_realease` are separate, manually invoked build/release preparation tools. - `rust-release.toml` builds Windows/Linux with Cross and Apple Silicon/Intel - macOS 13+ with a pinned cargo-zigbuild container. macOS canonical archives - require external FFprobe; named portable bundles remain Windows/Linux only. + macOS 13+ with a pinned cargo-zigbuild container. Portable bundles include + FFprobe on every platform; canonical cargo-binstall archives require external FFprobe. - `scripts/fetch_ffprobe.py` fetches checksum-verified target FFprobe builds into ignored `downloads/`. Named release bundles use `scripts/prepare-release.py` with per-target pins in `crates/pptx-reporter/ffprobe-pins.json`; the standalone packager retains - `--fetch-ffprobe`. Include upstream notices/provenance, never commit binaries. + `--fetch-ffprobe`. `scripts/build_macos_ffprobe.py` builds the macOS dependency + from pinned FFmpeg source, with source, LGPL notices and the recipe bundled. + Include upstream notices/provenance, never commit binaries. ## Repository layout diff --git a/Cargo.lock b/Cargo.lock index 47af8e4..27f9563 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3190,7 +3190,7 @@ checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" [[package]] name = "pptx-reporter" -version = "0.1.2" +version = "0.1.3" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index b8d10f9..de7c7ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ default-members = ["crates/pptx-reporter"] resolver = "3" [workspace.package] -version = "0.1.2" +version = "0.1.3" edition = "2024" license = "MIT" repository = "https://proxy.dandaedre.com/gitea/Beffroi/beffroi-tools" diff --git a/README.md b/README.md index 46e70df..27c3e21 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ On Windows, extract the portable ZIP and open `pptx-reporter-gui.exe`. Drop a presentation into the window, select report formats, and generate. Keep the bundled `ffprobe.exe` beside the app. -On macOS 13+, extract the TGZ for Apple Silicon (`aarch64`) or Intel (`x86_64`) -and run `./pptx-reporter-gui`. Supply FFprobe beside the app or on PATH. +On macOS 13+, extract the portable ZIP for Apple Silicon (`aarch64`) or Intel +(`x86_64`) and run `./pptx-reporter-gui`. FFprobe is included; keep it beside the app. See [macOS release details](docs/releases.md#macos). To run from source: diff --git a/crates/pptx-reporter/ffprobe-pins.json b/crates/pptx-reporter/ffprobe-pins.json index 1690a3f..9e2030b 100644 --- a/crates/pptx-reporter/ffprobe-pins.json +++ b/crates/pptx-reporter/ffprobe-pins.json @@ -8,5 +8,15 @@ "release": "autobuild-2026-10-20-13-08", "asset": "ffmpeg-n9.0.2-29-g6dfaba896f-linux64-lgpl-9.0.tar.xz", "sha256": "5ae5939260c72671383f0a0d8785695bfe06c94c27d4daac2e63eb8576417927" + }, + "aarch64-apple-darwin": { + "release": "9.0.2", + "asset": "ffmpeg-9.0.2.tar.xz", + "sha256": "8c3850283eb25fa026482078a04051e0be17347b09ef81a0849bec15a96e002e" + }, + "x86_64-apple-darwin": { + "release": "9.0.2", + "asset": "ffmpeg-9.0.2.tar.xz", + "sha256": "8c3850283eb25fa026482078a04051e0be17347b09ef81a0849bec15a96e002e" } } diff --git a/docs/portable-quickstart.txt b/docs/portable-quickstart.txt new file mode 100644 index 0000000..6a21143 --- /dev/null +++ b/docs/portable-quickstart.txt @@ -0,0 +1,28 @@ +PPTX Reporter — portable edition + +Extract the entire archive before starting. Keep ffprobe beside the applications +and keep the LICENSES directory. No separate FFmpeg, Python, PowerPoint or PDF +compiler installation is needed. + +Windows: open pptx-reporter-gui.exe. +Linux: run ./pptx-reporter-gui from the extracted folder. +macOS 13+: choose Apple Silicon (aarch64) or Intel (x86_64), then open Terminal +in the extracted folder and run ./pptx-reporter-gui. + +Drop a PowerPoint presentation into the window, choose your report formats and +generate the reports. Original presentations are never modified. + +Command line: + ./pptx-reporter presentation.pptx --report all +On Windows, use pptx-reporter.exe instead. + +macOS: if Gatekeeper blocks this unsigned download, verify that you trust the +release, open Terminal in the extracted folder, then run: + xattr -d com.apple.quarantine pptx-reporter pptx-reporter-gui ffprobe + chmod +x pptx-reporter pptx-reporter-gui ffprobe + ./pptx-reporter-gui +An "attribute not found" message means that file is already unquarantined. +The quarantine commands do not require sudo for files you own. + +Mac builds have not yet undergone native macOS acceptance testing. +License notices and dependency provenance are included under LICENSES. diff --git a/docs/releases.md b/docs/releases.md index 7639c67..37ab699 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -58,7 +58,7 @@ Publication needs a clean checkout and a matching explicitly created/pushed version tag. The existing local `v0.1.0` marks the original trial, not subsequent changes; do not move it implicitly. -The `portable` named bundle adds a Windows ZIP and Linux `.tgz`, containing both +The `portable` named bundle adds Windows/macOS ZIPs and a Linux `.tgz`, containing both Cargo binaries, adjacent FFprobe, quick-start instructions and licenses. Canonical cargo-binstall `.tgz` archives remain unchanged and require external FFprobe. Python 3.11+ is required on the build host only. @@ -70,7 +70,7 @@ archive validation, checksums and upload. See the upstream [named-bundle contract](https://proxy.dandaedre.com/gitea/idle-org/rust_realease/raw/branch/main/docs/portable-bundles.md). `crates/pptx-reporter/ffprobe-pins.json` pins a dated BtbN release, exact non-shared -LGPL 9.0 asset name and published SHA-256 digest per target. Dated assets have +LGPL 9.0 asset name and published SHA-256 digest per Windows/Linux target. Dated assets have versioned names, unlike the downloader's default `latest` assets. To update, review the provider release and its notices, then update all three fields together. These dated releases are not marked immutable by GitHub; digest verification @@ -95,24 +95,56 @@ until those checks are complete. Jenkins remains checks/coverage/badges only. ## macOS Release builds include `aarch64-apple-darwin` (Apple Silicon) and -`x86_64-apple-darwin` (Intel), with macOS 13.0 as the minimum. Each canonical -TGZ contains `pptx-reporter` and `pptx-reporter-gui`. Extract the matching archive -and run `./pptx-reporter-gui`, or: +`x86_64-apple-darwin` (Intel), with macOS 13.0 as the minimum. Choose the +**portable ZIP** for your Mac. It contains `pptx-reporter`, `pptx-reporter-gui`, +FFprobe, notices and a quick-start guide. Extract the entire archive, then run +`./pptx-reporter-gui` in Terminal, or: ```sh ./pptx-reporter presentation.pptx --report all ``` -Supply a macOS FFprobe executable beside the reporter, under its `bin/` -directory, or on PATH. The pinned BtbN downloader and named portable bundle -currently cover Windows and Linux only. macOS archives contain no FFprobe. -PDF compilation and fonts remain embedded. +Keep FFprobe beside the applications. No separate FFmpeg, Python, PowerPoint +or PDF compiler is needed. The smaller canonical cargo-binstall TGZs contain +only the reporter applications and still require external FFprobe. These are separate architecture-specific executables, not universal binaries -or Finder `.app` bundles. They are not Developer ID signed or notarized; -Gatekeeper may require user approval before launch. Native startup, file -dialogs, drag/drop, report generation and Gatekeeper behavior still require -testing on compatible Macs. +or Finder `.app` bundles. They are not Developer ID signed or notarized. +If macOS blocks a trusted release, open Terminal in the extracted folder: + +```sh +xattr -d com.apple.quarantine pptx-reporter pptx-reporter-gui ffprobe +chmod +x pptx-reporter pptx-reporter-gui ffprobe +./pptx-reporter-gui +``` + +An attribute-not-found message means that file is already unquarantined. +Native startup, file dialogs, drag/drop, report generation and Gatekeeper +behavior still require testing on compatible Macs. + +### Bundled macOS FFprobe + +`scripts/build_macos_ffprobe.py` builds FFprobe from the exact official source +archive and SHA-256 pinned per Mac target in `ffprobe-pins.json`. It uses the +same digest-pinned cargo-zigbuild image as the reporter, enables local file +probing, and disables network input and external codec-library autodetection. +It does not enable GPL or nonfree components. The dependency is a separate +LGPL executable with statically linked FFmpeg libraries and only macOS system +libraries loaded dynamically. + +The source build needs Linux, Python 3.11+, and Podman or Docker (selected via +`CROSS_CONTAINER_ENGINE`, or Podman if available). The image must be available +to the engine. Compilation runs with container networking disabled; downloading +the pinned source occurs first on the host. First preparation can take several +minutes. The cache identity covers the source pin, target, image and recipe; +reused payloads have their hashes and Mach-O compatibility checked. A warm +cache needs no dependency download. + +Each Mac portable ZIP includes the complete FFmpeg source archive, LGPL license, +reproduction scripts and `BUILDING.txt` under `LICENSES/ffmpeg/`. The dependency +validator checks the CPU, macOS minimum and system-only library paths without +running foreign binaries. The publisher must include Mach-O support for named +bundle executables, in addition to its existing macOS build support. On the Linux release host, the same publisher routes Apple targets through the digest-pinned cargo-zigbuild image in `[build.apple]`; Cross remains responsible @@ -131,7 +163,7 @@ rust_realease . --package pptx-reporter \ rust_realease . --package pptx-reporter --compile-only ``` -Review `SHA256SUMS` and archive executable modes. The publisher checks Mach-O +Review `SHA256SUMS` and archive executable modes, including the bundled FFprobe. The publisher checks Mach-O architecture, the exact macOS deployment minimum and system-only dynamic dependencies. Foreign cargo-binstall checks install files without executing them; macOS runtime validation is explicitly skipped on Linux. diff --git a/rust-release.toml b/rust-release.toml index fc34444..0e7ef8e 100644 --- a/rust-release.toml +++ b/rust-release.toml @@ -21,21 +21,21 @@ format = "tgz" [[package.bundles]] name = "portable" -targets = ["x86_64-pc-windows-gnu", "x86_64-unknown-linux-gnu"] -formats = { x86_64-pc-windows-gnu = "zip", x86_64-unknown-linux-gnu = "tgz" } +targets = ["x86_64-pc-windows-gnu", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "x86_64-apple-darwin"] +formats = { x86_64-pc-windows-gnu = "zip", x86_64-unknown-linux-gnu = "tgz", aarch64-apple-darwin = "zip", x86_64-apple-darwin = "zip" } archive-name = "{name}-{bundle}-{target}-v{version}" root = "{name}-{bundle}-{target}-v{version}" cargo-binaries = "all" files = [ { source = "LICENSE", destination = "LICENSES/reporter-MIT.txt", workspace-relative = true }, { source = "assets/fonts/LICENSE", destination = "LICENSES/DejaVu.txt" }, - { source = "docs/windows-quickstart.txt", destination = "README.txt", workspace-relative = true }, + { source = "docs/portable-quickstart.txt", destination = "README.txt", workspace-relative = true }, ] [package.bundles.prepare] command = ["python3", "../../scripts/prepare-release.py", "--context", "{context-file}", "--manifest", "{manifest-file}"] -inputs = ["ffprobe-pins.json", "workspace:scripts/prepare-release.py", "workspace:scripts/fetch_ffprobe.py"] -timeout-seconds = 600 +inputs = ["ffprobe-pins.json", "workspace:scripts/prepare-release.py", "workspace:scripts/fetch_ffprobe.py", "workspace:scripts/build_macos_ffprobe.py", "workspace:scripts/ffprobe-macos-cc.py", "workspace:scripts/macos_binary.py"] +timeout-seconds = 1800 required-destinations = ["ffprobe{binary-ext}", "LICENSES/ffmpeg/LICENSE.txt", "LICENSES/ffmpeg/provenance.json"] [[repository.fallbacks]] diff --git a/scripts/build_macos_ffprobe.py b/scripts/build_macos_ffprobe.py new file mode 100644 index 0000000..a416e00 --- /dev/null +++ b/scripts/build_macos_ffprobe.py @@ -0,0 +1,176 @@ +#!/usr/bin/env python3 +"""Build standalone LGPL macOS FFprobe from checksum-pinned official source.""" +import argparse +import hashlib +import json +import os +from pathlib import Path, PurePosixPath +import platform +import re +import shlex +import shutil +import subprocess +import tarfile +import tempfile + +from fetch_ffprobe import download, sha256 +from macos_binary import validate_macos + +SCRIPTS = Path(__file__).resolve().parent +IMAGE = "ghcr.io/rust-cross/cargo-zigbuild@sha256:9b4f6b3eb9e8f9fefb3960fe288ddba576627d66d9eff851ce7f62d2455564a5" +TARGETS = {"aarch64-apple-darwin": "aarch64", "x86_64-apple-darwin": "x86_64"} +RECIPE_FILES = ("build_macos_ffprobe.py", "ffprobe-macos-cc.py", "fetch_ffprobe.py", "macos_binary.py") +FLAGS = [ + "--target-os=darwin", "--enable-cross-compile", "--cc=python3 /work/cc.py", + "--ar=zig ar", "--ranlib=zig ranlib", "--disable-autodetect", + "--disable-network", "--disable-protocols", "--enable-protocol=file", + "--disable-programs", "--enable-ffprobe", "--disable-doc", "--disable-debug", + "--disable-asm", "--disable-encoders", "--disable-muxers", "--disable-filters", + "--disable-avdevice", "--disable-shared", "--enable-static", "--disable-stripping", +] + + +def regular_under(root, relative): + path = PurePosixPath(relative) + if (not path.parts or path.is_absolute() or any(p in ("", ".", "..") for p in relative.split("/")) + or "\\" in relative or ":" in relative): + raise ValueError("Unsafe FFprobe cache receipt path") + current = root + for part in path.parts: + current /= part + if current.is_symlink(): + raise ValueError("Symlinked FFprobe cache input") + if not current.is_file(): + raise ValueError(f"Missing cached FFprobe input: {relative}") + return current + + +def recipe_digest(pin, target): + identity = json.dumps({"pin": pin, "target": target, "image": IMAGE, "flags": FLAGS}, + sort_keys=True).encode() + for name in RECIPE_FILES: + identity += (SCRIPTS / name).read_bytes() + return hashlib.sha256(identity).hexdigest() + + +def extract_source(archive, directory, release): + with tarfile.open(archive, "r:xz") as source: + for member in source.getmembers(): + parts = PurePosixPath(member.name).parts + if (not parts or parts[0] != f"ffmpeg-{release}" or ".." in parts + or "\\" in member.name or not (member.isfile() or member.isdir())): + raise ValueError(f"Unsafe FFmpeg source member: {member.name}") + source.extractall(directory, filter="data") + return directory / f"ffmpeg-{release}" + + +def build(target, cache, pin): + if target not in TARGETS or not re.fullmatch(r"\d+\.\d+(?:\.\d+)?", pin["release"]): + raise ValueError("Unsupported macOS FFprobe target or source version") + if pin["asset"] != f"ffmpeg-{pin['release']}.tar.xz" or not re.fullmatch(r"[a-f0-9]{64}", pin["sha256"]): + raise ValueError("FFprobe requires an exact official source archive and SHA-256") + identity = recipe_digest(pin, target) + cache = Path(cache).resolve() + folder = cache / target / identity + binary, notices = folder / "ffprobe", folder / "licenses" + if folder.exists(): + if folder.is_symlink() or folder.parent.is_symlink(): + raise ValueError("Symlinked FFprobe cache") + receipt = json.loads(regular_under(folder, "licenses/provenance.json").read_text()) + if (receipt["recipe_sha256"] != identity or receipt["source_sha256"] != pin["sha256"] + or "ffprobe" not in receipt["files"] + or "licenses/COPYING.LGPLv2.1" not in receipt["files"] + or f"licenses/{pin['asset']}" not in receipt["files"]): + raise ValueError("Invalid FFprobe cache receipt") + for name, digest in receipt["files"].items(): + if sha256(regular_under(folder, name)) != digest: + raise ValueError("Corrupt macOS FFprobe cache; remove it and retry") + validate_macos(binary, target) + print(f"Using verified macOS FFprobe cache: {binary}", flush=True) + return binary, notices + + engine = os.environ.get("CROSS_CONTAINER_ENGINE") + if engine is None: + engine = "podman" if shutil.which("podman") else "docker" + if engine not in ("podman", "docker"): + raise ValueError("Use podman or docker for the macOS FFprobe source build") + cache.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(prefix=".source-", dir=cache) as temporary: + work = Path(temporary) + archive = work / pin["asset"] + url = f"https://ffmpeg.org/releases/{pin['asset']}" + print(f"Downloading pinned FFmpeg source: {url}", flush=True) + download(url, archive) + if sha256(archive) != pin["sha256"]: + raise ValueError("FFmpeg source SHA-256 mismatch") + source = extract_source(archive, work, pin["release"]) + shutil.copyfile(SCRIPTS / "ffprobe-macos-cc.py", work / "cc.py") + configure = ["./configure", "--arch=" + TARGETS[target], *FLAGS] + command = [ + engine, "run", "--rm", "--network=none", "--user", f"{os.getuid()}:{os.getgid()}", + ] + if engine == "podman": + command.append("--userns=keep-id") + command += [ + "-v", f"{work}:/work", "-w", f"/work/ffmpeg-{pin['release']}", + "-e", "ZIG_GLOBAL_CACHE_DIR=/work/zig-cache", "-e", f"FFPROBE_ARCH={TARGETS[target]}", + IMAGE, "sh", "-c", shlex.join(configure) + " && make -j16 ffprobe", + ] + print(f"Building LGPL FFprobe for {target}", flush=True) + subprocess.run(command, check=True, timeout=1500) + configuration = (source / "config.h").read_text() + for feature in ("GPL", "GPLV3", "NONFREE"): + if f"#define CONFIG_{feature} 0" not in configuration: + raise ValueError(f"Unexpected FFmpeg license configuration: {feature}") + checks = validate_macos(source / "ffprobe", target) + stage = work / "payload" + staged_notices = stage / "licenses" + staged_notices.mkdir(parents=True) + shutil.copyfile(source / "ffprobe", stage / "ffprobe") + (stage / "ffprobe").chmod(0o755) + for name in ("COPYING.LGPLv2.1", "LICENSE.md"): + shutil.copyfile(source / name, staged_notices / name) + shutil.copyfile(archive, staged_notices / pin["asset"]) + for name in RECIPE_FILES: + shutil.copyfile(SCRIPTS / name, staged_notices / name) + (staged_notices / "ffprobe-pins.json").write_text(json.dumps({target: pin}, indent=2) + "\n") + (staged_notices / "BUILDING.txt").write_text( + "FFprobe is a separate LGPL-2.1-or-later executable built from unmodified FFmpeg source.\n" + "The complete source archive and build scripts are included here.\n" + "On a Linux host with Python 3.11+ and Podman/Docker, reproduce with:\n" + f"python3 build_macos_ffprobe.py --target {target} --pins ffprobe-pins.json --cache ./build-cache\n" + "The container image is pinned by digest in the build script.\n" + "Only local-file input is enabled; no separate third-party codec libraries are bundled.\n" + ) + native_version = None + if platform.system() == "Darwin" and platform.machine().lower() == ( + "arm64" if target.startswith("aarch64") else "x86_64"): + result = subprocess.run([str(stage / "ffprobe"), "-version"], check=True, + capture_output=True, text=True, timeout=30) + native_version = result.stdout.splitlines()[0] + if not native_version.startswith("ffprobe version "): + raise ValueError("Executable did not identify itself as FFprobe") + receipt = { + "provider": "https://ffmpeg.org", "release": pin["release"], "target": target, + "url": url, "source_sha256": pin["sha256"], "recipe_sha256": identity, + "image": IMAGE, "configure": configure, "checks": checks, + "native_version_check": native_version, + "files": {p.relative_to(stage).as_posix(): sha256(p) for p in stage.rglob("*") if p.is_file()}, + } + (staged_notices / "provenance.json").write_text(json.dumps(receipt, indent=2) + "\n") + folder.parent.mkdir(parents=True, exist_ok=True) + stage.rename(folder) + return binary, notices + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--target", required=True, choices=TARGETS) + parser.add_argument("--cache", type=Path, required=True) + parser.add_argument("--pins", type=Path, required=True) + args = parser.parse_args() + build(args.target, args.cache, json.loads(args.pins.read_text())[args.target]) + + +if __name__ == "__main__": + main() diff --git a/scripts/ffprobe-macos-cc.py b/scripts/ffprobe-macos-cc.py new file mode 100644 index 0000000..d6e339b --- /dev/null +++ b/scripts/ffprobe-macos-cc.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +"""Adapt FFmpeg's Darwin driver flags to the pinned Zig compiler.""" +import os +import sys + + +def main(): + arch = os.environ["FFPROBE_ARCH"] + if arch not in ("aarch64", "x86_64"): + raise ValueError("Unsupported FFprobe architecture") + # Zig's Mach-O linker selects dynamic/system-library lookup itself. + arguments = [arg for arg in sys.argv[1:] if arg != "-Wl,-dynamic,-search_paths_first"] + os.execvp("zig", ["zig", "cc", "-target", f"{arch}-macos.13.0", + "-isysroot", "/opt/MacOSX11.3.sdk", *arguments]) + + +if __name__ == "__main__": + main() diff --git a/scripts/macos_binary.py b/scripts/macos_binary.py new file mode 100644 index 0000000..3c552a8 --- /dev/null +++ b/scripts/macos_binary.py @@ -0,0 +1,47 @@ +"""Inspect thin macOS executables without running foreign code.""" +import struct + + +def validate_macos(path, target, maximum_minimum=(13, 0, 0)): + cpus = {"aarch64-apple-darwin": 0x100000C, "x86_64-apple-darwin": 0x1000007} + with path.open("rb") as stream: + header = stream.read(32) + if len(header) != 32: + raise ValueError("Truncated Mach-O header") + magic, cpu, _, kind, count, size, _, _ = struct.unpack("<8I", header) + if magic != 0xFEEDFACF or cpu != cpus.get(target) or kind != 2: + raise ValueError("Expected a matching thin 64-bit Mach-O executable") + if size > 16 * 1024 * 1024: + raise ValueError("Mach-O load commands exceed limit") + commands = stream.read(size) + if len(commands) != size: + raise ValueError("Truncated Mach-O load commands") + offset = 0 + minimum = None + dependencies = [] + for _ in range(count): + if offset + 8 > size: + raise ValueError("Truncated Mach-O command") + command, length = struct.unpack_from("<2I", commands, offset) + if length < 8 or offset + length > size: + raise ValueError("Invalid Mach-O command size") + data = commands[offset:offset + length] + if command == 0x32: + if length < 24 or struct.unpack_from("> 16, (encoded >> 8) & 255, encoded & 255) + elif command in (0xC, 0x80000018, 0x8000001F, 0x80000023): + if length < 24: + raise ValueError("Truncated Mach-O dependency") + start = struct.unpack_from("= length or b"\0" not in data[start:]: + raise ValueError("Invalid Mach-O dependency name") + name = data[start:].split(b"\0", 1)[0].decode("utf-8") + if not name.startswith(("/usr/lib/", "/System/Library/")) or ".." in name.split("/"): + raise ValueError(f"Non-system macOS dependency: {name}") + dependencies.append(name) + offset += length + if offset != size or minimum is None or minimum > maximum_minimum or not dependencies: + raise ValueError("Invalid or incompatible macOS deployment/load commands") + return {"minimum_macos": ".".join(map(str, minimum)), "dependencies": dependencies} diff --git a/scripts/prepare-release.py b/scripts/prepare-release.py index 4642598..9739afa 100644 --- a/scripts/prepare-release.py +++ b/scripts/prepare-release.py @@ -9,6 +9,7 @@ import sys sys.path.insert(0, str(Path(__file__).resolve().parent)) from fetch_ffprobe import fetch, sha256 +from build_macos_ffprobe import build as build_macos def prepare(context, manifest): @@ -20,10 +21,13 @@ def prepare(context, manifest): pin = pins[context["target"]] if pin["release"] == "latest" or not re.fullmatch(r"[0-9a-f]{64}", pin["sha256"]): raise ValueError("FFprobe requires a dated release and SHA-256 pin") - binary, notices = fetch( - context["target"], Path(context["cache_directory"]), - pin["release"], pin["sha256"], asset_name=pin["asset"], - ) + if context["target"].endswith("-apple-darwin"): + binary, notices = build_macos(context["target"], Path(context["cache_directory"]), pin) + else: + binary, notices = fetch( + context["target"], Path(context["cache_directory"]), + pin["release"], pin["sha256"], asset_name=pin["asset"], + ) out = Path(context["output_directory"]) files = [] diff --git a/tests/test_macos_ffprobe.py b/tests/test_macos_ffprobe.py new file mode 100644 index 0000000..e2f0331 --- /dev/null +++ b/tests/test_macos_ffprobe.py @@ -0,0 +1,122 @@ +import hashlib +import io +import json +from pathlib import Path +import struct +import sys +import tarfile +import tempfile +import unittest +from unittest.mock import patch + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +import build_macos_ffprobe as builder +from macos_binary import validate_macos + + +def macho(cpu=0x100000C, minimum=13 << 16, dependency="/usr/lib/libSystem.B.dylib"): + name = dependency.encode() + b"\0" + length = (24 + len(name) + 7) // 8 * 8 + version = struct.pack("<6I", 0x32, 24, 1, minimum, 0, 0) + dylib = struct.pack("<6I", 0xC, length, 24, 0, 0, 0) + name + dylib += b"\0" * (length - len(dylib)) + return struct.pack("<8I", 0xFEEDFACF, cpu, 0, 2, 2, len(version + dylib), 0, 0) + version + dylib + + +def source_archive(): + stream = io.BytesIO() + with tarfile.open(fileobj=stream, mode="w:xz") as archive: + for name, data in [("COPYING.LGPLv2.1", b"license"), ("LICENSE.md", b"notices")]: + entry = tarfile.TarInfo("ffmpeg-9.0.2/" + name) + entry.size = len(data) + archive.addfile(entry, io.BytesIO(data)) + return stream.getvalue() + + +class MacosProbeTests(unittest.TestCase): + def test_macho_compatibility_and_dependency_checks(self): + with tempfile.TemporaryDirectory() as temp: + path = Path(temp) / "ffprobe" + for target, cpu in builder.TARGETS.items(): + code = 0x100000C if cpu == "aarch64" else 0x1000007 + path.write_bytes(macho(code)) + self.assertEqual(validate_macos(path, target)["minimum_macos"], "13.0.0") + invalid = [ + macho(0x1000007), macho(minimum=14 << 16), + macho(dependency="@rpath/libavcodec.dylib"), macho(dependency="/tmp/library"), + macho(dependency="/usr/lib/../../tmp/library"), macho()[:20], macho()[:-5], + ] + for data in invalid: + with self.subTest(data=data[:32]): + path.write_bytes(data) + with self.assertRaises(ValueError): + validate_macos(path, "aarch64-apple-darwin") + data = bytearray(macho()) + struct.pack_into("