use cc::Build;
use std::env;
use std::path::PathBuf;
fn secp256k1_build(base_config: &mut Build) {
base_config
.include("nostrdb/deps/secp256k1/")
.include("nostrdb/deps/secp256k1/include")
.include("nostrdb/deps/secp256k1/src")
.flag_if_supported("-Wno-unused-function") .flag_if_supported("-Wno-unused-parameter") .define("SECP256K1_STATIC", "1")
.define("ENABLE_MODULE_ECDH", Some("1"))
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"));
if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "wasm32" {
base_config.include("wasm/wasm-sysroot").file("wasm/wasm.c");
}
base_config
.file("nostrdb/deps/secp256k1/contrib/lax_der_parsing.c")
.file("nostrdb/deps/secp256k1/src/precomputed_ecmult_gen.c")
.file("nostrdb/deps/secp256k1/src/precomputed_ecmult.c")
.file("nostrdb/deps/secp256k1/src/secp256k1.c");
if env::var("PROFILE").unwrap() == "debug" {
base_config.flag("-O1");
}
}
fn bolt11_deps() -> &'static [&'static str] {
&[
"nostrdb/ccan/ccan/likely/likely.c",
"nostrdb/ccan/ccan/list/list.c",
"nostrdb/ccan/ccan/mem/mem.c",
"nostrdb/ccan/ccan/str/debug.c",
"nostrdb/ccan/ccan/str/str.c",
"nostrdb/ccan/ccan/take/take.c",
"nostrdb/ccan/ccan/tal/str/str.c",
"nostrdb/ccan/ccan/tal/tal.c",
"nostrdb/ccan/ccan/utf8/utf8.c",
"nostrdb/src/bolt11/bolt11.c",
"nostrdb/src/bolt11/amount.c",
"nostrdb/src/bolt11/hash_u5.c",
]
}
fn main() {
let mut build = Build::new();
build
.files([
"nostrdb/src/nostrdb.c",
"nostrdb/src/invoice.c",
"nostrdb/src/nostr_bech32.c",
"nostrdb/src/content_parser.c",
"nostrdb/ccan/ccan/crypto/sha256/sha256.c",
"nostrdb/src/bolt11/bech32.c",
"nostrdb/src/block.c",
"nostrdb/deps/flatcc/src/runtime/json_parser.c",
"nostrdb/deps/flatcc/src/runtime/verifier.c",
"nostrdb/deps/flatcc/src/runtime/builder.c",
"nostrdb/deps/flatcc/src/runtime/emitter.c",
"nostrdb/deps/flatcc/src/runtime/refmap.c",
"nostrdb/deps/lmdb/mdb.c",
"nostrdb/deps/lmdb/midl.c",
])
.include("nostrdb/deps/lmdb")
.include("nostrdb/deps/flatcc/include")
.include("nostrdb/deps/secp256k1/include")
.include("nostrdb/ccan")
.include("nostrdb/src");
if !cfg!(target_os = "windows") {
build.files(bolt11_deps());
build
.flag("-Wno-sign-compare")
.flag("-Wno-misleading-indentation")
.flag("-Wno-unused-function")
.flag("-Wno-unused-parameter");
} else {
println!("cargo:rustc-link-lib=bcrypt");
}
if env::var("PROFILE").unwrap() == "debug" {
build.flag("-DDEBUG");
build.flag("-O1");
}
if env::var("NDB_LOG").is_ok() {
build.flag("-DNDB_LOG");
}
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-search=native={}", out_path.display());
secp256k1_build(&mut build);
build.compile("libnostrdb.a");
println!("cargo:rustc-link-lib=static=nostrdb");
for file in &["nostrdb/src/nostrdb.c", "nostrdb/src/nostrdb.h"] {
println!("cargo:rerun-if-changed={}", file);
}
if std::env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" {
println!("cargo:rustc-link-lib=framework=Security");
}
#[cfg(target_os = "windows")]
println!("cargo:rustc-link-lib=advapi32");
#[cfg(feature = "bindgen")]
{
let bindings = bindgen::Builder::default()
.header("nostrdb/src/nostrdb.h")
.clang_arg("-Inostrdb/ccan")
.clang_arg("-Inostrdb/src")
.generate()
.expect("Unable to generate bindings");
#[cfg(target_os = "windows")]
let filename = "src/bindings_win.rs";
#[cfg(not(target_os = "windows"))]
let filename = "src/bindings_posix.rs";
bindings
.write_to_file(filename)
.expect("Couldn't write bindings!");
}
}