-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild.rs
203 lines (171 loc) · 6.87 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! Build script for zcash_script.
use std::{env, fmt, path::PathBuf};
type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
enum Error {
GenerateBindings,
WriteBindings(std::io::Error),
Env(std::env::VarError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::GenerateBindings => write!(f, "unable to generate bindings: try running 'git submodule init' and 'git submodule update'"),
Error::WriteBindings(source) => write!(f, "unable to write bindings: {}", source),
Error::Env(source) => source.fmt(f),
}
}
}
impl std::error::Error for Error {}
// `bindgen::RustTarget::Stable_*` is deprecated in bindgen >= 0.71.0, but we are constrained
// downstream by the version supported by librocksdb-sys. However, one of our CI jobs still manages
// to pull a newer version, so this silences the deprecation on that job.
#[allow(deprecated)]
fn bindgen_headers() -> Result<()> {
println!("cargo:rerun-if-changed=depend/zcash/src/script/zcash_script.h");
let bindings = bindgen::Builder::default()
.header("depend/zcash/src/script/zcash_script.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// This should not reference a version newer than rust-toolchain.toml. See
// rust-lang/rust-bindgen#3049 for a potential future solution.
.rust_target(bindgen::RustTarget::Stable_1_73)
// Finish the builder and generate the bindings.
.generate()
.map_err(|_| Error::GenerateBindings)?;
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = env::var("OUT_DIR").map_err(Error::Env)?;
let out_path = PathBuf::from(out_path);
bindings
.write_to_file(out_path.join("bindings.rs"))
.map_err(Error::WriteBindings)?;
Ok(())
}
fn main() -> Result<()> {
bindgen_headers()?;
let target = env::var("TARGET").expect("TARGET was not set");
let mut base_config = cc::Build::new();
language_std(&mut base_config, "c++17");
base_config
.include("depend/zcash/src/")
.include("depend/zcash/src/rust/include/")
.include("depend/zcash/src/secp256k1/include/")
.include("depend/expected/include/")
.flag_if_supported("-Wno-implicit-fallthrough")
.flag_if_supported("-Wno-catch-value")
.flag_if_supported("-Wno-reorder")
.flag_if_supported("-Wno-deprecated-copy")
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-variable")
.flag_if_supported("-Wno-ignored-qualifiers")
.flag_if_supported("-Wno-sign-compare")
// when compiling using Microsoft Visual C++, ignore warnings about unused arguments
.flag_if_supported("/wd4100")
.define("HAVE_DECL_STRNLEN", "1")
.define("__STDC_FORMAT_MACROS", None);
// **Secp256k1**
if !cfg!(feature = "external-secp") {
build_secp256k1();
}
if target.contains("windows") {
base_config.define("WIN32", "1");
}
base_config
.file("depend/zcash/src/amount.cpp")
.file("depend/zcash/src/crypto/ripemd160.cpp")
.file("depend/zcash/src/crypto/sha1.cpp")
.file("depend/zcash/src/crypto/sha256.cpp")
.file("depend/zcash/src/pubkey.cpp")
.file("depend/zcash/src/script/interpreter.cpp")
.file("depend/zcash/src/script/script_error.cpp")
.file("depend/zcash/src/script/script.cpp")
.file("depend/zcash/src/script/zcash_script.cpp")
.file("depend/zcash/src/uint256.cpp")
.file("depend/zcash/src/util/strencodings.cpp")
.compile("libzcash_script.a");
Ok(())
}
/// Build the `secp256k1` library.
fn build_secp256k1() {
let mut build = cc::Build::new();
// Compile C99 code
language_std(&mut build, "c99");
// Define configuration constants
build
// This matches the #define in depend/zcash/src/secp256k1/src/secp256k1.c
.define("SECP256K1_BUILD", "")
.define("USE_NUM_NONE", "1")
.define("USE_FIELD_INV_BUILTIN", "1")
.define("USE_SCALAR_INV_BUILTIN", "1")
.define("ECMULT_WINDOW_SIZE", "15")
.define("ECMULT_GEN_PREC_BITS", "4")
// Use the endomorphism optimization now that the patents have expired.
.define("USE_ENDOMORPHISM", "1")
// Technically libconsensus doesn't require the recovery feature, but `pubkey.cpp` does.
.define("ENABLE_MODULE_RECOVERY", "1")
// The source files look for headers inside an `include` sub-directory
.include("depend/zcash/src/secp256k1")
// Some ecmult stuff is defined but not used upstream
.flag_if_supported("-Wno-unused-function")
.flag_if_supported("-Wno-unused-parameter");
if is_big_endian() {
build.define("WORDS_BIGENDIAN", "1");
}
if is_64bit_compilation() {
build
.define("USE_FIELD_5X52", "1")
.define("USE_SCALAR_4X64", "1")
.define("HAVE___INT128", "1");
} else {
build
.define("USE_FIELD_10X26", "1")
.define("USE_SCALAR_8X32", "1");
}
build
.file("depend/zcash/src/secp256k1/src/secp256k1.c")
.file("depend/zcash/src/secp256k1/src/precomputed_ecmult.c")
.file("depend/zcash/src/secp256k1/src/precomputed_ecmult_gen.c")
.compile("libsecp256k1.a");
}
/// Checker whether the target architecture is big endian.
fn is_big_endian() -> bool {
let endianess = env::var("CARGO_CFG_TARGET_ENDIAN").expect("No endian is set");
endianess == "big"
}
/// Check whether we can use 64-bit compilation.
fn is_64bit_compilation() -> bool {
let target_pointer_width =
env::var("CARGO_CFG_TARGET_POINTER_WIDTH").expect("Target pointer width is not set");
if target_pointer_width == "64" {
let check = cc::Build::new()
.file("depend/check_uint128_t.c")
.cargo_metadata(false)
.try_compile("check_uint128_t")
.is_ok();
if !check {
println!(
"cargo:warning=Compiling in 32-bit mode on a 64-bit architecture due to lack of \
uint128_t support."
);
}
check
} else {
false
}
}
/// Configure the language standard used in the build.
///
/// Configures the appropriate flag based on the compiler that's used.
///
/// This will also enable or disable the `cpp` flag if the standard is for C++. The code determines
/// this based on whether `std` starts with `c++` or not.
fn language_std(build: &mut cc::Build, std: &str) {
build.cpp(std.starts_with("c++"));
let flag = if build.get_compiler().is_like_msvc() {
"/std:"
} else {
"-std="
};
build.flag(&[flag, std].concat());
}