diff --git a/libusb/build.sh b/libusb/build.sh new file mode 100755 index 0000000..84a900d --- /dev/null +++ b/libusb/build.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +set -e + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)" +cd "$DIR" + +VERSION="1.0.29" +ARCHIVE="libusb-${VERSION}.tar.bz2" +URL="https://github.com/libusb/libusb/releases/download/v${VERSION}/${ARCHIVE}" +INSTALL_DIR="$DIR/libusb/install" +SRC_DIR="$DIR/libusb-src" +VERSION_FILE="$SRC_DIR/.version" + +NJOBS="$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)" +export CC="ccache ${CC:-cc}" + +if [ ! -f "$VERSION_FILE" ] || [ "$(cat "$VERSION_FILE")" != "$VERSION" ]; then + rm -rf "$SRC_DIR" + mkdir -p "$SRC_DIR" + curl -fSL "$URL" | tar xj --strip-components=1 -C "$SRC_DIR" + echo "$VERSION" > "$VERSION_FILE" +fi + +PREFIX="$DIR/build/prefix" +rm -rf "$DIR/build" +mkdir -p "$DIR/build" + +CONFIGURE_ARGS=( + --prefix="$PREFIX" + --disable-shared + --enable-static +) + +if [ "$(uname)" = "Linux" ]; then + CONFIGURE_ARGS+=(--disable-udev) +fi + +cd "$SRC_DIR" +CFLAGS="-O2 -fPIC" ./configure "${CONFIGURE_ARGS[@]}" +make -j"$NJOBS" +make install +cd "$DIR" + +rm -rf "$INSTALL_DIR" +mkdir -p "$INSTALL_DIR/lib/pkgconfig" "$INSTALL_DIR/include/libusb-1.0" + +cp "$PREFIX/lib/libusb-1.0.a" "$INSTALL_DIR/lib/" +cp "$PREFIX/lib/pkgconfig/libusb-1.0.pc" "$INSTALL_DIR/lib/pkgconfig/" +cp "$PREFIX/include/libusb-1.0/libusb.h" "$INSTALL_DIR/include/libusb-1.0/" + +echo "Installed libusb to $INSTALL_DIR" +du -sh "$INSTALL_DIR" diff --git a/libusb/libusb/__init__.py b/libusb/libusb/__init__.py new file mode 100644 index 0000000..5bab3e4 --- /dev/null +++ b/libusb/libusb/__init__.py @@ -0,0 +1,12 @@ +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") +PKGCONFIG_DIR = os.path.join(LIB_DIR, "pkgconfig") + + +def smoketest(): + assert os.path.isfile(os.path.join(LIB_DIR, "libusb-1.0.a")), "libusb-1.0.a not found" + assert os.path.isfile(os.path.join(INCLUDE_DIR, "libusb-1.0", "libusb.h")), "libusb.h not found" + assert os.path.isfile(os.path.join(PKGCONFIG_DIR, "libusb-1.0.pc")), "libusb-1.0.pc not found" diff --git a/libusb/pyproject.toml b/libusb/pyproject.toml new file mode 100644 index 0000000..9b17ab1 --- /dev/null +++ b/libusb/pyproject.toml @@ -0,0 +1,15 @@ +[build-system] +requires = ["setuptools>=64", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "libusb" +version = "1.0.29" +description = "libusb USB device access library (static build)" +requires-python = ">=3.8" + +[tool.setuptools.packages.find] +include = ["libusb*"] + +[tool.setuptools.package-data] +libusb = ["install/**/*"] diff --git a/libusb/setup.py b/libusb/setup.py new file mode 100644 index 0000000..eb354f5 --- /dev/null +++ b/libusb/setup.py @@ -0,0 +1,58 @@ +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 BuildLibusb(build_py): + """Run build.sh to compile libusb before collecting package data.""" + + def run(self): + pkg_dir = os.path.dirname(os.path.abspath(__file__)) + build_script = os.path.join(pkg_dir, "build.sh") + subprocess.check_call(["bash", build_script], cwd=pkg_dir) + + super().run() + + +cmdclass = {"build_py": BuildLibusb} + +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 141a962..7a3f93d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ members = [ "git-lfs", "imgui", "json11", + "libusb", "libjpeg", "libyuv", "nanosvg",