2021-08-31 16:31:29 +00:00
|
|
|
//! A module for searching for libraries
|
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::FileMatch::*;
|
|
|
|
|
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
|
|
|
|
2020-04-06 07:44:30 +00:00
|
|
|
use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_fs_util::fix_windows_verbatim_for_gcc;
|
2020-08-14 06:05:01 +00:00
|
|
|
use tracing::debug;
|
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;
|
2019-12-22 22:42:04 +00:00
|
|
|
self.search_paths
|
|
|
|
.iter()
|
2018-11-23 02:36:41 +00:00
|
|
|
.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")
|
|
|
|
}
|
|
|
|
|
2015-01-06 16:46:07 +00:00
|
|
|
pub fn search<F>(&self, mut pick: F)
|
2019-12-22 22:42:04 +00:00
|
|
|
where
|
2020-04-06 07:44:30 +00:00
|
|
|
F: FnMut(&SearchPathFile, PathKind) -> FileMatch,
|
2015-01-06 16:46:07 +00:00
|
|
|
{
|
2018-11-23 02:36:41 +00:00
|
|
|
for search_path in self.search_paths() {
|
2018-11-22 05:33:07 +00:00
|
|
|
debug!("searching {}", search_path.dir.display());
|
2020-04-06 07:44:30 +00:00
|
|
|
fn is_rlib(spf: &SearchPathFile) -> bool {
|
|
|
|
if let Some(f) = &spf.file_name_str { f.ends_with(".rlib") } else { false }
|
2016-10-10 23:39:44 +00:00
|
|
|
}
|
|
|
|
// Reading metadata out of rlibs is faster, and if we find both
|
|
|
|
// an rlib and a dylib we only read one of the files of
|
|
|
|
// metadata, so in the name of speed, bring all rlib files to
|
|
|
|
// the front of the search list.
|
2020-04-06 07:44:30 +00:00
|
|
|
let files1 = search_path.files.iter().filter(|spf| is_rlib(&spf));
|
|
|
|
let files2 = search_path.files.iter().filter(|spf| !is_rlib(&spf));
|
|
|
|
for spf in files1.chain(files2) {
|
|
|
|
debug!("testing {}", spf.path.display());
|
|
|
|
let maybe_picked = pick(spf, search_path.kind);
|
2016-10-10 23:39:44 +00:00
|
|
|
match maybe_picked {
|
|
|
|
FileMatches => {
|
2020-04-06 07:44:30 +00:00
|
|
|
debug!("picked {}", spf.path.display());
|
2014-04-22 06:25:18 +00:00
|
|
|
}
|
2016-10-10 23:39:44 +00:00
|
|
|
FileDoesntMatch => {
|
2020-04-06 07:44:30 +00:00
|
|
|
debug!("rejected {}", spf.path.display());
|
2013-10-26 00:04:37 +00:00
|
|
|
}
|
2013-07-31 20:47:32 +00:00
|
|
|
}
|
2011-10-03 20:54:13 +00:00
|
|
|
}
|
2018-11-23 02:36:41 +00:00
|
|
|
}
|
2014-01-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn new(
|
|
|
|
sysroot: &'a Path,
|
|
|
|
triple: &'a str,
|
2020-12-30 11:59:07 +00:00
|
|
|
search_paths: &'a [SearchPath],
|
2019-12-22 22:42:04 +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);
|
2019-12-22 22:42:04 +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> {
|
2019-12-22 22:42:04 +00:00
|
|
|
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);
|
|
|
|
std::array::IntoIter::new([sysroot, Path::new(&rustlib_path), Path::new("lib")])
|
|
|
|
.collect::<PathBuf>()
|
2011-10-03 19:46:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 16:31:29 +00:00
|
|
|
/// This function checks if sysroot is found using env::args().next(), and if it
|
|
|
|
/// is not found, uses env::current_exe() to imply sysroot.
|
2015-02-27 05:00:43 +00:00
|
|
|
pub fn get_or_default_sysroot() -> PathBuf {
|
2014-01-22 22:45:52 +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
|
|
|
}
|
|
|
|
|
2020-11-21 04:11:54 +00:00
|
|
|
// Use env::current_exe() to get the path of the executable following
|
|
|
|
// symlinks/canonicalizing components.
|
|
|
|
fn from_current_exe() -> PathBuf {
|
|
|
|
match env::current_exe() {
|
|
|
|
Ok(exe) => {
|
|
|
|
let mut p = canonicalize(exe);
|
|
|
|
p.pop();
|
|
|
|
p.pop();
|
|
|
|
p
|
|
|
|
}
|
|
|
|
Err(e) => panic!("failed to get current_exe: {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if rustlib_path.exists() { Some(p) } else { None }
|
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
|
|
|
|
|
|
|
// Check if sysroot is found using env::args().next(), and if is not found,
|
|
|
|
// use env::current_exe() to imply sysroot.
|
2021-02-25 01:13:42 +00:00
|
|
|
from_env_args_next().unwrap_or_else(from_current_exe)
|
2011-11-10 16:41:42 +00:00
|
|
|
}
|