Skip to main content

khal_std/
build_script.rs

1//! Build-script helpers for shader crates.
2//!
3//! Host-only module (gated out of GPU-target builds). Intended to be called
4//! from a shader crate's `build.rs` — see [`setup_shader_crate_build`].
5
6/// Standard `build.rs` setup that every shader crate should run.
7///
8/// Does three things:
9///
10/// 1. Emits `cargo::metadata=manifest_dir=<path>` so that host crates
11///    consuming this shader crate via `KhalBuilder::from_dependency`
12///    discover the shader sources both in-workspace and from
13///    `crates.io`-fetched copies.
14/// 2. Declares the `target_arch_is_gpu` cfg via `cargo::rustc-check-cfg`
15///    so `#[cfg(target_arch_is_gpu)]` / `#[cfg(not(target_arch_is_gpu))]`
16///    don't trip the `unexpected_cfgs` lint.
17/// 3. Sets `target_arch_is_gpu` when compiling for any GPU target
18///    (SPIR-V, NVPTX). The host CPU build sees it unset.
19///
20/// Call from `build.rs`:
21///
22/// ```no_run
23/// khal_std::build_script::setup_shader_crate_build();
24/// ```
25///
26/// The shader crate must list `khal-std` as a `[build-dependencies]` entry
27/// (in addition to its regular `[dependencies]` use).
28pub fn setup_shader_crate_build() {
29    let manifest_dir =
30        std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set by cargo");
31    println!("cargo::metadata=manifest_dir={manifest_dir}");
32    println!("cargo:rerun-if-changed=build.rs");
33
34    println!("cargo::rustc-check-cfg=cfg(target_arch_is_gpu)");
35
36    let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
37    if matches!(target_arch.as_str(), "spirv" | "nvptx64") {
38        println!("cargo::rustc-cfg=target_arch_is_gpu");
39    }
40}