-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.rs
89 lines (79 loc) · 2.64 KB
/
build.rs
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
81
82
83
84
85
86
87
88
89
use std::{
env::var,
ffi::OsStr,
fs,
io::Result,
path::{Path, PathBuf},
process::{Command, Output},
};
fn main() {
if !should_skip_shader_compilation() {
compile_shaders();
}
}
fn should_skip_shader_compilation() -> bool {
var("SKIP_SHADER_COMPILATION")
.map(|var| var.parse::<bool>().unwrap_or(false))
.unwrap_or(false)
}
fn compile_shaders() {
println!("Compiling shaders");
let shader_dir_path = get_shader_source_dir_path();
fs::read_dir(shader_dir_path.clone())
.unwrap()
.map(Result::unwrap)
.filter(|dir| dir.file_type().unwrap().is_file())
.filter(|dir| dir.path().extension() != Some(OsStr::new("spv")))
.for_each(|dir| {
let path = dir.path();
let name = path.file_name().unwrap().to_str().unwrap();
let output_name = format!("{}.spv", &name);
println!("Found file {:?}.\nCompiling...", path.as_os_str());
let result = Command::new("glslangValidator")
.current_dir(&shader_dir_path)
.arg("-V")
.arg(&path)
.arg("-o")
.arg(output_name)
.output();
handle_program_result(result);
})
}
fn get_shader_source_dir_path() -> PathBuf {
let path = get_root_path().join("assets").join("shaders");
println!("Shader source directory: {:?}", path.as_os_str());
path
}
fn get_root_path() -> &'static Path {
Path::new(env!("CARGO_MANIFEST_DIR"))
}
fn handle_program_result(result: Result<Output>) {
match result {
Ok(output) => {
if output.status.success() {
println!("Shader compilation succedeed.");
print!(
"stdout: {}",
String::from_utf8(output.stdout)
.unwrap_or("Failed to print program stdout".to_string())
);
} else {
eprintln!("Shader compilation failed. Status: {}", output.status);
eprint!(
"stdout: {}",
String::from_utf8(output.stdout)
.unwrap_or("Failed to print program stdout".to_string())
);
eprint!(
"stderr: {}",
String::from_utf8(output.stderr)
.unwrap_or("Failed to print program stderr".to_string())
);
panic!("Shader compilation failed. Status: {}", output.status);
}
}
Err(error) => {
panic!("Failed to compile shader. Cause: {}", error);
}
}
}