WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 8b83d58

Browse files
committed
build_script: add forwarding of rustc warnings, with color support
1 parent 56a46c3 commit 8b83d58

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

420434
/// these all have the prefix `get` so the doc items link to the members, not these private fns
@@ -425,6 +439,12 @@ impl BuildScriptConfig {
425439
fn get_env_shader_spv_path(&self) -> bool {
426440
self.env_shader_spv_path.unwrap_or(false)
427441
}
442+
fn get_forward_rustc_warnings(&self) -> bool {
443+
self.forward_rustc_warnings.unwrap_or(self.defaults)
444+
}
445+
fn get_cargo_color_always(&self) -> bool {
446+
self.cargo_color_always.unwrap_or(self.defaults)
447+
}
428448
}
429449

430450
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
@@ -1090,6 +1110,16 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
10901110

10911111
cargo.arg("--target-dir").arg(target_dir);
10921112

1113+
// Args for warning and error forwarding
1114+
if builder.build_script.get_forward_rustc_warnings() {
1115+
// Quiet to remove all the status messages and only emit errors and warnings
1116+
cargo.args(["--quiet"]);
1117+
}
1118+
if builder.build_script.get_cargo_color_always() {
1119+
// Always emit color, since the outer cargo will remove ascii escape sequences if color is turned off
1120+
cargo.args(["--color", "always"]);
1121+
}
1122+
10931123
// NOTE(eddyb) this used to be just `RUSTFLAGS` but at some point Cargo
10941124
// added a separate environment variable using `\x1f` instead of spaces,
10951125
// which allows us to have spaces within individual `rustc` flags.
@@ -1107,10 +1137,20 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
11071137
num_cgus.to_string(),
11081138
);
11091139

1110-
cargo.stderr(Stdio::inherit()).current_dir(path_to_crate);
1140+
if !builder.build_script.get_forward_rustc_warnings() {
1141+
cargo.stderr(Stdio::inherit());
1142+
}
1143+
cargo.current_dir(path_to_crate);
11111144
log::debug!("building shaders with `{cargo:?}`");
11121145
let build = cargo.output().expect("failed to execute cargo build");
11131146

1147+
if builder.build_script.get_forward_rustc_warnings() {
1148+
let stderr = String::from_utf8(build.stderr).unwrap();
1149+
for line in stderr.lines() {
1150+
println!("cargo::warning={line}");
1151+
}
1152+
}
1153+
11141154
// `get_last_artifact` has the side-effect of printing invalid lines, so
11151155
// we do that even in case of an error, to let through any useful messages
11161156
// that ended up on stdout instead of stderr.

0 commit comments

Comments
 (0)