summary refs log tree commit diff
path: root/build.rs
blob: ce7485442d6a887e70d56a2bcd37931ff4868d24 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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(())
}