]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support no_std on the generated crate
authorMatt Corallo <git@bluematt.me>
Tue, 4 Jan 2022 20:30:55 +0000 (20:30 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 6 Jan 2022 03:56:51 +0000 (03:56 +0000)
.github/workflows/build.yml
c-bindings-gen/src/main.rs
lightning-c-bindings/src/c_types/mod.rs

index 64bc90c2e78e9c76225726c80eec59cb0068ce6a..4a42cc3e020318cf22fcd5070b0e1c9e3d3b9e7a 100644 (file)
@@ -25,7 +25,7 @@ jobs:
         run: |
           # Note that the version tags aren't checked into git
           touch src/version.rs
-          RUSTFLAGS="--cfg=c_bindings" cargo check
+          RUSTFLAGS="--cfg=c_bindings" cargo check --features std
       - name: Install cbindgen
         run: cargo install --force cbindgen
       - name: Checkout Rust-Lightning git
index da1ca4f8ecd99037896694479271a7366b40ab85..c6a74f66bcc5bbce40fc6cc05679e19aefb66d63 100644 (file)
@@ -33,7 +33,15 @@ mod blocks;
 use types::*;
 use blocks::*;
 
-const DEFAULT_IMPORTS: &'static str = "\nuse std::str::FromStr;\nuse std::ffi::c_void;\nuse core::convert::Infallible;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n";
+const DEFAULT_IMPORTS: &'static str = "
+use alloc::str::FromStr;
+use core::ffi::c_void;
+use core::convert::Infallible;
+use bitcoin::hashes::Hash;
+use crate::c_types::*;
+#[cfg(feature=\"no-std\")]
+use alloc::{vec::Vec, boxed::Box};
+";
 
 // *************************************
 // *** Manually-expanded conversions ***
@@ -1726,6 +1734,12 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &CrateTypes<'a>
                        writeln!(out, "#![allow(unused_braces)]").unwrap();
                        // 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, "extern crate alloc;").unwrap();
+
                        writeln!(out, "pub mod version;").unwrap();
                        writeln!(out, "pub mod c_types;").unwrap();
                        writeln!(out, "pub mod bitcoin;").unwrap();
@@ -1972,6 +1986,7 @@ fn main() {
 
        let mut derived_templates = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
                .open(&args[2]).expect("Unable to open new header file");
+       writeln!(&mut derived_templates, "{}", DEFAULT_IMPORTS).unwrap();
        let mut header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
                .open(&args[3]).expect("Unable to open new header file");
        let mut cpp_header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
index 9e928a6065922e87409de08506ddf04e26e945a4..0bd976bac8f1e47ee75ad5995a0bf123ce8f01dd 100644 (file)
@@ -20,6 +20,8 @@ use core::ffi::c_void;
 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};
 
 #[repr(C)]
 /// A dummy struct of which an instance must never exist.
@@ -192,6 +194,7 @@ pub enum IOError {
        Other,
        UnexpectedEof,
 }
+#[cfg(feature = "std")]
 impl IOError {
        pub(crate) fn from_rust(err: std::io::Error) -> Self {
                match err.kind() {
@@ -576,6 +579,9 @@ impl<T> TakePointer<*mut T> for *mut T {
 
 
 pub(crate) mod ObjOps {
+       #[cfg(feature = "no-std")]
+       use alloc::boxed::Box;
+
        #[inline]
        #[must_use = "returns new dangling pointer"]
        pub(crate) fn heap_alloc<T>(obj: T) -> *mut T {