@@ -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" ) ]
@@ -383,10 +386,14 @@ pub struct SpirvBuilder {
383386 clap(
384387 long,
385388 default_value = "spirv-unknown-vulkan1.2" ,
386- value_parser = SpirvTargetEnv :: parse_triple
389+ value_parser = Self :: parse_target
387390 )
388391 ) ]
389- pub target : Option < SpirvTargetEnv > ,
392+ #[ serde(
393+ serialize_with = "serialize_target" ,
394+ deserialize_with = "deserialize_target"
395+ ) ]
396+ pub target : Option < Result < SpirvTargetEnv , SpirvTargetParseError > > ,
390397 /// Cargo features specification for building the shader crate.
391398 #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
392399 #[ serde( flatten) ]
@@ -455,6 +462,12 @@ pub struct SpirvBuilder {
455462
456463#[ cfg( feature = "clap" ) ]
457464impl SpirvBuilder {
465+ fn parse_target (
466+ target : & str ,
467+ ) -> Result < Result < SpirvTargetEnv , SpirvTargetParseError > , clap:: Error > {
468+ Ok ( SpirvTargetEnv :: parse_triple ( target) )
469+ }
470+
458471 /// Clap value parser for `Capability`.
459472 fn parse_spirv_capability ( capability : & str ) -> Result < Capability , clap:: Error > {
460473 use core:: str:: FromStr ;
@@ -495,7 +508,7 @@ impl SpirvBuilder {
495508 pub fn new ( path_to_crate : impl AsRef < Path > , target : impl IntoSpirvTarget ) -> Self {
496509 Self {
497510 path_to_crate : Some ( path_to_crate. as_ref ( ) . to_owned ( ) ) ,
498- target : target. to_spirv_target_env ( ) . ok ( ) ,
511+ target : Some ( target. to_spirv_target_env ( ) ) ,
499512 ..SpirvBuilder :: default ( )
500513 }
501514 }
@@ -762,7 +775,10 @@ fn join_checking_for_separators(strings: Vec<impl Borrow<str>>, sep: &str) -> St
762775
763776// Returns path to the metadata json.
764777fn invoke_rustc ( builder : & SpirvBuilder ) -> Result < PathBuf , SpirvBuilderError > {
765- let target = builder. target . ok_or ( SpirvBuilderError :: MissingTarget ) ?;
778+ let target = builder
779+ . target
780+ . clone ( )
781+ . ok_or ( SpirvBuilderError :: MissingTarget ) ??;
766782 let path_to_crate = builder
767783 . path_to_crate
768784 . as_ref ( )
0 commit comments