@@ -93,6 +93,7 @@ use thiserror::Error;
93
93
pub use rustc_codegen_spirv_target_specs:: {
94
94
IntoSpirvTarget , SpirvTargetEnv , SpirvTargetParseError ,
95
95
} ;
96
+ pub use rustc_codegen_spirv_target_specs:: { deserialize_target, serialize_target} ;
96
97
pub use rustc_codegen_spirv_types:: * ;
97
98
98
99
#[ cfg( feature = "include-target-specs" ) ]
@@ -101,8 +102,10 @@ pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
101
102
#[ derive( Debug , Error ) ]
102
103
#[ non_exhaustive]
103
104
pub 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`" ) ]
105
106
MissingTarget ,
107
+ #[ error( "Error parsing target: {0}" ) ]
108
+ SpirvTargetParseError ( #[ from] SpirvTargetParseError ) ,
106
109
#[ error( "`path_to_crate` must be set" ) ]
107
110
MissingCratePath ,
108
111
#[ error( "crate path '{0}' does not exist" ) ]
@@ -409,10 +412,14 @@ pub struct SpirvBuilder {
409
412
clap(
410
413
long,
411
414
default_value = "spirv-unknown-vulkan1.2" ,
412
- value_parser = SpirvTargetEnv :: parse_triple
415
+ value_parser = Self :: parse_target
413
416
)
414
417
) ]
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 > > ,
416
423
/// Cargo features specification for building the shader crate.
417
424
#[ cfg_attr( feature = "clap" , clap( flatten) ) ]
418
425
#[ serde( flatten) ]
@@ -481,6 +488,12 @@ pub struct SpirvBuilder {
481
488
482
489
#[ cfg( feature = "clap" ) ]
483
490
impl SpirvBuilder {
491
+ fn parse_target (
492
+ target : & str ,
493
+ ) -> Result < Result < SpirvTargetEnv , SpirvTargetParseError > , clap:: Error > {
494
+ Ok ( SpirvTargetEnv :: parse_triple ( target) )
495
+ }
496
+
484
497
/// Clap value parser for `Capability`.
485
498
fn parse_spirv_capability ( capability : & str ) -> Result < Capability , clap:: Error > {
486
499
use core:: str:: FromStr ;
@@ -521,7 +534,7 @@ impl SpirvBuilder {
521
534
pub fn new ( path_to_crate : impl AsRef < Path > , target : impl IntoSpirvTarget ) -> Self {
522
535
Self {
523
536
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 ( ) ) ,
525
538
..SpirvBuilder :: default ( )
526
539
}
527
540
}
@@ -788,7 +801,10 @@ fn join_checking_for_separators(strings: Vec<impl Borrow<str>>, sep: &str) -> St
788
801
789
802
// Returns path to the metadata json.
790
803
fn 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 ) ??;
792
808
let path_to_crate = builder
793
809
. path_to_crate
794
810
. as_ref ( )
0 commit comments