1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 use std::fs::{canonicalize, read_dir, File};
7 use std::io::prelude::*;
8 use std::path::{Path, PathBuf};
10 fn write_shaders(glsl_files: Vec<PathBuf>, shader_file_path: &Path) {
11 let mut shader_file = File::create(shader_file_path).unwrap();
13 write!(shader_file, "/// AUTO GENERATED BY build.rs\n\n").unwrap();
14 write!(shader_file, "use std::collections::HashMap;\n").unwrap();
15 write!(shader_file, "lazy_static! {{\n").unwrap();
18 " pub static ref SHADERS: HashMap<&'static str, &'static str> = {{\n"
20 write!(shader_file, " let mut h = HashMap::new();\n").unwrap();
21 for glsl in glsl_files {
22 let shader_name = glsl.file_name().unwrap().to_str().unwrap();
24 let shader_name = shader_name.replace(".glsl", "");
25 let full_path = canonicalize(&glsl).unwrap();
26 let full_name = full_path.as_os_str().to_str().unwrap();
27 // if someone is building on a network share, I'm sorry.
28 let full_name = full_name.replace("\\\\?\\", "");
29 let full_name = full_name.replace("\\", "/");
32 " h.insert(\"{}\", include_str!(\"{}\"));\n",
37 write!(shader_file, " h\n").unwrap();
38 write!(shader_file, " }};\n").unwrap();
39 write!(shader_file, "}}\n").unwrap();
43 let out_dir = env::var("OUT_DIR").unwrap_or("out".to_owned());
45 let shaders_file = Path::new(&out_dir).join("shaders.rs");
46 let mut glsl_files = vec![];
48 println!("cargo:rerun-if-changed=res");
49 let res_dir = Path::new("res");
50 for entry in read_dir(res_dir).unwrap() {
51 let entry = entry.unwrap();
52 let path = entry.path();
54 if entry.file_name().to_str().unwrap().ends_with(".glsl") {
55 println!("cargo:rerun-if-changed={}", path.display());
56 glsl_files.push(path.to_owned());
60 // Sort the file list so that the shaders.rs file is filled
62 glsl_files.sort_by(|a, b| a.file_name().cmp(&b.file_name()));
64 write_shaders(glsl_files, &shaders_file);