rust-secp256k1-unsafe-fast/secp256k1-sys/build.rs

135 lines
5.0 KiB
Rust

// SPDX-License-Identifier: CC0-1.0
//! # Build script
// Coding conventions
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![warn(missing_docs)]
extern crate cc;
use std::env;
fn main() {
// Actual build
let mut base_config = cc::Build::new();
base_config.include("depend/secp256k1/")
.include("depend/secp256k1/include")
.include("depend/secp256k1/src")
.flag_if_supported("-Wno-unused-function") // some ecmult stuff is defined but not used upstream
.flag_if_supported("-Wno-unused-parameter") // patching out printf causes this warning
.define("SECP256K1_API", Some(""))
.define("ENABLE_MODULE_ECDH", Some("1"))
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"))
.define("ENABLE_MODULE_ELLSWIFT", Some("1"))
// upstream sometimes introduces calls to printf, which we cannot compile
// with WASM due to its lack of libc. printf is never necessary and we can
// just #define it away.
.define("printf(...)", Some(""));
if cfg!(feature = "lowmemory") {
base_config.define("ECMULT_WINDOW_SIZE", Some("4")); // A low-enough value to consume negligible memory
base_config.define("ECMULT_GEN_PREC_BITS", Some("2"));
} else {
// definitely slower, not meaningful
// base_config.define("ECMULT_GEN_PREC_BITS", Some("2"));
// base_config.define("ECMULT_GEN_PREC_BITS", Some("4"));
// going from the default 4 to 8 is a huge performance increase
// when combined with other optimizations
base_config.define("ECMULT_GEN_PREC_BITS", Some("8"));
// TODO highly experimental
// non-standard optimization
// slow compilation
// base_config.define("ECMULT_GEN_PREC_BITS", Some("16"));
// minor factor, the 15 default is already optimized
base_config.define("ECMULT_WINDOW_SIZE", Some("15")); // This is the default in the configure file (`auto`)
// TODO do some fine tuning for a slightly higher than default ECMULT_WINDOW_SIZE
// base_config.define("ECMULT_WINDOW_SIZE", Some("17"));
// base_config.define("ECMULT_WINDOW_SIZE", Some("18"));
// too large
// base_config.define("ECMULT_WINDOW_SIZE", Some("22"));
}
base_config.define("USE_EXTERNAL_DEFAULT_CALLBACKS", Some("1"));
#[cfg(feature = "recovery")]
base_config.define("ENABLE_MODULE_RECOVERY", Some("1"));
// WASM headers and size/align defines.
if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "wasm32" {
base_config.include("wasm/wasm-sysroot")
.file("wasm/wasm.c");
}
// secp256k1
base_config.file("depend/secp256k1/contrib/lax_der_parsing.c")
.file("depend/secp256k1/src/precomputed_ecmult_gen.c")
.file("depend/secp256k1/src/precomputed_ecmult.c")
.file("depend/secp256k1/src/secp256k1.c");
// default CC is gcc on the tested system
// GCC 12.2 slightly faster than clang-18 for the unmodified code?
// for the modified code, clang is slightly faster?
// // force clang
base_config.compiler("clang");
// // gcc is faster without native CPU settings on a Ryzen Zen 3 desktop CPU
base_config.flag("-march=native");
// doesn't work as-is
// base_config.flag("-flto=thin");
// shouldn't do much
base_config.define("NDEBUG", Some(""));
// enable custom modifications on the codebase
base_config.define("OPTIMIZE_UNSAFE_VAR_VARIANT", Some(""));
base_config.define("OPTIMIZE_UNSAFE_SHORTCIRCUIT", Some(""));
base_config.define("OPTIMIZE_UNSAFE_SKIP_ZEROING", Some(""));
base_config.define("OPTIMIZE_UNSAFE_ECMULT_GEN_CORE", Some(""));
base_config.define("OPTIMIZE_UNSAFE_SKIP_MASKING", Some(""));
// This is slower, and removed in a future secp256k1 code version
// base_config.define("USE_ASM_X86_64", Some(""));
// -O3 seems to be better than -O2 for clang
// -O3 is already the default for --release builds
// set anyway so that rust non-release builds don't fall back to -O1 here
base_config.flag("-O3");
// base_config.flag("-O1");
// Extra slow, for benchmark testing
// base_config.flag("-Os");
// the default seems to set -gdwarf-4
// slightly worse on gcc?
// slightly better on clang-18 ?
// base_config.flag("-g0");
// potential minor improvement
// to counteract the -fno-omit-frame-pointer
// base_config.flag("-fomit-frame-pointer");
if base_config.try_compile("libsecp256k1.a").is_err() {
// Some embedded platforms may not have, eg, string.h available, so if the build fails
// simply try again with the wasm sysroot (but without the wasm type sizes) in the hopes
// that it works.
// base_config.include("wasm/wasm-sysroot");
// base_config.compile("libsecp256k1.a");
}
}