nostrdb-rs

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

build.rs (7512B)


      1 // build.rs
      2 use cc::Build;
      3 use std::env;
      4 use std::path::{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     // libsodium
     34     //base_config
     35     //.file("nostrdb/deps/libsodium/src/libsodium/crypto_stream/chacha20/stream_chacha20.c");
     36 
     37     if env::var("PROFILE").unwrap() == "debug" {
     38         base_config.flag("-O1");
     39     }
     40 
     41     //base_config.compile("libsecp256k1.a");
     42 }
     43 
     44 /// bolt11 deps with portability issues, exclude these on windows build
     45 fn bolt11_deps() -> &'static [&'static str] {
     46     &[
     47         "nostrdb/ccan/ccan/likely/likely.c",
     48         "nostrdb/ccan/ccan/list/list.c",
     49         "nostrdb/ccan/ccan/mem/mem.c",
     50         "nostrdb/ccan/ccan/str/debug.c",
     51         "nostrdb/ccan/ccan/str/str.c",
     52         "nostrdb/ccan/ccan/take/take.c",
     53         "nostrdb/ccan/ccan/tal/str/str.c",
     54         "nostrdb/ccan/ccan/tal/tal.c",
     55         "nostrdb/ccan/ccan/utf8/utf8.c",
     56         "nostrdb/src/bolt11/bolt11.c",
     57         "nostrdb/src/bolt11/amount.c",
     58         "nostrdb/src/bolt11/hash_u5.c",
     59     ]
     60 }
     61 
     62 fn main() {
     63     // Compile the C file
     64     let mut build = Build::new();
     65 
     66     build
     67         .files([
     68             "nostrdb/src/nostrdb.c",
     69             "nostrdb/src/invoice.c",
     70             "nostrdb/src/nostr_bech32.c",
     71             "nostrdb/src/content_parser.c",
     72             "nostrdb/ccan/ccan/crypto/sha256/sha256.c",
     73             "nostrdb/src/bolt11/bech32.c",
     74             "nostrdb/src/block.c",
     75             "nostrdb/src/metadata.c",
     76             "nostrdb/src/nip44.c",
     77             "nostrdb/src/base64.c",
     78             "nostrdb/src/hmac_sha256.c",
     79             "nostrdb/src/hkdf_sha256.c",
     80             "nostrdb/src/binmoji.c",
     81             "nostrdb/deps/flatcc/src/runtime/json_parser.c",
     82             "nostrdb/deps/flatcc/src/runtime/verifier.c",
     83             "nostrdb/deps/flatcc/src/runtime/builder.c",
     84             "nostrdb/deps/flatcc/src/runtime/emitter.c",
     85             "nostrdb/deps/flatcc/src/runtime/refmap.c",
     86             "nostrdb/deps/lmdb/mdb.c",
     87             "nostrdb/deps/lmdb/midl.c",
     88         ])
     89         .include("nostrdb/deps/lmdb")
     90         .include("nostrdb/deps/flatcc/include")
     91         .include("nostrdb/deps/secp256k1/include")
     92         .include("nostrdb/ccan")
     93         .include("nostrdb/src");
     94     // Add other include paths
     95     //.flag("-Wall")
     96     //.flag("-Werror")
     97     //.flag("-g")
     98 
     99     {
    100         // Provided by libsodium-sys-stable’s build script
    101         let dep_include = std::env::var("DEP_SODIUM_INCLUDE")
    102             .expect("DEP_SODIUM_INCLUDE not set; is libsodium-sys-stable a dependency?");
    103 
    104         let dep_include_path = Path::new(&dep_include);
    105 
    106         // If DEP_SODIUM_INCLUDE doesn't directly contain `sodium/`, try the known Windows layout:
    107         //   <...>/out/installed/libsodium/include
    108         let mut include_root = dep_include_path.to_path_buf();
    109 
    110         if !include_root.join("sodium").is_dir() {
    111             // try sibling layout relative to the libsodium build output
    112             // (DEP_SODIUM_INCLUDE is often <...>/out/installed/include on Windows)
    113             let candidate = dep_include_path
    114                 .parent() // <...>/out/installed
    115                 .unwrap_or(dep_include_path)
    116                 .join("libsodium")
    117                 .join("include");
    118 
    119             if candidate.join("sodium").is_dir() {
    120                 include_root = candidate;
    121             }
    122         }
    123 
    124         build.include(include_root);
    125 
    126         // static libsodium on MSVC: avoid __imp_ dllimport mismatch
    127         if cfg!(target_env = "msvc") {
    128             build.define("SODIUM_STATIC", Some("1"));
    129         }
    130 
    131         // Optionally use DEP_SODIUM_LIB as well
    132         let sodium_lib_dir = std::env::var("DEP_SODIUM_LIB").ok();
    133         // Make sure the linker knows where libsodium lives
    134         if let Some(lib_dir) = sodium_lib_dir {
    135             println!("cargo:rustc-link-search=native={lib_dir}");
    136         }
    137     }
    138 
    139     // Link Security framework on macOS
    140     if !cfg!(target_os = "windows") {
    141         build.files(bolt11_deps());
    142         build
    143             .flag("-Wno-sign-compare")
    144             .flag("-Wno-misleading-indentation")
    145             .flag("-Wno-unused-function")
    146             .flag("-Wno-unused-parameter");
    147     } else {
    148         // need this on windows
    149         println!("cargo:rustc-link-lib=bcrypt");
    150     }
    151 
    152     if env::var("PROFILE").unwrap() == "debug" {
    153         build.flag("-DDEBUG");
    154         build.flag("-O1");
    155     }
    156 
    157     if env::var("NDB_LOG").is_ok() {
    158         build.flag("-DNDB_LOG");
    159     }
    160 
    161     // Print out the path to the compiled library
    162     let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    163     println!("cargo:rustc-link-search=native={}", out_path.display());
    164 
    165     secp256k1_build(&mut build);
    166 
    167     build.compile("libnostrdb.a");
    168     build.define("SODIUM_STATIC", Some("1"));
    169 
    170     {
    171         let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
    172         let is_windows = target_env == "msvc";
    173         let sodium_name = if is_windows { "libsodium" } else { "sodium" };
    174 
    175         println!("cargo:rustc-link-lib=static={sodium_name}");
    176     }
    177 
    178     println!("cargo:rustc-link-lib=static=nostrdb");
    179 
    180     // Re-run the build script if any of the C files or headers change
    181     for file in &["nostrdb/src/nostrdb.c", "nostrdb/src/nostrdb.h"] {
    182         println!("cargo:rerun-if-changed={file}");
    183     }
    184 
    185     // Link Security framework on macOS
    186     if std::env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" {
    187         println!("cargo:rustc-link-lib=framework=Security");
    188     }
    189 
    190     // windows needs advapi32 due to a call in mdb_env_setup_locks
    191     #[cfg(target_os = "windows")]
    192     println!("cargo:rustc-link-lib=advapi32");
    193 
    194     //
    195     // We only need bindgen when we update the bindings.
    196     // I don't want to complicate the build with it.
    197     //
    198 
    199     #[cfg(feature = "bindgen")]
    200     {
    201         let bindings = bindgen::Builder::default()
    202             .header("nostrdb/src/nostrdb.h")
    203             .header("nostrdb/src/metadata.h")
    204             .clang_arg("-Inostrdb/ccan")
    205             .clang_arg("-Inostrdb/src")
    206             .generate()
    207             .expect("Unable to generate bindings");
    208 
    209         #[cfg(target_os = "windows")]
    210         let filename = "src/bindings_win.rs";
    211 
    212         #[cfg(not(target_os = "windows"))]
    213         let filename = "src/bindings_posix.rs";
    214 
    215         bindings
    216             .write_to_file(filename)
    217             .expect("Couldn't write bindings!");
    218     }
    219 }