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
use candid::Principal;
fn validate_principal(label: &str, text: &str) {
if let Err(err) = Principal::from_text(text) {
panic!("Invalid principal literal {label}: {text} ({err})");
}
}
macro_rules! static_canisters {
($($name:ident = $id:expr;)+) => {{
$(validate_principal(stringify!($name), $id);)+
}};
}
macro_rules! sns_table {
(
$(
$name:ident {
root: $root:expr,
governance: $gov:expr,
index: $idx:expr,
ledger: $led:expr $(,)?
}
),+ $(,)?
) => {{
$(
validate_principal(concat!(stringify!($name), ".root"), $root);
validate_principal(concat!(stringify!($name), ".governance"), $gov);
validate_principal(concat!(stringify!($name), ".index"), $idx);
validate_principal(concat!(stringify!($name), ".ledger"), $led);
)+
}};
}
fn main() {
// Re-run this build script if the build-time network changes.
// This ensures downstream code sees the correct value via env!/option_env!.
println!("cargo:rerun-if-env-changed=DFX_NETWORK");
println!("cargo:rerun-if-changed=src/env/ck.inc.rs");
println!("cargo:rerun-if-changed=src/env/nns.inc.rs");
println!("cargo:rerun-if-changed=src/env/sns.inc.rs");
// Share the same principal data with build-time validation.
include!("src/env/ck.inc.rs");
include!("src/env/nns.inc.rs");
include!("src/env/sns.inc.rs");
match std::env::var("DFX_NETWORK") {
// Valid, explicit network: propagate it as a compile-time environment variable.
//
// Library crates do not *require* this, but if present and valid we make it
// available so dependent crates can read a consistent build-time value.
Ok(val) if val == "local" || val == "ic" => {
println!("cargo:rustc-env=DFX_NETWORK={val}");
}
// Invalid value provided: warn, but do not fail the build.
//
// This keeps library builds usable under raw Cargo while clearly signaling
// that top-level canisters are expected to enforce correctness.
Ok(other) => {
println!(
"cargo:warning=Invalid DFX_NETWORK='{other}'; expected 'local' or 'ic'. \
Downstream canisters must enforce this explicitly.",
);
}
// No network specified: assume a library/dependency build.
//
// Cargo does not provide DFX_NETWORK by default, and dependency crates must
// remain buildable in isolation. Canister/root crates are responsible for
// enforcing a concrete network at their build boundary.
Err(_) => {
println!(
"cargo:warning=DFX_NETWORK not set; assuming library/dependency build. \
Canister crates must enforce this at their boundary."
);
}
}
}