2021-08-31 16:31:29 +00:00
|
|
|
//! A module for searching for libraries
|
|
|
|
|
2022-10-28 07:20:51 +00:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2015-01-27 20:20:58 +00:00
|
|
|
use std::env;
|
2015-02-27 05:00:43 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::path::{Path, PathBuf};
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2022-02-01 05:32:13 +00:00
|
|
|
use crate::search_paths::{PathKind, SearchPath};
|
2018-08-03 21:31:03 +00:00
|
|
|
use rustc_fs_util::fix_windows_verbatim_for_gcc;
|
2014-04-08 17:15:46 +00:00
|
|
|
|
2015-03-30 13:38:44 +00:00
|
|
|
#[derive(Copy, Clone)]
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-06 01:01:33 +00:00
|
|
|
pub enum FileMatch {
|
|
|
|
FileMatches,
|
|
|
|
FileDoesntMatch,
|
|
|
|
}
|
|
|
|
|
2019-01-13 08:18:37 +00:00
|
|
|
#[derive(Clone)]
|
2014-03-09 12:24:58 +00:00
|
|
|
pub struct FileSearch<'a> {
|
2018-11-23 02:36:41 +00:00
|
|
|
sysroot: &'a Path,
|
|
|
|
triple: &'a str,
|
|
|
|
search_paths: &'a [SearchPath],
|
|
|
|
tlib_path: &'a SearchPath,
|
|
|
|
kind: PathKind,
|
2012-01-13 08:32:05 +00:00
|
|
|
}
|
2011-10-03 19:46:22 +00:00
|
|
|
|
2014-03-09 12:24:58 +00:00
|
|
|
impl<'a> FileSearch<'a> {
|
2018-11-23 02:36:41 +00:00
|
|
|
pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
|
|
|
|
let kind = self.kind;
|
|
|
|
self.search_paths
|
|
|
|
.iter()
|
|
|
|
.filter(move |sp| sp.kind.matches(kind))
|
|
|
|
.chain(std::iter::once(self.tlib_path))
|
2011-10-03 19:46:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn get_lib_path(&self) -> PathBuf {
|
2014-04-17 15:52:25 +00:00
|
|
|
make_target_lib_path(self.sysroot, self.triple)
|
2014-01-13 16:31:57 +00:00
|
|
|
}
|
2011-10-03 19:46:22 +00:00
|
|
|
|
2020-06-22 17:56:56 +00:00
|
|
|
pub fn get_self_contained_lib_path(&self) -> PathBuf {
|
2020-06-08 10:47:56 +00:00
|
|
|
self.get_lib_path().join("self-contained")
|
|
|
|
}
|
|
|
|
|
2014-03-09 12:24:58 +00:00
|
|
|
pub fn new(
|
|
|
|
sysroot: &'a Path,
|
2014-04-17 15:52:25 +00:00
|
|
|
triple: &'a str,
|
2020-12-30 11:59:07 +00:00
|
|
|
search_paths: &'a [SearchPath],
|
2018-11-20 00:06:45 +00:00
|
|
|
tlib_path: &'a SearchPath,
|
|
|
|
kind: PathKind,
|
|
|
|
) -> FileSearch<'a> {
|
2014-04-17 15:52:25 +00:00
|
|
|
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
|
2018-11-20 00:06:45 +00:00
|
|
|
FileSearch { sysroot, triple, search_paths, tlib_path, kind }
|
2014-01-13 16:31:57 +00:00
|
|
|
}
|
2014-04-29 18:38:51 +00:00
|
|
|
|
2021-08-31 16:31:29 +00:00
|
|
|
/// Returns just the directories within the search paths.
|
2018-11-23 02:36:41 +00:00
|
|
|
pub fn search_path_dirs(&self) -> Vec<PathBuf> {
|
|
|
|
self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
|
2014-09-03 07:46:23 +00:00
|
|
|
}
|
2011-10-03 20:54:13 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 00:06:45 +00:00
|
|
|
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
|
2021-05-10 16:15:19 +00:00
|
|
|
let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
|
2021-09-03 10:36:33 +00:00
|
|
|
PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("lib")])
|
2011-10-03 19:46:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 07:20:51 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn current_dll_path() -> Result<PathBuf, String> {
|
|
|
|
use std::ffi::{CStr, OsStr};
|
|
|
|
use std::os::unix::prelude::*;
|
|
|
|
|
2023-03-23 08:50:49 +00:00
|
|
|
#[cfg(not(target_os = "aix"))]
|
2022-10-28 07:20:51 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
if info.dli_fname.is_null() {
|
|
|
|
return Err("dladdr returned null pointer".into());
|
|
|
|
}
|
|
|
|
let bytes = CStr::from_ptr(info.dli_fname).to_bytes();
|
|
|
|
let os = OsStr::from_bytes(bytes);
|
|
|
|
Ok(PathBuf::from(os))
|
|
|
|
}
|
2023-03-23 08:50:49 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "aix")]
|
|
|
|
unsafe {
|
|
|
|
let addr = current_dll_path as u64;
|
2023-03-24 02:25:52 +00:00
|
|
|
let mut buffer = vec![std::mem::zeroed::<libc::ld_info>(); 64];
|
2023-03-23 08:50:49 +00:00
|
|
|
loop {
|
2023-03-24 02:25:52 +00:00
|
|
|
if libc::loadquery(
|
|
|
|
libc::L_GETINFO,
|
|
|
|
buffer.as_mut_ptr() as *mut i8,
|
|
|
|
(std::mem::size_of::<libc::ld_info>() * buffer.len()) as u32,
|
|
|
|
) >= 0
|
|
|
|
{
|
2023-03-23 08:50:49 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if std::io::Error::last_os_error().raw_os_error().unwrap() != libc::ENOMEM {
|
|
|
|
return Err("loadquery failed".into());
|
|
|
|
}
|
2023-03-24 02:25:52 +00:00
|
|
|
buffer.resize(buffer.len() * 2, std::mem::zeroed::<libc::ld_info>());
|
2023-03-23 08:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut current = buffer.as_mut_ptr() as *mut libc::ld_info;
|
|
|
|
loop {
|
|
|
|
let data_base = (*current).ldinfo_dataorg as u64;
|
|
|
|
let data_end = data_base + (*current).ldinfo_datasize;
|
|
|
|
let text_base = (*current).ldinfo_textorg as u64;
|
|
|
|
let text_end = text_base + (*current).ldinfo_textsize;
|
|
|
|
if (data_base <= addr && addr < data_end) || (text_base <= addr && addr < text_end) {
|
|
|
|
let bytes = CStr::from_ptr(&(*current).ldinfo_filename[0]).to_bytes();
|
|
|
|
let os = OsStr::from_bytes(bytes);
|
|
|
|
return Ok(PathBuf::from(os));
|
|
|
|
}
|
|
|
|
if (*current).ldinfo_next == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current = ((current as u64) + ((*current).ldinfo_next) as u64) as *mut libc::ld_info;
|
|
|
|
}
|
|
|
|
return Err(format!("current dll's address {} is not in the load map", addr));
|
|
|
|
}
|
2022-10-28 07:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn current_dll_path() -> Result<PathBuf, String> {
|
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::io;
|
|
|
|
use std::os::windows::prelude::*;
|
|
|
|
|
2023-01-15 18:43:15 +00:00
|
|
|
use windows::{
|
|
|
|
core::PCWSTR,
|
|
|
|
Win32::Foundation::HINSTANCE,
|
|
|
|
Win32::System::LibraryLoader::{
|
|
|
|
GetModuleFileNameW, GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
|
|
|
|
},
|
2022-10-28 07:20:51 +00:00
|
|
|
};
|
|
|
|
|
2023-01-15 18:43:15 +00:00
|
|
|
let mut module = HINSTANCE::default();
|
2022-10-28 07:20:51 +00:00
|
|
|
unsafe {
|
2023-01-15 18:43:15 +00:00
|
|
|
GetModuleHandleExW(
|
2022-10-28 07:20:51 +00:00
|
|
|
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
|
2023-01-15 18:43:15 +00:00
|
|
|
PCWSTR(current_dll_path as *mut u16),
|
2022-10-28 07:20:51 +00:00
|
|
|
&mut module,
|
2023-01-15 18:43:15 +00:00
|
|
|
)
|
2022-10-28 07:20:51 +00:00
|
|
|
}
|
2023-01-15 18:43:15 +00:00
|
|
|
.ok()
|
|
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
|
|
|
|
let mut filename = vec![0; 1024];
|
|
|
|
let n = unsafe { GetModuleFileNameW(module, &mut filename) } as usize;
|
|
|
|
if n == 0 {
|
|
|
|
return Err(format!("GetModuleFileNameW failed: {}", io::Error::last_os_error()));
|
|
|
|
}
|
|
|
|
if n >= filename.capacity() {
|
|
|
|
return Err(format!("our buffer was too small? {}", io::Error::last_os_error()));
|
|
|
|
}
|
|
|
|
|
|
|
|
filename.truncate(n);
|
|
|
|
|
|
|
|
Ok(OsString::from_wide(&filename).into())
|
2022-10-28 07:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> {
|
|
|
|
let target = crate::config::host_triple();
|
|
|
|
let mut sysroot_candidates: SmallVec<[PathBuf; 2]> =
|
|
|
|
smallvec![get_or_default_sysroot().expect("Failed finding sysroot")];
|
2022-12-18 11:45:56 +00:00
|
|
|
let path = current_dll_path().and_then(|s| s.canonicalize().map_err(|e| e.to_string()));
|
2022-10-28 07:20:51 +00:00
|
|
|
if let Ok(dll) = path {
|
|
|
|
// use `parent` twice to chop off the file name and then also the
|
|
|
|
// directory containing the dll which should be either `lib` or `bin`.
|
|
|
|
if let Some(path) = dll.parent().and_then(|p| p.parent()) {
|
|
|
|
// The original `path` pointed at the `rustc_driver` crate's dll.
|
|
|
|
// Now that dll should only be in one of two locations. The first is
|
|
|
|
// in the compiler's libdir, for example `$sysroot/lib/*.dll`. The
|
|
|
|
// other is the target's libdir, for example
|
|
|
|
// `$sysroot/lib/rustlib/$target/lib/*.dll`.
|
|
|
|
//
|
|
|
|
// We don't know which, so let's assume that if our `path` above
|
|
|
|
// ends in `$target` we *could* be in the target libdir, and always
|
|
|
|
// assume that we may be in the main libdir.
|
|
|
|
sysroot_candidates.push(path.to_owned());
|
|
|
|
|
|
|
|
if path.ends_with(target) {
|
|
|
|
sysroot_candidates.extend(
|
|
|
|
path.parent() // chop off `$target`
|
|
|
|
.and_then(|p| p.parent()) // chop off `rustlib`
|
|
|
|
.and_then(|p| p.parent()) // chop off `lib`
|
|
|
|
.map(|s| s.to_owned()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sysroot_candidates;
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:31:29 +00:00
|
|
|
/// This function checks if sysroot is found using env::args().next(), and if it
|
2022-10-28 07:20:51 +00:00
|
|
|
/// is not found, finds sysroot from current rustc_driver dll.
|
|
|
|
pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
|
2022-11-16 20:34:16 +00:00
|
|
|
// Follow symlinks. If the resolved path is relative, make it absolute.
|
2020-07-29 16:10:07 +00:00
|
|
|
fn canonicalize(path: PathBuf) -> PathBuf {
|
|
|
|
let path = fs::canonicalize(&path).unwrap_or(path);
|
|
|
|
// See comments on this target function, but the gist is that
|
|
|
|
// gcc chokes on verbatim paths which fs::canonicalize generates
|
|
|
|
// so we try to avoid those kinds of paths.
|
|
|
|
fix_windows_verbatim_for_gcc(&path)
|
2014-01-22 22:45:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 07:20:51 +00:00
|
|
|
fn default_from_rustc_driver_dll() -> Result<PathBuf, String> {
|
2022-12-18 11:45:56 +00:00
|
|
|
let dll = current_dll_path().map(|s| canonicalize(s))?;
|
2022-10-28 07:20:51 +00:00
|
|
|
|
|
|
|
// `dll` will be in one of the following two:
|
|
|
|
// - compiler's libdir: $sysroot/lib/*.dll
|
|
|
|
// - target's libdir: $sysroot/lib/rustlib/$target/lib/*.dll
|
|
|
|
//
|
|
|
|
// use `parent` twice to chop off the file name and then also the
|
|
|
|
// directory containing the dll
|
|
|
|
let dir = dll.parent().and_then(|p| p.parent()).ok_or(format!(
|
|
|
|
"Could not move 2 levels upper using `parent()` on {}",
|
|
|
|
dll.display()
|
|
|
|
))?;
|
|
|
|
|
|
|
|
// if `dir` points target's dir, move up to the sysroot
|
|
|
|
if dir.ends_with(crate::config::host_triple()) {
|
|
|
|
dir.parent() // chop off `$target`
|
|
|
|
.and_then(|p| p.parent()) // chop off `rustlib`
|
2023-02-27 20:31:35 +00:00
|
|
|
.and_then(|p| {
|
|
|
|
// chop off `lib` (this could be also $arch dir if the host sysroot uses a
|
|
|
|
// multi-arch layout like Debian or Ubuntu)
|
|
|
|
match p.parent() {
|
|
|
|
Some(p) => match p.file_name() {
|
|
|
|
Some(f) if f == "lib" => p.parent(), // first chop went for $arch, so chop again for `lib`
|
|
|
|
_ => Some(p),
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
})
|
2022-10-28 07:20:51 +00:00
|
|
|
.map(|s| s.to_owned())
|
|
|
|
.ok_or(format!(
|
|
|
|
"Could not move 3 levels upper using `parent()` on {}",
|
|
|
|
dir.display()
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(dir.to_owned())
|
2020-11-21 04:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use env::args().next() to get the path of the executable without
|
|
|
|
// following symlinks/canonicalizing any component. This makes the rustc
|
|
|
|
// binary able to locate Rust libraries in systems using content-addressable
|
|
|
|
// storage (CAS).
|
|
|
|
fn from_env_args_next() -> Option<PathBuf> {
|
|
|
|
match env::args_os().next() {
|
|
|
|
Some(first_arg) => {
|
|
|
|
let mut p = PathBuf::from(first_arg);
|
|
|
|
|
|
|
|
// Check if sysroot is found using env::args().next() only if the rustc in argv[0]
|
|
|
|
// is a symlink (see #79253). We might want to change/remove it to conform with
|
|
|
|
// https://www.gnu.org/prep/standards/standards.html#Finding-Program-Files in the
|
|
|
|
// future.
|
|
|
|
if fs::read_link(&p).is_err() {
|
|
|
|
// Path is not a symbolic link or does not exist.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:15:19 +00:00
|
|
|
// Pop off `bin/rustc`, obtaining the suspected sysroot.
|
2020-11-21 04:11:54 +00:00
|
|
|
p.pop();
|
|
|
|
p.pop();
|
2021-05-10 16:15:19 +00:00
|
|
|
// Look for the target rustlib directory in the suspected sysroot.
|
|
|
|
let mut rustlib_path = rustc_target::target_rustlib_path(&p, "dummy");
|
|
|
|
rustlib_path.pop(); // pop off the dummy target.
|
2023-02-15 17:39:43 +00:00
|
|
|
rustlib_path.exists().then_some(p)
|
2020-11-21 04:11:54 +00:00
|
|
|
}
|
|
|
|
None => None,
|
2020-07-29 16:10:07 +00:00
|
|
|
}
|
2011-10-03 19:46:22 +00:00
|
|
|
}
|
2020-11-21 04:11:54 +00:00
|
|
|
|
2022-10-28 07:20:51 +00:00
|
|
|
Ok(from_env_args_next().unwrap_or(default_from_rustc_driver_dll()?))
|
2011-11-10 16:41:42 +00:00
|
|
|
}
|