Skip to content

Commit cbd1dcf

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

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
@@ -89,7 +89,9 @@ use std::path::{Path, PathBuf};
8989
use std::process::{Command, Stdio};
9090
use thiserror::Error;
9191

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

9597
#[cfg(feature = "include-target-specs")]
@@ -98,10 +100,8 @@ pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
98100
#[derive(Debug, Error)]
99101
#[non_exhaustive]
100102
pub enum SpirvBuilderError {
101-
#[error("`target` must be set, for example `spirv-unknown-vulkan1.2`")]
103+
#[error("`target` must be set or was invalid, for example `spirv-unknown-vulkan1.2`")]
102104
MissingTarget,
103-
#[error("Error parsing target: {0}")]
104-
SpirvTargetParseError(#[from] SpirvTargetParseError),
105105
#[error("`path_to_crate` must be set")]
106106
MissingCratePath,
107107
#[error("crate path '{0}' does not exist")]
@@ -379,9 +379,13 @@ pub struct SpirvBuilder {
379379
/// The target triple, eg. `spirv-unknown-vulkan1.2`
380380
#[cfg_attr(
381381
feature = "clap",
382-
clap(long, default_value = "spirv-unknown-vulkan1.2")
382+
clap(
383+
long,
384+
default_value = "spirv-unknown-vulkan1.2",
385+
value_parser = SpirvTargetEnv::parse_triple
386+
)
383387
)]
384-
pub target: Option<String>,
388+
pub target: Option<SpirvTargetEnv>,
385389
/// Cargo features specification for building the shader crate.
386390
#[cfg_attr(feature = "clap", clap(flatten))]
387391
#[serde(flatten)]
@@ -487,10 +491,10 @@ impl Default for SpirvBuilder {
487491
}
488492

489493
impl SpirvBuilder {
490-
pub fn new(path_to_crate: impl AsRef<Path>, target: impl Into<String>) -> Self {
494+
pub fn new(path_to_crate: impl AsRef<Path>, target: impl IntoSpirvTarget) -> Self {
491495
Self {
492496
path_to_crate: Some(path_to_crate.as_ref().to_owned()),
493-
target: Some(target.into()),
497+
target: target.to_spirv_target_env().ok(),
494498
..SpirvBuilder::default()
495499
}
496500
}
@@ -757,11 +761,7 @@ fn join_checking_for_separators(strings: Vec<impl Borrow<str>>, sep: &str) -> St
757761

758762
// Returns path to the metadata json.
759763
fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
760-
let target = builder
761-
.target
762-
.as_ref()
763-
.ok_or(SpirvBuilderError::MissingTarget)?;
764-
let target = SpirvTargetEnv::parse_triple(target)?;
764+
let target = builder.target.ok_or(SpirvBuilderError::MissingTarget)?;
765765
let path_to_crate = builder
766766
.path_to_crate
767767
.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)