Skip to content

Commit 6a5d512

Browse files
committed
Always compile rustdoc with debug logging enabled with download-ci-llvm
Previously, logging at DEBUG or below would always be silenced, because rustc compiles tracing with the `static_max_level_info` feature. That makes sense for release artifacts, but not for developing rustdoc. Instead, this compiles two different versions of tracing: one in the release artifacts, distributed in the sysroot, and a new version compiled by rustdoc. Since `rustc_driver` is always linked to the version of sysroot, this copy/pastes `init_env_logging` into rustdoc. To avoid compiling an unnecessary version of tracing when `download-stage1` isn't set, this adds a new `using-ci-artifacts` feature for rustdoc and passes that feature in bootstrap.
1 parent b2b4a56 commit 6a5d512

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

Cargo.lock

+3
Original file line numberDiff line numberDiff line change
@@ -4361,6 +4361,9 @@ dependencies = [
43614361
"serde_json",
43624362
"smallvec 1.4.2",
43634363
"tempfile",
4364+
"tracing",
4365+
"tracing-subscriber",
4366+
"tracing-tree",
43644367
]
43654368

43664369
[[package]]

src/bootstrap/tool.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,16 @@ impl Step for Rustdoc {
493493
// rustc compiler it's paired with, so it must be built with the previous stage compiler.
494494
let build_compiler = builder.compiler(target_compiler.stage - 1, builder.config.build);
495495

496+
// Rustdoc uses its own version of `tracing` when downloading CI
497+
// artifacts so that debug logging is available.
498+
let using_ci_artifacts;
499+
let extra_features: &[_] = if builder.build.config.download_stage1 {
500+
using_ci_artifacts = ["using-ci-artifacts".into()];
501+
&using_ci_artifacts
502+
} else {
503+
&[]
504+
};
505+
496506
// The presence of `target_compiler` ensures that the necessary libraries (codegen backends,
497507
// compiler libraries, ...) are built. Rustdoc does not require the presence of any
498508
// libraries within sysroot_libdir (i.e., rustlib), though doctests may want it (since
@@ -508,7 +518,7 @@ impl Step for Rustdoc {
508518
"build",
509519
"src/tools/rustdoc",
510520
SourceType::InTree,
511-
&[],
521+
extra_features,
512522
);
513523

514524
builder.info(&format!(

src/librustdoc/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ smallvec = "1.0"
1717
tempfile = "3"
1818
itertools = "0.9"
1919
regex = "1"
20+
tracing = { version = "0.1", optional = true }
21+
tracing-tree = { version = "0.1.6", optional = true }
22+
23+
[dependencies.tracing-subscriber]
24+
version = "0.2.13"
25+
default-features = false
26+
features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"]
27+
optional = true
28+
29+
[features]
30+
using-ci-artifacts = ["tracing", "tracing-tree", "tracing-subscriber"]
2031

2132
[dev-dependencies]
2233
expect-test = "1.0"

src/librustdoc/lib.rs

+76
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,90 @@ mod visit_lib;
9595
pub fn main() {
9696
rustc_driver::set_sigpipe_handler();
9797
rustc_driver::install_ice_hook();
98+
99+
// When using CI artifacts (with `download_stage1 = true`),
100+
// tracing is built with `--features=static_max_level_info`, which disables
101+
// almost all rustdoc logging. To avoid this, compile our own version of
102+
// `tracing` that logs all levels.
103+
#[cfg(feature = "using-ci-artifacts")]
104+
init_logging();
105+
// When using CI artifacts, rustc logging is always disabled. To avoid spurious
106+
// warnings, don't enable both rustdoc and rustc's version of `tracing`.
107+
#[cfg(not(feature = "using-ci-artifacts"))]
98108
rustc_driver::init_env_logger("RUSTDOC_LOG");
109+
99110
let exit_code = rustc_driver::catch_with_exit_code(|| match get_args() {
100111
Some(args) => main_args(&args),
101112
_ => Err(ErrorReported),
102113
});
103114
process::exit(exit_code);
104115
}
105116

117+
#[cfg(feature = "using-ci-artifacts")]
118+
fn init_logging() {
119+
extern crate libc;
120+
#[cfg(windows)]
121+
extern crate winapi;
122+
123+
use std::io;
124+
125+
// FIXME remove these and use winapi 0.3 instead
126+
// Duplicates: bootstrap/compile.rs, librustc_errors/emitter.rs, rustc_driver/lib.rs
127+
#[cfg(unix)]
128+
fn stdout_isatty() -> bool {
129+
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
130+
}
131+
132+
#[cfg(windows)]
133+
fn stdout_isatty() -> bool {
134+
use winapi::um::consoleapi::GetConsoleMode;
135+
use winapi::um::processenv::GetStdHandle;
136+
use winapi::um::winbase::STD_OUTPUT_HANDLE;
137+
138+
unsafe {
139+
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
140+
let mut out = 0;
141+
GetConsoleMode(handle, &mut out) != 0
142+
}
143+
}
144+
145+
let color_logs = match std::env::var("RUSTDOC_LOG_COLOR") {
146+
Ok(value) => match value.as_ref() {
147+
"always" => true,
148+
"never" => false,
149+
"auto" => stdout_isatty(),
150+
_ => early_error(
151+
ErrorOutputType::default(),
152+
&format!(
153+
"invalid log color value '{}': expected one of always, never, or auto",
154+
value
155+
),
156+
),
157+
},
158+
Err(std::env::VarError::NotPresent) => stdout_isatty(),
159+
Err(std::env::VarError::NotUnicode(_value)) => early_error(
160+
ErrorOutputType::default(),
161+
"non-Unicode log color value: expected one of always, never, or auto",
162+
),
163+
};
164+
let filter = tracing_subscriber::EnvFilter::from_env("RUSTDOC_LOG");
165+
let layer = tracing_tree::HierarchicalLayer::default()
166+
.with_writer(io::stderr)
167+
.with_indent_lines(true)
168+
.with_ansi(color_logs)
169+
.with_targets(true)
170+
.with_wraparound(10)
171+
.with_verbose_exit(true)
172+
.with_verbose_entry(true)
173+
.with_indent_amount(2);
174+
#[cfg(parallel_compiler)]
175+
let layer = layer.with_thread_ids(true).with_thread_names(true);
176+
177+
use tracing_subscriber::layer::SubscriberExt;
178+
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
179+
tracing::subscriber::set_global_default(subscriber).unwrap();
180+
}
181+
106182
fn get_args() -> Option<Vec<String>> {
107183
env::args_os()
108184
.enumerate()

src/tools/rustdoc/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ path = "main.rs"
1313

1414
[dependencies]
1515
rustdoc = { path = "../../librustdoc" }
16+
17+
[features]
18+
using-ci-artifacts = ["rustdoc/using-ci-artifacts"]

0 commit comments

Comments
 (0)