Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1a92b11

Browse files
committed
run_make_support: move path-related helpers into own module
1 parent e1af0b8 commit 1a92b11

File tree

3 files changed

+75
-34
lines changed

3 files changed

+75
-34
lines changed

src/tools/run-make-support/src/external_deps/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//! This module contains external tool dependencies that we assume are available in the environment,
22
//! such as `cc` or `python`.
3+
//!
4+
//! # Notes
5+
//!
6+
//! - This is not the *only* place where external dependencies are assumed or referenced. For
7+
//! example, see [`cygpath_windows`][crate::path_helpers::cygpath_windows].
38
49
pub mod cc;
510
pub mod clang;

src/tools/run-make-support/src/lib.rs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod diff;
1212
pub mod env_checked;
1313
pub mod external_deps;
1414
pub mod fs_wrapper;
15+
pub mod path_helpers;
1516
pub mod run;
1617
pub mod targets;
1718

@@ -67,18 +68,10 @@ pub use artifact_names::{
6768
bin_name, dynamic_lib_extension, dynamic_lib_name, rust_lib_name, static_lib_name,
6869
};
6970

70-
use command::{Command, CompletedProcess};
71-
72-
/// Returns the path for a local test file.
73-
pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
74-
cwd().join(p.as_ref())
75-
}
71+
/// Path-related helpers.
72+
pub use path_helpers::{cwd, cygpath_windows, path, source_root};
7673

77-
/// Path to the root rust-lang/rust source checkout.
78-
#[must_use]
79-
pub fn source_root() -> PathBuf {
80-
env_var("SOURCE_ROOT").into()
81-
}
74+
use command::{Command, CompletedProcess};
8275

8376
/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
8477
#[cfg(target_family = "windows")]
@@ -108,12 +101,6 @@ pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
108101
));
109102
}
110103

111-
/// Return the current working directory.
112-
#[must_use]
113-
pub fn cwd() -> PathBuf {
114-
std::env::current_dir().unwrap()
115-
}
116-
117104
// FIXME(Oneirical): This will no longer be required after compiletest receives the ability
118105
// to manipulate read-only files. See https://github.com/rust-lang/rust/issues/126334
119106
/// Ensure that the path P is read-only while the test runs, and restore original permissions
@@ -207,23 +194,6 @@ pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str)
207194
count
208195
}
209196

210-
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
211-
/// available on the platform!
212-
#[track_caller]
213-
#[must_use]
214-
pub fn cygpath_windows<P: AsRef<Path>>(path: P) -> String {
215-
let caller = panic::Location::caller();
216-
let mut cygpath = Command::new("cygpath");
217-
cygpath.arg("-w");
218-
cygpath.arg(path.as_ref());
219-
let output = cygpath.run();
220-
if !output.status().success() {
221-
handle_failed_output(&cygpath, output, caller.line());
222-
}
223-
// cygpath -w can attach a newline
224-
output.stdout_utf8().trim().to_string()
225-
}
226-
227197
pub(crate) fn handle_failed_output(
228198
cmd: &Command,
229199
output: CompletedProcess,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Collection of path-related helpers.
2+
3+
use std::panic;
4+
use std::path::{Path, PathBuf};
5+
6+
use crate::command::Command;
7+
use crate::env_checked::env_var;
8+
use crate::handle_failed_output;
9+
10+
/// Return the current working directory.
11+
///
12+
/// This forwards to [`std::env::current_dir`], please see its docs regarding platform-specific
13+
/// behavior.
14+
#[must_use]
15+
pub fn cwd() -> PathBuf {
16+
std::env::current_dir().unwrap()
17+
}
18+
19+
/// Construct a `PathBuf` relative to the current working directory by joining `cwd()` with the
20+
/// relative path. This is mostly a convenience helper so the test writer does not need to write
21+
/// `PathBuf::from(path_like_string)`.
22+
///
23+
/// # Example
24+
///
25+
/// ```rust
26+
/// let p = path("support_file.txt");
27+
/// ```
28+
pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
29+
cwd().join(p.as_ref())
30+
}
31+
32+
/// Path to the root `rust-lang/rust` source checkout.
33+
#[must_use]
34+
pub fn source_root() -> PathBuf {
35+
env_var("SOURCE_ROOT").into()
36+
}
37+
38+
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
39+
/// available on the platform!
40+
///
41+
/// # FIXME
42+
///
43+
/// FIXME(jieyouxu): we should consider not depending on `cygpath`.
44+
///
45+
/// > The cygpath program is a utility that converts Windows native filenames to Cygwin POSIX-style
46+
/// > pathnames and vice versa.
47+
/// >
48+
/// > [irrelevant entries omitted...]
49+
/// >
50+
/// > `-w, --windows print Windows form of NAMEs (C:\WINNT)`
51+
/// >
52+
/// > -- *from [cygpath documentation](https://cygwin.com/cygwin-ug-net/cygpath.html)*.
53+
#[track_caller]
54+
#[must_use]
55+
pub fn cygpath_windows<P: AsRef<Path>>(path: P) -> String {
56+
let caller = panic::Location::caller();
57+
let mut cygpath = Command::new("cygpath");
58+
cygpath.arg("-w");
59+
cygpath.arg(path.as_ref());
60+
let output = cygpath.run();
61+
if !output.status().success() {
62+
handle_failed_output(&cygpath, output, caller.line());
63+
}
64+
// cygpath -w can attach a newline
65+
output.stdout_utf8().trim().to_string()
66+
}

0 commit comments

Comments
 (0)