Skip to content

Commit de94a54

Browse files
committed
Move UniFFI types to own, feature-gated module
While we need quite a bit of dependencies and custom impls for UniFFI support, there is no reason to pull them in for Rust-only builds. Here we move the UniFFI-specific types and trait impls to a dedicated module and hide it behind a new `uniffi` feature.
1 parent 44dab65 commit de94a54

12 files changed

+225
-201
lines changed

.github/workflows/build.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ jobs:
2222
profile: minimal
2323
- name: Build on Rust ${{ matrix.toolchain }}
2424
run: cargo build --verbose --color always
25+
- name: Build with UniFFI support on Rust ${{ matrix.toolchain }}
26+
run: cargo build --features uniffi --verbose --color always
2527
- name: Check release build on Rust ${{ matrix.toolchain }}
2628
run: cargo check --release --verbose --color always
29+
- name: Check release build with UniFFI support on Rust ${{ matrix.toolchain }}
30+
run: cargo check --release --features uniffi --verbose --color always
31+
- name: Test on Rust ${{ matrix.toolchain }}
32+
run: cargo test
33+
- name: Test with UniFFI support on Rust ${{ matrix.toolchain }}
34+
run: cargo test --features uniffi
2735
- name: Check formatting on Rust ${{ matrix.toolchain }}
2836
if: matrix.check-fmt
2937
run: rustup component add rustfmt && cargo fmt --all -- --check
30-
- name: Test on Rust ${{ matrix.toolchain }}
31-
run: cargo test

Cargo.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ opt-level = 'z' # Optimize for size.
2727
lto = true # Enable Link Time Optimization
2828
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
2929
panic = 'abort' # Abort on panic
30-
strip = true # Strip symbols from binary*
30+
31+
[features]
32+
default = []
3133

3234
[dependencies]
3335
lightning = { version = "0.0.115", features = ["max_level_trace", "std"] }
@@ -67,7 +69,7 @@ serde_json = { version = "1.0" }
6769
tokio = { version = "1", default-features = false, features = [ "rt-multi-thread", "time", "sync" ] }
6870
esplora-client = { version = "0.4", default-features = false }
6971
libc = "0.2"
70-
uniffi = { version = "0.23.0", features = ["build"] }
72+
uniffi = { version = "0.23.0", features = ["build"], optional = true }
7173

7274
[dev-dependencies]
7375
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
@@ -76,8 +78,7 @@ proptest = "1.0.0"
7678
regex = "1.5.6"
7779

7880
[build-dependencies]
79-
uniffi = { version = "0.23.0", features = ["build", "cli"] }
80-
81+
uniffi = { version = "0.23.0", features = ["build", "cli"], optional = true }
8182

8283
[profile.release]
8384
panic = "abort"

build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
fn main() {
2+
#[cfg(feature = "uniffi")]
23
uniffi::generate_scaffolding("bindings/ldk_node.udl").unwrap();
34
}

scripts/uniffi_bindgen_generate_kotlin.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fi
1212

1313
#rustup target add aarch64-apple-darwin
1414
#cargo build --target aarch64-apple-darwin || exit 1
15-
cargo build --release || exit 1
15+
cargo build --release --features uniffi || exit 1
1616
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language kotlin -o "$TARGET_DIR" || exit 1
1717

1818
mkdir -p "$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/kotlin/"$PACKAGE_DIR" || exit 1

scripts/uniffi_bindgen_generate_kotlin_android.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ LLVM_ARCH_PATH="darwin-x86_64"
99
PATH="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$LLVM_ARCH_PATH/bin:$PATH"
1010

1111
rustup +nightly target add x86_64-linux-android aarch64-linux-android armv7-linux-androideabi
12-
#cargo build --release || exit 1
13-
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER="x86_64-linux-android21-clang" CC="x86_64-linux-android21-clang" cargo +nightly build --profile release-smaller --target x86_64-linux-android || exit 1
14-
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="armv7a-linux-androideabi21-clang" CC="armv7a-linux-androideabi21-clang" cargo +nightly build --profile release-smaller --target armv7-linux-androideabi || exit 1
15-
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="aarch64-linux-android21-clang" CC="aarch64-linux-android21-clang" cargo +nightly build --profile release-smaller --target aarch64-linux-android || exit 1
12+
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER="x86_64-linux-android21-clang" CC="x86_64-linux-android21-clang" cargo +nightly build --profile release-smaller --features uniffi --target x86_64-linux-android || exit 1
13+
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="armv7a-linux-androideabi21-clang" CC="armv7a-linux-androideabi21-clang" cargo +nightly build --profile release-smaller --features uniffi --target armv7-linux-androideabi || exit 1
14+
CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="aarch64-linux-android21-clang" CC="aarch64-linux-android21-clang" cargo +nightly build --profile release-smaller --features uniffi --target aarch64-linux-android || exit 1
1615
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language kotlin -o "$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/kotlin || exit 1
1716

1817
JNI_LIB_DIR="$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/jniLibs/

scripts/uniffi_bindgen_generate_python.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
BINDINGS_DIR="./bindings/python"
33
UNIFFI_BINDGEN_BIN="cargo +nightly run --features=uniffi/cli --bin uniffi-bindgen"
44

5-
cargo +nightly build --release || exit 1
5+
cargo +nightly build --release --features uniffi || exit 1
66
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --language python -o "$BINDINGS_DIR" || exit 1
77
cp ./target/release/libldk_node.dylib "$BINDINGS_DIR"/libldk_node.dylib || exit 1

scripts/uniffi_bindgen_generate_swift.sh

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ rustup target add aarch64-apple-ios-sim --toolchain nightly
1515
rustup target add aarch64-apple-darwin x86_64-apple-darwin
1616

1717
# Build rust target libs
18-
cargo build --profile release-smaller || exit 1
19-
cargo build --profile release-smaller --target x86_64-apple-darwin || exit 1
20-
cargo build --profile release-smaller --target aarch64-apple-darwin || exit 1
21-
cargo build --profile release-smaller --target x86_64-apple-ios || exit 1
22-
cargo build --profile release-smaller --target aarch64-apple-ios || exit 1
23-
cargo +nightly build --release --target aarch64-apple-ios-sim || exit 1
18+
cargo build --profile release-smaller --features uniffi || exit 1
19+
cargo build --profile release-smaller --features uniffi --target x86_64-apple-darwin || exit 1
20+
cargo build --profile release-smaller --features uniffi --target aarch64-apple-darwin || exit 1
21+
cargo build --profile release-smaller --features uniffi --target x86_64-apple-ios || exit 1
22+
cargo build --profile release-smaller --features uniffi --target aarch64-apple-ios || exit 1
23+
cargo +nightly build --release --features uniffi --target aarch64-apple-ios-sim || exit 1
2424

2525
# Combine ios-sim and apple-darwin (macos) libs for x86_64 and aarch64 (m1)
2626
mkdir -p target/lipo-ios-sim/release-smaller || exit 1

src/hex_utils.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fmt::Write;
22

3+
#[cfg(feature = "uniffi")]
34
pub fn to_vec(hex: &str) -> Option<Vec<u8>> {
45
let mut out = Vec::with_capacity(hex.len() / 2);
56

src/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ mod peer_store;
8686
#[cfg(test)]
8787
mod test;
8888
mod types;
89+
#[cfg(feature = "uniffi")]
90+
mod uniffi_types;
8991
mod wallet;
9092

9193
pub use bip39;
@@ -100,6 +102,9 @@ use error::Error;
100102
pub use event::Event;
101103
pub use types::NetAddress;
102104

105+
#[cfg(feature = "uniffi")]
106+
use {bitcoin::OutPoint, lightning::ln::PaymentSecret, uniffi_types::*};
107+
103108
use event::{EventHandler, EventQueue};
104109
use gossip::GossipSource;
105110
use io::fs_store::FilesystemStore;
@@ -122,7 +127,7 @@ use lightning::ln::channelmanager::{
122127
self, ChainParameters, ChannelManagerReadArgs, PaymentId, RecipientOnionFields, Retry,
123128
};
124129
use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler};
125-
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
130+
use lightning::ln::{PaymentHash, PaymentPreimage};
126131
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
127132

128133
use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, UserConfig};
@@ -147,7 +152,7 @@ use bitcoin::Network;
147152

148153
use bip39::Mnemonic;
149154

150-
use bitcoin::{Address, BlockHash, OutPoint, Txid};
155+
use bitcoin::{Address, BlockHash, Txid};
151156

152157
use rand::Rng;
153158

@@ -159,6 +164,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
159164
use std::sync::{Arc, Mutex, RwLock};
160165
use std::time::{Duration, Instant, SystemTime};
161166

167+
#[cfg(feature = "uniffi")]
162168
uniffi::include_scaffolding!("ldk_node");
163169

164170
// Config defaults
@@ -703,10 +709,6 @@ impl Builder {
703709
}
704710
}
705711

706-
/// This type alias is required as Uniffi doesn't support generics, i.e., we can only expose the
707-
/// concretized types via this aliasing hack.
708-
type LDKNode = Node<FilesystemStore>;
709-
710712
/// The main interface object of LDK Node, wrapping the necessary LDK and BDK functionalities.
711713
///
712714
/// Needs to be initialized and instantiated through [`Builder::build`].

0 commit comments

Comments
 (0)