ctb_build_support/
build_support.rs1pub(crate) mod utilities {
23 pub use anyhow::{Context, Result, bail, ensure};
24}
25
26pub use fs2;
27pub use sha2;
28
29#[path = "../storage/minimal/global_graph_layout/regions.rs"]
30pub mod global_graph_layout;
31
32#[path = "../storage/minimal/shorthand/core.rs"]
33pub mod shorthand;
34
35#[path = "../formats/dctf/codec.rs"]
36pub mod dctf;
37
38pub mod asset_packer;
39pub mod ctbnbdl;
40pub mod dc_codegen;
41pub mod encoding_codegen;
42pub mod extension_codegen;
43pub mod fnv;
44pub mod format_id_codegen;
45pub mod format_support_codegen;
46pub mod graph_index;
47pub mod ipc_codegen;
48pub mod license_consts;
49pub mod lint;
50pub mod macho_ld;
51pub mod seabios_builder;
52pub mod standard_boilerplate;
53pub mod v86_generator;
54pub mod v86_packer;
55
56use anyhow::{Context, Result, bail};
57use std::path::{Path, PathBuf};
58
59#[must_use]
61pub fn is_workspace_root(dir: &Path) -> bool {
62 dir.join(".ctoolbox_workspace_root").is_file()
63 && (dir.join("Cargo.toml").is_file()
64 && dir.join("src").join("formats").is_dir())
65}
66
67pub fn find_repository_root() -> Result<PathBuf> {
73 if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
74 let manifest_path = PathBuf::from(manifest_dir);
75 let mut cur = manifest_path.as_path();
76 while let Some(parent) = cur.parent() {
77 if is_workspace_root(parent) {
78 return Ok(parent.to_path_buf());
79 }
80 cur = parent;
81 }
82 }
83
84 let mut cur =
85 std::env::current_dir().context("Failed to get current dir")?;
86 loop {
87 if is_workspace_root(&cur) {
88 return Ok(cur);
89 }
90 let Some(parent) = cur.parent() else {
91 break;
92 };
93 cur = parent.to_path_buf();
94 }
95
96 if let Ok(win_root) = std::env::var("CTB_WIN_TEST_ROOT") {
97 let p = PathBuf::from(win_root);
98 if is_workspace_root(&p) {
99 return Ok(p);
100 }
101 }
102
103 if let Ok(exe) = std::env::current_exe() {
104 let mut cur = exe.as_path();
105 while let Some(parent) = cur.parent() {
106 if is_workspace_root(parent) {
107 return Ok(parent.to_path_buf());
108 }
109 cur = parent;
110 }
111 }
112
113 bail!("Could not locate repository root containing src/formats/")
114}
115
116pub fn find_repository_root_from(start: &Path) -> Result<PathBuf> {
121 let mut cur = Some(start);
122 while let Some(dir) = cur {
123 if is_workspace_root(dir) {
124 return Ok(dir.to_path_buf());
125 }
126 cur = dir.parent();
127 }
128 find_repository_root()
129}
130
131#[must_use]
133pub fn is_unassigned_id(val: &str) -> bool {
134 let trimmed = val.trim();
135 if trimmed.is_empty() {
136 return true;
137 }
138 if trimmed.eq_ignore_ascii_case("auto")
139 || trimmed.eq_ignore_ascii_case("tbd")
140 || trimmed == "?"
141 || trimmed == "-"
142 || trimmed.eq_ignore_ascii_case("todo")
143 || trimmed.eq_ignore_ascii_case("unassigned")
144 || trimmed.eq_ignore_ascii_case("none")
145 || trimmed.eq_ignore_ascii_case("new")
146 {
147 return true;
148 }
149 trimmed.parse::<u64>().is_err()
150}