Skip to content

Commit

Permalink
Merge #204: ffi: use uniffi pro-macro instead of UDL
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Dec 7, 2023
2 parents 8d39bb6 + 62f7375 commit ef019ba
Show file tree
Hide file tree
Showing 66 changed files with 707 additions and 1,664 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ jobs:
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
packages_dir: dist/*/
packages-dir: dist/*/
2 changes: 1 addition & 1 deletion .github/workflows/publish-sdk-python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ jobs:
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
packages_dir: dist/*/
packages-dir: dist/*/
61 changes: 41 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ thiserror = "1.0"
tokio = { version = "1.32", default-features = false }
tracing = { version = "0.1", default-features = false }
tracing-subscriber = "0.3"
uniffi = "0.25"
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "d380d164cfdba9e091c461baa6855f0a2294ac5b" }
url-fork = { version = "3.0", default-features = false }

[profile.release]
Expand Down
24 changes: 14 additions & 10 deletions bindings/nostr-ffi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ sdk-root:
exit 1 ; \
fi

kotlin: android
find ./ffi/kotlin/jniLibs -name libnostr_sdk_ffi.so -type f -delete
cargo run -p uniffi-bindgen generate src/nostr.udl --language kotlin --no-format -o ffi/kotlin
clean-android:
rm -rf ffi/android
rm -rf ffi/kotlin

kotlin: clean-android android
cargo run -p uniffi-bindgen generate --library ../../target/x86_64-linux-android/release/libnostr_ffi.so --language kotlin --no-format -o ffi/kotlin

android: aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android

Expand All @@ -46,7 +49,8 @@ bindings-android: sdk-root kotlin
rm -rf bindings-android/lib/src/main/jniLibs
rm -rf bindings-android/lib/src/main/kotlin
cp -r ffi/kotlin/jniLibs bindings-android/lib/src/main
cp -r ffi/kotlin/nostr bindings-android/lib/src/main/kotlin/
mkdir -p bindings-android/lib/src/main/kotlin/
cp -r ffi/kotlin/rust bindings-android/lib/src/main/kotlin/
cd bindings-android && ./gradlew assemble
mkdir -p ffi/android
cp bindings-android/lib/build/outputs/aar/lib-release.aar ffi/android
Expand All @@ -71,18 +75,18 @@ darwin-universal: $(SOURCES)
lipo -create -output ../../target/darwin-universal/release/libnostr_ffi.a ../../target/aarch64-apple-darwin/release/libnostr_ffi.a ../../target/x86_64-apple-darwin/release/libnostr_ffi.a

swift-ios: ios-universal
cargo run -p uniffi-bindgen generate src/nostr.udl -l swift -o ffi/swift-ios
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-apple-ios/release/libnostr_ffi.a --language swift -o ffi/swift-ios
cp ../../target/ios-universal/release/libnostr_ffi.a ffi/swift-ios
cd ffi/swift-ios && "swiftc" "-emit-module" "-module-name" "nostr_ffi" "-Xcc" "-fmodule-map-file=$(CURRENT_DIR)/ffi/swift-ios/nostrFFI.modulemap" "-I" "." "-L" "." "-lnostr_ffi" nostr.swift

swift-darwin: darwin-universal
cargo run -p uniffi-bindgen generate src/nostr.udl -l swift -o ffi/swift-darwin
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-apple-darwin/release/libnostr_ffi.a --language swift -o ffi/swift-darwin
cp ../../target/darwin-universal/release/libnostr_ffi.dylib ffi/swift-darwin
cd ffi/swift-darwin && "swiftc" "-emit-module" "-module-name" "nostr_ffi" "-Xcc" "-fmodule-map-file=$(CURRENT_DIR)/ffi/swift-darwin/nostrFFI.modulemap" "-I" "." "-L" "." "-lnostr_ffi" nostr.swift

bindings-swift: ios-universal darwin-universal
mkdir -p bindings-swift/Sources/Nostr
cargo run -p uniffi-bindgen generate src/nostr.udl --no-format --language swift --out-dir bindings-swift/Sources/Nostr
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-apple-ios/release/libnostr_ffi.a --no-format --language swift --out-dir bindings-swift/Sources/Nostr
mv bindings-swift/Sources/Nostr/nostr.swift bindings-swift/Sources/Nostr/Nostr.swift
cp bindings-swift/Sources/Nostr/nostrFFI.h bindings-swift/nostrFFI.xcframework/ios-arm64/nostrFFI.framework/Headers
cp bindings-swift/Sources/Nostr/nostrFFI.h bindings-swift/nostrFFI.xcframework/ios-arm64_x86_64-simulator/nostrFFI.framework/Headers
Expand All @@ -97,7 +101,7 @@ python:
rm -rf bindings-python/dist
pip install -r bindings-python/requirements.txt
cargo build --release
cargo run -p uniffi-bindgen generate src/nostr.udl --language python --no-format -o bindings-python/src/nostr/
cargo run -p uniffi-bindgen generate --library ../../target/release/libnostr_ffi.so --language python --no-format -o bindings-python/src/nostr/
cp ../../target/release/libnostr_ffi.so bindings-python/src/nostr/ | true
cp ../../target/release/libnostr_ffi.dylib bindings-python/src/nostr/ | true
cd bindings-python && python setup.py --verbose bdist_wheel
Expand All @@ -106,8 +110,8 @@ python:
python-win:
pip install -r bindings-python\requirements.txt
cargo build --release
cargo run -p uniffi-bindgen generate src/nostr.udl --language python --no-format -o bindings-python/src/nostr/
cargo run -p uniffi-bindgen generate --library ..\..\target\release\nostr_ffi.dll --language python --no-format -o bindings-python/src/nostr/
copy ..\..\target\release\nostr_ffi.dll bindings-python\src\nostr
del /F /Q bindings-python\dist\*
del /F /Q bindings-python\dist\* 2>nul || exit /b 0
cd bindings-python && python setup.py --verbose bdist_wheel
FOR %%i in (.\bindings-python\dist\*.whl) DO pip install %i --force-reinstall
2 changes: 0 additions & 2 deletions bindings/nostr-ffi/bindings-android/lib/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@
-dontwarn java.awt.*
-keep class com.sun.jna.* { *; }
-keep class rust.nostr.* { *; }
-keep class nostr.** { *; }
-keepclassmembers class * extends rust.nostr.* { public *; }
-keepclassmembers class * extends nostr.** { public *; }
-keepclassmembers class * extends com.sun.jna.* { public *; }
4 changes: 4 additions & 0 deletions bindings/nostr-ffi/bindings-python/examples/git_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from nostr_protocol import NostrLibrary

hash = NostrLibrary().git_hash_version()
print(hash)
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ set -euo pipefail
python --version
pip install -r requirements.txt

echo "Generating nostr.py..."
cd ../
cargo run -p uniffi-bindgen generate src/nostr.udl --language python --no-format -o bindings-python/src/nostr/

echo "Generating native binaries..."
rustup target add aarch64-unknown-linux-gnu
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc cargo build --release --target aarch64-unknown-linux-gnu

echo "Generating nostr.py..."
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-unknown-linux-gnu/release/libnostr_ffi.so --language python --no-format -o bindings-python/src/nostr/

echo "Copying linux libnostr_ffi.so..."
cp ../../target/aarch64-unknown-linux-gnu/release/libnostr_ffi.so bindings-python/src/nostr/

Expand Down
5 changes: 3 additions & 2 deletions bindings/nostr-ffi/bindings-python/scripts/generate-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ set -euo pipefail
${PYBIN}/python --version
${PYBIN}/pip install -r requirements.txt

echo "Generating nostr.py..."
cd ../
cargo run -p uniffi-bindgen generate src/nostr.udl --language python --no-format -o bindings-python/src/nostr/

echo "Generating native binaries..."
cargo build --release

echo "Generating nostr.py..."
cargo run -p uniffi-bindgen generate --library ../../target/release/libnostr_ffi.so --language python --no-format -o bindings-python/src/nostr/

echo "Copying linux libnostr_ffi.so..."
cp ../../target/release/libnostr_ffi.so bindings-python/src/nostr/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ set -euo pipefail
python3 --version
pip install --user -r requirements.txt

echo "Generating nostr.py..."
cd ../
cargo run -p uniffi-bindgen generate src/nostr.udl --language python --no-format -o bindings-python/src/nostr/

echo "Generating native binaries..."
rustup target add aarch64-apple-darwin
cargo build --release --target aarch64-apple-darwin

echo "Generating nostr.py..."
cargo run -p uniffi-bindgen generate --library ../../target/aarch64-apple-darwin/release/libnostr_ffi.a --language python --no-format -o bindings-python/src/nostr/

echo "Copying libraries libnostr_ffi.dylib..."
cp ../../target/aarch64-apple-darwin/release/libnostr_ffi.dylib bindings-python/src/nostr/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ set -euo pipefail
python3 --version
pip install --user -r requirements.txt

echo "Generating nostr.py..."
cd ../
cargo run -p uniffi-bindgen generate src/nostr.udl --language python --no-format -o bindings-python/src/nostr/

echo "Generating native binaries..."
rustup target add x86_64-apple-darwin
cargo build --release --target x86_64-apple-darwin

echo "Generating nostr.py..."
cargo run -p uniffi-bindgen generate --library ../../target/x86_64-apple-darwin/release/libnostr_ffi.a --language python --no-format -o bindings-python/src/nostr/

echo "Copying libraries libnostr_ffi.dylib..."
cp ../../target/x86_64-apple-darwin/release/libnostr_ffi.dylib bindings-python/src/nostr/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ set -euo pipefail
python3 --version
pip install --user -r requirements.txt

echo "Generating nostr.py..."
cd ../
cargo run -p uniffi-bindgen generate src/nostr.udl --language python --no-format -o bindings-python/src/nostr/

echo "Generating native binaries..."
rustup target add x86_64-pc-windows-msvc
cargo build --release --target x86_64-pc-windows-msvc

echo "Generating nostr.py..."
cargo run -p uniffi-bindgen generate --library ../../target/x86_64-pc-windows-msvc/release/nostr_ffi.dll --language python --no-format -o bindings-python/src/nostr/

echo "Copying libraries nostr_ffi.dll..."
cp ../../target/x86_64-pc-windows-msvc/release/nostr_ffi.dll bindings-python/src/nostr/

Expand Down
6 changes: 0 additions & 6 deletions bindings/nostr-ffi/build.rs

This file was deleted.

4 changes: 3 additions & 1 deletion bindings/nostr-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use std::fmt;
use std::net::AddrParseError;

use uniffi::Error;

pub type Result<T, E = NostrError> = std::result::Result<T, E>;

#[derive(Debug)]
#[derive(Debug, Error)]
pub enum NostrError {
Generic { err: String },
}
Expand Down
Loading

0 comments on commit ef019ba

Please sign in to comment.