build.rs (4551B)
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/sha256.c", 54 "nostrdb/src/bolt11/bolt11.c", 55 "nostrdb/src/bolt11/amount.c", 56 "nostrdb/src/bolt11/bech32.c", 57 "nostrdb/src/bolt11/hash_u5.c", 58 "nostrdb/src/bolt11/list.c", 59 "nostrdb/src/bolt11/take.c", 60 "nostrdb/src/bolt11/tal.c", 61 "nostrdb/src/bolt11/talstr.c", 62 "nostrdb/src/bolt11/utf8.c", 63 "nostrdb/src/invoice.c", 64 "nostrdb/src/nostr_bech32.c", 65 "nostrdb/src/content_parser.c", 66 "nostrdb/src/block.c", 67 "nostrdb/deps/flatcc/src/runtime/json_parser.c", 68 "nostrdb/deps/flatcc/src/runtime/verifier.c", 69 "nostrdb/deps/flatcc/src/runtime/builder.c", 70 "nostrdb/deps/flatcc/src/runtime/emitter.c", 71 "nostrdb/deps/flatcc/src/runtime/refmap.c", 72 "nostrdb/deps/lmdb/mdb.c", 73 "nostrdb/deps/lmdb/midl.c", 74 ]) 75 .include("nostrdb/deps/lmdb") 76 .include("nostrdb/deps/flatcc/include") 77 .include("nostrdb/deps/secp256k1/include") 78 // Add other include paths 79 //.flag("-Wall") 80 .flag("-Wno-sign-compare") 81 .flag("-Wno-misleading-indentation") 82 .flag("-Wno-unused-function") 83 .flag("-Wno-unused-parameter"); 84 //.flag("-Werror") 85 //.flag("-g") 86 87 if env::var("PROFILE").unwrap() == "debug" { 88 build.flag("-DDEBUG"); 89 build.flag("-O1"); 90 } 91 92 build.compile("libnostrdb.a"); 93 94 secp256k1_build(); 95 96 // Re-run the build script if any of the C files or headers change 97 for file in &[ 98 "nostrdb/src/nostrdb.c", 99 "nostrdb/src/sha256.c", 100 "nostrdb/src/nostrdb.h", 101 "nostrdb/src/sha256.h", 102 ] { 103 println!("cargo:rerun-if-changed={}", file); 104 } 105 106 println!("cargo:rustc-link-lib=secp256k1"); 107 108 // Print out the path to the compiled library 109 let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 110 println!("cargo:rustc-link-search=native={}", out_path.display()); 111 println!("cargo:rustc-link-lib=static=nostrdb"); 112 113 // Link Security framework on macOS 114 if cfg!(target_os = "macos") { 115 println!("cargo:rustc-link-lib=framework=Security"); 116 } 117 118 // 119 // We only need bindgen when we update the bindings. 120 // I don't want to complicate the build with it. 121 // 122 123 #[cfg(feature = "bindgen")] 124 { 125 let bindings = bindgen::Builder::default() 126 .header("nostrdb/src/nostrdb.h") 127 .generate() 128 .expect("Unable to generate bindings"); 129 130 bindings 131 .write_to_file("src/bindings.rs") 132 .expect("Couldn't write bindings!"); 133 } 134 }