Skip to content

Commit e873ba2

Browse files
committed
docs: autogenerate compiler flag stubs based on -Zhelp
1 parent 5771665 commit e873ba2

File tree

2 files changed

+79
-9
lines changed
  • src
    • bootstrap/src/core/build_steps
    • tools/unstable-book-gen/src

2 files changed

+79
-9
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ book!(
8080

8181
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
8282
pub struct UnstableBook {
83+
build_compiler: Compiler,
8384
target: TargetSelection,
8485
}
8586

@@ -93,11 +94,24 @@ impl Step for UnstableBook {
9394
}
9495

9596
fn make_run(run: RunConfig<'_>) {
96-
run.builder.ensure(UnstableBook { target: run.target });
97+
// Bump the stage to 2, because the unstable book requires an in-tree compiler.
98+
// At the same time, since this step is enabled by default, we don't want `x doc` to fail
99+
// in stage 1.
100+
let stage = if run.builder.config.is_explicit_stage() || run.builder.top_stage >= 2 {
101+
run.builder.top_stage
102+
} else {
103+
2
104+
};
105+
106+
run.builder.ensure(UnstableBook {
107+
build_compiler: prepare_doc_compiler(run.builder, run.target, stage),
108+
target: run.target,
109+
});
97110
}
98111

99112
fn run(self, builder: &Builder<'_>) {
100-
builder.ensure(UnstableBookGen { target: self.target });
113+
builder
114+
.ensure(UnstableBookGen { build_compiler: self.build_compiler, target: self.target });
101115
builder.ensure(RustbookSrc {
102116
target: self.target,
103117
name: "unstable-book".to_owned(),
@@ -1175,6 +1189,7 @@ impl Step for ErrorIndex {
11751189

11761190
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
11771191
pub struct UnstableBookGen {
1192+
build_compiler: Compiler,
11781193
target: TargetSelection,
11791194
}
11801195

@@ -1189,11 +1204,15 @@ impl Step for UnstableBookGen {
11891204
}
11901205

11911206
fn make_run(run: RunConfig<'_>) {
1192-
run.builder.ensure(UnstableBookGen { target: run.target });
1207+
run.builder.ensure(UnstableBookGen {
1208+
build_compiler: prepare_doc_compiler(run.builder, run.target, run.builder.top_stage),
1209+
target: run.target,
1210+
});
11931211
}
11941212

11951213
fn run(self, builder: &Builder<'_>) {
11961214
let target = self.target;
1215+
let rustc_path = builder.rustc(self.build_compiler);
11971216

11981217
builder.info(&format!("Generating unstable book md files ({target})"));
11991218
let out = builder.md_doc_out(target).join("unstable-book");
@@ -1203,6 +1222,7 @@ impl Step for UnstableBookGen {
12031222
cmd.arg(builder.src.join("library"));
12041223
cmd.arg(builder.src.join("compiler"));
12051224
cmd.arg(builder.src.join("src"));
1225+
cmd.arg(rustc_path);
12061226
cmd.arg(out);
12071227

12081228
cmd.run(builder);

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

Lines changed: 56 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,48 @@ 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+
assert!(!parts[1..].is_empty(), "no -Z options were found");
133+
134+
let mut features = Features::new();
135+
for part in parts.into_iter().skip(1) {
136+
let (name, description) =
137+
part.split_once("--").expect("name and description should be delimited by '--'");
138+
let name = name.trim().trim_end_matches("=val");
139+
let description = description.trim();
140+
141+
features.insert(
142+
name.replace('-', "_"),
143+
Feature {
144+
level: Status::Unstable,
145+
since: None,
146+
has_gate_test: false,
147+
tracking_issue: None,
148+
file: "".into(),
149+
line: 0,
150+
description: Some(description.to_owned()),
151+
},
152+
);
153+
}
154+
features
155+
}
156+
115157
fn main() {
116158
let library_path_str = env::args_os().nth(1).expect("library/ path required");
117159
let compiler_path_str = env::args_os().nth(2).expect("compiler/ path required");
118160
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");
161+
let rustc_path_str = env::args_os().nth(4).expect("rustc path required");
162+
let dest_path_str = env::args_os().nth(5).expect("destination path required");
120163
let library_path = Path::new(&library_path_str);
121164
let compiler_path = Path::new(&compiler_path_str);
122165
let src_path = Path::new(&src_path_str);
166+
let rustc_path = Path::new(&rustc_path_str);
123167
let dest_path = Path::new(&dest_path_str);
124168

125169
let lang_features = collect_lang_features(compiler_path, &mut false);
@@ -128,6 +172,7 @@ fn main() {
128172
.filter(|&(ref name, _)| !lang_features.contains_key(name))
129173
.collect();
130174
let env_vars = collect_env_vars(compiler_path);
175+
let compiler_flags = collect_compiler_flags(rustc_path);
131176

132177
let doc_src_path = src_path.join(PATH_STR);
133178

@@ -143,9 +188,14 @@ fn main() {
143188
&dest_path.join(LIB_FEATURES_DIR),
144189
&lib_features,
145190
);
191+
generate_feature_files(
192+
&doc_src_path.join(COMPILER_FLAGS_DIR),
193+
&dest_path.join(COMPILER_FLAGS_DIR),
194+
&compiler_flags,
195+
);
146196
generate_env_files(&doc_src_path.join(ENV_VARS_DIR), &dest_path.join(ENV_VARS_DIR), &env_vars);
147197

148198
copy_recursive(&doc_src_path, &dest_path);
149199

150-
generate_summary(&dest_path, &lang_features, &lib_features);
200+
generate_summary(&dest_path, &lang_features, &lib_features, &compiler_flags);
151201
}

0 commit comments

Comments
 (0)