mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-30 22:12:15 +00:00
run_make_support: move path-related helpers into own module
This commit is contained in:
parent
483328d401
commit
dc9531533c
@ -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;
|
||||
|
@ -12,6 +12,7 @@ pub mod diff;
|
||||
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 @@ pub use artifact_names::{
|
||||
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: AsRef<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<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
|
||||
@ -108,12 +101,6 @@ pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(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<P: AsRef<Path>>(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,
|
||||
|
66
src/tools/run-make-support/src/path_helpers.rs
Normal file
66
src/tools/run-make-support/src/path_helpers.rs
Normal file
@ -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: AsRef<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<P: AsRef<Path>>(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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user