Simplify current_dll_path for Cygwin

This commit is contained in:
Berrysoft
2025-05-16 09:41:52 +08:00
parent 12fda865f2
commit 5e048e0786

View File

@@ -58,7 +58,7 @@ pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf {
sysroot.join(rustlib_path).join("bin")
}
#[cfg(all(unix, not(target_os = "cygwin")))]
#[cfg(unix)]
fn current_dll_path() -> Result<PathBuf, String> {
use std::sync::OnceLock;
@@ -78,10 +78,16 @@ fn current_dll_path() -> Result<PathBuf, String> {
if libc::dladdr(addr, &mut info) == 0 {
return Err("dladdr failed".into());
}
#[cfg(target_os = "cygwin")]
let fname_ptr = info.dli_fname.as_ptr();
#[cfg(not(target_os = "cygwin"))]
let fname_ptr = {
if info.dli_fname.is_null() {
return Err("dladdr returned null pointer".into());
}
let bytes = CStr::from_ptr(info.dli_fname).to_bytes();
info.dli_fname
};
let bytes = CStr::from_ptr(fname_ptr).to_bytes();
let os = OsStr::from_bytes(bytes);
Ok(PathBuf::from(os))
}
@@ -132,23 +138,6 @@ fn current_dll_path() -> Result<PathBuf, String> {
.clone()
}
#[cfg(target_os = "cygwin")]
fn current_dll_path() -> Result<PathBuf, String> {
use std::ffi::{CStr, OsStr};
use std::os::unix::prelude::*;
unsafe {
let addr = current_dll_path as usize as *mut _;
let mut info = std::mem::zeroed();
if libc::dladdr(addr, &mut info) == 0 {
return Err("dladdr failed".into());
}
let bytes = CStr::from_ptr(info.dli_fname.as_ptr()).to_bytes();
let os = OsStr::from_bytes(bytes);
Ok(PathBuf::from(os))
}
}
#[cfg(windows)]
fn current_dll_path() -> Result<PathBuf, String> {
use std::ffi::OsString;