spirv_builder/
lib.rs

1// FIXME(eddyb) update/review these lints.
2//
3// BEGIN - Embark standard lints v0.4
4// do not change or add/remove here, but one can add exceptions after this section
5// for more info see: <https://github.com/EmbarkStudios/rust-ecosystem/issues/59>
6#![deny(unsafe_code)]
7#![warn(
8    clippy::all,
9    clippy::await_holding_lock,
10    clippy::char_lit_as_u8,
11    clippy::checked_conversions,
12    clippy::dbg_macro,
13    clippy::debug_assert_with_mut_call,
14    clippy::doc_markdown,
15    clippy::empty_enums,
16    clippy::enum_glob_use,
17    clippy::exit,
18    clippy::expl_impl_clone_on_copy,
19    clippy::explicit_deref_methods,
20    clippy::explicit_into_iter_loop,
21    clippy::fallible_impl_from,
22    clippy::filter_map_next,
23    clippy::float_cmp_const,
24    clippy::fn_params_excessive_bools,
25    clippy::if_let_mutex,
26    clippy::implicit_clone,
27    clippy::imprecise_flops,
28    clippy::inefficient_to_string,
29    clippy::invalid_upcast_comparisons,
30    clippy::large_types_passed_by_value,
31    clippy::let_unit_value,
32    clippy::linkedlist,
33    clippy::lossy_float_literal,
34    clippy::macro_use_imports,
35    clippy::manual_ok_or,
36    clippy::map_err_ignore,
37    clippy::map_flatten,
38    clippy::map_unwrap_or,
39    clippy::match_same_arms,
40    clippy::match_wildcard_for_single_variants,
41    clippy::mem_forget,
42    clippy::mut_mut,
43    clippy::mutex_integer,
44    clippy::needless_borrow,
45    clippy::needless_continue,
46    clippy::option_option,
47    clippy::path_buf_push_overwrite,
48    clippy::ptr_as_ptr,
49    clippy::ref_option_ref,
50    clippy::rest_pat_in_fully_bound_structs,
51    clippy::same_functions_in_if_condition,
52    clippy::semicolon_if_nothing_returned,
53    clippy::string_add_assign,
54    clippy::string_add,
55    clippy::string_lit_as_bytes,
56    clippy::todo,
57    clippy::trait_duplication_in_bounds,
58    clippy::unimplemented,
59    clippy::unnested_or_patterns,
60    clippy::unused_self,
61    clippy::useless_transmute,
62    clippy::verbose_file_reads,
63    clippy::zero_sized_map_values,
64    future_incompatible,
65    nonstandard_style,
66    rust_2018_idioms
67)]
68// END - Embark standard lints v0.4
69// crate-specific exceptions:
70// #![allow()]
71#![doc = include_str!("../README.md")]
72
73pub mod cargo_cmd;
74mod depfile;
75#[cfg(test)]
76mod tests;
77#[cfg(feature = "watch")]
78mod watch;
79
80use raw_string::{RawStr, RawString};
81use semver::Version;
82use serde::Deserialize;
83use std::borrow::Borrow;
84use std::collections::HashMap;
85use std::env;
86use std::fs::File;
87use std::io::BufReader;
88use std::path::{Path, PathBuf};
89use std::process::Stdio;
90use thiserror::Error;
91
92#[cfg(feature = "watch")]
93pub use self::watch::{SpirvWatcher, SpirvWatcherError};
94pub use rustc_codegen_spirv_types::*;
95
96#[derive(Debug, Error)]
97#[non_exhaustive]
98pub enum SpirvBuilderError {
99    #[error("`target` must be set, for example `spirv-unknown-vulkan1.2`")]
100    MissingTarget,
101    #[error("TargetError: {0}")]
102    TargetError(#[from] TargetError),
103    #[error("`path_to_crate` must be set")]
104    MissingCratePath,
105    #[error("crate path '{0}' does not exist")]
106    CratePathDoesntExist(PathBuf),
107    #[error(
108        "Without feature `rustc_codegen_spirv`, you need to set the path of the dylib with `rustc_codegen_spirv_location`"
109    )]
110    MissingRustcCodegenSpirvDylib,
111    #[error("`rustc_codegen_spirv_location` path '{0}' is not a file")]
112    RustcCodegenSpirvDylibDoesNotExist(PathBuf),
113    #[error("build failed")]
114    BuildFailed,
115    #[error(
116        "`multimodule: true` build cannot be used together with `build_script.env_shader_spv_path: true`"
117    )]
118    MultiModuleWithEnvShaderSpvPath,
119    #[error("multi-module metadata file missing")]
120    MetadataFileMissing(#[from] std::io::Error),
121    #[error("unable to parse multi-module metadata file")]
122    MetadataFileMalformed(#[from] serde_json::Error),
123    #[error(
124        "`{ARTIFACT_SUFFIX}` artifact not found in (supposedly successful) build output.\n--- build output ---\n{stdout}"
125    )]
126    NoArtifactProduced { stdout: String },
127    #[error("cargo metadata error")]
128    CargoMetadata(#[from] cargo_metadata::Error),
129    #[cfg(feature = "watch")]
130    #[error(transparent)]
131    WatchFailed(#[from] SpirvWatcherError),
132}
133
134#[derive(Debug, PartialEq, Eq, Clone, Copy, Default, serde::Deserialize, serde::Serialize)]
135#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
136#[non_exhaustive]
137pub enum SpirvMetadata {
138    /// Strip all names and other debug information from SPIR-V output.
139    #[default]
140    None,
141    /// Only include `OpName`s for public interface variables (uniforms and the like), to allow
142    /// shader reflection.
143    NameVariables,
144    /// Include all `OpName`s for everything, and `OpLine`s. Significantly increases binary size.
145    Full,
146}
147
148/// Strategy used to handle Rust `panic!`s in shaders compiled to SPIR-V.
149#[derive(Debug, PartialEq, Eq, Clone, Copy, Default, serde::Deserialize, serde::Serialize)]
150#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
151#[non_exhaustive]
152pub enum ShaderPanicStrategy {
153    /// Return from shader entry-point with no side-effects **(default)**.
154    ///
155    /// While similar to the standard SPIR-V `OpTerminateInvocation`, this is
156    /// *not* limited to fragment shaders, and instead supports all shaders
157    /// (as it's handled via control-flow rewriting, instead of SPIR-V features).
158    #[default]
159    SilentExit,
160
161    /// Like `SilentExit`, but also using `debugPrintf` to report the panic in
162    /// a way that can reach the user, before returning from the entry-point.
163    ///
164    /// Quick setup for enabling `debugPrintf` output (to stdout) at runtime:
165    /// - **set these environment variables**:
166    ///   - `VK_LOADER_LAYERS_ENABLE=VK_LAYER_KHRONOS_validation`
167    ///   - `VK_LAYER_PRINTF_ONLY_PRESET=1`
168    ///   - `VK_LAYER_PRINTF_TO_STDOUT=1` (not always needed, but can help)
169    /// - if using `wgpu`, enable `wgpu::Features::SPIRV_SHADER_PASSTHROUGH`,
170    ///   and use `create_shader_module_passthrough` instead of `create_shader_module`
171    /// - in case of errors, or no output (from a `panic!()`/`debug_printf!()`),
172    ///   keep reading below for additional information and alternatives
173    ///
174    /// ---
175    ///
176    /// **Note**: enabling this automatically adds the `SPV_KHR_non_semantic_info`
177    /// extension, as `debugPrintf` is from a "non-semantic extended instruction set".
178    ///
179    /// **Note**: `debugPrintf` output reaching the user involves:
180    /// - being able to load the shader in the first place:
181    ///   - for `wgpu`, use "SPIR-V shader passthrough" (Naga lacks `debugPrintf`):
182    ///     - enable `wgpu::Features::SPIRV_SHADER_PASSTHROUGH`
183    ///     - replace `create_shader_module` calls with `create_shader_module_passthrough`
184    ///   - *in theory*, the `VK_KHR_shader_non_semantic_info` Vulkan *Device* extension
185    ///     (or requiring at least Vulkan 1.3, which incorporated it)
186    ///     - *however*, Validation Layers don't actually check this anymore,
187    ///       since Vulkan SDK version 1.4.313.0 (and drivers shouldn't care either)
188    /// - **general configurability** of [Vulkan SDK](https://vulkan.lunarg.com/sdk/home)
189    ///   and/or [Vulkan Loader](https://github.com/KhronosGroup/Vulkan-Loader)
190    ///   - *(this list doubles as a legend for shorthands used later below)*
191    ///   - **env**: setting environment variables on the fly
192    ///     - easiest for quick testing, no code changes/rebuilding needed
193    ///     - e.g. `FOO=1 cargo run ...` (in UNIX-style shells)
194    ///   - **instance**: programmatic control via `vkCreateInstance()` params
195    ///     - best for integration with app-specific debugging functionality
196    ///     - limited to direct Vulkan usage (e.g. `ash`, not `wgpu`)
197    ///     - `VK_EXT_layer_settings` as a `VK_LAYER_*` environment variables
198    ///       analogue, e.g. `VK_LAYER_FOO` controlled by a `VkLayerSettingEXT`
199    ///       with `"foo"` as `pSettingName` (and an appropriate `type`/value),
200    ///       included in `VkLayerSettingsCreateInfoEXT`'s `pSettings`
201    ///   - on-disk configuration and interactive tooling, e.g.:
202    ///     - `vk_layer_settings.txt` files, either hand-written, or generated by
203    ///       the "Vulkan Configurator" GUI tool (included with the Vulkan SDK)
204    ///     - third-party Vulkan debuggers like `RenderDoc`
205    /// - [Vulkan Validation Layers](https://github.com/KhronosGroup/Vulkan-ValidationLayers)
206    ///   - (they contain the `debugPrintf` implementation, a SPIR-V -> SPIR-V translation)
207    ///   - enabled by one of (as per "**general configurability**" above):
208    ///     - **env**: `VK_LOADER_LAYERS_ENABLE=VK_LAYER_KHRONOS_validation`
209    ///     - **instance**: `"VK_LAYER_KHRONOS_validation"` in the list of layers
210    ///     - via `wgpu`: `wgpu::InstanceFlags::VALIDATION`
211    /// - Validation Layers' `debugPrintf` support
212    ///   ([official docs](https://github.com/KhronosGroup/Vulkan-ValidationLayers/blob/main/docs/debug_printf.md)):
213    ///   - enabled by one of (as per "**general configurability**" above):
214    ///     - **env**: `VK_LAYER_PRINTF_ENABLE=1` (validation + `debugPrintf`)
215    ///     - **env**: `VK_LAYER_PRINTF_ONLY_PRESET=1` (*only* `debugPrintf`, no validation)
216    ///     - **instance**: `"printf_enable"` / `"printf_only_preset"` via `VkLayerSettingEXT`
217    ///       (i.e. analogues for the two environment variables)
218    ///     - **instance**: `VkValidationFeaturesEXT` with `pEnabledValidationFeatures`
219    ///       containing `VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT`
220    /// - outputting the `debugPrintf` messages sent back from the GPU:
221    ///   - defaults to common validation logging (itself defaulting to stdout)
222    ///   - **env**: `VK_LAYER_PRINTF_TO_STDOUT=1` (and its **instance** analogue)
223    ///     forces direct printing to stdout, bypassing `VK_EXT_debug_utils` etc.
224    ///   - validation logging can itself be controlled via `VK_EXT_debug_utils`
225    ///   - `wgpu` built in debug mode (and/or with debug-assertions enabled):
226    ///     - it uses `VK_EXT_debug_utils` internally, exposing it via `log`
227    ///     - with e.g. `env_logger`, `RUST_LOG=info` suffices for `debugPrintf`
228    ///       messages (as they specifically have the "info" level)
229    ///     - other `log`/`tracing` subscribers should be configured similarly
230    #[cfg_attr(feature = "clap", clap(skip))]
231    DebugPrintfThenExit {
232        /// Whether to also print the entry-point inputs (excluding buffers/resources),
233        /// which should uniquely identify the panicking shader invocation.
234        print_inputs: bool,
235
236        /// Whether to also print a "backtrace" (i.e. the chain of function calls
237        /// that led to the `panic!`).
238        ///
239        /// As there is no way to dynamically compute this information, the string
240        /// containing the full backtrace of each `panic!` is statically generated,
241        /// meaning this option could significantly increase binary size.
242        print_backtrace: bool,
243    },
244
245    /// **Warning**: this is _**unsound**_ (i.e. adds Undefined Behavior to *safe* Rust code)
246    ///
247    /// This option only exists for testing (hence the unfriendly name it has),
248    /// and more specifically testing whether conditional panics are responsible
249    /// for performance differences when upgrading from older Rust-GPU versions
250    /// (which used infinite loops for panics, that `spirv-opt`/drivers could've
251    /// sometimes treated as UB, and optimized as if they were impossible to reach).
252    ///
253    /// Unlike those infinite loops, however, this uses `OpUnreachable`, so it
254    /// forces the old worst-case (all `panic!`s become UB and are optimized out).
255    #[allow(non_camel_case_types)]
256    UNSOUND_DO_NOT_USE_UndefinedBehaviorViaUnreachable,
257}
258
259/// Options for specifying the behavior of the validator
260/// Copied from `spirv-tools/src/val.rs` struct `ValidatorOptions`, with some fields disabled.
261#[derive(Default, Debug, Clone, serde::Deserialize, serde::Serialize)]
262#[cfg_attr(feature = "clap", derive(clap::Parser))]
263#[non_exhaustive]
264pub struct ValidatorOptions {
265    /// Record whether or not the validator should relax the rules on types for
266    /// stores to structs.  When relaxed, it will allow a type mismatch as long as
267    /// the types are structs with the same layout.  Two structs have the same layout
268    /// if
269    ///
270    /// 1) the members of the structs are either the same type or are structs with
271    ///    same layout, and
272    ///
273    /// 2) the decorations that affect the memory layout are identical for both
274    ///    types.  Other decorations are not relevant.
275    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
276    pub relax_struct_store: bool,
277    /// Records whether or not the validator should relax the rules on pointer usage
278    /// in logical addressing mode.
279    ///
280    /// When relaxed, it will allow the following usage cases of pointers:
281    /// 1) `OpVariable` allocating an object whose type is a pointer type
282    /// 2) `OpReturnValue` returning a pointer value
283    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
284    pub relax_logical_pointer: bool,
285    // /// Records whether or not the validator should relax the rules because it is
286    // /// expected that the optimizations will make the code legal.
287    // ///
288    // /// When relaxed, it will allow the following:
289    // /// 1) It will allow relaxed logical pointers.  Setting this option will also
290    // ///    set that option.
291    // /// 2) Pointers that are pass as parameters to function calls do not have to
292    // ///    match the storage class of the formal parameter.
293    // /// 3) Pointers that are actaul parameters on function calls do not have to point
294    // ///    to the same type pointed as the formal parameter.  The types just need to
295    // ///    logically match.
296    // pub before_legalization: bool,
297    /// Records whether the validator should use "relaxed" block layout rules.
298    /// Relaxed layout rules are described by Vulkan extension
299    /// `VK_KHR_relaxed_block_layout`, and they affect uniform blocks, storage blocks,
300    /// and push constants.
301    ///
302    /// This is enabled by default when targeting Vulkan 1.1 or later.
303    /// Relaxed layout is more permissive than the default rules in Vulkan 1.0.
304    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
305    pub relax_block_layout: Option<bool>,
306    /// Records whether the validator should use standard block layout rules for
307    /// uniform blocks.
308    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
309    pub uniform_buffer_standard_layout: bool,
310    /// Records whether the validator should use "scalar" block layout rules.
311    /// Scalar layout rules are more permissive than relaxed block layout.
312    ///
313    /// See Vulkan extnesion `VK_EXT_scalar_block_layout`.  The scalar alignment is
314    /// defined as follows:
315    /// - scalar alignment of a scalar is the scalar size
316    /// - scalar alignment of a vector is the scalar alignment of its component
317    /// - scalar alignment of a matrix is the scalar alignment of its component
318    /// - scalar alignment of an array is the scalar alignment of its element
319    /// - scalar alignment of a struct is the max scalar alignment among its
320    ///   members
321    ///
322    /// For a struct in Uniform, `StorageClass`, or `PushConstant`:
323    /// - a member Offset must be a multiple of the member's scalar alignment
324    /// - `ArrayStride` or `MatrixStride` must be a multiple of the array or matrix
325    ///   scalar alignment
326    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
327    pub scalar_block_layout: bool,
328    /// Records whether or not the validator should skip validating standard
329    /// uniform/storage block layout.
330    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
331    pub skip_block_layout: bool,
332    // /// Applies a maximum to one or more Universal limits
333    // pub max_limits: Vec<(ValidatorLimits, u32)>,
334}
335
336/// Options for specifying the behavior of the optimizer
337/// Copied from `spirv-tools/src/opt.rs` struct `Options`, with some fields disabled.
338#[derive(Default, Debug, Clone, serde::Deserialize, serde::Serialize)]
339#[cfg_attr(feature = "clap", derive(clap::Parser))]
340#[non_exhaustive]
341pub struct OptimizerOptions {
342    // /// Records the validator options that should be passed to the validator,
343    // /// the validator will run with the options before optimizer.
344    // pub validator_options: Option<crate::val::ValidatorOptions>,
345    // /// Records the maximum possible value for the id bound.
346    // pub max_id_bound: Option<u32>,
347    /// Records whether all bindings within the module should be preserved.
348    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
349    pub preserve_bindings: bool,
350    // /// Records whether all specialization constants within the module
351    // /// should be preserved.
352    // pub preserve_spec_constants: bool,
353}
354
355/// Cargo features specification for building the shader crate.
356#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
357#[cfg_attr(feature = "clap", derive(clap::Parser))]
358#[non_exhaustive]
359pub struct ShaderCrateFeatures {
360    /// Set --default-features for the target shader crate.
361    #[cfg_attr(feature = "clap", clap(long = "no-default-features", default_value = "true", action = clap::ArgAction::SetFalse))]
362    pub default_features: bool,
363    /// Set --features for the target shader crate.
364    #[cfg_attr(feature = "clap", clap(long))]
365    pub features: Vec<String>,
366}
367
368impl Default for ShaderCrateFeatures {
369    fn default() -> Self {
370        Self {
371            default_features: true,
372            features: Vec::new(),
373        }
374    }
375}
376
377/// Configuration for build scripts
378#[derive(Clone, Debug, Default)]
379#[non_exhaustive]
380pub struct BuildScriptConfig {
381    /// Enable this if you are using `spirv-builder` from a build script to apply some recommended default options, such
382    /// as [`Self::dependency_info`], [`Self::forward_rustc_warnings`] and [`Self::cargo_color_always`].
383    pub defaults: bool,
384
385    /// Print dependency information for cargo build scripts (with `cargo::rerun-if-changed={}` and such).
386    /// Dependency information makes cargo rerun the build script is rerun when shader source files change, thus
387    /// rebuilding the shader.
388    ///
389    /// Default: [`Self::defaults`]
390    pub dependency_info: Option<bool>,
391
392    /// Whether to emit an env var pointing to the shader module file  (via `cargo::rustc-env={}`). The name of the env
393    /// var is the crate name with `.spv` appended, e.g. `sky_shader.spv`.
394    /// Not supported together with `multimodule=true`.
395    ///
396    /// Some examples on how to include the shader module in the source code:
397    /// * wgpu:
398    /// ```rust,ignore
399    /// let shader: ShaderModuleDescriptorPassthrough = include_spirv_raw!(env!("my_shader.spv"));
400    /// ```
401    /// * ash
402    /// ```rust,ignore
403    /// let bytes: &[u8] = include_bytes!(env!("my_shader.spv"))
404    /// let words = ash::util::read_spv(&mut std::io::Cursor::new(bytes)).unwrap();
405    /// ```
406    ///
407    /// Default: `false`
408    pub env_shader_spv_path: Option<bool>,
409
410    /// Forwards any warnings or errors by rustc as build script warnings (via `cargo::warning=`). Not enabling this
411    /// option may hide warnings if the build succeeds.
412    ///
413    /// Default: [`Self::defaults`]
414    pub forward_rustc_warnings: Option<bool>,
415
416    /// Pass `--color always` to cargo to force enable colorful error messages. Particularly in build scripts, these
417    /// are disabled by default, even though we'll forward them to your console. Should your console not support colors,
418    /// then the outer cargo executing the build script will filter out all ansi escape sequences anyway, so we're free
419    /// to always emit them.
420    ///
421    /// Default: [`Self::defaults`]
422    pub cargo_color_always: Option<bool>,
423}
424
425/// these all have the prefix `get` so the doc items link to the members, not these private fns
426impl BuildScriptConfig {
427    fn get_dependency_info(&self) -> bool {
428        self.dependency_info.unwrap_or(self.defaults)
429    }
430    fn get_env_shader_spv_path(&self) -> bool {
431        self.env_shader_spv_path.unwrap_or(false)
432    }
433    fn get_forward_rustc_warnings(&self) -> bool {
434        self.forward_rustc_warnings.unwrap_or(self.defaults)
435    }
436    fn get_cargo_color_always(&self) -> bool {
437        self.cargo_color_always.unwrap_or(self.defaults)
438    }
439}
440
441#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
442#[cfg_attr(feature = "clap", derive(clap::Parser))]
443#[non_exhaustive]
444pub struct SpirvBuilder {
445    /// The path to the shader crate to compile
446    #[cfg_attr(feature = "clap", clap(skip))]
447    pub path_to_crate: Option<PathBuf>,
448    /// The cargo command to run, formatted like `cargo {cargo_cmd} ...`. Defaults to `rustc`.
449    #[cfg_attr(feature = "clap", clap(skip))]
450    pub cargo_cmd: Option<String>,
451    /// Whether the cargo command set in `cargo_cmd` behaves like `cargo rustc` and allows passing args such as
452    /// `--crate-type dylib`. Defaults to true if `cargo_cmd` is `None` or `Some("rustc")`.
453    #[cfg_attr(feature = "clap", clap(skip))]
454    pub cargo_cmd_like_rustc: Option<bool>,
455    /// Configuration for build scripts
456    #[cfg_attr(feature = "clap", clap(skip))]
457    #[serde(skip)]
458    pub build_script: BuildScriptConfig,
459    /// Build in release. Defaults to true.
460    #[cfg_attr(feature = "clap", clap(long = "debug", default_value = "true", action = clap::ArgAction::SetFalse))]
461    pub release: bool,
462    /// The target triple, eg. `spirv-unknown-vulkan1.2`
463    #[cfg_attr(
464        feature = "clap",
465        clap(long, default_value = "spirv-unknown-vulkan1.2")
466    )]
467    pub target: Option<String>,
468    /// Cargo features specification for building the shader crate.
469    #[cfg_attr(feature = "clap", clap(flatten))]
470    #[serde(flatten)]
471    pub shader_crate_features: ShaderCrateFeatures,
472    /// Deny any warnings, as they may never be printed when building within a build script. Defaults to false.
473    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
474    pub deny_warnings: bool,
475    /// Splits the resulting SPIR-V file into one module per entry point. This is useful in cases
476    /// where ecosystem tooling has bugs around multiple entry points per module - having all entry
477    /// points bundled into a single file is the preferred system.
478    #[cfg_attr(feature = "clap", arg(long, default_value = "false"))]
479    pub multimodule: bool,
480    /// Sets the level of metadata (primarily `OpName` and `OpLine`) included in the SPIR-V binary.
481    /// Including metadata significantly increases binary size.
482    #[cfg_attr(feature = "clap", arg(long, default_value = "none"))]
483    pub spirv_metadata: SpirvMetadata,
484    /// Adds a capability to the SPIR-V module. Checking if a capability is enabled in code can be
485    /// done via `#[cfg(target_feature = "TheCapability")]`.
486    #[cfg_attr(feature = "clap", arg(long, value_parser=Self::parse_spirv_capability))]
487    pub capabilities: Vec<Capability>,
488    /// Adds an extension to the SPIR-V module. Checking if an extension is enabled in code can be
489    /// done via `#[cfg(target_feature = "ext:the_extension")]`.
490    #[cfg_attr(feature = "clap", arg(long))]
491    pub extensions: Vec<String>,
492    /// Set additional "codegen arg". Note: the `RUSTGPU_CODEGEN_ARGS` environment variable
493    /// takes precedence over any set arguments using this function.
494    #[cfg_attr(feature = "clap", clap(skip))]
495    pub extra_args: Vec<String>,
496    // Location of a known `rustc_codegen_spirv` dylib, only required without feature `rustc_codegen_spirv`.
497    #[cfg_attr(feature = "clap", clap(skip))]
498    pub rustc_codegen_spirv_location: Option<PathBuf>,
499    // Overwrite the toolchain like `cargo +nightly`
500    #[cfg_attr(feature = "clap", clap(skip))]
501    pub toolchain_overwrite: Option<String>,
502    // Set the rustc version of the toolchain, used to adjust params to support older toolchains
503    #[cfg_attr(feature = "clap", clap(skip))]
504    pub toolchain_rustc_version: Option<Version>,
505
506    /// Set the target dir path to use for building shaders. Relative paths will be resolved
507    /// relative to the `target` dir of the shader crate, absolute paths are used as is.
508    /// Defaults to `spirv-builder`, resulting in the path `./target/spirv-builder`.
509    #[cfg_attr(feature = "clap", clap(skip))]
510    pub target_dir_path: Option<PathBuf>,
511
512    // `rustc_codegen_spirv::linker` codegen args
513    /// Change the shader `panic!` handling strategy (see [`ShaderPanicStrategy`]).
514    #[cfg_attr(feature = "clap", clap(skip))]
515    pub shader_panic_strategy: ShaderPanicStrategy,
516
517    /// spirv-val flags
518    #[cfg_attr(feature = "clap", clap(flatten))]
519    #[serde(flatten)]
520    pub validator: ValidatorOptions,
521
522    /// spirv-opt flags
523    #[cfg_attr(feature = "clap", clap(flatten))]
524    #[serde(flatten)]
525    pub optimizer: OptimizerOptions,
526}
527
528#[cfg(feature = "clap")]
529impl SpirvBuilder {
530    /// Clap value parser for `Capability`.
531    fn parse_spirv_capability(capability: &str) -> Result<Capability, clap::Error> {
532        use core::str::FromStr;
533        Capability::from_str(capability).map_or_else(
534            |()| Err(clap::Error::new(clap::error::ErrorKind::InvalidValue)),
535            Ok,
536        )
537    }
538}
539
540impl Default for SpirvBuilder {
541    fn default() -> Self {
542        Self {
543            path_to_crate: None,
544            cargo_cmd: None,
545            cargo_cmd_like_rustc: None,
546            build_script: BuildScriptConfig::default(),
547            release: true,
548            target: None,
549            deny_warnings: false,
550            multimodule: false,
551            spirv_metadata: SpirvMetadata::default(),
552            capabilities: Vec::new(),
553            extensions: Vec::new(),
554            extra_args: Vec::new(),
555            rustc_codegen_spirv_location: None,
556            target_dir_path: None,
557            toolchain_overwrite: None,
558            toolchain_rustc_version: None,
559            shader_panic_strategy: ShaderPanicStrategy::default(),
560            validator: ValidatorOptions::default(),
561            optimizer: OptimizerOptions::default(),
562            shader_crate_features: ShaderCrateFeatures::default(),
563        }
564    }
565}
566
567impl SpirvBuilder {
568    pub fn new(path_to_crate: impl AsRef<Path>, target: impl Into<String>) -> Self {
569        Self {
570            path_to_crate: Some(path_to_crate.as_ref().to_owned()),
571            target: Some(target.into()),
572            ..SpirvBuilder::default()
573        }
574    }
575
576    #[must_use]
577    pub fn deny_warnings(mut self, v: bool) -> Self {
578        self.deny_warnings = v;
579        self
580    }
581
582    /// Build in release. Defaults to true.
583    #[must_use]
584    pub fn release(mut self, v: bool) -> Self {
585        self.release = v;
586        self
587    }
588
589    /// Splits the resulting SPIR-V file into one module per entry point. This is useful in cases
590    /// where ecosystem tooling has bugs around multiple entry points per module - having all entry
591    /// points bundled into a single file is the preferred system.
592    #[must_use]
593    pub fn multimodule(mut self, v: bool) -> Self {
594        self.multimodule = v;
595        self
596    }
597
598    /// Sets the level of metadata (primarily `OpName` and `OpLine`) included in the SPIR-V binary.
599    /// Including metadata significantly increases binary size.
600    #[must_use]
601    pub fn spirv_metadata(mut self, v: SpirvMetadata) -> Self {
602        self.spirv_metadata = v;
603        self
604    }
605
606    /// Adds a capability to the SPIR-V module. Checking if a capability is enabled in code can be
607    /// done via `#[cfg(target_feature = "TheCapability")]`.
608    #[must_use]
609    pub fn capability(mut self, capability: Capability) -> Self {
610        self.capabilities.push(capability);
611        self
612    }
613
614    /// Adds an extension to the SPIR-V module. Checking if an extension is enabled in code can be
615    /// done via `#[cfg(target_feature = "ext:the_extension")]`.
616    #[must_use]
617    pub fn extension(mut self, extension: impl Into<String>) -> Self {
618        self.extensions.push(extension.into());
619        self
620    }
621
622    /// Change the shader `panic!` handling strategy (see [`ShaderPanicStrategy`]).
623    #[must_use]
624    pub fn shader_panic_strategy(mut self, shader_panic_strategy: ShaderPanicStrategy) -> Self {
625        self.shader_panic_strategy = shader_panic_strategy;
626        self
627    }
628
629    /// Allow store from one struct type to a different type with compatible layout and members.
630    #[must_use]
631    pub fn relax_struct_store(mut self, v: bool) -> Self {
632        self.validator.relax_struct_store = v;
633        self
634    }
635
636    /// Allow allocating an object of a pointer type and returning a pointer value from a function
637    /// in logical addressing mode
638    #[must_use]
639    pub fn relax_logical_pointer(mut self, v: bool) -> Self {
640        self.validator.relax_logical_pointer = v;
641        self
642    }
643
644    /// Enable `VK_KHR_relaxed_block_layout` when checking standard uniform, storage buffer, and
645    /// push constant layouts. This is the default when targeting Vulkan 1.1 or later.
646    #[must_use]
647    pub fn relax_block_layout(mut self, v: bool) -> Self {
648        self.validator.relax_block_layout = Some(v);
649        self
650    }
651
652    /// Enable `VK_KHR_uniform_buffer_standard_layout` when checking standard uniform buffer
653    /// layouts.
654    #[must_use]
655    pub fn uniform_buffer_standard_layout(mut self, v: bool) -> Self {
656        self.validator.uniform_buffer_standard_layout = v;
657        self
658    }
659
660    /// Enable `VK_EXT_scalar_block_layout` when checking standard uniform, storage buffer, and
661    /// push constant layouts. Scalar layout rules are more permissive than relaxed block layout so
662    /// in effect this will override the --relax-block-layout option.
663    #[must_use]
664    pub fn scalar_block_layout(mut self, v: bool) -> Self {
665        self.validator.scalar_block_layout = v;
666        self
667    }
668
669    /// Skip checking standard uniform/storage buffer layout. Overrides any --relax-block-layout or
670    /// --scalar-block-layout option.
671    #[must_use]
672    pub fn skip_block_layout(mut self, v: bool) -> Self {
673        self.validator.skip_block_layout = v;
674        self
675    }
676
677    /// Preserve unused descriptor bindings. Useful for reflection.
678    #[must_use]
679    pub fn preserve_bindings(mut self, v: bool) -> Self {
680        self.optimizer.preserve_bindings = v;
681        self
682    }
683
684    /// Set additional "codegen arg". Note: the `RUSTGPU_CODEGEN_ARGS` environment variable
685    /// takes precedence over any set arguments using this function.
686    #[must_use]
687    pub fn extra_arg(mut self, arg: impl Into<String>) -> Self {
688        self.extra_args.push(arg.into());
689        self
690    }
691
692    /// Set --default-features for the target shader crate.
693    #[must_use]
694    pub fn shader_crate_default_features(mut self, default_features: bool) -> Self {
695        self.shader_crate_features.default_features = default_features;
696        self
697    }
698
699    /// Set --features for the target shader crate.
700    #[must_use]
701    pub fn shader_crate_features(mut self, features: impl IntoIterator<Item = String>) -> Self {
702        self.shader_crate_features.features = features.into_iter().collect();
703        self
704    }
705
706    #[must_use]
707    pub fn rustc_codegen_spirv_location(mut self, path_to_dylib: impl AsRef<Path>) -> Self {
708        self.rustc_codegen_spirv_location = Some(path_to_dylib.as_ref().to_path_buf());
709        self
710    }
711
712    /// Set the target dir path to use for building shaders. Relative paths will be resolved
713    /// relative to the `target` dir of the shader crate, absolute paths are used as is.
714    /// Defaults to `spirv-builder`, resulting in the path `./target/spirv-builder`.
715    #[must_use]
716    pub fn target_dir_path(mut self, name: impl Into<PathBuf>) -> Self {
717        self.target_dir_path = Some(name.into());
718        self
719    }
720
721    /// Shortcut for `cargo check`
722    pub fn check(&mut self) -> Result<CompileResult, SpirvBuilderError> {
723        self.run_cargo_cmd("check")
724    }
725
726    /// Shortcut for `cargo clippy`
727    pub fn clippy(&mut self) -> Result<CompileResult, SpirvBuilderError> {
728        self.run_cargo_cmd("clippy")
729    }
730
731    /// Run the supplied cargo cmd, and ensure to reset the state so [`Self::build`] still works as normal
732    fn run_cargo_cmd(&mut self, cmd: &str) -> Result<CompileResult, SpirvBuilderError> {
733        let old = self.cargo_cmd.replace(cmd.into());
734        let result = self.build();
735        self.cargo_cmd = old;
736        result
737    }
738
739    /// Builds the module
740    pub fn build(&self) -> Result<CompileResult, SpirvBuilderError> {
741        let metadata_file = invoke_rustc(self)?;
742        if self.build_script.get_dependency_info() {
743            leaf_deps(&metadata_file, |artifact| {
744                println!("cargo:rerun-if-changed={artifact}");
745            })
746            // Close enough
747            .map_err(SpirvBuilderError::MetadataFileMissing)?;
748        }
749        let metadata = self.parse_metadata_file(&metadata_file)?;
750
751        Ok(metadata)
752    }
753
754    pub(crate) fn parse_metadata_file(
755        &self,
756        at: &Path,
757    ) -> Result<CompileResult, SpirvBuilderError> {
758        let metadata_contents = File::open(at).map_err(SpirvBuilderError::MetadataFileMissing)?;
759        // FIXME(eddyb) move this functionality into `rustc_codegen_spirv_types`.
760        let metadata: CompileResult =
761            rustc_codegen_spirv_types::serde_json::from_reader(BufReader::new(metadata_contents))
762                .map_err(SpirvBuilderError::MetadataFileMalformed)?;
763        match &metadata.module {
764            ModuleResult::SingleModule(spirv_module) => {
765                assert!(!self.multimodule);
766                if self.build_script.get_env_shader_spv_path() {
767                    let env_var = format!(
768                        "{}.spv",
769                        at.file_name()
770                            .unwrap()
771                            .to_str()
772                            .unwrap()
773                            .strip_suffix(ARTIFACT_SUFFIX)
774                            .unwrap()
775                    );
776                    println!("cargo::rustc-env={}={}", env_var, spirv_module.display());
777                }
778            }
779            ModuleResult::MultiModule(_) => {
780                assert!(self.multimodule);
781            }
782        }
783        Ok(metadata)
784    }
785}
786
787// https://github.com/rust-lang/cargo/blob/1857880b5124580c4aeb4e8bc5f1198f491d61b1/src/cargo/util/paths.rs#L29-L52
788fn dylib_path_envvar() -> &'static str {
789    if cfg!(windows) {
790        "PATH"
791    } else if cfg!(target_os = "macos") {
792        "DYLD_FALLBACK_LIBRARY_PATH"
793    } else {
794        "LD_LIBRARY_PATH"
795    }
796}
797fn dylib_path() -> Vec<PathBuf> {
798    let mut dylibs = match env::var_os(dylib_path_envvar()) {
799        Some(var) => env::split_paths(&var).collect(),
800        None => Vec::new(),
801    };
802    if let Ok(dir) = env::current_dir() {
803        dylibs.push(dir);
804    }
805    dylibs
806}
807
808fn find_rustc_codegen_spirv() -> Result<PathBuf, SpirvBuilderError> {
809    if cfg!(feature = "rustc_codegen_spirv") {
810        let filename = format!(
811            "{}rustc_codegen_spirv{}",
812            env::consts::DLL_PREFIX,
813            env::consts::DLL_SUFFIX
814        );
815        let dylib_paths = dylib_path();
816        for mut path in dylib_paths {
817            path.push(&filename);
818            if path.is_file() {
819                return Ok(path);
820            }
821        }
822        panic!("Could not find {filename} in library path");
823    } else {
824        Err(SpirvBuilderError::MissingRustcCodegenSpirvDylib)
825    }
826}
827
828/// Joins strings together while ensuring none of the strings contain the separator.
829// NOTE(eddyb) this intentionally consumes the `Vec` to limit accidental misuse.
830fn join_checking_for_separators(strings: Vec<impl Borrow<str>>, sep: &str) -> String {
831    for s in &strings {
832        let s = s.borrow();
833        assert!(!s.contains(sep), "{s:?} may not contain separator {sep:?}");
834    }
835    strings.join(sep)
836}
837
838// Returns path to the metadata json.
839fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
840    let path_to_crate = builder
841        .path_to_crate
842        .as_ref()
843        .ok_or(SpirvBuilderError::MissingCratePath)?;
844    let target;
845    {
846        let target_str = builder
847            .target
848            .as_ref()
849            .ok_or(SpirvBuilderError::MissingTarget)?;
850        target = SpirvTarget::parse(target_str)?;
851
852        if builder.build_script.get_env_shader_spv_path() && builder.multimodule {
853            return Err(SpirvBuilderError::MultiModuleWithEnvShaderSpvPath);
854        }
855        if !path_to_crate.is_dir() {
856            return Err(SpirvBuilderError::CratePathDoesntExist(
857                path_to_crate.clone(),
858            ));
859        }
860    }
861
862    let toolchain_rustc_version =
863        if let Some(toolchain_rustc_version) = &builder.toolchain_rustc_version {
864            toolchain_rustc_version.clone()
865        } else {
866            query_rustc_version(builder.toolchain_overwrite.as_deref())?
867        };
868
869    // Okay, this is a little bonkers: in a normal world, we'd have the user clone
870    // rustc_codegen_spirv and pass in the path to it, and then we'd invoke cargo to build it, grab
871    // the resulting .so, and pass it into -Z codegen-backend. But that's really gross: the user
872    // needs to clone rustc_codegen_spirv and tell us its path! So instead, we *directly reference
873    // rustc_codegen_spirv in spirv-builder's Cargo.toml*, which means that it will get built
874    // alongside build.rs, and cargo will helpfully add it to LD_LIBRARY_PATH for us! However,
875    // rustc expects a full path, instead of a filename looked up via LD_LIBRARY_PATH, so we need
876    // to copy cargo's understanding of library lookup and find the library and its full path.
877    let rustc_codegen_spirv = Ok(builder.rustc_codegen_spirv_location.clone())
878        .transpose()
879        .unwrap_or_else(find_rustc_codegen_spirv)?;
880    if !rustc_codegen_spirv.is_file() {
881        return Err(SpirvBuilderError::RustcCodegenSpirvDylibDoesNotExist(
882            rustc_codegen_spirv,
883        ));
884    }
885
886    let mut rustflags = vec![
887        format!("-Zcodegen-backend={}", rustc_codegen_spirv.display()),
888        // Ensure the codegen backend is emitted in `.d` files to force Cargo
889        // to rebuild crates compiled with it when it changes (this used to be
890        // the default until https://github.com/rust-lang/rust/pull/93969).
891        "-Zbinary-dep-depinfo".to_string(),
892        "-Csymbol-mangling-version=v0".to_string(),
893        "-Zcrate-attr=feature(register_tool)".to_string(),
894        "-Zcrate-attr=register_tool(rust_gpu)".to_string(),
895        // HACK(eddyb) this is the same configuration that we test with, and
896        // ensures no unwanted surprises from e.g. `core` debug assertions.
897        "-Coverflow-checks=off".to_string(),
898        "-Cdebug-assertions=off".to_string(),
899        // HACK(eddyb) we need this for `core::fmt::rt::Argument::new_*` calls
900        // to *never* be inlined, so we can pattern-match the calls themselves.
901        "-Zinline-mir=off".to_string(),
902        // HACK(eddyb) similar to turning MIR inlining off, we also can't allow
903        // optimizations that drastically impact (the quality of) codegen, and
904        // GVN currently can lead to the memcpy-out-of-const-alloc-global-var
905        // pattern, even for `ScalarPair` (e.g. `return None::<u32>;`).
906        "-Zmir-enable-passes=-GVN".to_string(),
907        // HACK(eddyb) avoid ever reusing instantiations from `compiler_builtins`
908        // which is special-cased to turn calls to functions that never return,
909        // into aborts, and this applies to the panics of UB-checking helpers
910        // (https://github.com/rust-lang/rust/pull/122580#issuecomment-3033026194)
911        // but while upstream that only loses the panic message, for us it's even
912        // worse, as we lose the chance to remove otherwise-dead `fmt::Arguments`.
913        "-Zshare-generics=off".to_string(),
914    ];
915
916    // Wrapper for `env::var` that appropriately informs Cargo of the dependency.
917    let tracked_env_var_get = |name| {
918        if builder.build_script.get_dependency_info() {
919            println!("cargo:rerun-if-env-changed={name}");
920        }
921        env::var(name)
922    };
923
924    let mut llvm_args = vec![];
925    if builder.multimodule {
926        llvm_args.push("--module-output=multiple".to_string());
927    }
928    match builder.spirv_metadata {
929        SpirvMetadata::None => (),
930        SpirvMetadata::NameVariables => {
931            llvm_args.push("--spirv-metadata=name-variables".to_string());
932        }
933        SpirvMetadata::Full => llvm_args.push("--spirv-metadata=full".to_string()),
934    }
935    if builder.validator.relax_struct_store {
936        llvm_args.push("--relax-struct-store".to_string());
937    }
938    if builder.validator.relax_logical_pointer {
939        llvm_args.push("--relax-logical-pointer".to_string());
940    }
941    if builder.validator.relax_block_layout.unwrap_or(false) {
942        llvm_args.push("--relax-block-layout".to_string());
943    }
944    if builder.validator.uniform_buffer_standard_layout {
945        llvm_args.push("--uniform-buffer-standard-layout".to_string());
946    }
947    if builder.validator.scalar_block_layout {
948        llvm_args.push("--scalar-block-layout".to_string());
949    }
950    if builder.validator.skip_block_layout {
951        llvm_args.push("--skip-block-layout".to_string());
952    }
953    if builder.optimizer.preserve_bindings {
954        llvm_args.push("--preserve-bindings".to_string());
955    }
956    let mut target_features = vec![];
957    let abort_strategy = match builder.shader_panic_strategy {
958        ShaderPanicStrategy::SilentExit => None,
959        ShaderPanicStrategy::DebugPrintfThenExit {
960            print_inputs,
961            print_backtrace,
962        } => {
963            target_features.push("+ext:SPV_KHR_non_semantic_info".into());
964            Some(format!(
965                "debug-printf{}{}",
966                if print_inputs { "+inputs" } else { "" },
967                if print_backtrace { "+backtrace" } else { "" }
968            ))
969        }
970        ShaderPanicStrategy::UNSOUND_DO_NOT_USE_UndefinedBehaviorViaUnreachable => {
971            Some("unreachable".into())
972        }
973    };
974    llvm_args.extend(abort_strategy.map(|strategy| format!("--abort-strategy={strategy}")));
975
976    if let Ok(extra_codegen_args) = tracked_env_var_get("RUSTGPU_CODEGEN_ARGS") {
977        llvm_args.extend(extra_codegen_args.split_whitespace().map(|s| s.to_string()));
978    } else {
979        llvm_args.extend(builder.extra_args.iter().cloned());
980    }
981
982    let llvm_args = join_checking_for_separators(llvm_args, " ");
983    if !llvm_args.is_empty() {
984        rustflags.push(["-Cllvm-args=", &llvm_args].concat());
985    }
986
987    target_features.extend(builder.capabilities.iter().map(|cap| format!("+{cap:?}")));
988    target_features.extend(builder.extensions.iter().map(|ext| format!("+ext:{ext}")));
989    let target_features = join_checking_for_separators(target_features, ",");
990    if !target_features.is_empty() {
991        rustflags.push(["-Ctarget-feature=", &target_features].concat());
992    }
993
994    if builder.deny_warnings {
995        rustflags.push("-Dwarnings".to_string());
996    }
997
998    if let Ok(extra_rustflags) = tracked_env_var_get("RUSTGPU_RUSTFLAGS") {
999        rustflags.extend(extra_rustflags.split_whitespace().map(|s| s.to_string()));
1000    }
1001
1002    let target_dir_path = builder
1003        .target_dir_path
1004        .clone()
1005        .unwrap_or_else(|| PathBuf::from("spirv-builder"));
1006    let target_dir = if target_dir_path.is_absolute() {
1007        target_dir_path
1008    } else {
1009        let metadata = cargo_metadata::MetadataCommand::new()
1010            .current_dir(path_to_crate)
1011            .exec()?;
1012        metadata
1013            .target_directory
1014            .into_std_path_buf()
1015            .join(target_dir_path)
1016    };
1017
1018    let mut cargo = cargo_cmd::CargoCmd::new();
1019    if let Some(toolchain) = &builder.toolchain_overwrite {
1020        cargo.arg(format!("+{toolchain}"));
1021    }
1022
1023    let cargo_cmd = builder.cargo_cmd.as_ref().map_or("rustc", |s| s.as_str());
1024    let cargo_cmd_like_rustc = builder.cargo_cmd_like_rustc.unwrap_or(cargo_cmd == "rustc");
1025    let profile = if builder.release { "release" } else { "dev" };
1026    cargo.args([
1027        cargo_cmd,
1028        "--lib",
1029        "--message-format=json-render-diagnostics",
1030        "-Zbuild-std=core",
1031        "-Zbuild-std-features=compiler-builtins-mem",
1032        "--profile",
1033        profile,
1034    ]);
1035    if cargo_cmd_like_rustc {
1036        // About `crate-type`: We use it to determine whether the crate needs to be linked into shaders. For `rlib`,
1037        // we're emitting regular rust libraries as is expected. For `dylib` or `cdylib`, we're linking all `rlib`s
1038        // together, legalize them in many passes and emit a final `*.spv` file. Quirk: If you depend on a crate
1039        // that has crate-type `dylib`, we also link it, and it will fail if it has no shaders, which may not be
1040        // desired. (Gathered from reading source code and experimenting, @firestar99)
1041        cargo.args(["--crate-type", "dylib"]);
1042    }
1043
1044    if let Ok(extra_cargoflags) = tracked_env_var_get("RUSTGPU_CARGOFLAGS") {
1045        cargo.args(extra_cargoflags.split_whitespace());
1046    }
1047
1048    let target_spec_dir = target_dir.join("target-specs");
1049    let target = TargetSpecVersion::target_arg(toolchain_rustc_version, &target, &target_spec_dir)?;
1050    cargo.arg("--target").arg(target);
1051
1052    if !builder.shader_crate_features.default_features {
1053        cargo.arg("--no-default-features");
1054    }
1055
1056    if !builder.shader_crate_features.features.is_empty() {
1057        cargo
1058            .arg("--features")
1059            .arg(builder.shader_crate_features.features.join(","));
1060    }
1061
1062    cargo.arg("--target-dir").arg(target_dir);
1063
1064    // Args for warning and error forwarding
1065    if builder.build_script.get_forward_rustc_warnings() {
1066        // Quiet to remove all the status messages and only emit errors and warnings
1067        cargo.args(["--quiet"]);
1068    }
1069    if builder.build_script.get_cargo_color_always() {
1070        // Always emit color, since the outer cargo will remove ascii escape sequences if color is turned off
1071        cargo.args(["--color", "always"]);
1072    }
1073
1074    // NOTE(eddyb) this used to be just `RUSTFLAGS` but at some point Cargo
1075    // added a separate environment variable using `\x1f` instead of spaces,
1076    // which allows us to have spaces within individual `rustc` flags.
1077    cargo.env(
1078        "CARGO_ENCODED_RUSTFLAGS",
1079        join_checking_for_separators(rustflags, "\x1f"),
1080    );
1081
1082    // NOTE(eddyb) there's no parallelism to take advantage of multiple CGUs,
1083    // and inter-CGU duplication can be wasteful, so this forces 1 CGU for now.
1084    let profile_in_env_var = profile.replace('-', "_").to_ascii_uppercase();
1085    let num_cgus = 1;
1086    cargo.env(
1087        format!("CARGO_PROFILE_{profile_in_env_var}_CODEGEN_UNITS"),
1088        num_cgus.to_string(),
1089    );
1090
1091    if !builder.build_script.get_forward_rustc_warnings() {
1092        cargo.stderr(Stdio::inherit());
1093    }
1094    cargo.current_dir(path_to_crate);
1095    log::debug!("building shaders with `{cargo:?}`");
1096    let build = cargo.output().expect("failed to execute cargo build");
1097
1098    if builder.build_script.get_forward_rustc_warnings() {
1099        let stderr = String::from_utf8_lossy(&build.stderr);
1100        for line in stderr.lines() {
1101            println!("cargo::warning={line}");
1102        }
1103    }
1104
1105    // `get_last_artifact` has the side-effect of printing invalid lines, so
1106    // we do that even in case of an error, to let through any useful messages
1107    // that ended up on stdout instead of stderr.
1108    let stdout = String::from_utf8(build.stdout).unwrap();
1109    if build.status.success() {
1110        get_sole_artifact(&stdout).ok_or(SpirvBuilderError::NoArtifactProduced { stdout })
1111    } else {
1112        Err(SpirvBuilderError::BuildFailed)
1113    }
1114}
1115
1116#[derive(Deserialize)]
1117struct RustcOutput {
1118    reason: String,
1119    filenames: Option<Vec<String>>,
1120}
1121
1122const ARTIFACT_SUFFIX: &str = ".spv.json";
1123
1124fn get_sole_artifact(out: &str) -> Option<PathBuf> {
1125    let mut last_compiler_artifact = None;
1126    for line in out.lines() {
1127        let Ok(msg) = serde_json::from_str::<RustcOutput>(line) else {
1128            // Pass through invalid lines
1129            println!("{line}");
1130            continue;
1131        };
1132        if msg.reason == "compiler-artifact" {
1133            last_compiler_artifact = Some(msg);
1134        }
1135    }
1136    let last_compiler_artifact =
1137        last_compiler_artifact.expect("Did not find output file in rustc output");
1138
1139    let mut filenames = last_compiler_artifact
1140        .filenames
1141        .unwrap()
1142        .into_iter()
1143        .filter(|v| v.ends_with(ARTIFACT_SUFFIX));
1144    let filename = filenames.next()?;
1145    assert_eq!(
1146        filenames.next(),
1147        None,
1148        "build had multiple `{ARTIFACT_SUFFIX}` artifacts"
1149    );
1150    Some(filename.into())
1151}
1152
1153/// Internally iterate through the leaf dependencies of the artifact at `artifact`
1154fn leaf_deps(artifact: &Path, mut handle: impl FnMut(&RawStr)) -> std::io::Result<()> {
1155    let deps_file = artifact.with_extension("d");
1156    let mut deps_map = HashMap::new();
1157    depfile::read_deps_file(&deps_file, |item, deps| {
1158        deps_map.insert(item, deps);
1159        Ok(())
1160    })?;
1161    fn recurse(
1162        map: &HashMap<RawString, Vec<RawString>>,
1163        artifact: &RawStr,
1164        handle: &mut impl FnMut(&RawStr),
1165    ) {
1166        match map.get(artifact) {
1167            Some(entries) => {
1168                for entry in entries {
1169                    recurse(map, entry, handle);
1170                }
1171            }
1172            None => handle(artifact),
1173        }
1174    }
1175    recurse(&deps_map, artifact.to_str().unwrap().into(), &mut handle);
1176    Ok(())
1177}