Skip to content

Commit 1393a87

Browse files
Rollup merge of #125022 - GuillaumeGomez:migrate-rustdoc-scrape-examples-ordering, r=jieyouxu
Migrate rustdoc scrape examples ordering Part of #121876. This one adds a lot of utility methods/functions. To prevent having too much changes at once, I didn't make the existing rmake tests use these yet but I'll send a follow-up so they all use it. r? `@jieyouxu`
2 parents 5087947 + 114e257 commit 1393a87

File tree

6 files changed

+102
-7
lines changed

6 files changed

+102
-7
lines changed

Diff for: src/tools/run-make-support/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ pub fn static_lib(name: &str) -> PathBuf {
5454
tmp_dir().join(static_lib_name(name))
5555
}
5656

57+
pub fn python_command() -> Command {
58+
let python_path = std::env::var("PYTHON").expect("PYTHON environment variable does not exist");
59+
Command::new(python_path)
60+
}
61+
62+
pub fn source_path() -> PathBuf {
63+
std::env::var("S").expect("S variable does not exist").into()
64+
}
65+
5766
/// Construct the static library name based on the platform.
5867
pub fn static_lib_name(name: &str) -> String {
5968
// See tools.mk (irrelevant lines omitted):

Diff for: src/tools/run-make-support/src/rustc.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::env;
2-
use std::ffi::OsString;
2+
use std::ffi::{OsStr, OsString};
33
use std::io::Write;
44
use std::path::Path;
55
use std::process::{Command, Output, Stdio};
@@ -176,6 +176,13 @@ impl Rustc {
176176
self
177177
}
178178

179+
/// Specify the crate name.
180+
pub fn crate_name<S: AsRef<OsStr>>(&mut self, name: S) -> &mut Self {
181+
self.cmd.arg("--crate-name");
182+
self.cmd.arg(name.as_ref());
183+
self
184+
}
185+
179186
/// Get the [`Output`][::std::process::Output] of the finished process.
180187
#[track_caller]
181188
pub fn command_output(&mut self) -> ::std::process::Output {

Diff for: src/tools/run-make-support/src/rustdoc.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::env;
2+
use std::ffi::OsStr;
23
use std::io::Write;
34
use std::path::Path;
45
use std::process::{Command, Output, Stdio};
@@ -45,6 +46,21 @@ impl Rustdoc {
4546
Self { cmd, stdin: None }
4647
}
4748

49+
/// Specify where an external library is located.
50+
pub fn extern_<P: AsRef<Path>>(&mut self, crate_name: &str, path: P) -> &mut Self {
51+
assert!(
52+
!crate_name.contains(|c: char| c.is_whitespace() || c == '\\' || c == '/'),
53+
"crate name cannot contain whitespace or path separators"
54+
);
55+
56+
let path = path.as_ref().to_string_lossy();
57+
58+
self.cmd.arg("--extern");
59+
self.cmd.arg(format!("{crate_name}={path}"));
60+
61+
self
62+
}
63+
4864
/// Specify path to the input file.
4965
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
5066
self.cmd.arg(path.as_ref());
@@ -107,6 +123,20 @@ impl Rustdoc {
107123
self
108124
}
109125

126+
/// Specify the crate type.
127+
pub fn crate_type(&mut self, crate_type: &str) -> &mut Self {
128+
self.cmd.arg("--crate-type");
129+
self.cmd.arg(crate_type);
130+
self
131+
}
132+
133+
/// Specify the crate name.
134+
pub fn crate_name<S: AsRef<OsStr>>(&mut self, name: S) -> &mut Self {
135+
self.cmd.arg("--crate-name");
136+
self.cmd.arg(name.as_ref());
137+
self
138+
}
139+
110140
#[track_caller]
111141
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
112142
let caller_location = std::panic::Location::caller();

Diff for: src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ run-make/rustdoc-io-error/Makefile
248248
run-make/rustdoc-scrape-examples-invalid-expr/Makefile
249249
run-make/rustdoc-scrape-examples-macros/Makefile
250250
run-make/rustdoc-scrape-examples-multiple/Makefile
251-
run-make/rustdoc-scrape-examples-ordering/Makefile
252251
run-make/rustdoc-scrape-examples-remap/Makefile
253252
run-make/rustdoc-scrape-examples-test/Makefile
254253
run-make/rustdoc-scrape-examples-whitespace/Makefile

Diff for: tests/run-make/rustdoc-scrape-examples-ordering/Makefile

-5
This file was deleted.
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use run_make_support::{python_command, rustc, rustdoc, source_path, tmp_dir};
2+
use std::fs::read_dir;
3+
use std::path::Path;
4+
5+
fn main() {
6+
let lib_dir = tmp_dir();
7+
let out_dir = tmp_dir().join("rustdoc");
8+
let crate_name = "foobar";
9+
let deps = read_dir("examples")
10+
.unwrap()
11+
.filter_map(|entry| entry.ok().map(|e| e.path()))
12+
.filter(|path| path.is_file() && path.extension().is_some_and(|ext| ext == "rs"))
13+
.collect::<Vec<_>>();
14+
15+
rustc().input("src/lib.rs").crate_name(crate_name).crate_type("lib").emit("metadata").run();
16+
17+
let mut out_deps = Vec::with_capacity(deps.len());
18+
for dep in deps {
19+
let dep_stem = dep.file_stem().unwrap();
20+
let out_example = out_dir.join(format!("{}.calls", dep_stem.to_str().unwrap()));
21+
rustdoc()
22+
.input(&dep)
23+
.crate_name(&dep_stem)
24+
.crate_type("bin")
25+
.output(&out_dir)
26+
.extern_(crate_name, lib_dir.join(format!("lib{crate_name}.rmeta")))
27+
.arg("-Zunstable-options")
28+
.arg("--scrape-examples-output-path")
29+
.arg(&out_example)
30+
.arg("--scrape-examples-target-crate")
31+
.arg(crate_name)
32+
.run();
33+
out_deps.push(out_example);
34+
}
35+
36+
let mut rustdoc = rustdoc();
37+
rustdoc
38+
.input("src/lib.rs")
39+
.output(&out_dir)
40+
.crate_name(crate_name)
41+
.crate_type("lib")
42+
.arg("-Zunstable-options");
43+
for dep in out_deps {
44+
rustdoc.arg("--with-examples").arg(dep);
45+
}
46+
rustdoc.run();
47+
48+
python_command()
49+
.arg(source_path().join("/src/etc/htmldocck.py"))
50+
.arg(out_dir)
51+
.arg("src/lib.rs")
52+
.status()
53+
.unwrap()
54+
.success();
55+
}

0 commit comments

Comments
 (0)