feat: allow shader crate cargo features to be passed through spirv-builder

This commit is contained in:
Schell Carl Scivally 2024-02-01 11:49:01 +13:00
parent 1a9c8b8ba1
commit e6cb118000

View File

@ -255,11 +255,19 @@ pub enum ShaderPanicStrategy {
UNSOUND_DO_NOT_USE_UndefinedBehaviorViaUnreachable,
}
/// Cargo features specification for building the shader crate.
#[derive(Default)]
struct ShaderCrateFeatures {
default_features: Option<bool>,
features: Vec<String>,
}
pub struct SpirvBuilder {
path_to_crate: PathBuf,
print_metadata: MetadataPrintout,
release: bool,
target: String,
shader_crate_features: ShaderCrateFeatures,
deny_warnings: bool,
multimodule: bool,
spirv_metadata: SpirvMetadata,
@ -306,6 +314,7 @@ impl SpirvBuilder {
skip_block_layout: false,
preserve_bindings: false,
shader_crate_features: ShaderCrateFeatures::default(),
}
}
@ -432,6 +441,20 @@ impl SpirvBuilder {
self
}
/// Set --default-features for the target shader crate.
#[must_use]
pub fn shader_crate_default_features(mut self, default_features: bool) -> Self {
self.shader_crate_features.default_features = Some(default_features);
self
}
/// Set --features for the target shader crate.
#[must_use]
pub fn shader_crate_features(mut self, features: impl IntoIterator<Item = String>) -> Self {
self.shader_crate_features.features = features.into_iter().collect();
self
}
/// Builds the module. If `print_metadata` is [`MetadataPrintout::Full`], you usually don't have to inspect the path
/// in the result, as the environment variable for the path to the module will already be set.
pub fn build(mut self) -> Result<CompileResult, SpirvBuilderError> {
@ -690,6 +713,18 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
&*builder.target,
]);
if let Some(default_features) = builder.shader_crate_features.default_features {
if !default_features {
cargo.arg("--no-default-features");
}
}
if !builder.shader_crate_features.features.is_empty() {
cargo
.arg("--features")
.arg(builder.shader_crate_features.features.join(","));
}
// NOTE(eddyb) see above how this is computed and why it might be missing.
if let Some(target_dir) = target_dir {
cargo.arg("--target-dir").arg(target_dir);