build.rs (2530B)
1 extern crate bindgen; 2 3 use std::env; 4 use std::path::PathBuf; 5 use std::process::Command; 6 7 #[derive(Debug)] 8 struct IgnoreMacros(String); 9 10 impl bindgen::callbacks::ParseCallbacks for IgnoreMacros { 11 fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior { 12 if name == self.0 { 13 bindgen::callbacks::MacroParsingBehavior::Ignore 14 } else { 15 bindgen::callbacks::MacroParsingBehavior::Default 16 } 17 } 18 } 19 20 fn main() { 21 // fetch deps 22 std::process::Command::new("git") 23 .args([ 24 "submodule", 25 "update", 26 "--init", 27 "--depth 1", 28 "--recommend-shallow", 29 ]) 30 .output() 31 .expect("Failed to fetch git submodules!"); 32 33 // Build the library 34 Command::new("make").status() 35 .expect("Failed to build library"); 36 37 // Copy library 38 let lib_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); 39 let input_path = lib_path.join("lnsocket.a"); 40 let output_path = lib_path.join("liblnsocket.a"); 41 let res = std::fs::copy(input_path, output_path); 42 println!("cargo:warning={:#?}",res); 43 44 // Tell cargo to look for shared libraries in the specified directory 45 println!("cargo:rustc-link-search={}", lib_path.display()); 46 47 // Tell cargo to tell rustc to link the shared library. 48 println!("cargo:rustc-link-lib=static=lnsocket"); 49 println!("cargo:rustc-link-lib=static=secp256k1"); 50 println!("cargo:rustc-link-lib=static=sodium"); 51 52 let ignored_macros = IgnoreMacros("IPPORT_RESERVED".to_string()); 53 54 // The bindgen::Builder is the main entry point 55 // to bindgen, and lets you build up options for 56 // the resulting bindings. 57 let bindings = bindgen::Builder::default() 58 // The input header we would like to generate 59 // bindings for. 60 .header("lnsocket.h") 61 .header("lnsocket_internal.h") 62 .clang_arg(format!("-I{}", lib_path.join("deps/secp256k1/include").display())) 63 .clang_arg(format!("-I{}", lib_path.join("deps/libsodium/src/libsodium/include").display())) 64 .parse_callbacks(Box::new(ignored_macros)) 65 .trust_clang_mangling(false) 66 // Finish the builder and generate the bindings. 67 .generate() 68 // Unwrap the Result and panic on failure. 69 .expect("Unable to generate bindings"); 70 71 // Write the bindings to the $OUT_DIR/bindings.rs file. 72 bindings 73 .write_to_file("./rust/bindings.rs") 74 .expect("Couldn't write bindings!"); 75 }