-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild.rs
268 lines (227 loc) · 9.08 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::{env, fmt, fs, io::Read, path::PathBuf};
use syn::__private::ToTokens;
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 {}
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))
// 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(())
}
/// Use cxx_gen to generate headers and source files for FFI bindings,
/// just like zcash does (see depend/zcash/src/Makefile.am).
/// (Note that zcash uses the cxxbridge-cmd binary, while we use the
/// cxx_gen library, but the result is the same.)
///
/// We could use [`cxx_build`](https://cxx.rs/tutorial.html#compiling-the-c-code-with-cargo)
/// to do this automatically. But zcash uses the
/// [manual approach](https://cxx.rs/tutorial.html#c-generated-code) which
/// creates the headers with non-standard names and paths (e.g. "blake2b.h",
/// instead of "blake2b.rs.h" which what cxx_build would create). This would
/// requires us to rename/link files which is awkward.
///
/// Note that we must generate the files in the target dir (OUT_DIR) and not in
/// any source folder, because `cargo package` does not allow that.
/// (This is in contrast to zcash which generates in `depend/zcash/src/rust/gen/`)
fn gen_cxxbridge() -> Result<()> {
let out_path = env::var("OUT_DIR").map_err(Error::Env)?;
let out_path = PathBuf::from(out_path).join("include");
// These must match `CXXBRIDGE_RS` in depend/zcash/src/Makefile.am
let filenames = [
"blake2b",
"bundlecache",
"equihash",
"orchard_bundle",
"sapling",
"wallet_scanner",
];
// The output folder must exist
fs::create_dir_all(out_path.join("rust")).unwrap();
// Generate the generic header file
fs::write(out_path.join("rust/cxx.h"), cxx_gen::HEADER).unwrap();
// Generate the source and header for each bridge file
for filename in filenames {
println!(
"cargo:rerun-if-changed=depend/zcash/src/rust/src/{}.rs",
filename
);
let mut file =
fs::File::open(format!("depend/zcash/src/rust/src/{}.rs", filename).as_str()).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
let ast = syn::parse_file(&content).unwrap();
let token_stream = ast.to_token_stream();
let mut opt = cxx_gen::Opt::default();
opt.include.push(cxx_gen::Include {
path: "rust/cxx.h".to_string(),
kind: cxx_gen::IncludeKind::Quoted,
});
let output = cxx_gen::generate_header_and_cc(token_stream, &opt).unwrap();
fs::write(out_path.join(format!("rust/{}.h", filename)), output.header).unwrap();
fs::write(
out_path.join(format!("rust/{}.c", filename)),
output.implementation,
)
.unwrap();
}
Ok(())
}
fn main() -> Result<()> {
bindgen_headers()?;
gen_cxxbridge()?;
let include_path = env::var("OUT_DIR").map_err(Error::Env)?;
let include_path = PathBuf::from(include_path).join("include");
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(&include_path)
.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/script/zcash_script.cpp")
.file("depend/zcash/src/util/strencodings.cpp")
.file("depend/zcash/src/uint256.cpp")
.file("depend/zcash/src/pubkey.cpp")
.file("depend/zcash/src/hash.cpp")
.file("depend/zcash/src/primitives/transaction.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/crypto/sha512.cpp")
.file("depend/zcash/src/crypto/hmac_sha512.cpp")
.file("depend/zcash/src/script/interpreter.cpp")
.file("depend/zcash/src/script/script.cpp")
.file("depend/zcash/src/script/script_error.cpp")
.file("depend/zcash/src/support/cleanse.cpp")
.file(include_path.join("rust/blake2b.c"))
.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
.define("SECP256K1_BUILD", "1")
.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")
.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());
}