nostrdb-rs

nostrdb in rust!
git clone git://jb55.com/nostrdb-rs
Log | Files | Refs | Submodules | README | LICENSE

build.rs (4968B)


      1 // build.rs
      2 use cc::Build;
      3 use std::env;
      4 use std::path::PathBuf;
      5 
      6 fn secp256k1_build(base_config: &mut Build) {
      7     // Actual build
      8     //let mut base_config = cc::Build::new();
      9     base_config
     10         .include("nostrdb/deps/secp256k1/")
     11         .include("nostrdb/deps/secp256k1/include")
     12         .include("nostrdb/deps/secp256k1/src")
     13         .flag_if_supported("-Wno-unused-function") // some ecmult stuff is defined but not used upstream
     14         .flag_if_supported("-Wno-unused-parameter") // patching out printf causes this warning
     15         .define("SECP256K1_STATIC", "1")
     16         .define("ENABLE_MODULE_ECDH", Some("1"))
     17         .define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
     18         .define("ENABLE_MODULE_EXTRAKEYS", Some("1"));
     19     //.define("ENABLE_MODULE_ELLSWIFT", Some("1"))
     20 
     21     // WASM headers and size/align defines.
     22     if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "wasm32" {
     23         base_config.include("wasm/wasm-sysroot").file("wasm/wasm.c");
     24     }
     25 
     26     // secp256k1
     27     base_config
     28         .file("nostrdb/deps/secp256k1/contrib/lax_der_parsing.c")
     29         .file("nostrdb/deps/secp256k1/src/precomputed_ecmult_gen.c")
     30         .file("nostrdb/deps/secp256k1/src/precomputed_ecmult.c")
     31         .file("nostrdb/deps/secp256k1/src/secp256k1.c");
     32 
     33     if env::var("PROFILE").unwrap() == "debug" {
     34         base_config.flag("-O1");
     35     }
     36 
     37     //base_config.compile("libsecp256k1.a");
     38 }
     39 
     40 /// bolt11 deps with portability issues, exclude these on windows build
     41 fn bolt11_deps() -> &'static [&'static str] {
     42     &[
     43         "nostrdb/ccan/ccan/likely/likely.c",
     44         "nostrdb/ccan/ccan/list/list.c",
     45         "nostrdb/ccan/ccan/mem/mem.c",
     46         "nostrdb/ccan/ccan/str/debug.c",
     47         "nostrdb/ccan/ccan/str/str.c",
     48         "nostrdb/ccan/ccan/take/take.c",
     49         "nostrdb/ccan/ccan/tal/str/str.c",
     50         "nostrdb/ccan/ccan/tal/tal.c",
     51         "nostrdb/ccan/ccan/utf8/utf8.c",
     52         "nostrdb/src/bolt11/bolt11.c",
     53         "nostrdb/src/bolt11/amount.c",
     54         "nostrdb/src/bolt11/hash_u5.c",
     55     ]
     56 }
     57 
     58 fn main() {
     59     // Compile the C file
     60     let mut build = Build::new();
     61 
     62     build
     63         .files([
     64             "nostrdb/src/nostrdb.c",
     65             "nostrdb/src/invoice.c",
     66             "nostrdb/src/nostr_bech32.c",
     67             "nostrdb/src/content_parser.c",
     68             "nostrdb/ccan/ccan/crypto/sha256/sha256.c",
     69             "nostrdb/src/bolt11/bech32.c",
     70             "nostrdb/src/block.c",
     71             "nostrdb/deps/flatcc/src/runtime/json_parser.c",
     72             "nostrdb/deps/flatcc/src/runtime/verifier.c",
     73             "nostrdb/deps/flatcc/src/runtime/builder.c",
     74             "nostrdb/deps/flatcc/src/runtime/emitter.c",
     75             "nostrdb/deps/flatcc/src/runtime/refmap.c",
     76             "nostrdb/deps/lmdb/mdb.c",
     77             "nostrdb/deps/lmdb/midl.c",
     78         ])
     79         .include("nostrdb/deps/lmdb")
     80         .include("nostrdb/deps/flatcc/include")
     81         .include("nostrdb/deps/secp256k1/include")
     82         .include("nostrdb/ccan")
     83         .include("nostrdb/src");
     84     // Add other include paths
     85     //.flag("-Wall")
     86     //.flag("-Werror")
     87     //.flag("-g")
     88 
     89     // Link Security framework on macOS
     90     if !cfg!(target_os = "windows") {
     91         build.files(bolt11_deps());
     92         build
     93             .flag("-Wno-sign-compare")
     94             .flag("-Wno-misleading-indentation")
     95             .flag("-Wno-unused-function")
     96             .flag("-Wno-unused-parameter");
     97     } else {
     98         // need this on windows
     99         println!("cargo:rustc-link-lib=bcrypt");
    100     }
    101 
    102     if env::var("PROFILE").unwrap() == "debug" {
    103         build.flag("-DDEBUG");
    104         build.flag("-O1");
    105     }
    106 
    107     // Print out the path to the compiled library
    108     let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    109     println!("cargo:rustc-link-search=native={}", out_path.display());
    110 
    111     secp256k1_build(&mut build);
    112 
    113     build.compile("libnostrdb.a");
    114 
    115     println!("cargo:rustc-link-lib=static=nostrdb");
    116 
    117     // Re-run the build script if any of the C files or headers change
    118     for file in &["nostrdb/src/nostrdb.c", "nostrdb/src/nostrdb.h"] {
    119         println!("cargo:rerun-if-changed={}", file);
    120     }
    121 
    122     // Link Security framework on macOS
    123     if cfg!(target_os = "macos") {
    124         println!("cargo:rustc-link-lib=framework=Security");
    125     }
    126 
    127     //
    128     // We only need bindgen when we update the bindings.
    129     // I don't want to complicate the build with it.
    130     //
    131 
    132     #[cfg(feature = "bindgen")]
    133     {
    134         let bindings = bindgen::Builder::default()
    135             .header("nostrdb/src/nostrdb.h")
    136             .clang_arg("-Inostrdb/ccan")
    137             .clang_arg("-Inostrdb/src")
    138             .generate()
    139             .expect("Unable to generate bindings");
    140 
    141         #[cfg(target_os = "windows")]
    142         let filename = "src/bindings_win.rs";
    143 
    144         #[cfg(not(target_os = "windows"))]
    145         let filename = "src/bindings_posix.rs";
    146 
    147         bindings
    148             .write_to_file(filename)
    149             .expect("Couldn't write bindings!");
    150     }
    151 }