Skip to content

Commit 195417a

Browse files
committed
build_script: add forwarding of rustc warnings, with color support
1 parent e7a928f commit 195417a

File tree

1 file changed

+41
-1
lines changed
  • crates/spirv-builder/src

1 file changed

+41
-1
lines changed

crates/spirv-builder/src/lib.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,20 @@ pub struct BuildScriptConfig {
414414
///
415415
/// Default: `false`
416416
pub env_shader_spv_path: Option<bool>,
417+
418+
/// Forwards any warnings or errors by rustc as build script warnings (via `cargo::warning=`). Not enabling this
419+
/// option may hide warnings if the build succeeds.
420+
///
421+
/// Default: [`Self::defaults`]
422+
pub forward_rustc_warnings: Option<bool>,
423+
424+
/// Pass `--color always` to cargo to force enable colorful error messages. Particularly in build scripts, these
425+
/// are disabled by default, even though we'll forward them to your console. Should your console not support colors,
426+
/// then the outer cargo executing the build script will filter out all ansi escape sequences anyway, so we're free
427+
/// to always emit them.
428+
///
429+
/// Default: [`Self::defaults`]
430+
pub cargo_color_always: Option<bool>,
417431
}
418432

419433
impl BuildScriptConfig {
@@ -423,6 +437,12 @@ impl BuildScriptConfig {
423437
fn env_shader_spv_path(&self) -> bool {
424438
self.env_shader_spv_path.unwrap_or(false)
425439
}
440+
fn forward_rustc_warnings(&self) -> bool {
441+
self.forward_rustc_warnings.unwrap_or(self.defaults)
442+
}
443+
fn cargo_color_always(&self) -> bool {
444+
self.cargo_color_always.unwrap_or(self.defaults)
445+
}
426446
}
427447

428448
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
@@ -1083,6 +1103,16 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
10831103

10841104
cargo.arg("--target-dir").arg(target_dir);
10851105

1106+
// Args for warning and error forwarding
1107+
if builder.build_script.forward_rustc_warnings() {
1108+
// Quiet to remove all the status messages and only emit errors and warnings
1109+
cargo.args(["--quiet"]);
1110+
}
1111+
if builder.build_script.cargo_color_always() {
1112+
// Always emit color, since the outer cargo will remove ascii escape sequences if color is turned off
1113+
cargo.args(["--color", "always"]);
1114+
}
1115+
10861116
// NOTE(eddyb) this used to be just `RUSTFLAGS` but at some point Cargo
10871117
// added a separate environment variable using `\x1f` instead of spaces,
10881118
// which allows us to have spaces within individual `rustc` flags.
@@ -1100,10 +1130,20 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
11001130
num_cgus.to_string(),
11011131
);
11021132

1103-
cargo.stderr(Stdio::inherit()).current_dir(path_to_crate);
1133+
if !builder.build_script.forward_rustc_warnings() {
1134+
cargo.stderr(Stdio::inherit());
1135+
}
1136+
cargo.current_dir(path_to_crate);
11041137
log::debug!("building shaders with `{cargo:?}`");
11051138
let build = cargo.output().expect("failed to execute cargo build");
11061139

1140+
if builder.build_script.forward_rustc_warnings() {
1141+
let stderr = String::from_utf8(build.stderr).unwrap();
1142+
for line in stderr.lines() {
1143+
println!("cargo::warning={line}");
1144+
}
1145+
}
1146+
11071147
// `get_last_artifact` has the side-effect of printing invalid lines, so
11081148
// we do that even in case of an error, to let through any useful messages
11091149
// that ended up on stdout instead of stderr.

0 commit comments

Comments
 (0)