Skip to content

Commit 30721b0

Browse files
committed
Add stubs for environment variables; document some of the important ones
This uses a very hacky regex that will probably miss some variables. But having some docs seems better than none at all. This uses a very hacky regex that will probably miss some variables. But having some docs seems better than none at all. In particular, this generates stubs for the following env vars: - COLORTERM - QNX_TARGET - RUST_BACKTRACE - RUSTC_BLESS - RUSTC_BOOTSTRAP - RUSTC_BREAK_ON_ICE - RUSTC_CTFE_BACKTRACE - RUSTC_FORCE_RUSTC_VERSION - RUSTC_GRAPHVIZ_FONT - RUSTC_ICE - RUSTC_LOG - RUSTC_OVERRIDE_VERSION_STRING - RUSTC_RETRY_LINKER_ON_SEGFAULT - RUSTC_TRANSLATION_NO_DEBUG_ASSERT - RUST_DEP_GRAPH_FILTER - RUST_DEP_GRAPH - RUST_FORBID_DEP_GRAPH_EDGE - RUST_MIN_STACK - RUST_TARGET_PATH - SDKROOT - TERM - UNSTABLE_RUSTDOC_TEST_LINE - UNSTABLE_RUSTDOC_TEST_PATH [rendered](![screenshot of unstable-book running locally, with 14 environment variables shown in the sidebar](https://github.com/user-attachments/assets/8238d094-fb7a-456f-ad43-7c07aa2c44dd))
1 parent 231e8cb commit 30721b0

File tree

9 files changed

+109
-6
lines changed

9 files changed

+109
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `COLORTERM`
2+
3+
This environment variable is used by [`-Zterminal-urls`] to detect if URLs are supported by the terminal emulator.
4+
5+
[`-Zterminal-urls`]: ../compiler-flags/terminal-urls.html
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `QNX_TARGET`
2+
3+
----
4+
5+
This environment variable is mandatory when linking on `nto-qnx*_iosock` platforms. It is used to determine an `-L` path to pass to the QNX linker.
6+
7+
You should [set this variable] by running `source qnxsdp-env.sh`.
8+
See [the QNX docs] for more background information.
9+
10+
[set this variable]: https://www.qnx.com/developers/docs/qsc/com.qnx.doc.qsc.inst_larg_org/topic/build_server_developer_steps.html
11+
[the QNX docs]: https://www.qnx.com/developers/docs/7.1/index.html#com.qnx.doc.neutrino.io_sock/topic/migrate_app.html.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# `SDKROOT`
2+
3+
This environment variable is used on MacOS targets.
4+
It is passed through to the linker (either as `-isysroot` or `-syslibroot`) without changes.
5+
6+
Note that this variable is not always respected. When the SDKROOT is clearly wrong (e.g. when the platform of the SDK does not match the `--target` used by rustc), this is ignored and rustc does its own detection.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `TERM`
2+
3+
This environment variable is used by [`-Zterminal-urls`] to detect if URLs are supported by the terminal emulator.
4+
5+
[`-Zterminal-urls`]: ../compiler-flags/terminal-urls.html
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `-Z terminal-urls`
2+
3+
The tracking feature for this issue is [#125586]
4+
5+
[#125586]: https://github.com/rust-lang/rust/issues/125586
6+
7+
---
8+
9+
This flag takes either a boolean or the string "auto".
10+
11+
When enabled, use the OSC 8 hyperlink terminal specification to print hyperlinks in the compiler output.
12+
Use "auto" to try and autodetect whether the terminal emulator supports hyperlinks.
13+
Currently, "auto" only enables hyperlinks if `COLORTERM=truecolor` and `TERM=xterm-256color`.

src/tools/tidy/src/features.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! * All unstable lang features have tests to ensure they are actually unstable.
1010
//! * Language features in a group are sorted by feature name.
1111
12+
use std::collections::BTreeSet;
1213
use std::collections::hash_map::{Entry, HashMap};
1314
use std::ffi::OsStr;
1415
use std::num::NonZeroU32;
@@ -21,6 +22,7 @@ use crate::walk::{filter_dirs, filter_not_rust, walk, walk_many};
2122
mod tests;
2223

2324
mod version;
25+
use regex::Regex;
2426
use version::Version;
2527

2628
const FEATURE_GROUP_START_PREFIX: &str = "// feature-group-start";
@@ -610,3 +612,36 @@ fn map_lib_features(
610612
},
611613
);
612614
}
615+
616+
fn should_document(var: &str) -> bool {
617+
if var.starts_with("RUSTC_") || var.starts_with("RUST_") || var.starts_with("UNSTABLE_RUSTDOC_")
618+
{
619+
return true;
620+
}
621+
["SDKROOT", "QNX_TARGET", "COLORTERM", "TERM"].contains(&var)
622+
}
623+
624+
pub fn collect_env_vars(compiler: &Path) -> BTreeSet<String> {
625+
let env_var_regex: Regex = Regex::new(r#"env::var(_os)?\("([^"]+)"#).unwrap();
626+
627+
let mut vars = BTreeSet::new();
628+
walk(
629+
compiler,
630+
// skip build scripts, tests, and non-rust files
631+
|path, _is_dir| {
632+
filter_dirs(path)
633+
|| filter_not_rust(path)
634+
|| path.ends_with("build.rs")
635+
|| path.ends_with("tests.rs")
636+
},
637+
&mut |_entry, contents| {
638+
for env_var in env_var_regex.captures_iter(contents).map(|c| c.get(2).unwrap().as_str())
639+
{
640+
if should_document(env_var) {
641+
vars.insert(env_var.to_owned());
642+
}
643+
}
644+
},
645+
);
646+
vars
647+
}

src/tools/tidy/src/unstable_book.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::features::{CollectedFeatures, Features, Status};
66

77
pub const PATH_STR: &str = "doc/unstable-book";
88

9+
pub const ENV_VARS_DIR: &str = "src/compiler-environment-variables";
10+
911
pub const COMPILER_FLAGS_DIR: &str = "src/compiler-flags";
1012

1113
pub const LANG_FEATURES_DIR: &str = "src/language-features";

src/tools/unstable-book-gen/src/main.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use std::env;
55
use std::fs::{self, write};
66
use std::path::Path;
77

8-
use tidy::features::{Features, collect_lang_features, collect_lib_features};
8+
use tidy::features::{Features, collect_env_vars, collect_lang_features, collect_lib_features};
99
use tidy::t;
1010
use tidy::unstable_book::{
11-
LANG_FEATURES_DIR, LIB_FEATURES_DIR, PATH_STR, collect_unstable_book_section_file_names,
12-
collect_unstable_feature_names,
11+
ENV_VARS_DIR, LANG_FEATURES_DIR, LIB_FEATURES_DIR, PATH_STR,
12+
collect_unstable_book_section_file_names, collect_unstable_feature_names,
1313
};
1414

1515
fn generate_stub_issue(path: &Path, name: &str, issue: u32) {
@@ -22,6 +22,11 @@ fn generate_stub_no_issue(path: &Path, name: &str) {
2222
t!(write(path, content), path);
2323
}
2424

25+
fn generate_stub_env_var(path: &Path, name: &str) {
26+
let content = format!(include_str!("stub-env-var.md"), name = name);
27+
t!(write(path, content), path);
28+
}
29+
2530
fn set_to_summary_str(set: &BTreeSet<String>, dir: &str) -> String {
2631
set.iter()
2732
.map(|ref n| format!(" - [{}]({}/{}.md)", n.replace('-', "_"), dir, n))
@@ -54,7 +59,7 @@ fn generate_summary(path: &Path, lang_features: &Features, lib_features: &Featur
5459
t!(write(&summary_path, content), summary_path);
5560
}
5661

57-
fn generate_unstable_book_files(src: &Path, out: &Path, features: &Features) {
62+
fn generate_feature_files(src: &Path, out: &Path, features: &Features) {
5863
let unstable_features = collect_unstable_feature_names(features);
5964
let unstable_section_file_names = collect_unstable_book_section_file_names(src);
6065
t!(fs::create_dir_all(&out));
@@ -72,6 +77,16 @@ fn generate_unstable_book_files(src: &Path, out: &Path, features: &Features) {
7277
}
7378
}
7479

80+
fn generate_env_files(src: &Path, out: &Path, env_vars: &BTreeSet<String>) {
81+
let env_var_file_names = collect_unstable_book_section_file_names(src);
82+
t!(fs::create_dir_all(&out));
83+
for env_var in env_vars - &env_var_file_names {
84+
let file_name = format!("{env_var}.md");
85+
let out_file_path = out.join(&file_name);
86+
generate_stub_env_var(&out_file_path, &env_var);
87+
}
88+
}
89+
7590
fn copy_recursive(from: &Path, to: &Path) {
7691
for entry in t!(fs::read_dir(from)) {
7792
let e = t!(entry);
@@ -101,21 +116,23 @@ fn main() {
101116
.into_iter()
102117
.filter(|&(ref name, _)| !lang_features.contains_key(name))
103118
.collect();
119+
let env_vars = collect_env_vars(compiler_path);
104120

105121
let doc_src_path = src_path.join(PATH_STR);
106122

107123
t!(fs::create_dir_all(&dest_path));
108124

109-
generate_unstable_book_files(
125+
generate_feature_files(
110126
&doc_src_path.join(LANG_FEATURES_DIR),
111127
&dest_path.join(LANG_FEATURES_DIR),
112128
&lang_features,
113129
);
114-
generate_unstable_book_files(
130+
generate_feature_files(
115131
&doc_src_path.join(LIB_FEATURES_DIR),
116132
&dest_path.join(LIB_FEATURES_DIR),
117133
&lib_features,
118134
);
135+
generate_env_files(&doc_src_path.join(ENV_VARS_DIR), &dest_path.join(ENV_VARS_DIR), &env_vars);
119136

120137
copy_recursive(&doc_src_path, &dest_path);
121138

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `{name}`
2+
3+
Environment variables have no tracking issue. This environment variable has no documentation, and therefore is likely internal to the compiler and not meant for general use.
4+
5+
See [the code][github search] for more information.
6+
7+
[github search]: https://github.com/search?q=repo%3Arust-lang%2Frust+%22{name}%22+path%3Acompiler&type=code
8+
9+
------------------------

0 commit comments

Comments
 (0)