]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Hard-code dependent crate feature flags
authorMatt Corallo <git@bluematt.me>
Wed, 5 Jun 2024 14:36:31 +0000 (14:36 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 6 Jun 2024 13:45:28 +0000 (13:45 +0000)
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
c-bindings-gen/src/main.rs
genbindings.sh
lightning-c-bindings/Cargo.toml
lightning-c-bindings/src/c_types/mod.rs
lightning-c-bindings/src/lib.rs

index 834954e137b148a8391d56cbf364d07af296bb82..1f474f537ff43ba97b158b14df77be28f8381ca0 100644 (file)
@@ -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
index 12d0772fbb496fab0c8522f04119a8c7a19bd408..26c63704118ac84fc9cebf5f2779b57477b19ed1 100644 (file)
@@ -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();
index bc2e073140bf8d30c11460928f466ab3c2580cb8..bb39994441bf5cb3d0882cce5c21926010aeeccc 100755 (executable)
@@ -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"
index a9279889bcbea81a9f77ff502fbd9bde32776b4e..f15af9fd312709b4ac72076554e77d06fef80503 100644 (file)
@@ -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 }
 
index c8e140b3eae35316a711c8f4a60735f30ee3a40e..858e227b650662169da0ccae576a01d705830225 100644 (file)
@@ -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<Str> for String {
                Str { chars: s.as_ptr(), len: s.len(), chars_is_owned: true }
        }
 }
-#[cfg(feature = "std")]
+#[cfg(not(feature = "no-std"))]
 impl Into<Str> for std::path::PathBuf {
        fn into(self) -> Str {
                self.into_os_string().into_string().expect("We expect paths to be UTF-8 valid").into()
index 53abe8ad960a8159d9703405f6a8e982c169a8c5..f4d957dc87f558620f8787639240d77476f27d6a 100644 (file)
@@ -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;