nostrdb-rs

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

build.rs (5348B)


      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/src/metadata.c",
     72             "nostrdb/src/binmoji.c",
     73             "nostrdb/deps/flatcc/src/runtime/json_parser.c",
     74             "nostrdb/deps/flatcc/src/runtime/verifier.c",
     75             "nostrdb/deps/flatcc/src/runtime/builder.c",
     76             "nostrdb/deps/flatcc/src/runtime/emitter.c",
     77             "nostrdb/deps/flatcc/src/runtime/refmap.c",
     78             "nostrdb/deps/lmdb/mdb.c",
     79             "nostrdb/deps/lmdb/midl.c",
     80         ])
     81         .include("nostrdb/deps/lmdb")
     82         .include("nostrdb/deps/flatcc/include")
     83         .include("nostrdb/deps/secp256k1/include")
     84         .include("nostrdb/ccan")
     85         .include("nostrdb/src");
     86     // Add other include paths
     87     //.flag("-Wall")
     88     //.flag("-Werror")
     89     //.flag("-g")
     90 
     91     // Link Security framework on macOS
     92     if !cfg!(target_os = "windows") {
     93         build.files(bolt11_deps());
     94         build
     95             .flag("-Wno-sign-compare")
     96             .flag("-Wno-misleading-indentation")
     97             .flag("-Wno-unused-function")
     98             .flag("-Wno-unused-parameter");
     99     } else {
    100         // need this on windows
    101         println!("cargo:rustc-link-lib=bcrypt");
    102     }
    103 
    104     if env::var("PROFILE").unwrap() == "debug" {
    105         build.flag("-DDEBUG");
    106         build.flag("-O1");
    107     }
    108 
    109     if env::var("NDB_LOG").is_ok() {
    110         build.flag("-DNDB_LOG");
    111     }
    112 
    113     // Print out the path to the compiled library
    114     let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    115     println!("cargo:rustc-link-search=native={}", out_path.display());
    116 
    117     secp256k1_build(&mut build);
    118 
    119     build.compile("libnostrdb.a");
    120 
    121     println!("cargo:rustc-link-lib=static=nostrdb");
    122 
    123     // Re-run the build script if any of the C files or headers change
    124     for file in &["nostrdb/src/nostrdb.c", "nostrdb/src/nostrdb.h"] {
    125         println!("cargo:rerun-if-changed={file}");
    126     }
    127 
    128     // Link Security framework on macOS
    129     if std::env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" {
    130         println!("cargo:rustc-link-lib=framework=Security");
    131     }
    132 
    133     // windows needs advapi32 due to a call in mdb_env_setup_locks
    134     #[cfg(target_os = "windows")]
    135     println!("cargo:rustc-link-lib=advapi32");
    136 
    137     //
    138     // We only need bindgen when we update the bindings.
    139     // I don't want to complicate the build with it.
    140     //
    141 
    142     #[cfg(feature = "bindgen")]
    143     {
    144         let bindings = bindgen::Builder::default()
    145             .header("nostrdb/src/nostrdb.h")
    146             .header("nostrdb/src/metadata.h")
    147             .clang_arg("-Inostrdb/ccan")
    148             .clang_arg("-Inostrdb/src")
    149             .generate()
    150             .expect("Unable to generate bindings");
    151 
    152         #[cfg(target_os = "windows")]
    153         let filename = "src/bindings_win.rs";
    154 
    155         #[cfg(not(target_os = "windows"))]
    156         let filename = "src/bindings_posix.rs";
    157 
    158         bindings
    159             .write_to_file(filename)
    160             .expect("Couldn't write bindings!");
    161     }
    162 }