ctb_build_support/
build_support.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Build support library for ctoolbox build process.
21
22pub(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/// Checks whether a directory appears to be the root of the ctoolbox repository.
60#[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
67/// Discovers the root directory of the ctoolbox repository.
68///
69/// # Errors
70/// Returns an error if directory traversal fails or the repository root
71/// cannot be found.
72pub 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
116/// Discovers the root directory of the ctoolbox repository starting from a path.
117///
118/// # Errors
119/// Returns an error if repository root cannot be determined.
120pub 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/// Checks whether a raw cell string represents an unassigned ID placeholder.
132#[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}