VoxelEngine2/build.rs
2025-06-04 15:50:43 +02:00

89 lines
No EOL
4.3 KiB
Rust

use std::process::Command;
use std::io::{self, Write};
use std::path::Path;
use std::fs::File;
use std::io::{BufReader, BufRead, Error};
fn insert_place_holders(path: &str) {
let input = File::open(path).unwrap();
let mut output = File::create(path.replace("_placeholder", "")).unwrap();
let buffered = BufReader::new(input);
for line in buffered.lines() {
let line_str = line.unwrap();
if line_str.contains("#include ") {
let replacer = File::open(format!("shaders/{}", line_str.clone().split_off(9))).expect(format!("could not find the lib file shaders/{}", line_str.clone().split_off(9)).as_str());
let replacement_buffered = BufReader::new(replacer);
for replacement_line in replacement_buffered.lines() {
write!(output, "{}\n", replacement_line.unwrap()).expect("could not write");
}
} else {
write!(output, "{}\n", line_str).expect("could not write");
}
}
}
fn main() {
println!("cargo::rerun-if-changed=shaders/cube.frag");
println!("cargo::rerun-if-changed=shaders/cube.geom");
println!("cargo::rerun-if-changed=shaders/cube.vert");
println!("cargo::rerun-if-changed=shaders/cuboid.frag");
println!("cargo::rerun-if-changed=shaders/cuboid.geom");
println!("cargo::rerun-if-changed=shaders/cuboid.vert");
println!("cargo::rerun-if-changed=shaders/rt_quad.vert");
println!("cargo::rerun-if-changed=shaders/rt_lib.frag");
println!("cargo::rerun-if-changed=shaders/rt_quad_placeholder.frag");
println!("cargo::rerun-if-changed=shaders/rt_compute_rasterize.comp");
println!("cargo::rerun-if-changed=shaders/rt_compute_grow_one.comp");
println!("cargo::rerun-if-changed=shaders/rt_compute_grow_two.comp");
println!("cargo::rerun-if-changed=shaders/rt_compute_grow_three.comp");
println!("cargo::rerun-if-changed=shaders/rt_compute_mempos.comp");
// replace placeholders
insert_place_holders("shaders/rt_quad_placeholder.frag");
std::fs::remove_file("shaders/compiled/geo_cube.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/frag_cube.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/vert_cube.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/geo_cuboid.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/frag_cuboid.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/vert_cuboid.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/vert_rt_quad.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/frag_rt_quad.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/rt_compute_rasterize.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/rt_compute_grow_one.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/rt_compute_grow_two.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/rt_compute_grow_three.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/rt_compute_mempos.spv").unwrap_or(());
if std::env::consts::OS == "windows" {
let mut command = Command::new("./shaders/compile.bat");
let output = command.output().expect("Failed to execute command");
println!("status: {}", output.status);
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
assert!(output.status.success());
} else if std::env::consts::OS == "linux" {
let mut command = Command::new("./shaders/compile.sh");
let output = command.output().expect("Failed to execute command");
println!("status: {}", output.status);
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
assert!(output.status.success());
}
assert!(Path::new("shaders/compiled/geo_cube.spv").exists());
assert!(Path::new("shaders/compiled/frag_cube.spv").exists());
assert!(Path::new("shaders/compiled/vert_cube.spv").exists());
assert!(Path::new("shaders/compiled/geo_cuboid.spv").exists());
assert!(Path::new("shaders/compiled/frag_cuboid.spv").exists());
assert!(Path::new("shaders/compiled/vert_cuboid.spv").exists());
assert!(Path::new("shaders/compiled/vert_rt_quad.spv").exists());
assert!(Path::new("shaders/compiled/frag_rt_quad.spv").exists());
}