Skip to content

Commit 67a5112

Browse files
Pass --target to lint docs
Otherwise, we may not have a standard library built for the native "host" target of the rustc being run.
1 parent 5fae569 commit 67a5112

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

src/bootstrap/doc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ impl Step for RustcBook {
752752
let out_listing = out_base.join("src/lints");
753753
builder.cp_r(&builder.src.join("src/doc/rustc"), &out_base);
754754
builder.info(&format!("Generating lint docs ({})", self.target));
755+
755756
let rustc = builder.rustc(self.compiler);
756757
// The tool runs `rustc` for extracting output examples, so it needs a
757758
// functional sysroot.
@@ -762,7 +763,8 @@ impl Step for RustcBook {
762763
cmd.arg("--out");
763764
cmd.arg(&out_listing);
764765
cmd.arg("--rustc");
765-
cmd.arg(rustc);
766+
cmd.arg(&rustc);
767+
cmd.arg("--rustc-target").arg(&self.target.rustc_target_arg());
766768
if builder.config.verbose() {
767769
cmd.arg("--verbose");
768770
}

src/tools/lint-docs/src/groups.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ static GROUP_DESCRIPTIONS: &[(&str, &str)] = &[
1818
/// Updates the documentation of lint groups.
1919
pub(crate) fn generate_group_docs(
2020
lints: &[Lint],
21-
rustc_path: &Path,
21+
rustc: crate::Rustc<'_>,
2222
out_path: &Path,
2323
) -> Result<(), Box<dyn Error>> {
24-
let groups = collect_groups(rustc_path)?;
24+
let groups = collect_groups(rustc)?;
2525
let groups_path = out_path.join("groups.md");
2626
let contents = fs::read_to_string(&groups_path)
2727
.map_err(|e| format!("could not read {}: {}", groups_path.display(), e))?;
@@ -36,9 +36,9 @@ pub(crate) fn generate_group_docs(
3636
type LintGroups = BTreeMap<String, BTreeSet<String>>;
3737

3838
/// Collects the group names from rustc.
39-
fn collect_groups(rustc: &Path) -> Result<LintGroups, Box<dyn Error>> {
39+
fn collect_groups(rustc: crate::Rustc<'_>) -> Result<LintGroups, Box<dyn Error>> {
4040
let mut result = BTreeMap::new();
41-
let mut cmd = Command::new(rustc);
41+
let mut cmd = Command::new(rustc.path);
4242
cmd.arg("-Whelp");
4343
let output = cmd.output().map_err(|e| format!("failed to run command {:?}\n{}", cmd, e))?;
4444
if !output.status.success() {

src/tools/lint-docs/src/lib.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,22 @@ impl Level {
4545
}
4646
}
4747

48+
#[derive(Copy, Clone)]
49+
pub struct Rustc<'a> {
50+
pub path: &'a Path,
51+
pub target: &'a str,
52+
}
53+
4854
/// Collects all lints, and writes the markdown documentation at the given directory.
4955
pub fn extract_lint_docs(
5056
src_path: &Path,
5157
out_path: &Path,
52-
rustc_path: &Path,
58+
rustc: Rustc<'_>,
5359
verbose: bool,
5460
) -> Result<(), Box<dyn Error>> {
5561
let mut lints = gather_lints(src_path)?;
5662
for lint in &mut lints {
57-
generate_output_example(lint, rustc_path, verbose).map_err(|e| {
63+
generate_output_example(lint, rustc, verbose).map_err(|e| {
5864
format!(
5965
"failed to test example in lint docs for `{}` in {}:{}: {}",
6066
lint.name,
@@ -65,7 +71,7 @@ pub fn extract_lint_docs(
6571
})?;
6672
}
6773
save_lints_markdown(&lints, &out_path.join("listing"))?;
68-
groups::generate_group_docs(&lints, rustc_path, out_path)?;
74+
groups::generate_group_docs(&lints, rustc, out_path)?;
6975
Ok(())
7076
}
7177

@@ -208,7 +214,7 @@ fn lint_name(line: &str) -> Result<String, &'static str> {
208214
/// actual output from the compiler.
209215
fn generate_output_example(
210216
lint: &mut Lint,
211-
rustc_path: &Path,
217+
rustc: Rustc<'_>,
212218
verbose: bool,
213219
) -> Result<(), Box<dyn Error>> {
214220
// Explicit list of lints that are allowed to not have an example. Please
@@ -230,7 +236,7 @@ fn generate_output_example(
230236
// separate test suite, and use an include mechanism such as mdbook's
231237
// `{{#rustdoc_include}}`.
232238
if !lint.is_ignored() {
233-
replace_produces(lint, rustc_path, verbose)?;
239+
replace_produces(lint, rustc, verbose)?;
234240
}
235241
Ok(())
236242
}
@@ -261,7 +267,7 @@ fn check_style(lint: &Lint) -> Result<(), Box<dyn Error>> {
261267
/// output from the compiler.
262268
fn replace_produces(
263269
lint: &mut Lint,
264-
rustc_path: &Path,
270+
rustc: Rustc<'_>,
265271
verbose: bool,
266272
) -> Result<(), Box<dyn Error>> {
267273
let mut lines = lint.doc.iter_mut();
@@ -302,7 +308,7 @@ fn replace_produces(
302308
Some(line) if line.is_empty() => {}
303309
Some(line) if line == "{{produces}}" => {
304310
let output =
305-
generate_lint_output(&lint.name, &example, &options, rustc_path, verbose)?;
311+
generate_lint_output(&lint.name, &example, &options, rustc, verbose)?;
306312
line.replace_range(
307313
..,
308314
&format!(
@@ -329,7 +335,7 @@ fn generate_lint_output(
329335
name: &str,
330336
example: &[&mut String],
331337
options: &[&str],
332-
rustc_path: &Path,
338+
rustc: Rustc<'_>,
333339
verbose: bool,
334340
) -> Result<String, Box<dyn Error>> {
335341
if verbose {
@@ -364,13 +370,14 @@ fn generate_lint_output(
364370
}
365371
fs::write(&tempfile, source)
366372
.map_err(|e| format!("failed to write {}: {}", tempfile.display(), e))?;
367-
let mut cmd = Command::new(rustc_path);
373+
let mut cmd = Command::new(rustc.path);
368374
if options.contains(&"edition2015") {
369375
cmd.arg("--edition=2015");
370376
} else {
371377
cmd.arg("--edition=2018");
372378
}
373379
cmd.arg("--error-format=json");
380+
cmd.arg("--target").arg(rustc.target);
374381
if options.contains(&"test") {
375382
cmd.arg("--test");
376383
}

src/tools/lint-docs/src/main.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn doit() -> Result<(), Box<dyn Error>> {
1313
let mut src_path = None;
1414
let mut out_path = None;
1515
let mut rustc_path = None;
16+
let mut rustc_target = None;
1617
let mut verbose = false;
1718
while let Some(arg) = args.next() {
1819
match arg.as_str() {
@@ -34,6 +35,12 @@ fn doit() -> Result<(), Box<dyn Error>> {
3435
None => return Err("--rustc requires a value".into()),
3536
};
3637
}
38+
"--rustc-target" => {
39+
rustc_target = match args.next() {
40+
Some(s) => Some(s),
41+
None => return Err("--rustc-target requires a value".into()),
42+
};
43+
}
3744
"-v" | "--verbose" => verbose = true,
3845
s => return Err(format!("unexpected argument `{}`", s).into()),
3946
}
@@ -47,10 +54,16 @@ fn doit() -> Result<(), Box<dyn Error>> {
4754
if rustc_path.is_none() {
4855
return Err("--rustc must be specified to the path of rustc".into());
4956
}
57+
if rustc_target.is_none() {
58+
return Err("--rustc-target must be specified to the rustc target".into());
59+
}
5060
lint_docs::extract_lint_docs(
5161
&src_path.unwrap(),
5262
&out_path.unwrap(),
53-
&rustc_path.unwrap(),
63+
lint_docs::Rustc {
64+
path: rustc_path.as_deref().unwrap(),
65+
target: rustc_target.as_deref().unwrap(),
66+
},
5467
verbose,
5568
)
5669
}

0 commit comments

Comments
 (0)