From dc9531533c50ade680b98b3ed7e4e1a2f408b474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Mon, 15 Jul 2024 10:50:28 +0000 Subject: [PATCH] run_make_support: move path-related helpers into own module --- .../run-make-support/src/external_deps/mod.rs | 5 ++ src/tools/run-make-support/src/lib.rs | 38 ++--------- .../run-make-support/src/path_helpers.rs | 66 +++++++++++++++++++ 3 files changed, 75 insertions(+), 34 deletions(-) create mode 100644 src/tools/run-make-support/src/path_helpers.rs diff --git a/src/tools/run-make-support/src/external_deps/mod.rs b/src/tools/run-make-support/src/external_deps/mod.rs index f4f03aa7efb..bcd7dce0f96 100644 --- a/src/tools/run-make-support/src/external_deps/mod.rs +++ b/src/tools/run-make-support/src/external_deps/mod.rs @@ -1,5 +1,10 @@ //! This module contains external tool dependencies that we assume are available in the environment, //! such as `cc` or `python`. +//! +//! # Notes +//! +//! - This is not the *only* place where external dependencies are assumed or referenced. For +//! example, see [`cygpath_windows`][crate::path_helpers::cygpath_windows]. pub mod cc; pub mod clang; diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 5ca15b88530..5ec466af6ce 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -12,6 +12,7 @@ pub mod env_checked; pub mod external_deps; pub mod fs_wrapper; +pub mod path_helpers; pub mod run; pub mod targets; @@ -67,19 +68,11 @@ bin_name, dynamic_lib_extension, dynamic_lib_name, rust_lib_name, static_lib_name, }; +/// Path-related helpers. +pub use path_helpers::{cwd, cygpath_windows, path, source_root}; + use command::{Command, CompletedProcess}; -/// Returns the path for a local test file. -pub fn path>(p: P) -> PathBuf { - cwd().join(p.as_ref()) -} - -/// Path to the root rust-lang/rust source checkout. -#[must_use] -pub fn source_root() -> PathBuf { - env_var("SOURCE_ROOT").into() -} - /// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix. #[cfg(target_family = "windows")] pub fn create_symlink, Q: AsRef>(original: P, link: Q) { @@ -108,12 +101,6 @@ pub fn create_symlink, Q: AsRef>(original: P, link: Q) { )); } -/// Return the current working directory. -#[must_use] -pub fn cwd() -> PathBuf { - std::env::current_dir().unwrap() -} - // FIXME(Oneirical): This will no longer be required after compiletest receives the ability // to manipulate read-only files. See https://github.com/rust-lang/rust/issues/126334 /// Ensure that the path P is read-only while the test runs, and restore original permissions @@ -227,23 +214,6 @@ pub fn count_regex_matches_in_files_with_extension(re: ®ex::Regex, ext: &str) count } -/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is -/// available on the platform! -#[track_caller] -#[must_use] -pub fn cygpath_windows>(path: P) -> String { - let caller = panic::Location::caller(); - let mut cygpath = Command::new("cygpath"); - cygpath.arg("-w"); - cygpath.arg(path.as_ref()); - let output = cygpath.run(); - if !output.status().success() { - handle_failed_output(&cygpath, output, caller.line()); - } - // cygpath -w can attach a newline - output.stdout_utf8().trim().to_string() -} - pub(crate) fn handle_failed_output( cmd: &Command, output: CompletedProcess, diff --git a/src/tools/run-make-support/src/path_helpers.rs b/src/tools/run-make-support/src/path_helpers.rs new file mode 100644 index 00000000000..59e0cec0be4 --- /dev/null +++ b/src/tools/run-make-support/src/path_helpers.rs @@ -0,0 +1,66 @@ +//! Collection of path-related helpers. + +use std::panic; +use std::path::{Path, PathBuf}; + +use crate::command::Command; +use crate::env_checked::env_var; +use crate::handle_failed_output; + +/// Return the current working directory. +/// +/// This forwards to [`std::env::current_dir`], please see its docs regarding platform-specific +/// behavior. +#[must_use] +pub fn cwd() -> PathBuf { + std::env::current_dir().unwrap() +} + +/// Construct a `PathBuf` relative to the current working directory by joining `cwd()` with the +/// relative path. This is mostly a convenience helper so the test writer does not need to write +/// `PathBuf::from(path_like_string)`. +/// +/// # Example +/// +/// ```rust +/// let p = path("support_file.txt"); +/// ``` +pub fn path>(p: P) -> PathBuf { + cwd().join(p.as_ref()) +} + +/// Path to the root `rust-lang/rust` source checkout. +#[must_use] +pub fn source_root() -> PathBuf { + env_var("SOURCE_ROOT").into() +} + +/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is +/// available on the platform! +/// +/// # FIXME +/// +/// FIXME(jieyouxu): we should consider not depending on `cygpath`. +/// +/// > The cygpath program is a utility that converts Windows native filenames to Cygwin POSIX-style +/// > pathnames and vice versa. +/// > +/// > [irrelevant entries omitted...] +/// > +/// > `-w, --windows print Windows form of NAMEs (C:\WINNT)` +/// > +/// > -- *from [cygpath documentation](https://cygwin.com/cygwin-ug-net/cygpath.html)*. +#[track_caller] +#[must_use] +pub fn cygpath_windows>(path: P) -> String { + let caller = panic::Location::caller(); + let mut cygpath = Command::new("cygpath"); + cygpath.arg("-w"); + cygpath.arg(path.as_ref()); + let output = cygpath.run(); + if !output.status().success() { + handle_failed_output(&cygpath, output, caller.line()); + } + // cygpath -w can attach a newline + output.stdout_utf8().trim().to_string() +}