Skip to content

Commit db46641

Browse files
add bzip2 package (#28)
Static build of bzip2 1.0.8 (libbz2.a + bzlib.h) for use as a vendored dependency in openpilot replay/cabana tools. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8472bcb commit db46641

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

bzip2/build.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.8"
8+
INSTALL_DIR="$DIR/bzip2/install"
9+
10+
# Idempotent: skip if already built
11+
if [ -f "$INSTALL_DIR/lib/libbz2.a" ]; then
12+
echo "bzip2 already present, skipping build."
13+
exit 0
14+
fi
15+
16+
NJOBS="$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)"
17+
18+
# Download
19+
if [ ! -d "bzip2-src" ]; then
20+
curl -L "https://sourceware.org/pub/bzip2/bzip2-${VERSION}.tar.gz" -o bzip2.tar.gz
21+
mkdir -p bzip2-src
22+
tar xzf bzip2.tar.gz -C bzip2-src --strip-components=1
23+
rm bzip2.tar.gz
24+
fi
25+
26+
# Build static library with -fPIC
27+
cd bzip2-src
28+
make -j"$NJOBS" libbz2.a CC="${CC:-cc}" CFLAGS="-Wall -Winline -O2 -fPIC -D_FILE_OFFSET_BITS=64"
29+
cd "$DIR"
30+
31+
# Copy to package install dir
32+
rm -rf "$INSTALL_DIR"
33+
mkdir -p "$INSTALL_DIR"/{lib,include}
34+
35+
# Library
36+
cp bzip2-src/libbz2.a "$INSTALL_DIR/lib/"
37+
38+
# Headers
39+
cp bzip2-src/bzlib.h "$INSTALL_DIR/include/"
40+
41+
# Clean up
42+
rm -rf bzip2-src
43+
44+
echo "Installed bzip2 to $INSTALL_DIR"
45+
du -sh "$INSTALL_DIR"

bzip2/bzip2/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
7+
8+
def smoketest():
9+
assert os.path.isfile(os.path.join(LIB_DIR, "libbz2.a")), "libbz2.a not found"
10+
assert os.path.isfile(os.path.join(INCLUDE_DIR, "bzlib.h")), "bzlib.h not found"

bzip2/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 = "bzip2"
7+
version = "1.0.8"
8+
description = "bzip2 compression library (static build)"
9+
requires-python = ">=3.8"
10+
11+
[tool.setuptools.packages.find]
12+
include = ["bzip2*"]
13+
14+
[tool.setuptools.package-data]
15+
bzip2 = ["install/**/*"]

bzip2/setup.py

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

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[tool.uv.workspace]
22
members = [
3+
"bzip2",
34
"capnproto",
45
"cppcheck",
56
"eigen",

0 commit comments

Comments
 (0)