Skip to content

Commit ded8307

Browse files
committed
Revamped setup to create proper wheel out-of-the-box
We used to generate our wheels in a very hacky way, by: - manually downloading libzim binaries into a dedicated location - building the extension, manipulating the LD_LIBRARY_PATH - manipulate the build directory to place libzim binary inside - eventually creating the wheel It was: - not straightforward. - unclear what the outcome was - often bundling libzim in sdist - not integratable with other tools This now builds correct wheels and source using an in-setup-only process.
1 parent cd8766f commit ded8307

9 files changed

Lines changed: 437 additions & 147 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Changed
11+
12+
- Revamped setup to create proper wheel and sdist out-of-the-box (`python3 -m build`)
13+
814
## [3.0.0] - 2023-03-16
915

1016
### Added

MANIFEST.in

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ include pyproject.toml
55
include setup.cfg
66
include requirements-dev.txt
77
include tasks.py
8-
include lib/libzim.8.dylib
9-
include lib/libzim.so.8
10-
include libzim/*.cxx
8+
include libzim/*.cpp
119
include libzim/*.h
1210
include libzim/*.py
1311
include libzim/*.pxd
1412
include libzim/*.pyx
1513
global-exclude __pycache__/*
1614
exclude *.egg-info/*
15+
exclude libzim/*.dylib
16+
exclude libzim/*.so
17+
exclude libzim/*.so.*

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ It is primarily used in [openZIM](https://github.com/openzim/) scrapers like [`s
1818
pip install libzim
1919
```
2020

21-
The [PyPI package](https://pypi.org/project/libzim/) is available for x86_64 macOS and GNU/Linux only. It bundles a [recent release](https://download.openzim.org/release/libzim/) of the C++ libzim.
21+
Our [PyPI wheels](https://pypi.org/project/libzim/) bundle a [recent release](https://download.openzim.org/release/libzim/) of the C++ libzim and are available for the following platforms:
22+
23+
- macOS for `x86_64` and `arm64`
24+
- GNU/Linux for `x86_64`, `armhf` and `aarch64`
25+
- Linux+musl for `x86_64` and `aarch64`
26+
27+
Wheels are available for both CPython and PyPy.
28+
29+
Users on other platforms can install the source distribution (see [Building](#Building) below).
2230

23-
On other platforms, you'd have to [compile C++ libzim from
24-
source](https://github.com/openzim/libzim) first then build this one, adjusting `LD_LIBRARY_PATH`.
2531

2632
## Contributions
2733

@@ -121,6 +127,51 @@ with Creator("test.zim").config_indexing(True, "eng") as creator:
121127
creator.add_metadata(name.title(), value)
122128
```
123129

130+
## Building
131+
132+
`libzim` package building offers different behaviors via environment variables
133+
134+
| Variable | Example | Use case |
135+
| -------------------------------- | ---------------------------------------- | -------- |
136+
| `LIBZIM_DL_VERSION` | `8.1.1` or `2023-04-14` | Specify the C++ libzim binary version to download and bundle. Either a release version string or a date, in which case it downloads a nightly |
137+
| `USE_SYSTEM_LIBZIM` | `1` | Uses `LDFLAG` and `CFLAGS` to find the libzim to link against. Resulting wheel won't bundle C++ libzim. |
138+
| `DONT_DOWNLOAD_LIBZIM` | `1` | Disable downloading of C++ libzim. Place headers in `include/` and libzim dylib/so in `libzim/` if no using system libzim. It will be bundled in wheel. |
139+
| `PROFILE` | `1` | Enable profile tracing in Cython extension. Required for Cython code coverage reporting. |
140+
| `SIGN_APPLE` | `1` | Set to sign and notarize the extension for macOS. Requires following informations |
141+
| `APPLE_SIGNING_IDENTITY` | `Developer ID Application: OrgName (ID)` | Required for signing on macOS |
142+
| `APPLE_SIGNING_KEYCHAIN_PATH` | `/tmp/build.keychain` | Path to the Keychain containing the certificate to sign for macOS with |
143+
| `APPLE_SIGNING_KEYCHAIN_PROFILE` | `build` | Name of the profile in the specified Keychain |
144+
145+
### Examples
146+
147+
##### Default: downloading and bundling most appropriate libzim release binary
148+
149+
```sh
150+
python3 -m build
151+
```
152+
153+
#### Using system libzim (brew, debian or manually installed) - not bundled
154+
155+
```sh
156+
# using system-installed C++ libzim
157+
brew install libzim # macOS
158+
apt-get install libzim-devel # debian
159+
dnf install libzim-dev # fedora
160+
USE_SYSTEM_LIBZIM=1 python3 -m build --wheel
161+
162+
# using a specific C++ libzim
163+
USE_SYSTEM_LIBZIM=1 \
164+
CFLAGS="-I/usr/local/include" \
165+
LDFLAGS="-L/usr/local/lib"
166+
DYLD_LIBRARY_PATH="/usr/local/lib" \
167+
LD_LIBRARY_PATH="/usr/local/lib" \
168+
python3 -m build --wheel
169+
```
170+
171+
#### Other platforms
172+
173+
On platforms for which there is no [official binary](https://download.openzim.org/release/libzim/) available, you'd have to [compile C++ libzim from source](https://github.com/openzim/libzim) first then either use `DONT_DOWNLOAD_LIBZIM` or `USE_SYSTEM_LIBZIM`.
174+
124175

125176
## License
126177

include/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

pyproject.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = [ "setuptools >= 35.0.2", "wheel >= 0.29.0", "twine", "cython >= 0.29.32,<3.0" ]
2+
requires = [ "setuptools >= 35.0.2", "wheel >= 0.29.0", "twine", "cython >= 0.29.34,<3.0" ]
33
build-backend = "setuptools.build_meta"
44

55
[tool.black]
@@ -9,3 +9,13 @@ target-version = ['py36', 'py37', 'py38', 'py39', 'py310', 'py311']
99
[tool.pytest.ini_options]
1010
testpaths = ["tests"]
1111
pythonpath = ["."]
12+
13+
[tool.cibuildwheel]
14+
build = "*"
15+
skip = "*musllinux* *-win*"
16+
17+
[tool.cibuildwheel.linux]
18+
archs = ["x86_64", "aarch64"]
19+
20+
[tool.cibuildwheel.macos]
21+
archs = ["x86_64", "arm64"]

setup.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ setup_requires =
4747
test_requires =
4848
pytest
4949

50+
[options.package_data]
51+
libzim =
52+
libzim.8.dylib
53+
libzim.so.8
54+
5055
[isort]
5156
profile = black
5257

0 commit comments

Comments
 (0)