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
fn main() {
#[cfg(feature = "flatc")] {
use std::fs::rename;
use std::path::Path;
use std::process::Command;
match Command::new("flatc").args(&[
"--rust",
"--cpp",
"-o", "src/",
"../zkinterface.fbs",
]).output() {
Ok(flatc) => {
if !
flatc.status.success() {
panic!("\n\nFlatBuffers code generation failed.\n{}\n{}\n",
String::from_utf8_lossy(&flatc.stdout),
String::from_utf8_lossy(&flatc.stderr));
}
// Move C++ file.
rename(
Path::new("src").join("zkinterface_generated.h"),
Path::new("..").join("cpp").join("zkinterface_generated.h"),
).expect("Failed to rename");
// Fix an issue in generated code.
// The lifetime 'a should be on the return value, not on &self.
// Published at https://github.com/google/flatbuffers/pull/5140
{
let file = &Path::new("src").join("zkinterface_generated.rs");
let code = std::fs::read_to_string(file).expect("could not read file");
let re = regex::Regex::new(
r"pub fn (\w+)_as_(\w+)\(&'a self\) -> Option<(\w+)> \{"
).unwrap();
let fixed = re.replace_all(
&code,
r"pub fn ${1}_as_${2}(&self) -> Option<${3}<'a>> {",
).to_string();
let re2 = regex::Regex::new(
r"\(&self\) -> Option<flatbuffers::Vector<flatbuffers::ForwardsUOffset<"
).unwrap();
let fixed2 = re2.replace_all(
&fixed,
r"(&self) -> Option<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<",
).to_string();
std::fs::write(file, fixed2).expect("could not write file");
}
}
Err(_) => {
println!("cargo:warning=Install FlatBuffers (flatc) if you modify `zkinterface.fbs`. Code was not regenerated.");
}
}
}
#[cfg(feature = "cpp")] {
let dst = cmake::Config::new("../cpp").build();
println!("cargo:rustc-link-search=native={}", dst.display());
println!("cargo:rustc-link-lib=gadget_example");
println!("cargo:rustc-link-lib=stdc++"); // GCC
//println!("cargo:rustc-link-lib=c++"); // Clang
let out_dir = std::env::var("OUT_DIR").unwrap();
println!("cargo:include={}", out_dir);
// To use the C++ part in another Rust project, include the environment variable DEP_ZKINTERFACE_INCLUDE
// See https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key
//
// For instance, if you use the cc crate, add:
// .include(std::env::var("DEP_ZKINTERFACE_INCLUDE").unwrap())
}
}