@@ -407,6 +407,20 @@ pub struct BuildScriptConfig {
407407 ///
408408 /// Default: `false`
409409 pub env_shader_spv_path : Option < bool > ,
410+
411+ /// Forwards any warnings or errors by rustc as build script warnings (via `cargo::warning=`). Not enabling this
412+ /// option may hide warnings if the build succeeds.
413+ ///
414+ /// Default: [`Self::defaults`]
415+ pub forward_rustc_warnings : Option < bool > ,
416+
417+ /// Pass `--color always` to cargo to force enable colorful error messages. Particularly in build scripts, these
418+ /// are disabled by default, even though we'll forward them to your console. Should your console not support colors,
419+ /// then the outer cargo executing the build script will filter out all ansi escape sequences anyway, so we're free
420+ /// to always emit them.
421+ ///
422+ /// Default: [`Self::defaults`]
423+ pub cargo_color_always : Option < bool > ,
410424}
411425
412426/// these all have the prefix `get` so the doc items link to the members, not these private fns
@@ -417,6 +431,12 @@ impl BuildScriptConfig {
417431 fn get_env_shader_spv_path ( & self ) -> bool {
418432 self . env_shader_spv_path . unwrap_or ( false )
419433 }
434+ fn get_forward_rustc_warnings ( & self ) -> bool {
435+ self . forward_rustc_warnings . unwrap_or ( self . defaults )
436+ }
437+ fn get_cargo_color_always ( & self ) -> bool {
438+ self . cargo_color_always . unwrap_or ( self . defaults )
439+ }
420440}
421441
422442#[ derive( Clone , Debug , serde:: Deserialize , serde:: Serialize ) ]
@@ -1024,6 +1044,16 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
10241044
10251045 cargo. arg ( "--target-dir" ) . arg ( target_dir) ;
10261046
1047+ // Args for warning and error forwarding
1048+ if builder. build_script . get_forward_rustc_warnings ( ) {
1049+ // Quiet to remove all the status messages and only emit errors and warnings
1050+ cargo. args ( [ "--quiet" ] ) ;
1051+ }
1052+ if builder. build_script . get_cargo_color_always ( ) {
1053+ // Always emit color, since the outer cargo will remove ascii escape sequences if color is turned off
1054+ cargo. args ( [ "--color" , "always" ] ) ;
1055+ }
1056+
10271057 // NOTE(eddyb) this used to be just `RUSTFLAGS` but at some point Cargo
10281058 // added a separate environment variable using `\x1f` instead of spaces,
10291059 // which allows us to have spaces within individual `rustc` flags.
@@ -1041,10 +1071,20 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
10411071 num_cgus. to_string ( ) ,
10421072 ) ;
10431073
1044- cargo. stderr ( Stdio :: inherit ( ) ) . current_dir ( path_to_crate) ;
1074+ if !builder. build_script . get_forward_rustc_warnings ( ) {
1075+ cargo. stderr ( Stdio :: inherit ( ) ) ;
1076+ }
1077+ cargo. current_dir ( path_to_crate) ;
10451078 log:: debug!( "building shaders with `{cargo:?}`" ) ;
10461079 let build = cargo. output ( ) . expect ( "failed to execute cargo build" ) ;
10471080
1081+ if builder. build_script . get_forward_rustc_warnings ( ) {
1082+ let stderr = String :: from_utf8 ( build. stderr ) . unwrap ( ) ;
1083+ for line in stderr. lines ( ) {
1084+ println ! ( "cargo::warning={line}" ) ;
1085+ }
1086+ }
1087+
10481088 // `get_last_artifact` has the side-effect of printing invalid lines, so
10491089 // we do that even in case of an error, to let through any useful messages
10501090 // that ended up on stdout instead of stderr.
0 commit comments