Skip to content

Commit 42a4c2d

Browse files
committed
docs: autogenerate compiler flag stubs based on -Zhelp
1 parent df8102f commit 42a4c2d

File tree

2 files changed

+64
-7
lines changed
  • src
    • bootstrap/src/core/build_steps
    • tools/unstable-book-gen/src

2 files changed

+64
-7
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,15 @@ impl Step for UnstableBookGen {
11211121
fn run(self, builder: &Builder<'_>) {
11221122
let target = self.target;
11231123

1124+
let stage = builder.top_stage;
1125+
let compiler = builder.compiler(stage, self.target);
1126+
let rustc_path = builder
1127+
.out
1128+
.join(compiler.host)
1129+
.join(format!("stage{}", stage))
1130+
.join("bin")
1131+
.join("rustc");
1132+
11241133
builder.info(&format!("Generating unstable book md files ({target})"));
11251134
let out = builder.md_doc_out(target).join("unstable-book");
11261135
builder.create_dir(&out);
@@ -1129,6 +1138,7 @@ impl Step for UnstableBookGen {
11291138
cmd.arg(builder.src.join("library"));
11301139
cmd.arg(builder.src.join("compiler"));
11311140
cmd.arg(builder.src.join("src"));
1141+
cmd.arg(rustc_path);
11321142
cmd.arg(out);
11331143

11341144
cmd.run(builder);

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

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ 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_lang_features, collect_lib_features};
9+
use tidy::features::{Feature, Features, Status, collect_lang_features, collect_lib_features};
910
use tidy::t;
1011
use tidy::unstable_book::{
11-
LANG_FEATURES_DIR, LIB_FEATURES_DIR, PATH_STR, collect_unstable_book_section_file_names,
12-
collect_unstable_feature_names,
12+
COMPILER_FLAGS_DIR, LANG_FEATURES_DIR, LIB_FEATURES_DIR, PATH_STR,
13+
collect_unstable_book_section_file_names, collect_unstable_feature_names,
1314
};
1415

1516
fn generate_stub_issue(path: &Path, name: &str, issue: u32, description: &str) {
@@ -33,8 +34,15 @@ fn set_to_summary_str(set: &BTreeSet<String>, dir: &str) -> String {
3334
.fold("".to_owned(), |s, a| s + &a + "\n")
3435
}
3536

36-
fn generate_summary(path: &Path, lang_features: &Features, lib_features: &Features) {
37-
let compiler_flags = collect_unstable_book_section_file_names(&path.join("src/compiler-flags"));
37+
fn generate_summary(
38+
path: &Path,
39+
lang_features: &Features,
40+
lib_features: &Features,
41+
compiler_flags: &Features,
42+
) {
43+
let compiler_flags =
44+
&collect_unstable_book_section_file_names(&path.join("src/compiler-flags"))
45+
| &collect_unstable_feature_names(&compiler_flags);
3846
let compiler_env_vars =
3947
collect_unstable_book_section_file_names(&path.join("src/compiler-environment-variables"));
4048

@@ -97,21 +105,55 @@ fn copy_recursive(from: &Path, to: &Path) {
97105
}
98106
}
99107

108+
fn collect_compiler_flags(rustc_path: impl AsRef<Path>) -> Features {
109+
let mut rustc = Command::new(rustc_path.as_ref());
110+
rustc.arg("-Zhelp");
111+
112+
let output = t!(rustc.output());
113+
let help_str = t!(String::from_utf8(output.stdout));
114+
let parts = help_str.split("\n -Z").collect::<Vec<_>>();
115+
116+
let mut features = Features::new();
117+
for part in parts.into_iter().skip(1) {
118+
let (name, description) =
119+
part.split_once("--").expect("name and description should be delimited by '--'");
120+
let name = name.trim().trim_end_matches("=val");
121+
let description = description.trim();
122+
123+
features.insert(
124+
name.replace('-', "_"),
125+
Feature {
126+
level: Status::Unstable,
127+
since: None,
128+
has_gate_test: false,
129+
tracking_issue: None,
130+
file: "".into(),
131+
line: 0,
132+
description: Some(description.to_owned()),
133+
},
134+
);
135+
}
136+
features
137+
}
138+
100139
fn main() {
101140
let library_path_str = env::args_os().nth(1).expect("library/ path required");
102141
let compiler_path_str = env::args_os().nth(2).expect("compiler/ path required");
103142
let src_path_str = env::args_os().nth(3).expect("src/ path required");
104-
let dest_path_str = env::args_os().nth(4).expect("destination path required");
143+
let rustc_path_str = env::args_os().nth(4).expect("rustc path required");
144+
let dest_path_str = env::args_os().nth(5).expect("destination path required");
105145
let library_path = Path::new(&library_path_str);
106146
let compiler_path = Path::new(&compiler_path_str);
107147
let src_path = Path::new(&src_path_str);
148+
let rustc_path = Path::new(&rustc_path_str);
108149
let dest_path = Path::new(&dest_path_str);
109150

110151
let lang_features = collect_lang_features(compiler_path, &mut false);
111152
let lib_features = collect_lib_features(library_path)
112153
.into_iter()
113154
.filter(|&(ref name, _)| !lang_features.contains_key(name))
114155
.collect();
156+
let compiler_flags = collect_compiler_flags(rustc_path);
115157

116158
let doc_src_path = src_path.join(PATH_STR);
117159

@@ -127,8 +169,13 @@ fn main() {
127169
&dest_path.join(LIB_FEATURES_DIR),
128170
&lib_features,
129171
);
172+
generate_unstable_book_files(
173+
&doc_src_path.join(COMPILER_FLAGS_DIR),
174+
&dest_path.join(COMPILER_FLAGS_DIR),
175+
&compiler_flags,
176+
);
130177

131178
copy_recursive(&doc_src_path, &dest_path);
132179

133-
generate_summary(&dest_path, &lang_features, &lib_features);
180+
generate_summary(&dest_path, &lang_features, &lib_features, &compiler_flags);
134181
}

0 commit comments

Comments
 (0)