2014-02-10 15:36:31 +01:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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::*;
|
|
|
|
|
2018-08-18 13:55:43 +03:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
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
|
|
|
|
2014-12-16 14:32:02 -08:00
|
|
|
use session::search_paths::{SearchPaths, PathKind};
|
2018-08-03 15:31:03 -06: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
|
|
|
|
|
2014-03-09 14:24:58 +02:00
|
|
|
pub struct FileSearch<'a> {
|
2014-03-28 10:05:27 -07:00
|
|
|
pub sysroot: &'a Path,
|
2014-12-16 14:32:02 -08:00
|
|
|
pub search_paths: &'a SearchPaths,
|
2014-04-17 16:52:25 +01:00
|
|
|
pub triple: &'a str,
|
2014-12-16 14:32:02 -08:00
|
|
|
pub 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> {
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn for_each_lib_search_path<F>(&self, mut f: F) where
|
2015-11-24 19:37:40 +00:00
|
|
|
F: FnMut(&Path, PathKind)
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2018-08-18 13:55:43 +03:00
|
|
|
let mut visited_dirs = FxHashSet::default();
|
2018-10-10 15:30:53 +02:00
|
|
|
visited_dirs.reserve(self.search_paths.paths.len() + 1);
|
2015-01-06 08:46:07 -08:00
|
|
|
for (path, kind) in self.search_paths.iter(self.kind) {
|
2015-11-24 19:37:40 +00:00
|
|
|
f(path, kind);
|
2015-02-26 21:00:43 -08:00
|
|
|
visited_dirs.insert(path.to_path_buf());
|
2014-01-14 01:31:57 +09:00
|
|
|
}
|
2013-05-03 13:08:08 -04:00
|
|
|
|
2014-04-17 16:52:25 +01:00
|
|
|
debug!("filesearch: searching lib path");
|
2014-01-14 01:31:57 +09:00
|
|
|
let tlib_path = make_target_lib_path(self.sysroot,
|
2015-01-11 18:45:59 +13:00
|
|
|
self.triple);
|
2015-02-26 21:00:43 -08:00
|
|
|
if !visited_dirs.contains(&tlib_path) {
|
2015-11-24 19:37:40 +00:00
|
|
|
f(&tlib_path, PathKind::All);
|
2014-01-14 01:31:57 +09:00
|
|
|
}
|
2014-04-17 16:52:25 +01:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
visited_dirs.insert(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
|
|
|
|
2015-01-06 08:46:07 -08:00
|
|
|
pub fn search<F>(&self, mut pick: F)
|
|
|
|
where F: FnMut(&Path, PathKind) -> FileMatch
|
|
|
|
{
|
|
|
|
self.for_each_lib_search_path(|lib_search_path, kind| {
|
2014-01-14 01:31:57 +09:00
|
|
|
debug!("searching {}", lib_search_path.display());
|
2016-10-10 19:39:44 -04:00
|
|
|
let files = match fs::read_dir(lib_search_path) {
|
|
|
|
Ok(files) => files,
|
|
|
|
Err(..) => return,
|
|
|
|
};
|
|
|
|
let files = files.filter_map(|p| p.ok().map(|s| s.path()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
fn is_rlib(p: &Path) -> bool {
|
2016-10-10 19:46:18 -04:00
|
|
|
p.extension() == Some("rlib".as_ref())
|
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.
|
|
|
|
let files1 = files.iter().filter(|p| is_rlib(p));
|
|
|
|
let files2 = files.iter().filter(|p| !is_rlib(p));
|
|
|
|
for path in files1.chain(files2) {
|
|
|
|
debug!("testing {}", path.display());
|
|
|
|
let maybe_picked = pick(path, kind);
|
|
|
|
match maybe_picked {
|
|
|
|
FileMatches => {
|
|
|
|
debug!("picked {}", path.display());
|
2014-04-21 23:25:18 -07:00
|
|
|
}
|
2016-10-10 19:39:44 -04:00
|
|
|
FileDoesntMatch => {
|
|
|
|
debug!("rejected {}", 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
|
|
|
}
|
2014-01-14 01:31:57 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-09 14:24:58 +02:00
|
|
|
pub fn new(sysroot: &'a Path,
|
2014-04-17 16:52:25 +01:00
|
|
|
triple: &'a str,
|
2014-12-16 14:32:02 -08:00
|
|
|
search_paths: &'a SearchPaths,
|
|
|
|
kind: PathKind) -> FileSearch<'a> {
|
2014-04-17 16:52:25 +01:00
|
|
|
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
|
2014-03-09 14:24:58 +02:00
|
|
|
FileSearch {
|
2017-07-03 11:19:51 -07:00
|
|
|
sysroot,
|
|
|
|
search_paths,
|
|
|
|
triple,
|
|
|
|
kind,
|
2011-10-03 13:54:13 -07:00
|
|
|
}
|
2014-01-14 01:31:57 +09:00
|
|
|
}
|
2014-04-29 11:38:51 -07:00
|
|
|
|
2014-09-03 00:46:23 -07:00
|
|
|
// Returns a list of directories where target-specific dylibs might be located.
|
2015-02-26 21:00:43 -08:00
|
|
|
pub fn get_dylib_search_paths(&self) -> Vec<PathBuf> {
|
2014-09-03 00:46:23 -07:00
|
|
|
let mut paths = Vec::new();
|
2015-01-06 08:46:07 -08:00
|
|
|
self.for_each_lib_search_path(|lib_search_path, _| {
|
2015-02-26 21:00:43 -08:00
|
|
|
paths.push(lib_search_path.to_path_buf());
|
2014-09-03 00:46:23 -07:00
|
|
|
});
|
|
|
|
paths
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a list of directories where target-specific tool binaries are located.
|
2015-02-26 21:00:43 -08:00
|
|
|
pub fn get_tools_search_paths(&self) -> 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");
|
|
|
|
vec![p]
|
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
|
|
|
}
|
|
|
|
|
2012-08-24 15:28:43 -07:00
|
|
|
fn make_target_lib_path(sysroot: &Path,
|
2015-02-26 21:00:43 -08:00
|
|
|
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)),
|
2016-03-26 19:59:04 +01:00
|
|
|
Err(e) => bug!("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() {
|
|
|
|
Ok(exe) => {
|
|
|
|
match canonicalize(Some(exe)) {
|
2018-10-10 15:33:10 +02:00
|
|
|
Some(mut p) => { p.pop(); p.pop(); p },
|
2017-05-17 15:14:30 +02:00
|
|
|
None => bug!("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
|
|
|
|
// of the directory where librustc 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
|
2014-08-17 08:23:36 +02:00
|
|
|
// "lib" (i.e. non-default), this value is used (see issue #16552).
|
|
|
|
|
|
|
|
match option_env!("CFG_LIBDIR_RELATIVE") {
|
2016-10-10 19:28:16 -04:00
|
|
|
Some(libdir) if libdir != "lib" => return libdir.into(),
|
|
|
|
_ => if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
|
|
|
|
return PRIMARY_LIB_DIR.into();
|
2014-08-17 08:23:36 +02:00
|
|
|
} else {
|
2016-10-10 19:28:16 -04:00
|
|
|
return SECONDARY_LIB_DIR.into();
|
2014-08-17 08:23:36 +02:00
|
|
|
}
|
2014-03-25 21:25:43 -07:00
|
|
|
}
|
|
|
|
|
2015-01-16 17:01:02 +02:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2016-10-10 19:28:16 -04:00
|
|
|
const PRIMARY_LIB_DIR: &'static str = "lib64";
|
2014-03-25 21:25:43 -07:00
|
|
|
|
2015-01-16 17:01:02 +02:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2016-10-10 19:28:16 -04:00
|
|
|
const PRIMARY_LIB_DIR: &'static str = "lib32";
|
2014-03-25 21:25:43 -07:00
|
|
|
|
2016-10-10 19:28:16 -04:00
|
|
|
const SECONDARY_LIB_DIR: &'static str = "lib";
|
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"
|
2016-10-10 19:28:16 -04:00
|
|
|
const RUST_LIB_DIR: &'static str = "rustlib";
|