Skip to content

Commit 2a45deb

Browse files
committed
docs: autogenerate compiler flag stubs based on -Zhelp
1 parent 18eeac0 commit 2a45deb

File tree

2 files changed

+61
-6
lines changed
  • src
    • bootstrap/src/core/build_steps
    • tools/unstable-book-gen/src

2 files changed

+61
-6
lines changed

src/bootstrap/src/core/build_steps/doc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,11 @@ impl Step for UnstableBookGen {
11481148
fn run(self, builder: &Builder<'_>) {
11491149
let target = self.target;
11501150

1151+
let stage = builder.top_stage;
1152+
let compiler = builder.compiler(stage, self.target);
1153+
let rustc_path =
1154+
builder.out.join(compiler.host).join(format!("stage{stage}")).join("bin").join("rustc");
1155+
11511156
builder.info(&format!("Generating unstable book md files ({target})"));
11521157
let out = builder.md_doc_out(target).join("unstable-book");
11531158
builder.create_dir(&out);
@@ -1156,6 +1161,7 @@ impl Step for UnstableBookGen {
11561161
cmd.arg(builder.src.join("library"));
11571162
cmd.arg(builder.src.join("compiler"));
11581163
cmd.arg(builder.src.join("src"));
1164+
cmd.arg(rustc_path);
11591165
cmd.arg(out);
11601166

11611167
cmd.run(builder);

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

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ use std::collections::BTreeSet;
44
use std::env;
55
use std::fs::{self, write};
66
use std::path::Path;
7+
use std::process::Command;
78

8-
use tidy::features::{Features, collect_env_vars, collect_lang_features, collect_lib_features};
9+
use tidy::features::{
10+
Feature, Features, Status, collect_env_vars, collect_lang_features, collect_lib_features,
11+
};
912
use tidy::t;
1013
use tidy::unstable_book::{
11-
ENV_VARS_DIR, LANG_FEATURES_DIR, LIB_FEATURES_DIR, PATH_STR,
14+
COMPILER_FLAGS_DIR, ENV_VARS_DIR, LANG_FEATURES_DIR, LIB_FEATURES_DIR, PATH_STR,
1215
collect_unstable_book_section_file_names, collect_unstable_feature_names,
1316
};
1417

@@ -38,8 +41,15 @@ fn set_to_summary_str(set: &BTreeSet<String>, dir: &str) -> String {
3841
.fold("".to_owned(), |s, a| s + &a + "\n")
3942
}
4043

41-
fn generate_summary(path: &Path, lang_features: &Features, lib_features: &Features) {
42-
let compiler_flags = collect_unstable_book_section_file_names(&path.join("src/compiler-flags"));
44+
fn generate_summary(
45+
path: &Path,
46+
lang_features: &Features,
47+
lib_features: &Features,
48+
compiler_flags: &Features,
49+
) {
50+
let compiler_flags =
51+
&collect_unstable_book_section_file_names(&path.join("src/compiler-flags"))
52+
| &collect_unstable_feature_names(&compiler_flags);
4353
let compiler_env_vars =
4454
collect_unstable_book_section_file_names(&path.join("src/compiler-environment-variables"));
4555

@@ -112,14 +122,47 @@ fn copy_recursive(from: &Path, to: &Path) {
112122
}
113123
}
114124

125+
fn collect_compiler_flags(rustc_path: impl AsRef<Path>) -> Features {
126+
let mut rustc = Command::new(rustc_path.as_ref());
127+
rustc.arg("-Zhelp");
128+
129+
let output = t!(rustc.output());
130+
let help_str = t!(String::from_utf8(output.stdout));
131+
let parts = help_str.split("\n -Z").collect::<Vec<_>>();
132+
133+
let mut features = Features::new();
134+
for part in parts.into_iter().skip(1) {
135+
let (name, description) =
136+
part.split_once("--").expect("name and description should be delimited by '--'");
137+
let name = name.trim().trim_end_matches("=val");
138+
let description = description.trim();
139+
140+
features.insert(
141+
name.replace('-', "_"),
142+
Feature {
143+
level: Status::Unstable,
144+
since: None,
145+
has_gate_test: false,
146+
tracking_issue: None,
147+
file: "".into(),
148+
line: 0,
149+
description: Some(description.to_owned()),
150+
},
151+
);
152+
}
153+
features
154+
}
155+
115156
fn main() {
116157
let library_path_str = env::args_os().nth(1).expect("library/ path required");
117158
let compiler_path_str = env::args_os().nth(2).expect("compiler/ path required");
118159
let src_path_str = env::args_os().nth(3).expect("src/ path required");
119-
let dest_path_str = env::args_os().nth(4).expect("destination path required");
160+
let rustc_path_str = env::args_os().nth(4).expect("rustc path required");
161+
let dest_path_str = env::args_os().nth(5).expect("destination path required");
120162
let library_path = Path::new(&library_path_str);
121163
let compiler_path = Path::new(&compiler_path_str);
122164
let src_path = Path::new(&src_path_str);
165+
let rustc_path = Path::new(&rustc_path_str);
123166
let dest_path = Path::new(&dest_path_str);
124167

125168
let lang_features = collect_lang_features(compiler_path, &mut false);
@@ -128,6 +171,7 @@ fn main() {
128171
.filter(|&(ref name, _)| !lang_features.contains_key(name))
129172
.collect();
130173
let env_vars = collect_env_vars(compiler_path);
174+
let compiler_flags = collect_compiler_flags(rustc_path);
131175

132176
let doc_src_path = src_path.join(PATH_STR);
133177

@@ -143,9 +187,14 @@ fn main() {
143187
&dest_path.join(LIB_FEATURES_DIR),
144188
&lib_features,
145189
);
190+
generate_feature_files(
191+
&doc_src_path.join(COMPILER_FLAGS_DIR),
192+
&dest_path.join(COMPILER_FLAGS_DIR),
193+
&compiler_flags,
194+
);
146195
generate_env_files(&doc_src_path.join(ENV_VARS_DIR), &dest_path.join(ENV_VARS_DIR), &env_vars);
147196

148197
copy_recursive(&doc_src_path, &dest_path);
149198

150-
generate_summary(&dest_path, &lang_features, &lib_features);
199+
generate_summary(&dest_path, &lang_features, &lib_features, &compiler_flags);
151200
}

0 commit comments

Comments
 (0)