Skip to content

Commit

Permalink
ffi(nostr): use uniffi pro-macro instead of UDL
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Dec 6, 2023
1 parent 8d39bb6 commit 256a7bd
Show file tree
Hide file tree
Showing 35 changed files with 516 additions and 854 deletions.
19 changes: 11 additions & 8 deletions bindings/nostr-ffi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ sdk-root:
exit 1 ; \
fi

kotlin-clean:
mkdir -p ffi/kotlin/jniLibs
find ./ffi/kotlin/jniLibs -name libnostr_ffi.so -type f -delete

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
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
android: kotlin-clean aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android

aarch64-linux-android: $(SOURCES) ndk-home
cargo ndk -t aarch64-linux-android -o ffi/kotlin/jniLibs build --release
Expand Down Expand Up @@ -71,18 +74,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 +100,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,7 +109,7 @@ 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\*
cd bindings-python && python setup.py --verbose bdist_wheel
Expand Down
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 256a7bd

Please sign in to comment.