From 23e965eb68f5e903cbcb3bf86ca3431e4c272586 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 11 May 2024 20:31:07 +0200 Subject: [PATCH 1/5] Add `crate_type` method to `Rustdoc` --- src/tools/run-make-support/src/rustdoc.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tools/run-make-support/src/rustdoc.rs b/src/tools/run-make-support/src/rustdoc.rs index aa3c7dcf0e3..dcd5b5a93e1 100644 --- a/src/tools/run-make-support/src/rustdoc.rs +++ b/src/tools/run-make-support/src/rustdoc.rs @@ -107,6 +107,13 @@ pub fn edition(&mut self, edition: &str) -> &mut Self { self } + /// Specify the crate type. + pub fn crate_type(&mut self, crate_type: &str) -> &mut Self { + self.cmd.arg("--crate-type"); + self.cmd.arg(crate_type); + self + } + #[track_caller] pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output { let caller_location = std::panic::Location::caller(); From 4a00debfefda0ea72735252e098bd303df6a3ff3 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 11 May 2024 20:32:19 +0200 Subject: [PATCH 2/5] Add `crate_name` method to `Rustdoc` and `Rustc` --- src/tools/run-make-support/src/rustc.rs | 7 +++++++ src/tools/run-make-support/src/rustdoc.rs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs index de773d688ef..852353575a9 100644 --- a/src/tools/run-make-support/src/rustc.rs +++ b/src/tools/run-make-support/src/rustc.rs @@ -176,6 +176,13 @@ pub fn stdin>(&mut self, input: I) -> &mut Self { self } + /// Specify the crate name. + pub fn crate_name(&mut self, name: &str) -> &mut Self { + self.cmd.arg("--crate-name"); + self.cmd.arg(name); + self + } + /// Get the [`Output`][::std::process::Output] of the finished process. #[track_caller] pub fn command_output(&mut self) -> ::std::process::Output { diff --git a/src/tools/run-make-support/src/rustdoc.rs b/src/tools/run-make-support/src/rustdoc.rs index dcd5b5a93e1..df91742f4e2 100644 --- a/src/tools/run-make-support/src/rustdoc.rs +++ b/src/tools/run-make-support/src/rustdoc.rs @@ -114,6 +114,13 @@ pub fn crate_type(&mut self, crate_type: &str) -> &mut Self { self } + /// Specify the crate name. + pub fn crate_name(&mut self, name: &str) -> &mut Self { + self.cmd.arg("--crate-name"); + self.cmd.arg(name); + self + } + #[track_caller] pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output { let caller_location = std::panic::Location::caller(); From 0761802e50e3f11ef4cc170f63659f895bc25d42 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 11 May 2024 20:51:18 +0200 Subject: [PATCH 3/5] Add `python_command` and `source_path` functions --- src/tools/run-make-support/src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 9888ca2d1ed..cc81d23a8ff 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -54,6 +54,15 @@ pub fn static_lib(name: &str) -> PathBuf { tmp_dir().join(static_lib_name(name)) } +pub fn python_command() -> Command { + let python_path = std::env::var("PYTHON").expect("PYTHON environment variable does not exist"); + Command::new(python_path) +} + +pub fn source_path() -> PathBuf { + std::env::var("S").expect("S variable does not exist").into() +} + /// Construct the static library name based on the platform. pub fn static_lib_name(name: &str) -> String { // See tools.mk (irrelevant lines omitted): From 0712ae865f6658c5c36b5ccf347309a03742441d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 11 May 2024 20:52:10 +0200 Subject: [PATCH 4/5] Add `extern_` method to `Rustdoc` --- src/tools/run-make-support/src/rustdoc.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/tools/run-make-support/src/rustdoc.rs b/src/tools/run-make-support/src/rustdoc.rs index df91742f4e2..6a65d9616fc 100644 --- a/src/tools/run-make-support/src/rustdoc.rs +++ b/src/tools/run-make-support/src/rustdoc.rs @@ -45,6 +45,21 @@ pub fn new() -> Self { Self { cmd, stdin: None } } + /// Specify where an external library is located. + pub fn extern_>(&mut self, crate_name: &str, path: P) -> &mut Self { + assert!( + !crate_name.contains(|c: char| c.is_whitespace() || c == '\\' || c == '/'), + "crate name cannot contain whitespace or path separators" + ); + + let path = path.as_ref().to_string_lossy(); + + self.cmd.arg("--extern"); + self.cmd.arg(format!("{crate_name}={path}")); + + self + } + /// Specify path to the input file. pub fn input>(&mut self, path: P) -> &mut Self { self.cmd.arg(path.as_ref()); From 114e25761ff1cf1ee75aa85cabc197a5095cf484 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 11 May 2024 20:27:50 +0200 Subject: [PATCH 5/5] Migrate `rustdoc-scrape-examples-ordering` to `rmake` --- src/tools/run-make-support/src/rustc.rs | 6 +- src/tools/run-make-support/src/rustdoc.rs | 5 +- .../tidy/src/allowed_run_make_makefiles.txt | 1 - .../rustdoc-scrape-examples-ordering/Makefile | 5 -- .../rustdoc-scrape-examples-ordering/rmake.rs | 55 +++++++++++++++++++ 5 files changed, 61 insertions(+), 11 deletions(-) delete mode 100644 tests/run-make/rustdoc-scrape-examples-ordering/Makefile create mode 100644 tests/run-make/rustdoc-scrape-examples-ordering/rmake.rs diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs index 852353575a9..1671a01860a 100644 --- a/src/tools/run-make-support/src/rustc.rs +++ b/src/tools/run-make-support/src/rustc.rs @@ -1,5 +1,5 @@ use std::env; -use std::ffi::OsString; +use std::ffi::{OsStr, OsString}; use std::io::Write; use std::path::Path; use std::process::{Command, Output, Stdio}; @@ -177,9 +177,9 @@ pub fn stdin>(&mut self, input: I) -> &mut Self { } /// Specify the crate name. - pub fn crate_name(&mut self, name: &str) -> &mut Self { + pub fn crate_name>(&mut self, name: S) -> &mut Self { self.cmd.arg("--crate-name"); - self.cmd.arg(name); + self.cmd.arg(name.as_ref()); self } diff --git a/src/tools/run-make-support/src/rustdoc.rs b/src/tools/run-make-support/src/rustdoc.rs index 6a65d9616fc..75ca1fc2974 100644 --- a/src/tools/run-make-support/src/rustdoc.rs +++ b/src/tools/run-make-support/src/rustdoc.rs @@ -1,4 +1,5 @@ use std::env; +use std::ffi::OsStr; use std::io::Write; use std::path::Path; use std::process::{Command, Output, Stdio}; @@ -130,9 +131,9 @@ pub fn crate_type(&mut self, crate_type: &str) -> &mut Self { } /// Specify the crate name. - pub fn crate_name(&mut self, name: &str) -> &mut Self { + pub fn crate_name>(&mut self, name: S) -> &mut Self { self.cmd.arg("--crate-name"); - self.cmd.arg(name); + self.cmd.arg(name.as_ref()); self } diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index e87950b36d9..008fa5ec707 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -248,7 +248,6 @@ run-make/rustdoc-io-error/Makefile run-make/rustdoc-scrape-examples-invalid-expr/Makefile run-make/rustdoc-scrape-examples-macros/Makefile run-make/rustdoc-scrape-examples-multiple/Makefile -run-make/rustdoc-scrape-examples-ordering/Makefile run-make/rustdoc-scrape-examples-remap/Makefile run-make/rustdoc-scrape-examples-test/Makefile run-make/rustdoc-scrape-examples-whitespace/Makefile diff --git a/tests/run-make/rustdoc-scrape-examples-ordering/Makefile b/tests/run-make/rustdoc-scrape-examples-ordering/Makefile deleted file mode 100644 index bf45b8148c0..00000000000 --- a/tests/run-make/rustdoc-scrape-examples-ordering/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -deps := ex1 ex2 - -include ../rustdoc-scrape-examples-multiple/scrape.mk - -all: scrape diff --git a/tests/run-make/rustdoc-scrape-examples-ordering/rmake.rs b/tests/run-make/rustdoc-scrape-examples-ordering/rmake.rs new file mode 100644 index 00000000000..edcf3406d47 --- /dev/null +++ b/tests/run-make/rustdoc-scrape-examples-ordering/rmake.rs @@ -0,0 +1,55 @@ +use run_make_support::{python_command, rustc, rustdoc, source_path, tmp_dir}; +use std::fs::read_dir; +use std::path::Path; + +fn main() { + let lib_dir = tmp_dir(); + let out_dir = tmp_dir().join("rustdoc"); + let crate_name = "foobar"; + let deps = read_dir("examples") + .unwrap() + .filter_map(|entry| entry.ok().map(|e| e.path())) + .filter(|path| path.is_file() && path.extension().is_some_and(|ext| ext == "rs")) + .collect::>(); + + rustc().input("src/lib.rs").crate_name(crate_name).crate_type("lib").emit("metadata").run(); + + let mut out_deps = Vec::with_capacity(deps.len()); + for dep in deps { + let dep_stem = dep.file_stem().unwrap(); + let out_example = out_dir.join(format!("{}.calls", dep_stem.to_str().unwrap())); + rustdoc() + .input(&dep) + .crate_name(&dep_stem) + .crate_type("bin") + .output(&out_dir) + .extern_(crate_name, lib_dir.join(format!("lib{crate_name}.rmeta"))) + .arg("-Zunstable-options") + .arg("--scrape-examples-output-path") + .arg(&out_example) + .arg("--scrape-examples-target-crate") + .arg(crate_name) + .run(); + out_deps.push(out_example); + } + + let mut rustdoc = rustdoc(); + rustdoc + .input("src/lib.rs") + .output(&out_dir) + .crate_name(crate_name) + .crate_type("lib") + .arg("-Zunstable-options"); + for dep in out_deps { + rustdoc.arg("--with-examples").arg(dep); + } + rustdoc.run(); + + python_command() + .arg(source_path().join("/src/etc/htmldocck.py")) + .arg(out_dir) + .arg("src/lib.rs") + .status() + .unwrap() + .success(); +}