2014-03-21 18:05:05 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
2014-02-10 15:36:31 +01:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::FileMatch::*;
|
|
|
|
|
2016-10-10 19:35:22 -04:00
|
|
|
use std::borrow::Cow;
|
2015-01-27 12:20:58 -08:00
|
|
|
use std::env;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::fs;
|
|
|
|
use std::path::{Path, PathBuf};
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2020-04-06 17:44:30 +10:00
|
|
|
use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
|
2019-11-29 16:05:28 -05:00
|
|
|
use log::debug;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_fs_util::fix_windows_verbatim_for_gcc;
|
2014-04-08 10:15:46 -07:00
|
|
|
|
2015-03-30 09:38:44 -04: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-05 17:01:33 -08:00
|
|
|
pub enum FileMatch {
|
|
|
|
FileMatches,
|
|
|
|
FileDoesntMatch,
|
|
|
|
}
|
|
|
|
|
2011-10-03 13:54:13 -07:00
|
|
|
// A module for searching for libraries
|
|
|
|
|
2019-01-13 09:18:37 +01:00
|
|
|
#[derive(Clone)]
|
2014-03-09 14:24:58 +02:00
|
|
|
pub struct FileSearch<'a> {
|
2018-11-23 13:36:41 +11:00
|
|
|
sysroot: &'a Path,
|
|
|
|
triple: &'a str,
|
|
|
|
search_paths: &'a [SearchPath],
|
|
|
|
tlib_path: &'a SearchPath,
|
|
|
|
kind: PathKind,
|
2012-01-13 09:32:05 +01:00
|
|
|
}
|
2011-10-03 12:46:22 -07:00
|
|
|
|
2014-03-09 14:24:58 +02:00
|
|
|
impl<'a> FileSearch<'a> {
|
2018-11-23 13:36:41 +11:00
|
|
|
pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
|
|
|
|
let kind = self.kind;
|
2019-12-22 17:42:04 -05:00
|
|
|
self.search_paths
|
|
|
|
.iter()
|
2018-11-23 13:36:41 +11:00
|
|
|
.filter(move |sp| sp.kind.matches(kind))
|
|
|
|
.chain(std::iter::once(self.tlib_path))
|
2011-10-03 12:46:22 -07:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
pub fn get_lib_path(&self) -> PathBuf {
|
2014-04-17 16:52:25 +01:00
|
|
|
make_target_lib_path(self.sysroot, self.triple)
|
2014-01-14 01:31:57 +09:00
|
|
|
}
|
2011-10-03 12:46:22 -07:00
|
|
|
|
2020-06-08 12:47:56 +02:00
|
|
|
pub fn get_selfcontained_lib_path(&self) -> PathBuf {
|
|
|
|
self.get_lib_path().join("self-contained")
|
|
|
|
}
|
|
|
|
|
2015-01-06 08:46:07 -08:00
|
|
|
pub fn search<F>(&self, mut pick: F)
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
2020-04-06 17:44:30 +10:00
|
|
|
F: FnMut(&SearchPathFile, PathKind) -> FileMatch,
|
2015-01-06 08:46:07 -08:00
|
|
|
{
|
2018-11-23 13:36:41 +11:00
|
|
|
for search_path in self.search_paths() {
|
2018-11-22 16:33:07 +11:00
|
|
|
debug!("searching {}", search_path.dir.display());
|
2020-04-06 17:44:30 +10:00
|
|
|
fn is_rlib(spf: &SearchPathFile) -> bool {
|
|
|
|
if let Some(f) = &spf.file_name_str { f.ends_with(".rlib") } else { false }
|
2016-10-10 19:39:44 -04: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 17:44:30 +10: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 19:39:44 -04:00
|
|
|
match maybe_picked {
|
|
|
|
FileMatches => {
|
2020-04-06 17:44:30 +10:00
|
|
|
debug!("picked {}", spf.path.display());
|
2014-04-21 23:25:18 -07:00
|
|
|
}
|
2016-10-10 19:39:44 -04:00
|
|
|
FileDoesntMatch => {
|
2020-04-06 17:44:30 +10:00
|
|
|
debug!("rejected {}", spf.path.display());
|
2013-10-25 17:04:37 -07:00
|
|
|
}
|
2013-07-31 13:47:32 -07:00
|
|
|
}
|
2011-10-03 13:54:13 -07:00
|
|
|
}
|
2018-11-23 13:36:41 +11:00
|
|
|
}
|
2014-01-14 01:31:57 +09:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn new(
|
|
|
|
sysroot: &'a Path,
|
|
|
|
triple: &'a str,
|
|
|
|
search_paths: &'a Vec<SearchPath>,
|
|
|
|
tlib_path: &'a SearchPath,
|
|
|
|
kind: PathKind,
|
|
|
|
) -> FileSearch<'a> {
|
2014-04-17 16:52:25 +01:00
|
|
|
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
|
2019-12-22 17:42:04 -05:00
|
|
|
FileSearch { sysroot, triple, search_paths, tlib_path, kind }
|
2014-01-14 01:31:57 +09:00
|
|
|
}
|
2014-04-29 11:38:51 -07:00
|
|
|
|
2018-11-23 13:36:41 +11:00
|
|
|
// Returns just the directories within the search paths.
|
|
|
|
pub fn search_path_dirs(&self) -> Vec<PathBuf> {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
|
2014-09-03 00:46:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a list of directories where target-specific tool binaries are located.
|
2020-05-28 19:54:08 +02:00
|
|
|
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
|
2015-03-18 09:14:54 -07:00
|
|
|
let mut p = PathBuf::from(self.sysroot);
|
2016-10-10 19:35:22 -04:00
|
|
|
p.push(find_libdir(self.sysroot).as_ref());
|
2016-10-10 19:28:16 -04:00
|
|
|
p.push(RUST_LIB_DIR);
|
2015-02-26 21:00:43 -08:00
|
|
|
p.push(&self.triple);
|
2014-11-08 18:24:45 -08:00
|
|
|
p.push("bin");
|
2020-05-28 19:54:08 +02:00
|
|
|
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p.clone()] }
|
2014-04-29 11:38:51 -07:00
|
|
|
}
|
2011-10-03 13:54:13 -07:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
|
2016-10-10 19:35:22 -04:00
|
|
|
let mut p = PathBuf::from(find_libdir(sysroot).as_ref());
|
2013-09-26 17:21:59 -07:00
|
|
|
assert!(p.is_relative());
|
2016-10-10 19:28:16 -04:00
|
|
|
p.push(RUST_LIB_DIR);
|
2013-10-05 19:49:32 -07:00
|
|
|
p.push(target_triple);
|
2014-01-07 17:51:15 +01:00
|
|
|
p.push("lib");
|
2013-09-26 17:21:59 -07:00
|
|
|
p
|
2011-10-04 15:23:32 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 11:06:45 +11:00
|
|
|
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
|
2014-03-25 21:25:43 -07:00
|
|
|
sysroot.join(&relative_target_lib_path(sysroot, target_triple))
|
2011-10-03 12:46:22 -07:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
pub fn get_or_default_sysroot() -> PathBuf {
|
2014-01-22 23:45:52 +01:00
|
|
|
// Follow symlinks. If the resolved path is relative, make it absolute.
|
2015-02-26 21:00:43 -08:00
|
|
|
fn canonicalize(path: Option<PathBuf>) -> Option<PathBuf> {
|
|
|
|
path.and_then(|path| {
|
2015-04-15 23:21:13 -07:00
|
|
|
match fs::canonicalize(&path) {
|
2015-05-05 15:19:36 -07:00
|
|
|
// 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.
|
2018-08-03 15:31:03 -06:00
|
|
|
Ok(canon) => Some(fix_windows_verbatim_for_gcc(&canon)),
|
2019-11-29 16:05:28 -05:00
|
|
|
Err(e) => panic!("failed to get realpath: {}", e),
|
2015-02-26 21:00:43 -08:00
|
|
|
}
|
|
|
|
})
|
2014-01-22 23:45:52 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 15:14:30 +02:00
|
|
|
match env::current_exe() {
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(exe) => match canonicalize(Some(exe)) {
|
|
|
|
Some(mut p) => {
|
|
|
|
p.pop();
|
|
|
|
p.pop();
|
|
|
|
p
|
2017-05-17 15:14:30 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
None => panic!("can't determine value for sysroot"),
|
|
|
|
},
|
|
|
|
Err(ref e) => panic!(format!("failed to get current_exe: {}", e)),
|
2011-10-03 12:46:22 -07:00
|
|
|
}
|
2011-11-11 00:41:42 +08:00
|
|
|
}
|
2012-01-05 16:03:28 -08:00
|
|
|
|
2012-01-10 17:45:03 -08:00
|
|
|
// The name of the directory rustc expects libraries to be located.
|
2016-10-10 19:35:22 -04:00
|
|
|
fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
|
2014-03-25 21:25:43 -07:00
|
|
|
// FIXME: This is a quick hack to make the rustc binary able to locate
|
|
|
|
// Rust libraries in Linux environments where libraries might be installed
|
|
|
|
// to lib64/lib32. This would be more foolproof by basing the sysroot off
|
2020-04-03 19:03:13 +09:00
|
|
|
// of the directory where `librustc_driver` is located, rather than
|
|
|
|
// where the rustc binary is.
|
2018-10-10 15:24:31 +02:00
|
|
|
// If --libdir is set during configuration to the value other than
|
2018-11-27 02:59:49 +00:00
|
|
|
// "lib" (i.e., non-default), this value is used (see issue #16552).
|
2014-08-17 08:23:36 +02:00
|
|
|
|
2015-01-16 17:01:02 +02:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-12-04 12:46:10 +01:00
|
|
|
const PRIMARY_LIB_DIR: &str = "lib64";
|
2014-03-25 21:25:43 -07:00
|
|
|
|
2015-01-16 17:01:02 +02:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2018-12-04 12:46:10 +01:00
|
|
|
const PRIMARY_LIB_DIR: &str = "lib32";
|
2014-03-25 21:25:43 -07:00
|
|
|
|
2018-12-04 12:46:10 +01:00
|
|
|
const SECONDARY_LIB_DIR: &str = "lib";
|
2018-10-10 15:35:56 +02:00
|
|
|
|
|
|
|
match option_env!("CFG_LIBDIR_RELATIVE") {
|
|
|
|
Some(libdir) if libdir != "lib" => libdir.into(),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => {
|
|
|
|
if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
|
|
|
|
PRIMARY_LIB_DIR.into()
|
|
|
|
} else {
|
|
|
|
SECONDARY_LIB_DIR.into()
|
|
|
|
}
|
2018-10-10 15:35:56 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 19:17:02 -07:00
|
|
|
}
|
|
|
|
|
2014-01-05 01:55:20 +00:00
|
|
|
// The name of rustc's own place to organize libraries.
|
|
|
|
// Used to be "rustc", now the default is "rustlib"
|
2018-12-04 12:46:10 +01:00
|
|
|
const RUST_LIB_DIR: &str = "rustlib";
|