Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
143 lines (128 loc) · 5.14 KB
/
Copy pathbuild.rs
File metadata and controls
143 lines (128 loc) · 5.14 KB
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
// Copyright 2019-2026 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use ahash::HashMap;
use serde::{Deserialize, Serialize};
use std::{
fs::File,
io::{BufWriter, Write},
path::PathBuf,
};
fn main() {
// Only needed when profiling Forest with `gperftools`. This might not work on all platforms.
println!("cargo:rerun-if-env-changed=FOREST_PROFILING_GPERFTOOLS_BUILD");
if is_env_truthy("FOREST_PROFILING_GPERFTOOLS_BUILD") {
println!("cargo:rustc-link-lib=tcmalloc");
}
// whitelist the cfg for cargo clippy
println!("cargo::rustc-check-cfg=cfg(f3sidecar)");
println!("cargo:rerun-if-env-changed=FOREST_FFI_GO_REGENERATE");
if is_env_truthy("FOREST_FFI_GO_REGENERATE")
&& is_env_truthy("FOREST_F3_SIDECAR_FFI_BUILD_OPT_OUT")
{
println!(
"cargo:warning=FOREST_FFI_GO_REGENERATE has no effect on the f3-sidecar binding: \
FOREST_F3_SIDECAR_FFI_BUILD_OPT_OUT skips the build that regenerates it"
);
}
// Do not build f3-sidecar on docs.rs publishing
// No proper version of Go compiler is available.
println!("cargo:rerun-if-env-changed=FOREST_F3_SIDECAR_FFI_BUILD_OPT_OUT");
if !is_docs_rs() && !is_env_truthy("FOREST_F3_SIDECAR_FFI_BUILD_OPT_OUT") {
println!("cargo:rustc-cfg=f3sidecar");
println!("cargo::rerun-if-changed=f3-sidecar");
unsafe {
std::env::set_var("GOWORK", "off");
// `Netgo` is enabled for all the platforms to be consistent across different builds. It
// is using pure Go implementation for functionality like name resolution. In the case of
// sidecar it does not make much difference, but it does fix the Apple silicons builds.
// See <https://github.com/status-im/status-mobile/issues/20135#issuecomment-2137400475>
std::env::set_var("GOFLAGS", "-tags=netgo");
}
println!("cargo:rerun-if-changed=src/f3/go_ffi.rs");
let mut builder = rust2go::Builder::default().with_go_src("./f3-sidecar");
// the generated Go file has been committed to the git repository
// set the var to regenerate the file
if is_env_truthy("FOREST_FFI_GO_REGENERATE") {
builder = builder.with_regen_arg(rust2go::RegenArgs {
src: "./src/f3/go_ffi.rs".into(),
dst: "./f3-sidecar/ffi_gen.go".into(),
without_main: true,
..Default::default()
})
}
builder.build();
}
rpc_regression_tests_gen();
state_compute_tests_gen();
}
// See <https://docs.rs/about/builds#detecting-docsrs>
fn is_docs_rs() -> bool {
std::env::var("DOCS_RS").is_ok()
}
fn is_env_truthy(env: &str) -> bool {
std::env::var(env)
.ok()
.map(|var| matches!(var.to_lowercase().as_str(), "1" | "true" | "yes" | "_yes_"))
.unwrap_or_default()
}
fn rpc_regression_tests_gen() {
println!("cargo:rerun-if-changed=src/tool/subcommands/api_cmd/test_snapshots.txt");
let tests: Vec<&str> = include_str!("src/tool/subcommands/api_cmd/test_snapshots.txt")
.lines()
.map(|i| {
// Remove comment
i.split("#").next().unwrap().trim()
})
.filter(|l| !l.is_empty() && !l.starts_with('#'))
.collect();
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let out_path = out_dir.join("__rpc_regression_tests_gen.rs");
let mut w = BufWriter::new(File::create(&out_path).unwrap());
for test in tests {
// Derive a valid Rust identifier from the snapshot filename.
let ident = test
.strip_suffix(".rpcsnap.json.zst")
.unwrap_or(test)
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
.collect::<String>();
writeln!(
w,
r#"
#[tokio::test(flavor = "multi_thread")]
#[fickle::fickle]
async fn cargo_test_rpc_snapshot_test_{ident}() {{
rpc_regression_test_run("{test}").await
}}
"#,
)
.unwrap();
}
}
fn state_compute_tests_gen() {
println!("cargo:rerun-if-changed=src/state_manager/state_compute_tests.yaml");
let StateComputeTests { tests } =
serde_yaml::from_str(include_str!("src/state_manager/state_compute_tests.yaml")).unwrap();
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let out_path = out_dir.join("__state_compute_tests_gen.rs");
let mut w = BufWriter::new(File::create(&out_path).unwrap());
for (chain, epochs) in tests {
for epoch in epochs {
writeln!(
w,
r#"
#[tokio::test(flavor = "multi_thread")]
#[fickle::fickle]
async fn cargo_test_state_compute_{chain}_{epoch}() {{
state_compute_test_run("{chain}".parse().unwrap(), {epoch}).await
}}
"#,
)
.unwrap();
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct StateComputeTests {
tests: HashMap<String, Vec<i64>>,
}