rustc_session/
filesearch.rs1use std::path::{Path, PathBuf};
4use std::{env, fs};
5
6use rustc_fs_util::try_canonicalize;
7use rustc_target::spec::Target;
8use smallvec::{SmallVec, smallvec};
9
10use crate::search_paths::{PathKind, SearchPath};
11
12#[derive(Clone)]
13pub struct FileSearch {
14 cli_search_paths: Vec<SearchPath>,
15 tlib_path: SearchPath,
16}
17
18impl FileSearch {
19 pub fn cli_search_paths<'b>(&'b self, kind: PathKind) -> impl Iterator<Item = &'b SearchPath> {
20 self.cli_search_paths.iter().filter(move |sp| sp.kind.matches(kind))
21 }
22
23 pub fn search_paths<'b>(&'b self, kind: PathKind) -> impl Iterator<Item = &'b SearchPath> {
24 self.cli_search_paths
25 .iter()
26 .filter(move |sp| sp.kind.matches(kind))
27 .chain(std::iter::once(&self.tlib_path))
28 }
29
30 pub fn new(cli_search_paths: &[SearchPath], tlib_path: &SearchPath, target: &Target) -> Self {
31 let this = FileSearch {
32 cli_search_paths: cli_search_paths.to_owned(),
33 tlib_path: tlib_path.clone(),
34 };
35 this.refine(&["lib", &target.staticlib_prefix, &target.dll_prefix])
36 }
37 fn refine(mut self, allowed_prefixes: &[&str]) -> FileSearch {
39 self.cli_search_paths
40 .iter_mut()
41 .for_each(|search_paths| search_paths.files.retain(allowed_prefixes));
42 self.tlib_path.files.retain(allowed_prefixes);
43
44 self
45 }
46}
47
48pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
49 let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
50 sysroot.join(rustlib_path).join("lib")
51}
52
53pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf {
57 let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
58 sysroot.join(rustlib_path).join("bin")
59}
60
61#[cfg(unix)]
62fn current_dll_path() -> Result<PathBuf, String> {
63 use std::sync::OnceLock;
64
65 static CURRENT_DLL_PATH: OnceLock<Result<PathBuf, String>> = OnceLock::new();
69 CURRENT_DLL_PATH
70 .get_or_init(|| {
71 use std::ffi::{CStr, OsStr};
72 use std::os::unix::prelude::*;
73
74 #[cfg(not(target_os = "aix"))]
75 unsafe {
76 let addr = current_dll_path as usize as *mut _;
77 let mut info = std::mem::zeroed();
78 if libc::dladdr(addr, &mut info) == 0 {
79 return Err("dladdr failed".into());
80 }
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 assert!(!info.dli_fname.is_null(), "dli_fname cannot be null");
86 info.dli_fname
87 };
88 let bytes = CStr::from_ptr(fname_ptr).to_bytes();
89 let os = OsStr::from_bytes(bytes);
90 try_canonicalize(Path::new(os)).map_err(|e| e.to_string())
91 }
92
93 #[cfg(target_os = "aix")]
94 unsafe {
95 let addr = current_dll_path as u64;
102 let mut buffer = vec![std::mem::zeroed::<libc::ld_info>(); 64];
103 loop {
104 if libc::loadquery(
105 libc::L_GETINFO,
106 buffer.as_mut_ptr() as *mut u8,
107 (size_of::<libc::ld_info>() * buffer.len()) as u32,
108 ) >= 0
109 {
110 break;
111 } else {
112 if std::io::Error::last_os_error().raw_os_error().unwrap() != libc::ENOMEM {
113 return Err("loadquery failed".into());
114 }
115 buffer.resize(buffer.len() * 2, std::mem::zeroed::<libc::ld_info>());
116 }
117 }
118 let mut current = buffer.as_mut_ptr() as *mut libc::ld_info;
119 loop {
120 let data_base = (*current).ldinfo_dataorg as u64;
121 let data_end = data_base + (*current).ldinfo_datasize;
122 if (data_base..data_end).contains(&addr) {
123 let bytes = CStr::from_ptr(&(*current).ldinfo_filename[0]).to_bytes();
124 let os = OsStr::from_bytes(bytes);
125 return try_canonicalize(Path::new(os)).map_err(|e| e.to_string());
126 }
127 if (*current).ldinfo_next == 0 {
128 break;
129 }
130 current = (current as *mut i8).offset((*current).ldinfo_next as isize)
131 as *mut libc::ld_info;
132 }
133 return Err(format!("current dll's address {} is not in the load map", addr));
134 }
135 })
136 .clone()
137}
138
139#[cfg(windows)]
140fn current_dll_path() -> Result<PathBuf, String> {
141 use std::ffi::OsString;
142 use std::io;
143 use std::os::windows::prelude::*;
144
145 use windows::Win32::Foundation::HMODULE;
146 use windows::Win32::System::LibraryLoader::{
147 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, GetModuleFileNameW, GetModuleHandleExW,
148 };
149 use windows::core::PCWSTR;
150
151 let mut module = HMODULE::default();
152 unsafe {
153 GetModuleHandleExW(
154 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
155 PCWSTR(current_dll_path as *mut u16),
156 &mut module,
157 )
158 }
159 .map_err(|e| e.to_string())?;
160
161 let mut filename = vec![0; 1024];
162 let n = unsafe { GetModuleFileNameW(Some(module), &mut filename) } as usize;
163 if n == 0 {
164 return Err(format!("GetModuleFileNameW failed: {}", io::Error::last_os_error()));
165 }
166 if n >= filename.capacity() {
167 return Err(format!("our buffer was too small? {}", io::Error::last_os_error()));
168 }
169
170 filename.truncate(n);
171
172 let path = try_canonicalize(OsString::from_wide(&filename)).map_err(|e| e.to_string())?;
173
174 Ok(rustc_fs_util::fix_windows_verbatim_for_gcc(&path))
178}
179
180#[cfg(target_os = "wasi")]
181fn current_dll_path() -> Result<PathBuf, String> {
182 Err("current_dll_path is not supported on WASI".to_string())
183}
184
185pub fn sysroot_with_fallback(sysroot: &Path) -> SmallVec<[PathBuf; 2]> {
186 let mut candidates = smallvec![sysroot.to_owned()];
187 let default_sysroot = get_or_default_sysroot();
188 if default_sysroot != sysroot {
189 candidates.push(default_sysroot);
190 }
191 candidates
192}
193
194pub fn materialize_sysroot(maybe_sysroot: Option<PathBuf>) -> PathBuf {
197 maybe_sysroot.unwrap_or_else(|| get_or_default_sysroot())
198}
199
200pub fn get_or_default_sysroot() -> PathBuf {
203 fn default_from_rustc_driver_dll() -> Result<PathBuf, String> {
204 let dll = current_dll_path()?;
205
206 let dir = dll.parent().and_then(|p| p.parent()).ok_or_else(|| {
213 format!("Could not move 2 levels upper using `parent()` on {}", dll.display())
214 })?;
215
216 let mut sysroot_dir = if dir.ends_with(crate::config::host_tuple()) {
218 dir.parent() .and_then(|p| p.parent()) .and_then(|p| p.parent()) .map(|s| s.to_owned())
222 .ok_or_else(|| {
223 format!("Could not move 3 levels upper using `parent()` on {}", dir.display())
224 })?
225 } else {
226 dir.to_owned()
227 };
228
229 if sysroot_dir.ends_with("lib") {
233 sysroot_dir =
234 sysroot_dir.parent().map(|real_sysroot| real_sysroot.to_owned()).ok_or_else(
235 || format!("Could not move to parent path of {}", sysroot_dir.display()),
236 )?
237 }
238
239 Ok(sysroot_dir)
240 }
241
242 fn from_env_args_next() -> Option<PathBuf> {
247 let mut p = PathBuf::from(env::args_os().next()?);
248
249 if fs::read_link(&p).is_err() {
254 return None;
256 }
257
258 p.pop();
260 p.pop();
261 let mut rustlib_path = rustc_target::relative_target_rustlib_path(&p, "dummy");
263 rustlib_path.pop(); rustlib_path.exists().then_some(p)
265 }
266
267 from_env_args_next()
268 .unwrap_or_else(|| default_from_rustc_driver_dll().expect("Failed finding sysroot"))
269}