Skip to content

Commit 14d916d

Browse files
Add libusb package (#56)
1 parent 62afc9e commit 14d916d

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

libusb/build.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
5+
cd "$DIR"
6+
7+
VERSION="1.0.29"
8+
ARCHIVE="libusb-${VERSION}.tar.bz2"
9+
URL="https://github.com/libusb/libusb/releases/download/v${VERSION}/${ARCHIVE}"
10+
INSTALL_DIR="$DIR/libusb/install"
11+
SRC_DIR="$DIR/libusb-src"
12+
VERSION_FILE="$SRC_DIR/.version"
13+
14+
NJOBS="$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)"
15+
export CC="ccache ${CC:-cc}"
16+
17+
if [ ! -f "$VERSION_FILE" ] || [ "$(cat "$VERSION_FILE")" != "$VERSION" ]; then
18+
rm -rf "$SRC_DIR"
19+
mkdir -p "$SRC_DIR"
20+
curl -fSL "$URL" | tar xj --strip-components=1 -C "$SRC_DIR"
21+
echo "$VERSION" > "$VERSION_FILE"
22+
fi
23+
24+
PREFIX="$DIR/build/prefix"
25+
rm -rf "$DIR/build"
26+
mkdir -p "$DIR/build"
27+
28+
CONFIGURE_ARGS=(
29+
--prefix="$PREFIX"
30+
--disable-shared
31+
--enable-static
32+
)
33+
34+
if [ "$(uname)" = "Linux" ]; then
35+
CONFIGURE_ARGS+=(--disable-udev)
36+
fi
37+
38+
cd "$SRC_DIR"
39+
CFLAGS="-O2 -fPIC" ./configure "${CONFIGURE_ARGS[@]}"
40+
make -j"$NJOBS"
41+
make install
42+
cd "$DIR"
43+
44+
rm -rf "$INSTALL_DIR"
45+
mkdir -p "$INSTALL_DIR/lib/pkgconfig" "$INSTALL_DIR/include/libusb-1.0"
46+
47+
cp "$PREFIX/lib/libusb-1.0.a" "$INSTALL_DIR/lib/"
48+
cp "$PREFIX/lib/pkgconfig/libusb-1.0.pc" "$INSTALL_DIR/lib/pkgconfig/"
49+
cp "$PREFIX/include/libusb-1.0/libusb.h" "$INSTALL_DIR/include/libusb-1.0/"
50+
51+
echo "Installed libusb to $INSTALL_DIR"
52+
du -sh "$INSTALL_DIR"

libusb/libusb/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
3+
DIR = os.path.join(os.path.dirname(__file__), "install")
4+
LIB_DIR = os.path.join(DIR, "lib")
5+
INCLUDE_DIR = os.path.join(DIR, "include")
6+
PKGCONFIG_DIR = os.path.join(LIB_DIR, "pkgconfig")
7+
8+
9+
def smoketest():
10+
assert os.path.isfile(os.path.join(LIB_DIR, "libusb-1.0.a")), "libusb-1.0.a not found"
11+
assert os.path.isfile(os.path.join(INCLUDE_DIR, "libusb-1.0", "libusb.h")), "libusb.h not found"
12+
assert os.path.isfile(os.path.join(PKGCONFIG_DIR, "libusb-1.0.pc")), "libusb-1.0.pc not found"

libusb/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[build-system]
2+
requires = ["setuptools>=64", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "libusb"
7+
version = "1.0.29"
8+
description = "libusb USB device access library (static build)"
9+
requires-python = ">=3.8"
10+
11+
[tool.setuptools.packages.find]
12+
include = ["libusb*"]
13+
14+
[tool.setuptools.package-data]
15+
libusb = ["install/**/*"]

libusb/setup.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import os
2+
import platform
3+
import subprocess
4+
5+
from setuptools.command.build_py import build_py
6+
7+
try:
8+
from wheel.bdist_wheel import bdist_wheel
9+
except ImportError:
10+
bdist_wheel = None
11+
12+
13+
class BuildLibusb(build_py):
14+
"""Run build.sh to compile libusb before collecting package data."""
15+
16+
def run(self):
17+
pkg_dir = os.path.dirname(os.path.abspath(__file__))
18+
build_script = os.path.join(pkg_dir, "build.sh")
19+
subprocess.check_call(["bash", build_script], cwd=pkg_dir)
20+
21+
super().run()
22+
23+
24+
cmdclass = {"build_py": BuildLibusb}
25+
26+
if bdist_wheel is not None:
27+
28+
class PlatformWheel(bdist_wheel):
29+
"""Produce a platform-specific, Python-version-agnostic wheel."""
30+
31+
def finalize_options(self):
32+
super().finalize_options()
33+
self.root_is_pure = False
34+
35+
def get_tag(self):
36+
system = platform.system()
37+
machine = platform.machine()
38+
39+
if system == "Linux":
40+
plat = f"linux_{machine}"
41+
elif system == "Darwin":
42+
plat = "macosx_11_0_arm64"
43+
else:
44+
plat = f"{system.lower()}_{machine}"
45+
46+
return "py3", "none", plat
47+
48+
cmdclass["bdist_wheel"] = PlatformWheel
49+
50+
51+
def setup():
52+
from setuptools import setup as _setup
53+
54+
_setup(cmdclass=cmdclass)
55+
56+
57+
if __name__ == "__main__":
58+
setup()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ members = [
99
"git-lfs",
1010
"imgui",
1111
"json11",
12+
"libusb",
1213
"libjpeg",
1314
"libyuv",
1415
"nanosvg",

0 commit comments

Comments
 (0)