nostrdb-rs

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

build.rs (5240B)


      1 // build.rs
      2 use cc::Build;
      3 use std::env;
      4 use std::path::PathBuf;
      5 
      6 fn secp256k1_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_API", Some(""))
     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     if base_config.try_compile("libsecp256k1.a").is_err() {
     38         // Some embedded platforms may not have, eg, string.h available, so if the build fails
     39         // simply try again with the wasm sysroot (but without the wasm type sizes) in the hopes
     40         // that it works.
     41         base_config.include("wasm/wasm-sysroot");
     42         base_config.compile("libsecp256k1.a");
     43     }
     44 }
     45 
     46 fn main() {
     47     // Compile the C file
     48     let mut build = Build::new();
     49 
     50     build
     51         .files([
     52             "nostrdb/src/nostrdb.c",
     53             "nostrdb/src/bolt11/bolt11.c",
     54             "nostrdb/src/bolt11/amount.c",
     55             "nostrdb/src/bolt11/bech32.c",
     56             "nostrdb/src/bolt11/hash_u5.c",
     57             "nostrdb/src/invoice.c",
     58             "nostrdb/src/nostr_bech32.c",
     59             "nostrdb/src/content_parser.c",
     60             "nostrdb/ccan/ccan/crypto/sha256/sha256.c",
     61             //"nostrdb/ccan/ccan/htable/htable.c",
     62             //"nostrdb/ccan/ccan/htable/tools/density.c",
     63             //"nostrdb/ccan/ccan/htable/tools/hsearchspeed.c",
     64             //"nostrdb/ccan/ccan/htable/tools/speed.c",
     65             //"nostrdb/ccan/ccan/htable/tools/stringspeed.c",
     66             "nostrdb/ccan/ccan/likely/likely.c",
     67             "nostrdb/ccan/ccan/list/list.c",
     68             "nostrdb/ccan/ccan/mem/mem.c",
     69             "nostrdb/ccan/ccan/str/debug.c",
     70             "nostrdb/ccan/ccan/str/str.c",
     71             "nostrdb/ccan/ccan/take/take.c",
     72             //"nostrdb/ccan/ccan/tal/benchmark/samba-allocs.c",
     73             //"nostrdb/ccan/ccan/tal/benchmark/speed.c",
     74             "nostrdb/ccan/ccan/tal/str/str.c",
     75             "nostrdb/ccan/ccan/tal/tal.c",
     76             "nostrdb/ccan/ccan/utf8/utf8.c",
     77             "nostrdb/src/block.c",
     78             "nostrdb/deps/flatcc/src/runtime/json_parser.c",
     79             "nostrdb/deps/flatcc/src/runtime/verifier.c",
     80             "nostrdb/deps/flatcc/src/runtime/builder.c",
     81             "nostrdb/deps/flatcc/src/runtime/emitter.c",
     82             "nostrdb/deps/flatcc/src/runtime/refmap.c",
     83             "nostrdb/deps/lmdb/mdb.c",
     84             "nostrdb/deps/lmdb/midl.c",
     85         ])
     86         .include("nostrdb/deps/lmdb")
     87         .include("nostrdb/deps/flatcc/include")
     88         .include("nostrdb/deps/secp256k1/include")
     89         .include("nostrdb/ccan")
     90         .include("nostrdb/src")
     91         // Add other include paths
     92         //.flag("-Wall")
     93         .flag("-Wno-sign-compare")
     94         .flag("-Wno-misleading-indentation")
     95         .flag("-Wno-unused-function")
     96         .flag("-Wno-unused-parameter");
     97     //.flag("-Werror")
     98     //.flag("-g")
     99 
    100     if env::var("PROFILE").unwrap() == "debug" {
    101         build.flag("-DDEBUG");
    102         build.flag("-O1");
    103     }
    104 
    105     build.compile("libnostrdb.a");
    106 
    107     secp256k1_build();
    108 
    109     // Re-run the build script if any of the C files or headers change
    110     for file in &["nostrdb/src/nostrdb.c", "nostrdb/src/nostrdb.h"] {
    111         println!("cargo:rerun-if-changed={}", file);
    112     }
    113 
    114     println!("cargo:rustc-link-lib=secp256k1");
    115 
    116     // Print out the path to the compiled library
    117     let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    118     println!("cargo:rustc-link-search=native={}", out_path.display());
    119     println!("cargo:rustc-link-lib=static=nostrdb");
    120 
    121     // Link Security framework on macOS
    122     if cfg!(target_os = "macos") {
    123         println!("cargo:rustc-link-lib=framework=Security");
    124     }
    125 
    126     //
    127     // We only need bindgen when we update the bindings.
    128     // I don't want to complicate the build with it.
    129     //
    130 
    131     #[cfg(feature = "bindgen")]
    132     {
    133         let bindings = bindgen::Builder::default()
    134             .header("nostrdb/src/nostrdb.h")
    135             .clang_arg("-Inostrdb/ccan")
    136             .clang_arg("-Inostrdb/src")
    137             .generate()
    138             .expect("Unable to generate bindings");
    139 
    140         bindings
    141             .write_to_file("src/bindings.rs")
    142             .expect("Couldn't write bindings!");
    143     }
    144 }