Merge pull request #323 from armansito/shaders-fixes-for-bazel

[shaders] Shader path look up fixes for hermetic builds
This commit is contained in:
Arman Uguray 2023-05-16 15:38:00 -07:00 committed by GitHub
commit ef2630ad9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 30 deletions

View file

@ -8,14 +8,26 @@ mod types;
use std::env; use std::env;
use std::fmt::Write; use std::fmt::Write;
use std::path::Path; use std::path::{Path, PathBuf};
use compile::ShaderInfo; use compile::ShaderInfo;
fn main() { fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap(); let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("shaders.rs"); let dest_path = Path::new(&out_dir).join("shaders.rs");
let mut shaders = compile::ShaderInfo::from_dir("../../shader");
// The shaders are defined under the workspace root and not in this crate so we need to locate
// them somehow. Cargo doesn't define an environment variable that points at the root workspace
// directory. In hermetic build environments that don't support relative paths (such as Bazel)
// we support supplying a `WORKSPACE_MANIFEST_FILE` that is expected to be an absolute path to
// the Cargo.toml file at the workspace root. If that's not present, we use a relative path.
let workspace_dir = env::var("WORKSPACE_MANIFEST_FILE")
.ok()
.and_then(|p| Path::new(&p).parent().map(|p| p.to_owned()))
.unwrap_or(PathBuf::from("../../"));
let shader_dir = Path::new(&workspace_dir).join("shader");
let mut shaders = compile::ShaderInfo::from_dir(&shader_dir);
// Drop the HashMap and sort by name so that we get deterministic order. // Drop the HashMap and sort by name so that we get deterministic order.
let mut shaders = shaders.drain().collect::<Vec<_>>(); let mut shaders = shaders.drain().collect::<Vec<_>>();
shaders.sort_by(|x, y| x.0.cmp(&y.0)); shaders.sort_by(|x, y| x.0.cmp(&y.0));

View file

@ -166,9 +166,11 @@ impl ShaderInfo {
for entry in shader_dir for entry in shader_dir
.read_dir() .read_dir()
.expect("Can read shader import directory") .expect("Can read shader import directory")
.filter_map(move |e| {
e.ok()
.filter(|e| e.path().extension().map(|e| e == "wgsl").unwrap_or(false))
})
{ {
let entry = entry.expect("Can continue reading shader import directory");
if entry.file_type().unwrap().is_file() {
let file_name = entry.file_name(); let file_name = entry.file_name();
if let Some(name) = file_name.to_str() { if let Some(name) = file_name.to_str() {
let suffix = ".wgsl"; let suffix = ".wgsl";
@ -191,7 +193,6 @@ impl ShaderInfo {
} }
} }
} }
}
info info
} }
} }

View file

@ -14,9 +14,11 @@ pub fn get_imports(shader_dir: &Path) -> HashMap<String, String> {
for entry in imports_dir for entry in imports_dir
.read_dir() .read_dir()
.expect("Can read shader import directory") .expect("Can read shader import directory")
.filter_map(move |e| {
e.ok()
.filter(|e| e.path().extension().map(|e| e == "wgsl").unwrap_or(false))
})
{ {
let entry = entry.expect("Can continue reading shader import directory");
if entry.file_type().unwrap().is_file() {
let file_name = entry.file_name(); let file_name = entry.file_name();
if let Some(name) = file_name.to_str() { if let Some(name) = file_name.to_str() {
let suffix = ".wgsl"; let suffix = ".wgsl";
@ -27,7 +29,6 @@ pub fn get_imports(shader_dir: &Path) -> HashMap<String, String> {
} }
} }
} }
}
imports imports
} }