177 lines
8.7 KiB
Python
177 lines
8.7 KiB
Python
#!/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()
|