use std::env; use std::fs; use std::path::Path; use std::process::Command; fn main() { match process_all() { Err(error) => println!("cargo-error={}", error), _ => { }, } } fn process_all() -> std::io::Result<()> { process_shaders()?; process_textures()?; process_models()?; Ok(()) } fn process_shaders() -> std::io::Result<()> { let out_dir = env::var_os("OUT_DIR").unwrap(); for input in fs::read_dir("shaders")? { let input_path = input?.path(); if !input_path.is_file() { continue; } if let Some(Some(filename)) = input_path.file_name() .map(|name| name.to_str()) && filename.get(.. 1) == Some(".") { continue; } let output_path = Path::new(&out_dir) .join(format!("{}.spv", input_path.file_name().unwrap().to_str().unwrap())); let result = Command::new("glslang") .arg("-V") .arg(&input_path) .arg("-o") .arg(output_path) .output()?; if !result.status.success() { println!("cargo::error=Failed to compile shader {}; details follow.", input_path.display()); for line in std::str::from_utf8(&result.stdout).unwrap().lines() { println!("cargo::error={}", line); } } println!("cargo::rerun-if-changed={}", input_path.display()); } Ok(()) } fn process_textures() -> std::io::Result<()> { for input in fs::read_dir("textures")? { let input_path = input?.path(); if !input_path.is_file() { continue; } if let Some(Some(filename)) = input_path.file_name() .map(|name| name.to_str()) && filename.get(.. 1) == Some(".") { continue; } println!("cargo::rerun-if-changed={}", input_path.display()); } Ok(()) } fn process_models() -> std::io::Result<()> { for input in fs::read_dir("models")? { let input_path = input?.path(); if !input_path.is_file() { continue; } if let Some(Some(filename)) = input_path.file_name() .map(|name| name.to_str()) && filename.get(.. 1) == Some(".") { continue; } println!("cargo::rerun-if-changed={}", input_path.display()); } Ok(()) }