build.rs (4550B)
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("deps/secp256k1/") 11 .include("deps/secp256k1/include") 12 .include("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("0")) 20 // upstream sometimes introduces calls to printf, which we cannot compile 21 // with WASM due to its lack of libc. printf is never necessary and we can 22 // just #define it away. 23 .define("printf(...)", Some("")); 24 25 //if cfg!(feature = "lowmemory") { 26 // base_config.define("ECMULT_WINDOW_SIZE", Some("4")); // A low-enough value to consume negligible memory 27 // base_config.define("ECMULT_GEN_PREC_BITS", Some("2")); 28 //} else { 29 // base_config.define("ECMULT_GEN_PREC_BITS", Some("4")); 30 // base_config.define("ECMULT_WINDOW_SIZE", Some("15")); // This is the default in the configure file (`auto`) 31 //} 32 33 //base_config.define("USE_EXTERNAL_DEFAULT_CALLBACKS", Some("1")); 34 //#[cfg(feature = "recovery")] 35 //base_config.define("ENABLE_MODULE_RECOVERY", Some("1")); 36 37 // WASM headers and size/align defines. 38 if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "wasm32" { 39 base_config.include("wasm/wasm-sysroot").file("wasm/wasm.c"); 40 } 41 42 // secp256k1 43 base_config 44 .file("deps/secp256k1/contrib/lax_der_parsing.c") 45 .file("deps/secp256k1/src/precomputed_ecmult_gen.c") 46 .file("deps/secp256k1/src/precomputed_ecmult.c") 47 .file("deps/secp256k1/src/secp256k1.c"); 48 49 if base_config.try_compile("libsecp256k1.a").is_err() { 50 // Some embedded platforms may not have, eg, string.h available, so if the build fails 51 // simply try again with the wasm sysroot (but without the wasm type sizes) in the hopes 52 // that it works. 53 base_config.include("wasm/wasm-sysroot"); 54 base_config.compile("libsecp256k1.a"); 55 } 56 } 57 58 fn main() { 59 // Compile the C file 60 let mut build = Build::new(); 61 62 build 63 .files([ 64 "nostrdb.c", 65 "sha256.c", 66 "bech32.c", 67 "deps/flatcc/src/runtime/json_parser.c", 68 "deps/flatcc/src/runtime/verifier.c", 69 "deps/flatcc/src/runtime/builder.c", 70 "deps/flatcc/src/runtime/emitter.c", 71 "deps/flatcc/src/runtime/refmap.c", 72 "deps/lmdb/mdb.c", 73 "deps/lmdb/midl.c", 74 ]) 75 .include("deps/lmdb") 76 .include("deps/flatcc/include") 77 .include("deps/secp256k1/include") 78 // Add other include paths 79 //.flag("-Wall") 80 .flag("-Wno-misleading-indentation") 81 .flag("-Wno-unused-function") 82 //.flag("-Werror") 83 //.flag("-g") 84 .compile("libnostrdb.a"); 85 86 secp256k1_build(); 87 88 // Re-run the build script if any of the C files or headers change 89 for file in &[ 90 "src/nostrdb.c", 91 "src/sha256.c", 92 "src/bech32.c", 93 // Add all your C source files here 94 "include/nostrdb.h", 95 "include/sha256.h", 96 // Add all your header files here 97 ] { 98 println!("cargo:rerun-if-changed={}", file); 99 } 100 101 //println!("cargo:rustc-link-search=native=deps/lmdb"); 102 //println!("cargo:rustc-link-lib=static=lmdb"); 103 //println!("cargo:rustc-link-search=native=deps/secp256k1/.libs"); 104 println!("cargo:rustc-link-lib=secp256k1"); 105 106 // Print out the path to the compiled library 107 let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 108 println!("cargo:rustc-link-search=native={}", out_path.display()); 109 println!("cargo:rustc-link-lib=static=nostrdb"); 110 111 // The bindgen::Builder is the main entry point to bindgen, and lets you build up options for 112 // the resulting bindings. 113 let bindings = bindgen::Builder::default() 114 .header("nostrdb.h") 115 .generate() 116 .expect("Unable to generate bindings"); 117 118 // Write the bindings to the $OUT_DIR/bindings.rs file. 119 bindings 120 .write_to_file("src/bindings.rs") 121 .expect("Couldn't write bindings!"); 122 }