From 6ad10ff1534e3a490e5fb6d00e58ab702c510c29 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 5 Jun 2024 14:36:31 +0000 Subject: [PATCH] Hard-code dependent crate feature flags When we generate bindings, we generate bindings across the API surface of whatever features we had set. Thus, any generated bindings are specific to the features we had set and it doesn't make sense to have features on the `lightning-c-bindings` crate as setting the wrong one will simply lead to a compilation failure. Instead, here we hard-code the features in the dependencies section of `Cargo.toml`. Sadly, because we want to take an optional `core2` dependency, we can't quite fully drop the `lightning-c-bindings` features entirely, but after upgrading to rust-bitcoin 0.32 we should be able to. --- .github/workflows/build.yml | 4 ++-- c-bindings-gen/src/main.rs | 4 +--- genbindings.sh | 7 ++++--- lightning-c-bindings/Cargo.toml | 11 +++++------ lightning-c-bindings/src/c_types/mod.rs | 7 +++---- lightning-c-bindings/src/lib.rs | 4 +--- 6 files changed, 16 insertions(+), 21 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 834954e..1f474f5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,7 +34,7 @@ jobs: run: | # Note that the version tags aren't checked into git touch src/version.rs - RUSTFLAGS="--cfg=c_bindings" cargo check --features std + RUSTFLAGS="--cfg=c_bindings" cargo check - name: Install cbindgen run: | git clone https://github.com/eqrion/cbindgen @@ -94,7 +94,7 @@ jobs: run: | # Note that the version tags aren't checked into git touch src/version.rs - RUSTFLAGS="--cfg=c_bindings" cargo check --features std + RUSTFLAGS="--cfg=c_bindings" cargo check - name: Install cbindgen run: | git clone https://github.com/eqrion/cbindgen diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 12d0772..26c6370 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -2102,9 +2102,7 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &CrateTypes<'a> // TODO: We need to map deny(missing_docs) in the source crate(s) //writeln!(out, "#![deny(missing_docs)]").unwrap(); - writeln!(out, "#![cfg_attr(not(feature = \"std\"), no_std)]").unwrap(); - writeln!(out, "#[cfg(not(any(feature = \"std\", feature = \"no-std\")))]").unwrap(); - writeln!(out, "compile_error!(\"at least one of the `std` or `no-std` features must be enabled\");").unwrap(); + writeln!(out, "#![cfg_attr(feature = \"no-std\", no_std)]").unwrap(); writeln!(out, "extern crate alloc;").unwrap(); writeln!(out, "pub mod version;").unwrap(); diff --git a/genbindings.sh b/genbindings.sh index bc2e073..bb39994 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -180,11 +180,13 @@ pub mod $2 { echo "}" >> /tmp/$1-crate-source.txt cat /tmp/$1-crate-source.txt >> /tmp/crate-source.txt rm /tmp/$1-crate-source.txt + FEATURES="$(echo "$3\"" | sed 's/--features=/"/' | sed 's/,/", "/g')" + [ "$FEATURES" = '"' ] && FEATURES="" if is_gnu_sed; then - sed -E -i 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml + sed -E -i 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false, features = ['"$FEATURES"'] }|' lightning-c-bindings/Cargo.toml else # OSX sed is for some reason not compatible with GNU sed - sed -E -i '' 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml + sed -E -i '' 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false, features = ['"$FEATURES"'] }|' lightning-c-bindings/Cargo.toml fi } @@ -204,7 +206,6 @@ if [ "$2" = "true" ]; then add_crate "lightning-background-processor" "lightning_background_processor" --features=std add_crate "lightning-invoice" "lightning_invoice" --features=std add_crate "lightning-rapid-gossip-sync" "lightning_rapid_gossip_sync" --features=std - CARGO_BUILD_ARGS="--features=std" else add_crate lightning lightning --features=no-std drop_crate "lightning-persister" diff --git a/lightning-c-bindings/Cargo.toml b/lightning-c-bindings/Cargo.toml index a927988..f15af9f 100644 --- a/lightning-c-bindings/Cargo.toml +++ b/lightning-c-bindings/Cargo.toml @@ -15,18 +15,17 @@ crate-type = ["staticlib" ,"cdylib"] [features] -no-std = ["bitcoin/no-std", "lightning/no-std", "lightning-invoice/no-std", "lightning-background-processor/no-std", "core2"] -std = ["bitcoin/std", "lightning/std", "lightning-invoice/std", "lightning-background-processor/std"] +no-std = ["core2"] [dependencies] bitcoin = { version = "0.30", default-features = false } secp256k1 = { version = "0.27", features = ["global-context", "recovery"] } # Note that the following line is matched by genbindings to update the path -lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false } +lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false, features = ["std"] } lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false } -lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false } -lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false } -lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false } +lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false, features = ["std"] } +lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false, features = ["std"] } +lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false, features = ["std"] } core2 = { version = "0.3.0", optional = true, default-features = false } diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index c8e140b..858e227 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -24,11 +24,10 @@ use core::convert::TryInto; // Bindings need at least rustc 1.34 use alloc::borrow::ToOwned; use core::ffi::c_void; -#[cfg(feature = "std")] +#[cfg(not(feature = "no-std"))] pub(crate) use std::io::{self, Cursor, Read}; #[cfg(feature = "no-std")] pub(crate) use core2::io::{self, Cursor, Read}; -#[cfg(feature = "no-std")] use alloc::{boxed::Box, vec::Vec, string::String}; use core::convert::TryFrom; @@ -869,7 +868,7 @@ impl Str { }; String::from_utf8(bytes).unwrap() } - #[cfg(feature = "std")] + #[cfg(not(feature = "no-std"))] pub(crate) fn into_pathbuf(mut self) -> std::path::PathBuf { std::path::PathBuf::from(self.into_string()) } @@ -880,7 +879,7 @@ impl Into for String { Str { chars: s.as_ptr(), len: s.len(), chars_is_owned: true } } } -#[cfg(feature = "std")] +#[cfg(not(feature = "no-std"))] impl Into for std::path::PathBuf { fn into(self) -> Str { self.into_os_string().into_string().expect("We expect paths to be UTF-8 valid").into() diff --git a/lightning-c-bindings/src/lib.rs b/lightning-c-bindings/src/lib.rs index 53abe8a..f4d957d 100644 --- a/lightning-c-bindings/src/lib.rs +++ b/lightning-c-bindings/src/lib.rs @@ -16,9 +16,7 @@ #![allow(unused_parens)] #![allow(unused_unsafe)] #![allow(unused_braces)] -#![cfg_attr(not(feature = "std"), no_std)] -#[cfg(not(any(feature = "std", feature = "no-std")))] -compile_error!("at least one of the `std` or `no-std` features must be enabled"); +#![cfg_attr(feature = "no-std", no_std)] extern crate alloc; pub mod version; pub mod c_types; -- 2.39.5