diff --git a/libyuv/build.sh b/libyuv/build.sh new file mode 100755 index 0000000..c36a654 --- /dev/null +++ b/libyuv/build.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +set -euo pipefail + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)" +cd "$DIR" + +VERSION="6067afde563c3946eebd94f146b3824ab7a97a9c" +INSTALL_DIR="$DIR/libyuv/install" +VERSION_FILE="$INSTALL_DIR/VERSION" + +# Idempotent: skip if already built at this source revision. +if [ -f "$INSTALL_DIR/lib/libyuv.a" ] && [ -f "$VERSION_FILE" ] && [ "$(cat "$VERSION_FILE")" = "$VERSION" ]; then + echo "libyuv already present, skipping build." + exit 0 +fi + +NJOBS="$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)" + +if [ ! -d "libyuv-src/.git" ]; then + git clone https://chromium.googlesource.com/libyuv/libyuv libyuv-src +fi + +git -C libyuv-src fetch --force origin +git -C libyuv-src checkout --force "$VERSION" + +BUILD_DIR="$DIR/build/build" +rm -rf "$DIR/build" +mkdir -p "$BUILD_DIR" + +cmake -S "$DIR/libyuv-src" -B "$BUILD_DIR" \ + -DCMAKE_BUILD_TYPE=MinSizeRel \ + -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF \ + -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ + -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_C_FLAGS="-fPIC" \ + -DCMAKE_CXX_FLAGS="-fPIC" + +cmake --build "$BUILD_DIR" -j"$NJOBS" + +LIBYUV_STATIC="$(find "$BUILD_DIR" -name "libyuv.a" -type f | head -n 1)" +if [ -z "$LIBYUV_STATIC" ]; then + echo "libyuv.a not found in build output" >&2 + exit 1 +fi + +rm -rf "$INSTALL_DIR" +mkdir -p "$INSTALL_DIR"/{lib,include} +cp "$LIBYUV_STATIC" "$INSTALL_DIR/lib/libyuv.a" +cp -r "$DIR/libyuv-src/include/." "$INSTALL_DIR/include/" +echo "$VERSION" > "$VERSION_FILE" + +# Keep workspace small and deterministic across builds. +rm -rf "$DIR/libyuv-src" "$DIR/build" + +echo "Installed libyuv to $INSTALL_DIR" +du -sh "$INSTALL_DIR" diff --git a/libyuv/libyuv/__init__.py b/libyuv/libyuv/__init__.py new file mode 100644 index 0000000..5b9aa99 --- /dev/null +++ b/libyuv/libyuv/__init__.py @@ -0,0 +1,11 @@ +import os + +DIR = os.path.join(os.path.dirname(__file__), "install") +LIB_DIR = os.path.join(DIR, "lib") +INCLUDE_DIR = os.path.join(DIR, "include") + + +def smoketest(): + assert os.path.isfile(os.path.join(LIB_DIR, "libyuv.a")), "libyuv.a not found" + assert os.path.isfile(os.path.join(INCLUDE_DIR, "libyuv.h")), "libyuv.h not found" + assert os.path.isfile(os.path.join(INCLUDE_DIR, "libyuv", "version.h")), "libyuv/version.h not found" diff --git a/libyuv/pyproject.toml b/libyuv/pyproject.toml new file mode 100644 index 0000000..59facd7 --- /dev/null +++ b/libyuv/pyproject.toml @@ -0,0 +1,15 @@ +[build-system] +requires = ["setuptools>=64", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "libyuv" +version = "1922.0" +description = "libyuv image processing library (static build)" +requires-python = ">=3.8" + +[tool.setuptools.packages.find] +include = ["libyuv*"] + +[tool.setuptools.package-data] +libyuv = ["install/**/*"] diff --git a/libyuv/setup.py b/libyuv/setup.py new file mode 100644 index 0000000..37fd3ea --- /dev/null +++ b/libyuv/setup.py @@ -0,0 +1,61 @@ +import os +import platform +import subprocess + +from setuptools.command.build_py import build_py + +try: + from wheel.bdist_wheel import bdist_wheel +except ImportError: + bdist_wheel = None + + +class BuildLibyuv(build_py): + """Run build.sh to compile libyuv before collecting package data.""" + + def run(self): + pkg_dir = os.path.dirname(os.path.abspath(__file__)) + marker = os.path.join(pkg_dir, "libyuv", "install", "lib", "libyuv.a") + + if not os.path.exists(marker): + build_script = os.path.join(pkg_dir, "build.sh") + subprocess.check_call(["bash", build_script], cwd=pkg_dir) + + super().run() + + +cmdclass = {"build_py": BuildLibyuv} + +if bdist_wheel is not None: + + class PlatformWheel(bdist_wheel): + """Produce a platform-specific, Python-version-agnostic wheel.""" + + def finalize_options(self): + super().finalize_options() + self.root_is_pure = False + + def get_tag(self): + system = platform.system() + machine = platform.machine() + + if system == "Linux": + plat = f"linux_{machine}" + elif system == "Darwin": + plat = "macosx_11_0_arm64" + else: + plat = f"{system.lower()}_{machine}" + + return "py3", "none", plat + + cmdclass["bdist_wheel"] = PlatformWheel + + +def setup(): + from setuptools import setup as _setup + + _setup(cmdclass=cmdclass) + + +if __name__ == "__main__": + setup() diff --git a/pyproject.toml b/pyproject.toml index 7f3f202..51cbd87 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,6 +8,7 @@ members = [ "gcc-arm-none-eabi", "git-lfs", "libjpeg", + "libyuv", "ncurses", "openssl3", "python3-dev",