Skip to content

Commit 5e048e0

Browse files
committed
Simplify current_dll_path for Cygwin
1 parent 12fda86 commit 5e048e0

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

compiler/rustc_session/src/filesearch.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf {
5858
sysroot.join(rustlib_path).join("bin")
5959
}
6060

61-
#[cfg(all(unix, not(target_os = "cygwin")))]
61+
#[cfg(unix)]
6262
fn current_dll_path() -> Result<PathBuf, String> {
6363
use std::sync::OnceLock;
6464

@@ -78,10 +78,16 @@ fn current_dll_path() -> Result<PathBuf, String> {
7878
if libc::dladdr(addr, &mut info) == 0 {
7979
return Err("dladdr failed".into());
8080
}
81-
if info.dli_fname.is_null() {
82-
return Err("dladdr returned null pointer".into());
83-
}
84-
let bytes = CStr::from_ptr(info.dli_fname).to_bytes();
81+
#[cfg(target_os = "cygwin")]
82+
let fname_ptr = info.dli_fname.as_ptr();
83+
#[cfg(not(target_os = "cygwin"))]
84+
let fname_ptr = {
85+
if info.dli_fname.is_null() {
86+
return Err("dladdr returned null pointer".into());
87+
}
88+
info.dli_fname
89+
};
90+
let bytes = CStr::from_ptr(fname_ptr).to_bytes();
8591
let os = OsStr::from_bytes(bytes);
8692
Ok(PathBuf::from(os))
8793
}
@@ -132,23 +138,6 @@ fn current_dll_path() -> Result<PathBuf, String> {
132138
.clone()
133139
}
134140

135-
#[cfg(target_os = "cygwin")]
136-
fn current_dll_path() -> Result<PathBuf, String> {
137-
use std::ffi::{CStr, OsStr};
138-
use std::os::unix::prelude::*;
139-
140-
unsafe {
141-
let addr = current_dll_path as usize as *mut _;
142-
let mut info = std::mem::zeroed();
143-
if libc::dladdr(addr, &mut info) == 0 {
144-
return Err("dladdr failed".into());
145-
}
146-
let bytes = CStr::from_ptr(info.dli_fname.as_ptr()).to_bytes();
147-
let os = OsStr::from_bytes(bytes);
148-
Ok(PathBuf::from(os))
149-
}
150-
}
151-
152141
#[cfg(windows)]
153142
fn current_dll_path() -> Result<PathBuf, String> {
154143
use std::ffi::OsString;

0 commit comments

Comments
 (0)