Skip to content

Commit 5611e7e

Browse files
committed
Python cross-compiling script and ABI3
Pull-Request: #7 Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
1 parent d8b87a0 commit 5611e7e

File tree

6 files changed

+131
-1
lines changed

6 files changed

+131
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525

2626
## Unreleased
2727

28+
### Changed
29+
30+
* Publish python wheels with cp39-abi3 (https://github.com/rust-nostr/nostr-sdk-ffi/pull/7)
31+
2832
### Added
2933

30-
* Add support for event streaming (https://github.com/rust-nostr/nostr-sdk-ffi/pull/6)
34+
* Add support for event streaming (https://github.com/rust-nostr/nostr-sdk-ffi/pull/6)
3135

3236
## v0.41.0 - 2025/04/15
3337

justfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ aar:
4747
jar:
4848
@cd jvm && bash assemble.sh
4949

50+
# Assemble the python wheels
51+
py:
52+
@cd python && bash assemble.sh
53+
5054
# Assemble the C# package
5155
csharp:
5256
@cd csharp && bash assemble.sh
@@ -61,6 +65,11 @@ publish-aar: aar
6165
publish-jar: jar
6266
cd jvm && ./gradlew publishAndReleaseToMavenCentral --no-configuration-cache
6367

68+
# Publish Wheels
69+
[confirm]
70+
publish-py: py
71+
cd python && twine upload dist/*
72+
6473
# Compile and build Swift Package
6574
[macos]
6675
swift:

python/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM python:3.9-slim
2+
3+
# Set up working directory
4+
WORKDIR /build
5+
6+
# Create directories for mounting volumes
7+
RUN mkdir -p python binding binaries dist
8+
9+
# Copy source
10+
COPY src/nostr-sdk/__init__.py python/src/nostr-sdk/
11+
COPY LICENSE python/
12+
COPY MANIFEST.in python/
13+
COPY pyproject.toml python/
14+
COPY README.md python/
15+
COPY requirements.txt python/
16+
COPY setup.py python/
17+
18+
# Copy the build script
19+
COPY --chown=1000:1000 buildwheel.sh .
20+
RUN chmod +x buildwheel.sh
21+
22+
# Install Python build tools
23+
RUN pip install -r python/requirements.txt
24+
25+
# Update permissions
26+
RUN chown -R 1000:1000 /build
27+
RUN chmod -R 777 /build
28+
29+
# Change user
30+
USER 1000
31+
32+
# Set the entrypoint to our build script
33+
ENTRYPOINT ["/build/buildwheel.sh"]

python/assemble.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
set -exuo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
DIST_DIR="${SCRIPT_DIR}/dist"
7+
SRC_DIR="${SCRIPT_DIR}/src/nostr-sdk"
8+
FFI_DIR="${SCRIPT_DIR}/../ffi"
9+
10+
# Clean
11+
rm -rf "${DIST_DIR}"
12+
rm -rf "${SRC_DIR}/*.so"
13+
rm -rf "${SRC_DIR}/nostr_sdk.py"
14+
15+
# Make dir
16+
mkdir -p "${DIST_DIR}"
17+
18+
# Build docker image
19+
docker build -t wheel-builder "${SCRIPT_DIR}"
20+
21+
# Generate bindings
22+
cargo run -p nostr-sdk-ffi --features uniffi-cli --bin uniffi-bindgen generate --library "${FFI_DIR}/apple/macos/x86_64/libnostr_sdk_ffi.dylib" --language python --no-format -o "${SRC_DIR}"
23+
24+
# Build linux wheels
25+
docker run --rm -v "${FFI_DIR}/linux/x86/:/build/binaries" -v "${SRC_DIR}:/build/binding" -v "$(pwd)/dist:/build/dist" -e PLAT_NAME="manylinux_2_17_i686" wheel-builder
26+
docker run --rm -v "${FFI_DIR}/linux/x86_64/:/build/binaries" -v "${SRC_DIR}:/build/binding" -v "$(pwd)/dist:/build/dist" -e PLAT_NAME="manylinux_2_17_x86_64" wheel-builder
27+
docker run --rm -v "${FFI_DIR}/linux/aarch64/:/build/binaries" -v "${SRC_DIR}:/build/binding" -v "$(pwd)/dist:/build/dist" -e PLAT_NAME="manylinux_2_17_aarch64" wheel-builder
28+
29+
# Build macos wheels
30+
docker run --rm -v "${FFI_DIR}/apple/macos/x86_64/:/build/binaries" -v "${SRC_DIR}:/build/binding" -v "$(pwd)/dist:/build/dist" -e PLAT_NAME="macosx_11_0_x86_64" wheel-builder
31+
docker run --rm -v "${FFI_DIR}/apple/macos/aarch64/:/build/binaries" -v "${SRC_DIR}:/build/binding" -v "$(pwd)/dist:/build/dist" -e PLAT_NAME="macosx_11_0_arm64" wheel-builder
32+
33+
# Build win wheels
34+
docker run --rm -v "${FFI_DIR}/win/x86/:/build/binaries" -v "${SRC_DIR}:/build/binding" -v "$(pwd)/dist:/build/dist" -e PLAT_NAME="win32" wheel-builder
35+
docker run --rm -v "${FFI_DIR}/win/x86_64/:/build/binaries" -v "${SRC_DIR}:/build/binding" -v "$(pwd)/dist:/build/dist" -e PLAT_NAME="win_amd64" wheel-builder
36+
docker run --rm -v "${FFI_DIR}/win/aarch64/:/build/binaries" -v "${SRC_DIR}:/build/binding" -v "$(pwd)/dist:/build/dist" -e PLAT_NAME="win_arm64" wheel-builder

python/buildwheel.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
# Build a wheel. This script is used from the Dockerfile!
4+
5+
set -exuo pipefail
6+
7+
# Check if required arguments are provided
8+
if [ -z "$PLAT_NAME" ]; then
9+
echo "ERROR: PLAT_NAME environment variable is required"
10+
exit 1
11+
fi
12+
13+
echo "Building wheel for platform: $PLAT_NAME"
14+
15+
# Copy binaries to python directory if they exist
16+
if [ "$(ls -A /build/binaries)" ]; then
17+
cp -r /build/binaries/* /build/python/src/nostr-sdk/
18+
echo "Copied binaries to Python package directory"
19+
else
20+
echo "No binaries found in /build/binaries"
21+
exit 1
22+
fi
23+
24+
# Copy generated binding file to the correct location if it exists
25+
if [ -f "/build/binding/nostr_sdk.py" ]; then
26+
# Make sure the target directory exists
27+
cp /build/binding/nostr_sdk.py /build/python/src/nostr-sdk/
28+
echo "Copied binding file (nostr_sdk.py) to src/nostr-sdk/ directory"
29+
else
30+
echo "WARNING: Binding file (nostr_sdk.py) not found in /build/binding"
31+
exit 1
32+
fi
33+
34+
35+
# Enter Python package directory
36+
cd /build/python
37+
38+
# Build the wheel with Python 3.9 ABI3 compatibility
39+
python setup.py bdist_wheel --plat-name "$PLAT_NAME" --python-tag cp39.abi3
40+
41+
# Copy wheel to the output directory
42+
cp dist/*.whl /build/dist/

python/setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@
2222
# This is required to ensure the library name includes the python version, abi, and platform tags
2323
# See issue #350 for more information
2424
has_ext_modules=lambda: True,
25+
# This enables abi3 compatibility
26+
options={
27+
"bdist_wheel": {
28+
"py_limited_api": "cp39", # Support Python 3.9+
29+
}
30+
},
2531
)

0 commit comments

Comments
 (0)