Skip to content

Commit d80832e

Browse files
committed
target enum: change SpirvBuilder.target from String to SpirvTargetEnv
1 parent bb65113 commit d80832e

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

crates/rustc_codegen_spirv-target-specs/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ impl SpirvTargetEnv {
109109
}
110110
}
111111

112+
pub trait IntoSpirvTarget: Sized {
113+
fn to_spirv_target_env(&self) -> Result<SpirvTargetEnv, SpirvTargetParseError>;
114+
}
115+
116+
impl IntoSpirvTarget for SpirvTargetEnv {
117+
fn to_spirv_target_env(&self) -> Result<SpirvTargetEnv, SpirvTargetParseError> {
118+
Ok(*self)
119+
}
120+
}
121+
122+
impl IntoSpirvTarget for &str {
123+
fn to_spirv_target_env(&self) -> Result<SpirvTargetEnv, SpirvTargetParseError> {
124+
SpirvTargetEnv::parse_triple(self)
125+
}
126+
}
127+
112128
#[cfg(test)]
113129
mod tests {
114130
use super::*;

crates/spirv-builder/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ clap = ["dep:clap"]
3838
[dependencies]
3939
rustc_codegen_spirv = { workspace = true, optional = true }
4040
rustc_codegen_spirv-types = { workspace = true }
41-
rustc_codegen_spirv-target-specs = { workspace = true }
41+
rustc_codegen_spirv-target-specs = { workspace = true, features = ["serde"] }
4242

4343
memchr = "2.4"
4444
raw-string = "0.3.5"

crates/spirv-builder/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ use std::path::{Path, PathBuf};
9090
use std::process::{Command, Stdio};
9191
use thiserror::Error;
9292

93-
pub use rustc_codegen_spirv_target_specs::{SpirvTargetEnv, SpirvTargetParseError};
93+
pub use rustc_codegen_spirv_target_specs::{
94+
IntoSpirvTarget, SpirvTargetEnv, SpirvTargetParseError,
95+
};
9496
pub use rustc_codegen_spirv_types::*;
9597

9698
#[cfg(feature = "include-target-specs")]
@@ -99,10 +101,8 @@ pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
99101
#[derive(Debug, Error)]
100102
#[non_exhaustive]
101103
pub enum SpirvBuilderError {
102-
#[error("`target` must be set, for example `spirv-unknown-vulkan1.2`")]
104+
#[error("`target` must be set or was invalid, for example `spirv-unknown-vulkan1.2`")]
103105
MissingTarget,
104-
#[error("Error parsing target: {0}")]
105-
SpirvTargetParseError(#[from] SpirvTargetParseError),
106106
#[error("`path_to_crate` must be set")]
107107
MissingCratePath,
108108
#[error("crate path '{0}' does not exist")]
@@ -380,9 +380,13 @@ pub struct SpirvBuilder {
380380
/// The target triple, eg. `spirv-unknown-vulkan1.2`
381381
#[cfg_attr(
382382
feature = "clap",
383-
clap(long, default_value = "spirv-unknown-vulkan1.2")
383+
clap(
384+
long,
385+
default_value = "spirv-unknown-vulkan1.2",
386+
value_parser = SpirvTargetEnv::parse_triple
387+
)
384388
)]
385-
pub target: Option<String>,
389+
pub target: Option<SpirvTargetEnv>,
386390
/// Cargo features specification for building the shader crate.
387391
#[cfg_attr(feature = "clap", clap(flatten))]
388392
#[serde(flatten)]
@@ -488,10 +492,10 @@ impl Default for SpirvBuilder {
488492
}
489493

490494
impl SpirvBuilder {
491-
pub fn new(path_to_crate: impl AsRef<Path>, target: impl Into<String>) -> Self {
495+
pub fn new(path_to_crate: impl AsRef<Path>, target: impl IntoSpirvTarget) -> Self {
492496
Self {
493497
path_to_crate: Some(path_to_crate.as_ref().to_owned()),
494-
target: Some(target.into()),
498+
target: target.to_spirv_target_env().ok(),
495499
..SpirvBuilder::default()
496500
}
497501
}
@@ -758,11 +762,7 @@ fn join_checking_for_separators(strings: Vec<impl Borrow<str>>, sep: &str) -> St
758762

759763
// Returns path to the metadata json.
760764
fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
761-
let target = builder
762-
.target
763-
.as_ref()
764-
.ok_or(SpirvBuilderError::MissingTarget)?;
765-
let target = SpirvTargetEnv::parse_triple(target)?;
765+
let target = builder.target.ok_or(SpirvBuilderError::MissingTarget)?;
766766
let path_to_crate = builder
767767
.path_to_crate
768768
.as_ref()

tests/difftests/tests/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)