Skip to content

Commit 8bda250

Browse files
authored
feat: move debug logging behind an environment variable (#972)
1 parent 46eedd0 commit 8bda250

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/command_helpers.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use std::{
99
io::{self, Read, Write},
1010
path::Path,
1111
process::{Child, ChildStderr, Command, Stdio},
12-
sync::Arc,
12+
sync::{
13+
atomic::{AtomicBool, Ordering},
14+
Arc,
15+
},
1316
};
1417

1518
use crate::{Error, ErrorKind, Object};
@@ -18,13 +21,17 @@ use crate::{Error, ErrorKind, Object};
1821
pub(crate) struct CargoOutput {
1922
pub(crate) metadata: bool,
2023
pub(crate) warnings: bool,
24+
pub(crate) debug: bool,
25+
checked_dbg_var: Arc<AtomicBool>,
2126
}
2227

2328
impl CargoOutput {
24-
pub(crate) const fn new() -> Self {
29+
pub(crate) fn new() -> Self {
2530
Self {
2631
metadata: true,
2732
warnings: true,
33+
debug: std::env::var_os("CC_ENABLE_DEBUG_OUTPUT").is_some(),
34+
checked_dbg_var: Arc::new(AtomicBool::new(false)),
2835
}
2936
}
3037

@@ -40,6 +47,16 @@ impl CargoOutput {
4047
}
4148
}
4249

50+
pub(crate) fn print_debug(&self, arg: &dyn Display) {
51+
if self.metadata && !self.checked_dbg_var.load(Ordering::Relaxed) {
52+
self.checked_dbg_var.store(true, Ordering::Relaxed);
53+
println!("cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT");
54+
}
55+
if self.debug {
56+
println!("{}", arg);
57+
}
58+
}
59+
4360
fn stdio_for_warnings(&self) -> Stdio {
4461
if self.warnings {
4562
Stdio::piped()
@@ -217,7 +234,7 @@ fn wait_on_child(
217234
}
218235
};
219236

220-
cargo_output.print_warning(&status);
237+
cargo_output.print_debug(&status);
221238

222239
if status.success() {
223240
Ok(())
@@ -326,7 +343,7 @@ pub(crate) fn spawn(
326343
}
327344
}
328345

329-
cargo_output.print_warning(&format_args!("running: {:?}", cmd));
346+
cargo_output.print_debug(&format_args!("running: {:?}", cmd));
330347

331348
let cmd = ResetStderr(cmd);
332349
let child = cmd.0.stderr(cargo_output.stdio_for_warnings()).spawn();

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
//! some cross compiling scenarios. Setting this variable
8585
//! will disable the generation of default compiler
8686
//! flags.
87+
//! * `CC_ENABLE_DEBUG_OUTPUT` - if set, compiler command invocations and exit codes will
88+
//! be logged to stdout. This is useful for debugging build script issues, but can be
89+
//! overly verbose for normal use.
8790
//! * `CXX...` - see [C++ Support](#c-support).
8891
//!
8992
//! Furthermore, projects using this crate may specify custom environment variables
@@ -1119,6 +1122,16 @@ impl Build {
11191122
self
11201123
}
11211124

1125+
/// Define whether debug information should be emitted for cargo. Defaults to whether
1126+
/// or not the environment variable `CC_ENABLE_DEBUG_OUTPUT` is set.
1127+
///
1128+
/// If enabled, the compiler will emit debug information when generating object files,
1129+
/// such as the command invoked and the exit status.
1130+
pub fn cargo_debug(&mut self, cargo_debug: bool) -> &mut Build {
1131+
self.cargo_output.debug = cargo_debug;
1132+
self
1133+
}
1134+
11221135
/// Adds a native library modifier that will be added to the
11231136
/// `rustc-link-lib=static:MODIFIERS=LIBRARY_NAME` metadata line
11241137
/// emitted for cargo if `cargo_metadata` is enabled.

0 commit comments

Comments
 (0)