Skip to content

Commit e4c0a42

Browse files
committed
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.
1 parent a08c023 commit e4c0a42

File tree

5 files changed

+14
-19
lines changed

5 files changed

+14
-19
lines changed

c-bindings-gen/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,9 +2102,7 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &CrateTypes<'a>
21022102
// TODO: We need to map deny(missing_docs) in the source crate(s)
21032103
//writeln!(out, "#![deny(missing_docs)]").unwrap();
21042104

2105-
writeln!(out, "#![cfg_attr(not(feature = \"std\"), no_std)]").unwrap();
2106-
writeln!(out, "#[cfg(not(any(feature = \"std\", feature = \"no-std\")))]").unwrap();
2107-
writeln!(out, "compile_error!(\"at least one of the `std` or `no-std` features must be enabled\");").unwrap();
2105+
writeln!(out, "#![cfg_attr(feature = \"no-std\", no_std)]").unwrap();
21082106
writeln!(out, "extern crate alloc;").unwrap();
21092107

21102108
writeln!(out, "pub mod version;").unwrap();

genbindings.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,13 @@ pub mod $2 {
180180
echo "}" >> /tmp/$1-crate-source.txt
181181
cat /tmp/$1-crate-source.txt >> /tmp/crate-source.txt
182182
rm /tmp/$1-crate-source.txt
183+
FEATURES="$(echo "$3\"" | sed 's/--features=/"/' | sed 's/,/", "/g')"
184+
[ "$FEATURES" = '"' ] && FEATURES=""
183185
if is_gnu_sed; then
184-
sed -E -i 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml
186+
sed -E -i 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false, features = ['"$FEATURES"'] }|' lightning-c-bindings/Cargo.toml
185187
else
186188
# OSX sed is for some reason not compatible with GNU sed
187-
sed -E -i '' 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml
189+
sed -E -i '' 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false, features = ['"$FEATURES"'] }|' lightning-c-bindings/Cargo.toml
188190
fi
189191
}
190192

@@ -204,7 +206,6 @@ if [ "$2" = "true" ]; then
204206
add_crate "lightning-background-processor" "lightning_background_processor" --features=std
205207
add_crate "lightning-invoice" "lightning_invoice" --features=std
206208
add_crate "lightning-rapid-gossip-sync" "lightning_rapid_gossip_sync" --features=std
207-
CARGO_BUILD_ARGS="--features=std"
208209
else
209210
add_crate lightning lightning --features=no-std
210211
drop_crate "lightning-persister"

lightning-c-bindings/Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@ crate-type = ["staticlib"
1515
,"cdylib"]
1616

1717
[features]
18-
no-std = ["bitcoin/no-std", "lightning/no-std", "lightning-invoice/no-std", "lightning-background-processor/no-std", "core2"]
19-
std = ["bitcoin/std", "lightning/std", "lightning-invoice/std", "lightning-background-processor/std"]
18+
no-std = ["core2"]
2019

2120
[dependencies]
2221
bitcoin = { version = "0.30", default-features = false }
2322
secp256k1 = { version = "0.27", features = ["global-context", "recovery"] }
2423
# Note that the following line is matched by genbindings to update the path
25-
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false }
24+
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false, features = ["std"] }
2625
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false }
27-
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false }
28-
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false }
29-
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false }
26+
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false, features = ["std"] }
27+
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false, features = ["std"] }
28+
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch = "0.0.123-bindings", default-features = false, features = ["std"] }
3029

3130
core2 = { version = "0.3.0", optional = true, default-features = false }
3231

lightning-c-bindings/src/c_types/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ use core::convert::TryInto; // Bindings need at least rustc 1.34
2424
use alloc::borrow::ToOwned;
2525
use core::ffi::c_void;
2626

27-
#[cfg(feature = "std")]
27+
#[cfg(not(feature = "no-std"))]
2828
pub(crate) use std::io::{self, Cursor, Read};
2929
#[cfg(feature = "no-std")]
3030
pub(crate) use core2::io::{self, Cursor, Read};
31-
#[cfg(feature = "no-std")]
3231
use alloc::{boxed::Box, vec::Vec, string::String};
3332

3433
use core::convert::TryFrom;
@@ -869,7 +868,7 @@ impl Str {
869868
};
870869
String::from_utf8(bytes).unwrap()
871870
}
872-
#[cfg(feature = "std")]
871+
#[cfg(not(feature = "no-std"))]
873872
pub(crate) fn into_pathbuf(mut self) -> std::path::PathBuf {
874873
std::path::PathBuf::from(self.into_string())
875874
}
@@ -880,7 +879,7 @@ impl Into<Str> for String {
880879
Str { chars: s.as_ptr(), len: s.len(), chars_is_owned: true }
881880
}
882881
}
883-
#[cfg(feature = "std")]
882+
#[cfg(not(feature = "no-std"))]
884883
impl Into<Str> for std::path::PathBuf {
885884
fn into(self) -> Str {
886885
self.into_os_string().into_string().expect("We expect paths to be UTF-8 valid").into()

lightning-c-bindings/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
#![allow(unused_parens)]
1717
#![allow(unused_unsafe)]
1818
#![allow(unused_braces)]
19-
#![cfg_attr(not(feature = "std"), no_std)]
20-
#[cfg(not(any(feature = "std", feature = "no-std")))]
21-
compile_error!("at least one of the `std` or `no-std` features must be enabled");
19+
#![cfg_attr(feature = "no-std", no_std)]
2220
extern crate alloc;
2321
pub mod version;
2422
pub mod c_types;

0 commit comments

Comments
 (0)