@@ -92,6 +92,7 @@ use thiserror::Error;
9292pub use rustc_codegen_spirv_target_specs:: {
9393 IntoSpirvTarget , SpirvTargetEnv , SpirvTargetParseError ,
9494} ;
95+ pub use rustc_codegen_spirv_target_specs:: { deserialize_target, serialize_target} ;
9596pub use rustc_codegen_spirv_types:: * ;
9697
9798#[ cfg( feature = "include-target-specs" ) ]
@@ -100,8 +101,10 @@ pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
100101#[ derive( Debug , Error ) ]
101102#[ non_exhaustive]
102103pub enum SpirvBuilderError {
103- #[ error( "`target` must be set or was invalid , for example `spirv-unknown-vulkan1.2`" ) ]
104+ #[ error( "`target` must be set, for example `spirv-unknown-vulkan1.2`" ) ]
104105 MissingTarget ,
106+ #[ error( "Error parsing target: {0}" ) ]
107+ SpirvTargetParseError ( #[ from] SpirvTargetParseError ) ,
105108 #[ error( "`path_to_crate` must be set" ) ]
106109 MissingCratePath ,
107110 #[ error( "crate path '{0}' does not exist" ) ]
@@ -382,10 +385,14 @@ pub struct SpirvBuilder {
382385 clap(
383386 long,
384387 default_value = "spirv-unknown-vulkan1.2" ,
385- value_parser = SpirvTargetEnv :: parse_triple
388+ value_parser = Self :: parse_target
386389 )
387390 ) ]
388- pub target : Option < SpirvTargetEnv > ,
391+ #[ serde(
392+ serialize_with = "serialize_target" ,
393+ deserialize_with = "deserialize_target"
394+ ) ]
395+ pub target : Option < Result < SpirvTargetEnv , SpirvTargetParseError > > ,
389396 /// Cargo features specification for building the shader crate.
390397 #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
391398 #[ serde( flatten) ]
@@ -454,6 +461,12 @@ pub struct SpirvBuilder {
454461
455462#[ cfg( feature = "clap" ) ]
456463impl SpirvBuilder {
464+ fn parse_target (
465+ target : & str ,
466+ ) -> Result < Result < SpirvTargetEnv , SpirvTargetParseError > , clap:: Error > {
467+ Ok ( SpirvTargetEnv :: parse_triple ( target) )
468+ }
469+
457470 /// Clap value parser for `Capability`.
458471 fn parse_spirv_capability ( capability : & str ) -> Result < Capability , clap:: Error > {
459472 use core:: str:: FromStr ;
@@ -494,7 +507,7 @@ impl SpirvBuilder {
494507 pub fn new ( path_to_crate : impl AsRef < Path > , target : impl IntoSpirvTarget ) -> Self {
495508 Self {
496509 path_to_crate : Some ( path_to_crate. as_ref ( ) . to_owned ( ) ) ,
497- target : target. to_spirv_target_env ( ) . ok ( ) ,
510+ target : Some ( target. to_spirv_target_env ( ) ) ,
498511 ..SpirvBuilder :: default ( )
499512 }
500513 }
@@ -761,7 +774,10 @@ fn join_checking_for_separators(strings: Vec<impl Borrow<str>>, sep: &str) -> St
761774
762775// Returns path to the metadata json.
763776fn invoke_rustc ( builder : & SpirvBuilder ) -> Result < PathBuf , SpirvBuilderError > {
764- let target = builder. target . ok_or ( SpirvBuilderError :: MissingTarget ) ?;
777+ let target = builder
778+ . target
779+ . clone ( )
780+ . ok_or ( SpirvBuilderError :: MissingTarget ) ??;
765781 let path_to_crate = builder
766782 . path_to_crate
767783 . as_ref ( )
0 commit comments