@@ -93,6 +93,7 @@ use thiserror::Error;
9393pub use rustc_codegen_spirv_target_specs:: {
9494 IntoSpirvTarget , SpirvTargetEnv , SpirvTargetParseError ,
9595} ;
96+ pub use rustc_codegen_spirv_target_specs:: { deserialize_target, serialize_target} ;
9697pub use rustc_codegen_spirv_types:: * ;
9798
9899#[ cfg( feature = "include-target-specs" ) ]
@@ -101,8 +102,10 @@ pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
101102#[ derive( Debug , Error ) ]
102103#[ non_exhaustive]
103104pub enum SpirvBuilderError {
104- #[ error( "`target` must be set or was invalid , for example `spirv-unknown-vulkan1.2`" ) ]
105+ #[ error( "`target` must be set, for example `spirv-unknown-vulkan1.2`" ) ]
105106 MissingTarget ,
107+ #[ error( "Error parsing target: {0}" ) ]
108+ SpirvTargetParseError ( #[ from] SpirvTargetParseError ) ,
106109 #[ error( "`path_to_crate` must be set" ) ]
107110 MissingCratePath ,
108111 #[ error( "crate path '{0}' does not exist" ) ]
@@ -409,10 +412,14 @@ pub struct SpirvBuilder {
409412 clap(
410413 long,
411414 default_value = "spirv-unknown-vulkan1.2" ,
412- value_parser = SpirvTargetEnv :: parse_triple
415+ value_parser = Self :: parse_target
413416 )
414417 ) ]
415- pub target : Option < SpirvTargetEnv > ,
418+ #[ serde(
419+ serialize_with = "serialize_target" ,
420+ deserialize_with = "deserialize_target"
421+ ) ]
422+ pub target : Option < Result < SpirvTargetEnv , SpirvTargetParseError > > ,
416423 /// Cargo features specification for building the shader crate.
417424 #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
418425 #[ serde( flatten) ]
@@ -481,6 +488,12 @@ pub struct SpirvBuilder {
481488
482489#[ cfg( feature = "clap" ) ]
483490impl SpirvBuilder {
491+ fn parse_target (
492+ target : & str ,
493+ ) -> Result < Result < SpirvTargetEnv , SpirvTargetParseError > , clap:: Error > {
494+ Ok ( SpirvTargetEnv :: parse_triple ( target) )
495+ }
496+
484497 /// Clap value parser for `Capability`.
485498 fn parse_spirv_capability ( capability : & str ) -> Result < Capability , clap:: Error > {
486499 use core:: str:: FromStr ;
@@ -521,7 +534,7 @@ impl SpirvBuilder {
521534 pub fn new ( path_to_crate : impl AsRef < Path > , target : impl IntoSpirvTarget ) -> Self {
522535 Self {
523536 path_to_crate : Some ( path_to_crate. as_ref ( ) . to_owned ( ) ) ,
524- target : target. to_spirv_target_env ( ) . ok ( ) ,
537+ target : Some ( target. to_spirv_target_env ( ) ) ,
525538 ..SpirvBuilder :: default ( )
526539 }
527540 }
@@ -788,7 +801,10 @@ fn join_checking_for_separators(strings: Vec<impl Borrow<str>>, sep: &str) -> St
788801
789802// Returns path to the metadata json.
790803fn invoke_rustc ( builder : & SpirvBuilder ) -> Result < PathBuf , SpirvBuilderError > {
791- let target = builder. target . ok_or ( SpirvBuilderError :: MissingTarget ) ?;
804+ let target = builder
805+ . target
806+ . clone ( )
807+ . ok_or ( SpirvBuilderError :: MissingTarget ) ??;
792808 let path_to_crate = builder
793809 . path_to_crate
794810 . as_ref ( )
0 commit comments