1#![forbid(unsafe_code)]
6
7use std::{
8 env,
9 path::PathBuf,
10};
11
12use anyhow::Result;
13use autozig_engine::{
14 AutoZigEngine,
15 BuildOutput,
16};
17
18pub mod simd;
19
20pub use simd::{
21 detect_and_report,
22 SimdConfig,
23};
24
25pub struct Builder {
27 src_dir: PathBuf,
28}
29
30impl Builder {
31 pub fn new(src_dir: impl Into<PathBuf>) -> Self {
37 Self { src_dir: src_dir.into() }
38 }
39
40 pub fn build(&self) -> Result<BuildOutput> {
48 let out_dir = env::var("OUT_DIR")
50 .map(PathBuf::from)
51 .unwrap_or_else(|_| PathBuf::from("target/debug/build"));
52
53 let engine = AutoZigEngine::new(&self.src_dir, &out_dir);
55 engine.build()
56 }
57}
58
59pub fn build(src_dir: impl Into<PathBuf>) -> Result<BuildOutput> {
71 Builder::new(src_dir).build()
72}
73
74pub fn build_tests(zig_dir: impl Into<PathBuf>) -> Result<Vec<PathBuf>> {
94 use std::fs;
95
96 use autozig_engine::ZigCompiler;
97
98 let zig_dir = zig_dir.into();
99 let out_dir = env::var("OUT_DIR")
100 .map(PathBuf::from)
101 .unwrap_or_else(|_| PathBuf::from("target/debug/build"));
102
103 let compiler = ZigCompiler::new();
104 let mut test_executables = Vec::new();
105
106 if !zig_dir.exists() {
108 println!("cargo:warning=Zig directory not found: {}", zig_dir.display());
109 return Ok(test_executables);
110 }
111
112 for entry in fs::read_dir(&zig_dir)? {
113 let entry = entry?;
114 let path = entry.path();
115
116 if path.extension().and_then(|s| s.to_str()) == Some("zig") {
117 let file_stem = path.file_stem().unwrap().to_str().unwrap();
118 let test_exe = out_dir.join(format!("test_{}", file_stem));
119
120 println!("cargo:warning=Building Zig tests for: {}", path.display());
121
122 compiler.compile_tests(&path, &test_exe, "native")?;
124
125 test_executables.push(test_exe);
126
127 println!("cargo:rerun-if-changed={}", path.display());
128 }
129 }
130
131 println!("cargo:warning=Built {} Zig test executables", test_executables.len());
132
133 Ok(test_executables)
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn test_builder_creation() {
142 let builder = Builder::new("src");
143 assert_eq!(builder.src_dir, PathBuf::from("src"));
144 }
145}