Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d00e71e

Browse files
authoredJun 15, 2024
Rollup merge of #126488 - ChrisDenton:absolute, r=albertlarsan68
Use `std::path::absolute` in bootstrap `std::path::absolute` is now stable in 1.79 so we can get rid of the copy-pasted version.
2 parents 4f9bf4c + 78d4802 commit d00e71e

File tree

3 files changed

+2
-132
lines changed

3 files changed

+2
-132
lines changed
 

‎src/bootstrap/src/core/config/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::env;
1010
use std::fmt::{self, Display};
1111
use std::fs;
1212
use std::io::IsTerminal;
13-
use std::path::{Path, PathBuf};
13+
use std::path::{absolute, Path, PathBuf};
1414
use std::process::Command;
1515
use std::str::FromStr;
1616
use std::sync::OnceLock;
@@ -1437,7 +1437,7 @@ impl Config {
14371437
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
14381438
if !config.out.is_absolute() {
14391439
// `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.
1440-
config.out = crate::utils::helpers::absolute(&config.out);
1440+
config.out = absolute(&config.out).expect("can't make empty path absolute");
14411441
}
14421442

14431443
config.initial_rustc = if let Some(rustc) = rustc {

‎src/bootstrap/src/utils/helpers.rs

-109
Original file line numberDiff line numberDiff line change
@@ -331,115 +331,6 @@ fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
331331
})
332332
}
333333

334-
/// Copied from `std::path::absolute` until it stabilizes.
335-
///
336-
/// FIXME: this shouldn't exist.
337-
pub(crate) fn absolute(path: &Path) -> PathBuf {
338-
if path.as_os_str().is_empty() {
339-
panic!("can't make empty path absolute");
340-
}
341-
#[cfg(unix)]
342-
{
343-
t!(absolute_unix(path), format!("could not make path absolute: {}", path.display()))
344-
}
345-
#[cfg(windows)]
346-
{
347-
t!(absolute_windows(path), format!("could not make path absolute: {}", path.display()))
348-
}
349-
#[cfg(not(any(unix, windows)))]
350-
{
351-
println!("WARNING: bootstrap is not supported on non-unix platforms");
352-
t!(std::fs::canonicalize(t!(std::env::current_dir()))).join(path)
353-
}
354-
}
355-
356-
#[cfg(unix)]
357-
/// Make a POSIX path absolute without changing its semantics.
358-
fn absolute_unix(path: &Path) -> io::Result<PathBuf> {
359-
// This is mostly a wrapper around collecting `Path::components`, with
360-
// exceptions made where this conflicts with the POSIX specification.
361-
// See 4.13 Pathname Resolution, IEEE Std 1003.1-2017
362-
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
363-
364-
use std::os::unix::prelude::OsStrExt;
365-
let mut components = path.components();
366-
let path_os = path.as_os_str().as_bytes();
367-
368-
let mut normalized = if path.is_absolute() {
369-
// "If a pathname begins with two successive <slash> characters, the
370-
// first component following the leading <slash> characters may be
371-
// interpreted in an implementation-defined manner, although more than
372-
// two leading <slash> characters shall be treated as a single <slash>
373-
// character."
374-
if path_os.starts_with(b"//") && !path_os.starts_with(b"///") {
375-
components.next();
376-
PathBuf::from("//")
377-
} else {
378-
PathBuf::new()
379-
}
380-
} else {
381-
env::current_dir()?
382-
};
383-
normalized.extend(components);
384-
385-
// "Interfaces using pathname resolution may specify additional constraints
386-
// when a pathname that does not name an existing directory contains at
387-
// least one non- <slash> character and contains one or more trailing
388-
// <slash> characters".
389-
// A trailing <slash> is also meaningful if "a symbolic link is
390-
// encountered during pathname resolution".
391-
392-
if path_os.ends_with(b"/") {
393-
normalized.push("");
394-
}
395-
396-
Ok(normalized)
397-
}
398-
399-
#[cfg(windows)]
400-
fn absolute_windows(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
401-
use std::ffi::OsString;
402-
use std::io::Error;
403-
use std::os::windows::ffi::{OsStrExt, OsStringExt};
404-
use std::ptr::null_mut;
405-
#[link(name = "kernel32")]
406-
extern "system" {
407-
fn GetFullPathNameW(
408-
lpFileName: *const u16,
409-
nBufferLength: u32,
410-
lpBuffer: *mut u16,
411-
lpFilePart: *mut *const u16,
412-
) -> u32;
413-
}
414-
415-
unsafe {
416-
// encode the path as UTF-16
417-
let path: Vec<u16> = path.as_os_str().encode_wide().chain([0]).collect();
418-
let mut buffer = Vec::new();
419-
// Loop until either success or failure.
420-
loop {
421-
// Try to get the absolute path
422-
let len = GetFullPathNameW(
423-
path.as_ptr(),
424-
buffer.len().try_into().unwrap(),
425-
buffer.as_mut_ptr(),
426-
null_mut(),
427-
);
428-
match len as usize {
429-
// Failure
430-
0 => return Err(Error::last_os_error()),
431-
// Buffer is too small, resize.
432-
len if len > buffer.len() => buffer.resize(len, 0),
433-
// Success!
434-
len => {
435-
buffer.truncate(len);
436-
return Ok(OsString::from_wide(&buffer).into());
437-
}
438-
}
439-
}
440-
}
441-
}
442-
443334
/// Adapted from <https://github.com/llvm/llvm-project/blob/782e91224601e461c019e0a4573bbccc6094fbcd/llvm/cmake/modules/HandleLLVMOptions.cmake#L1058-L1079>
444335
///
445336
/// When `clang-cl` is used with instrumentation, we need to add clang's runtime library resource

‎src/bootstrap/src/utils/helpers/tests.rs

-21
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,6 @@ fn test_make() {
2525
}
2626
}
2727

28-
#[cfg(unix)]
29-
#[test]
30-
fn test_absolute_unix() {
31-
use crate::utils::helpers::absolute_unix;
32-
33-
// Test an absolute path
34-
let path = PathBuf::from("/home/user/file.txt");
35-
assert_eq!(absolute_unix(&path).unwrap(), PathBuf::from("/home/user/file.txt"));
36-
37-
// Test an absolute path with double leading slashes
38-
let path = PathBuf::from("//root//file.txt");
39-
assert_eq!(absolute_unix(&path).unwrap(), PathBuf::from("//root/file.txt"));
40-
41-
// Test a relative path
42-
let path = PathBuf::from("relative/path");
43-
assert_eq!(
44-
absolute_unix(&path).unwrap(),
45-
std::env::current_dir().unwrap().join("relative/path")
46-
);
47-
}
48-
4928
#[test]
5029
fn test_beta_rev_parsing() {
5130
// single digit revision

0 commit comments

Comments
 (0)
Failed to load comments.