canic_core/macros/
build.rs1#[macro_export]
10macro_rules! build {
11 ($file:expr) => {{
12 $crate::__canic_build_internal! {
13 $file,
14 |cfg_str, cfg_path, cfg| {
15 let _ = (&cfg_str, &cfg_path, &cfg);
16 }
17 }
18 }};
19}
20
21#[macro_export]
25macro_rules! build_root {
26 ($file:expr) => {{
27 $crate::__canic_build_internal! {
28 $file,
29 |_cfg_str, _cfg_path, _cfg| {}
30 }
31 }};
32}
33
34#[doc(hidden)]
36#[macro_export]
37macro_rules! __canic_build_internal {
38 ($file:expr, |$cfg_str:ident, $cfg_path:ident, $cfg:ident| $body:block) => {{
39 const DEFAULT_CANIC_TOML: &str = r#"controllers = []
40app_directory = []
41
42[subnets.prime]
43"#;
44
45 let mut emitted_config_path = false;
46
47 let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
48 let $cfg_path = std::path::PathBuf::from(manifest_dir).join($file);
49 println!("cargo:rerun-if-changed={}", $cfg_path.display());
50 if let Some(parent) = $cfg_path.parent() {
51 println!("cargo:rerun-if-changed={}", parent.display());
52 }
53
54 let $cfg_str = match std::fs::read_to_string(&$cfg_path) {
55 Ok(s) => s,
56 Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
57 let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
58 let fallback = out_dir.join("canic.default.toml");
59 std::fs::write(&fallback, DEFAULT_CANIC_TOML).expect("write default canic config");
60 println!("cargo:rustc-env=CANIC_CONFIG_PATH={}", fallback.display());
61 println!("cargo:rerun-if-changed={}", fallback.display());
62 emitted_config_path = true;
63 DEFAULT_CANIC_TOML.to_string()
64 }
65 Err(e) => panic!("Failed to read {}: {}", $cfg_path.display(), e),
66 };
67
68 let $cfg = canic::core::config::Config::init_from_toml(&$cfg_str)
70 .expect("Invalid Canic config");
71
72 $body
74
75 if !emitted_config_path {
76 let abs = $cfg_path.canonicalize().expect("canonicalize canic config path");
77 println!("cargo:rustc-env=CANIC_CONFIG_PATH={}", abs.display());
78 println!("cargo:rerun-if-changed={}", abs.display());
79 }
80 }};
81}