2023-05-03 01:37:07 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2015-10-26 15:21:01 +00:00
|
|
|
|
|
|
|
//! # Build script
|
|
|
|
|
|
|
|
// Coding conventions
|
|
|
|
#![deny(non_upper_case_globals)]
|
|
|
|
#![deny(non_camel_case_types)]
|
|
|
|
#![deny(non_snake_case)]
|
|
|
|
#![deny(unused_mut)]
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
2018-07-30 02:53:20 +00:00
|
|
|
extern crate cc;
|
2015-10-26 15:21:01 +00:00
|
|
|
|
2019-01-09 12:15:28 +00:00
|
|
|
use std::env;
|
2018-08-21 18:41:42 +00:00
|
|
|
|
2015-10-26 15:21:01 +00:00
|
|
|
fn main() {
|
2018-07-30 03:11:43 +00:00
|
|
|
// Actual build
|
2018-07-30 02:53:20 +00:00
|
|
|
let mut base_config = cc::Build::new();
|
2015-10-26 17:59:40 +00:00
|
|
|
base_config.include("depend/secp256k1/")
|
2015-10-26 21:26:45 +00:00
|
|
|
.include("depend/secp256k1/include")
|
2015-10-26 17:59:40 +00:00
|
|
|
.include("depend/secp256k1/src")
|
2018-08-26 18:58:33 +00:00
|
|
|
.flag_if_supported("-Wno-unused-function") // some ecmult stuff is defined but not used upstream
|
2023-11-01 17:06:16 +00:00
|
|
|
.flag_if_supported("-Wno-unused-parameter") // patching out printf causes this warning
|
2021-03-18 19:23:28 +00:00
|
|
|
.define("SECP256K1_API", Some(""))
|
2020-05-13 08:40:39 +00:00
|
|
|
.define("ENABLE_MODULE_ECDH", Some("1"))
|
2024-04-28 18:46:46 +00:00
|
|
|
.define("ENABLE_MODULE_SCHNORRSIG", Some("0"))
|
|
|
|
.define("ENABLE_MODULE_EXTRAKEYS", Some("0"))
|
|
|
|
.define("ENABLE_MODULE_ELLSWIFT", Some("0"))
|
2023-11-01 17:06:16 +00:00
|
|
|
// 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(""));
|
2021-06-30 13:56:12 +00:00
|
|
|
|
2019-08-07 18:00:35 +00:00
|
|
|
if cfg!(feature = "lowmemory") {
|
2021-07-21 07:10:35 +00:00
|
|
|
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"));
|
2019-08-07 18:00:35 +00:00
|
|
|
} else {
|
2023-12-09 18:01:52 +00:00
|
|
|
// 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
|
2024-04-28 18:46:46 +00:00
|
|
|
// base_config.define("ECMULT_GEN_PREC_BITS", Some("8"));
|
2023-12-09 18:01:52 +00:00
|
|
|
|
|
|
|
// TODO highly experimental
|
|
|
|
// non-standard optimization
|
|
|
|
// slow compilation
|
2024-04-28 18:46:46 +00:00
|
|
|
base_config.define("ECMULT_GEN_PREC_BITS", Some("16"));
|
2023-12-09 18:01:52 +00:00
|
|
|
|
2024-04-28 18:46:46 +00:00
|
|
|
// ECMULT_WINDOW_SIZE has only a minor performance impact
|
|
|
|
// the default value of 15 is already optimized and would be acceptable
|
|
|
|
// values 14, 16, 17 don't result in a meaningful speedup
|
2019-08-07 18:00:35 +00:00
|
|
|
base_config.define("ECMULT_WINDOW_SIZE", Some("15")); // This is the default in the configure file (`auto`)
|
2024-04-28 18:46:46 +00:00
|
|
|
// base_config.define("ECMULT_WINDOW_SIZE", Some("19"));
|
2019-08-07 18:00:35 +00:00
|
|
|
}
|
2019-10-28 21:05:38 +00:00
|
|
|
base_config.define("USE_EXTERNAL_DEFAULT_CALLBACKS", Some("1"));
|
2019-05-21 07:37:15 +00:00
|
|
|
#[cfg(feature = "recovery")]
|
|
|
|
base_config.define("ENABLE_MODULE_RECOVERY", Some("1"));
|
2015-10-26 17:59:40 +00:00
|
|
|
|
2022-03-29 00:24:47 +00:00
|
|
|
// WASM headers and size/align defines.
|
2021-03-08 13:37:18 +00:00
|
|
|
if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "wasm32" {
|
2022-03-29 00:24:47 +00:00
|
|
|
base_config.include("wasm/wasm-sysroot")
|
|
|
|
.file("wasm/wasm.c");
|
2020-04-16 17:07:14 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 17:59:40 +00:00
|
|
|
// secp256k1
|
2016-01-14 18:35:54 +00:00
|
|
|
base_config.file("depend/secp256k1/contrib/lax_der_parsing.c")
|
2022-03-08 19:45:41 +00:00
|
|
|
.file("depend/secp256k1/src/precomputed_ecmult_gen.c")
|
|
|
|
.file("depend/secp256k1/src/precomputed_ecmult.c")
|
2022-10-06 16:06:12 +00:00
|
|
|
.file("depend/secp256k1/src/secp256k1.c");
|
|
|
|
|
2023-12-09 18:01:52 +00:00
|
|
|
// default CC is gcc on the tested system
|
2024-04-28 18:46:46 +00:00
|
|
|
// observation: GCC 12.2 slightly faster than clang-18 for the unmodified code
|
|
|
|
// observation: clang-14 is slightly faster than gcc 12.2 for the modified code
|
2023-12-09 18:01:52 +00:00
|
|
|
|
2024-04-28 18:46:46 +00:00
|
|
|
// on the tested system, the compilers don't default to use some -fstack-protector level
|
|
|
|
// therefore the use of -fno-stack-protector isn't necessary
|
2023-12-09 18:01:52 +00:00
|
|
|
|
2024-04-28 18:46:46 +00:00
|
|
|
// force use of clang
|
2023-12-09 18:01:52 +00:00
|
|
|
base_config.compiler("clang");
|
|
|
|
|
2024-04-28 18:46:46 +00:00
|
|
|
// gcc 12.2 is faster without this setting on Ryzen Zen 3/4 CPUs
|
2023-12-09 18:01:52 +00:00
|
|
|
base_config.flag("-march=native");
|
|
|
|
|
2024-04-28 18:46:46 +00:00
|
|
|
// doesn't work yet
|
|
|
|
// relevant:
|
|
|
|
// RUSTFLAGS="-Clink-args=-fuse-ld=lld"
|
|
|
|
// installing the lld tool package
|
|
|
|
// for lto=thin, there are problems with "Opaque pointers are only supported in -opaque-pointers mode"
|
|
|
|
// for lto=fat, there are some problems with "error: undefined symbol"
|
2023-12-09 18:01:52 +00:00
|
|
|
// base_config.flag("-flto=thin");
|
2024-04-28 18:46:46 +00:00
|
|
|
// base_config.flag("-flto=fat");
|
2023-12-09 18:01:52 +00:00
|
|
|
|
|
|
|
// 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
|
2024-04-28 18:46:46 +00:00
|
|
|
// base_config.flag("-O3");
|
2023-12-09 18:01:52 +00:00
|
|
|
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 ?
|
2024-04-28 18:46:46 +00:00
|
|
|
base_config.flag("-g0");
|
2023-12-09 18:01:52 +00:00
|
|
|
|
|
|
|
// potential minor improvement
|
|
|
|
// to counteract the -fno-omit-frame-pointer
|
2024-04-28 18:46:46 +00:00
|
|
|
base_config.flag("-fomit-frame-pointer");
|
2023-12-09 18:01:52 +00:00
|
|
|
|
2022-10-06 16:06:12 +00:00
|
|
|
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.
|
2023-12-09 18:01:52 +00:00
|
|
|
// base_config.include("wasm/wasm-sysroot");
|
|
|
|
// base_config.compile("libsecp256k1.a");
|
2022-10-06 16:06:12 +00:00
|
|
|
}
|
2015-10-26 15:21:01 +00:00
|
|
|
}
|