Skip to content

Commit 8895848

Browse files
committed
Vendor SPIRV-Headers grammar for git dependency builds
When spirv-tools-core is used as a cargo git dependency, submodules are not cloned, so the grammar file at external/spirv-headers/ is unavailable. Vendor a copy under grammar/ and update build.rs to fall back to it when the submodule path does not exist. Resolution order: SPIRV_HEADERS_PATH env var > submodule > vendored.
1 parent ce2a362 commit 8895848

2 files changed

Lines changed: 19253 additions & 8 deletions

File tree

rust/spirv-tools-core/build.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,28 @@ fn bit_enum_ident(enumerant: &str) -> String {
107107

108108
fn main() {
109109
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR");
110-
// Allow overriding the SPIRV-Headers path via environment variable for standalone builds.
111-
// When used as a git dependency, the submodule isn't available, so users can set
112-
// SPIRV_HEADERS_PATH to point to their local SPIRV-Headers checkout.
110+
println!("cargo:rerun-if-env-changed=SPIRV_HEADERS_PATH");
111+
// Resolve the grammar file with the following priority:
112+
// 1. SPIRV_HEADERS_PATH env var (explicit override)
113+
// 2. ../../external/spirv-headers/ (submodule, for in-tree builds)
114+
// 3. grammar/ (vendored copy, for git dependency builds)
113115
let grammar_path = if let Ok(headers_path) = env::var("SPIRV_HEADERS_PATH") {
114-
println!("cargo:rerun-if-env-changed=SPIRV_HEADERS_PATH");
115116
PathBuf::from(headers_path).join("include/spirv/unified1/spirv.core.grammar.json")
116117
} else {
117-
PathBuf::from(&manifest_dir)
118-
.join("../../external/spirv-headers/include/spirv/unified1/spirv.core.grammar.json")
118+
let submodule_path = PathBuf::from(&manifest_dir)
119+
.join("../../external/spirv-headers/include/spirv/unified1/spirv.core.grammar.json");
120+
if submodule_path.exists() {
121+
submodule_path
122+
} else {
123+
// Fall back to vendored grammar for git dependency builds
124+
PathBuf::from(&manifest_dir).join("grammar/spirv.core.grammar.json")
125+
}
119126
};
120127
let grammar_contents = fs::read_to_string(&grammar_path).unwrap_or_else(|_| {
121128
panic!(
122129
"failed to read spirv.core.grammar.json from {:?}. \
123-
If building as a git dependency, set SPIRV_HEADERS_PATH to point to a \
124-
SPIRV-Headers checkout.",
130+
Set SPIRV_HEADERS_PATH to point to a SPIRV-Headers checkout, or ensure \
131+
the vendored grammar/ directory is present.",
125132
grammar_path
126133
)
127134
});

0 commit comments

Comments
 (0)