2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06: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 20:05:05 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
2014-02-10 08:36:31 -06:00
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::FileMatch::*;
|
|
|
|
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::collections::HashSet;
|
2015-01-27 14:20:58 -06:00
|
|
|
use std::env;
|
2015-01-22 18:31:00 -06:00
|
|
|
use std::old_io::fs::PathExtensions;
|
|
|
|
use std::old_io::fs;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2014-08-18 10:29:44 -05:00
|
|
|
use util::fs as myfs;
|
2014-12-16 16:32:02 -06:00
|
|
|
use session::search_paths::{SearchPaths, PathKind};
|
2014-04-08 12:15:46 -05:00
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Copy)]
|
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 19:01:33 -06:00
|
|
|
pub enum FileMatch {
|
|
|
|
FileMatches,
|
|
|
|
FileDoesntMatch,
|
|
|
|
}
|
|
|
|
|
2011-10-03 15:54:13 -05:00
|
|
|
// A module for searching for libraries
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2658): I'm not happy how this module turned out. Should
|
|
|
|
// probably just be folded into cstore.
|
2011-10-03 15:54:13 -05:00
|
|
|
|
2014-03-09 07:24:58 -05:00
|
|
|
pub struct FileSearch<'a> {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub sysroot: &'a Path,
|
2014-12-16 16:32:02 -06:00
|
|
|
pub search_paths: &'a SearchPaths,
|
2014-04-17 10:52:25 -05:00
|
|
|
pub triple: &'a str,
|
2014-12-16 16:32:02 -06:00
|
|
|
pub kind: PathKind,
|
2012-01-13 02:32:05 -06:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
|
2014-03-09 07:24:58 -05:00
|
|
|
impl<'a> FileSearch<'a> {
|
2014-12-08 19:26:43 -06:00
|
|
|
pub fn for_each_lib_search_path<F>(&self, mut f: F) where
|
2015-01-06 10:46:07 -06:00
|
|
|
F: FnMut(&Path, PathKind) -> FileMatch,
|
2014-12-08 19:26:43 -06:00
|
|
|
{
|
2014-01-13 10:31:57 -06:00
|
|
|
let mut visited_dirs = HashSet::new();
|
|
|
|
let mut found = false;
|
|
|
|
|
2015-01-06 10:46:07 -06:00
|
|
|
for (path, kind) in self.search_paths.iter(self.kind) {
|
|
|
|
match f(path, kind) {
|
2014-01-13 10:31:57 -06:00
|
|
|
FileMatches => found = true,
|
|
|
|
FileDoesntMatch => ()
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2014-07-16 15:37:28 -05:00
|
|
|
visited_dirs.insert(path.as_vec().to_vec());
|
2014-01-13 10:31:57 -06:00
|
|
|
}
|
2013-05-03 12:08:08 -05:00
|
|
|
|
2014-04-17 10:52:25 -05:00
|
|
|
debug!("filesearch: searching lib path");
|
2014-01-13 10:31:57 -06:00
|
|
|
let tlib_path = make_target_lib_path(self.sysroot,
|
2015-01-10 23:45:59 -06:00
|
|
|
self.triple);
|
2014-11-12 17:51:51 -06:00
|
|
|
if !visited_dirs.contains(tlib_path.as_vec()) {
|
2015-01-06 10:46:07 -06:00
|
|
|
match f(&tlib_path, PathKind::All) {
|
2014-01-13 10:31:57 -06:00
|
|
|
FileMatches => found = true,
|
|
|
|
FileDoesntMatch => ()
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
}
|
2014-04-17 10:52:25 -05:00
|
|
|
|
2014-07-16 15:37:28 -05:00
|
|
|
visited_dirs.insert(tlib_path.as_vec().to_vec());
|
2014-01-13 10:31:57 -06:00
|
|
|
// Try RUST_PATH
|
|
|
|
if !found {
|
|
|
|
let rustpath = rust_path();
|
2015-01-31 11:20:46 -06:00
|
|
|
for path in &rustpath {
|
2014-04-17 10:52:25 -05:00
|
|
|
let tlib_path = make_rustpkg_lib_path(
|
|
|
|
self.sysroot, path, self.triple);
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("is {} in visited_dirs? {}", tlib_path.display(),
|
2014-11-12 17:51:51 -06:00
|
|
|
visited_dirs.contains(&tlib_path.as_vec().to_vec()));
|
2014-01-13 10:31:57 -06:00
|
|
|
|
2014-11-12 17:51:51 -06:00
|
|
|
if !visited_dirs.contains(tlib_path.as_vec()) {
|
2014-07-16 15:37:28 -05:00
|
|
|
visited_dirs.insert(tlib_path.as_vec().to_vec());
|
2014-01-13 10:31:57 -06:00
|
|
|
// Don't keep searching the RUST_PATH if one match turns up --
|
|
|
|
// if we did, we'd get a "multiple matching crates" error
|
2015-01-06 10:46:07 -06:00
|
|
|
match f(&tlib_path, PathKind::All) {
|
2014-01-13 10:31:57 -06:00
|
|
|
FileMatches => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FileDoesntMatch => ()
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2013-08-23 13:51:45 -05:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2013-05-03 12:08:08 -05:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 10:52:25 -05:00
|
|
|
pub fn get_lib_path(&self) -> Path {
|
|
|
|
make_target_lib_path(self.sysroot, self.triple)
|
2014-01-13 10:31:57 -06:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
|
2015-01-06 10:46:07 -06: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-13 10:31:57 -06:00
|
|
|
debug!("searching {}", lib_search_path.display());
|
2014-01-29 20:42:19 -06:00
|
|
|
match fs::readdir(lib_search_path) {
|
2014-01-13 10:31:57 -06:00
|
|
|
Ok(files) => {
|
|
|
|
let mut rslt = FileDoesntMatch;
|
2014-04-22 01:25:18 -05:00
|
|
|
fn is_rlib(p: & &Path) -> bool {
|
2014-01-13 10:31:57 -06:00
|
|
|
p.extension_str() == Some("rlib")
|
2014-04-22 01:25:18 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06: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());
|
2015-01-06 10:46:07 -06:00
|
|
|
let maybe_picked = pick(path, kind);
|
2014-01-13 10:31:57 -06:00
|
|
|
match maybe_picked {
|
|
|
|
FileMatches => {
|
|
|
|
debug!("picked {}", path.display());
|
|
|
|
rslt = FileMatches;
|
|
|
|
}
|
|
|
|
FileDoesntMatch => {
|
|
|
|
debug!("rejected {}", path.display());
|
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
rslt
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
Err(..) => FileDoesntMatch,
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-09 07:24:58 -05:00
|
|
|
pub fn new(sysroot: &'a Path,
|
2014-04-17 10:52:25 -05:00
|
|
|
triple: &'a str,
|
2014-12-16 16:32:02 -06:00
|
|
|
search_paths: &'a SearchPaths,
|
|
|
|
kind: PathKind) -> FileSearch<'a> {
|
2014-04-17 10:52:25 -05:00
|
|
|
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
|
2014-03-09 07:24:58 -05:00
|
|
|
FileSearch {
|
2014-01-13 10:31:57 -06:00
|
|
|
sysroot: sysroot,
|
2014-12-16 16:32:02 -06:00
|
|
|
search_paths: search_paths,
|
2014-04-17 10:52:25 -05:00
|
|
|
triple: triple,
|
2014-12-16 16:32:02 -06:00
|
|
|
kind: kind,
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
}
|
2014-04-29 13:38:51 -05:00
|
|
|
|
2014-09-03 02:46:23 -05:00
|
|
|
// Returns a list of directories where target-specific dylibs might be located.
|
|
|
|
pub fn get_dylib_search_paths(&self) -> Vec<Path> {
|
|
|
|
let mut paths = Vec::new();
|
2015-01-06 10:46:07 -06:00
|
|
|
self.for_each_lib_search_path(|lib_search_path, _| {
|
2014-09-03 02:46:23 -05:00
|
|
|
paths.push(lib_search_path.clone());
|
2014-04-29 13:38:51 -05:00
|
|
|
FileDoesntMatch
|
2014-09-03 02:46:23 -05:00
|
|
|
});
|
|
|
|
paths
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a list of directories where target-specific tool binaries are located.
|
|
|
|
pub fn get_tools_search_paths(&self) -> Vec<Path> {
|
|
|
|
let mut p = Path::new(self.sysroot);
|
|
|
|
p.push(find_libdir(self.sysroot));
|
|
|
|
p.push(rustlibdir());
|
|
|
|
p.push(self.triple);
|
2014-11-08 20:24:45 -06:00
|
|
|
p.push("bin");
|
|
|
|
vec![p]
|
2014-04-29 13:38:51 -05:00
|
|
|
}
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
|
|
|
|
2014-03-25 23:25:43 -05:00
|
|
|
pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> Path {
|
|
|
|
let mut p = Path::new(find_libdir(sysroot));
|
2013-09-26 19:21:59 -05:00
|
|
|
assert!(p.is_relative());
|
2014-01-04 19:55:20 -06:00
|
|
|
p.push(rustlibdir());
|
2013-10-05 21:49:32 -05:00
|
|
|
p.push(target_triple);
|
2014-01-07 10:51:15 -06:00
|
|
|
p.push("lib");
|
2013-09-26 19:21:59 -05:00
|
|
|
p
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|
|
|
|
|
2012-08-24 17:28:43 -05:00
|
|
|
fn make_target_lib_path(sysroot: &Path,
|
|
|
|
target_triple: &str) -> Path {
|
2014-03-25 23:25:43 -05:00
|
|
|
sysroot.join(&relative_target_lib_path(sysroot, target_triple))
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 10:52:25 -05:00
|
|
|
fn make_rustpkg_lib_path(sysroot: &Path,
|
|
|
|
dir: &Path,
|
|
|
|
triple: &str) -> Path {
|
2014-03-25 23:25:43 -05:00
|
|
|
let mut p = dir.join(find_libdir(sysroot));
|
2014-04-17 10:52:25 -05:00
|
|
|
p.push(triple);
|
2013-09-26 19:21:59 -05:00
|
|
|
p
|
2013-09-12 21:29:21 -05:00
|
|
|
}
|
|
|
|
|
2013-08-07 02:11:34 -05:00
|
|
|
pub fn get_or_default_sysroot() -> Path {
|
2014-01-22 16:45:52 -06:00
|
|
|
// Follow symlinks. If the resolved path is relative, make it absolute.
|
|
|
|
fn canonicalize(path: Option<Path>) -> Option<Path> {
|
2014-04-08 12:15:46 -05:00
|
|
|
path.and_then(|path|
|
|
|
|
match myfs::realpath(&path) {
|
|
|
|
Ok(canon) => Some(canon),
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(e) => panic!("failed to get realpath: {}", e),
|
2014-01-22 16:45:52 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-27 14:20:58 -06:00
|
|
|
match canonicalize(env::current_exe().ok()) {
|
2014-03-09 07:24:58 -05:00
|
|
|
Some(mut p) => { p.pop(); p.pop(); p }
|
2014-10-09 14:17:22 -05:00
|
|
|
None => panic!("can't determine value for sysroot")
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
2011-11-10 10:41:42 -06:00
|
|
|
}
|
2012-01-05 18:03:28 -06:00
|
|
|
|
2013-07-31 15:47:32 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
static PATH_ENTRY_SEPARATOR: &'static str = ";";
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
static PATH_ENTRY_SEPARATOR: &'static str = ":";
|
|
|
|
|
|
|
|
/// Returns RUST_PATH as a string, without default paths added
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn get_rust_path() -> Option<String> {
|
2015-02-11 13:47:53 -06:00
|
|
|
env::var("RUST_PATH").ok()
|
2012-02-07 02:15:39 -06:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:47:32 -05:00
|
|
|
/// Returns the value of RUST_PATH, as a list
|
|
|
|
/// of Paths. Includes default entries for, if they exist:
|
|
|
|
/// $HOME/.rust
|
|
|
|
/// DIR/.rust for any DIR that's the current working directory
|
|
|
|
/// or an ancestor of it
|
2014-03-04 12:02:49 -06:00
|
|
|
pub fn rust_path() -> Vec<Path> {
|
|
|
|
let mut env_rust_path: Vec<Path> = match get_rust_path() {
|
2013-07-31 15:47:32 -05:00
|
|
|
Some(env_path) => {
|
2014-03-28 14:42:34 -05:00
|
|
|
let env_path_components =
|
2014-11-27 12:53:34 -06:00
|
|
|
env_path.split_str(PATH_ENTRY_SEPARATOR);
|
2014-03-28 14:42:34 -05:00
|
|
|
env_path_components.map(|s| Path::new(s)).collect()
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2014-03-04 12:02:49 -06:00
|
|
|
None => Vec::new()
|
2013-07-31 15:47:32 -05:00
|
|
|
};
|
2015-01-27 14:20:58 -06:00
|
|
|
let mut cwd = env::current_dir().unwrap();
|
2013-07-31 15:47:32 -05:00
|
|
|
// now add in default entries
|
2013-10-05 21:49:32 -05:00
|
|
|
let cwd_dot_rust = cwd.join(".rust");
|
2013-07-31 15:47:32 -05:00
|
|
|
if !env_rust_path.contains(&cwd_dot_rust) {
|
|
|
|
env_rust_path.push(cwd_dot_rust);
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
if !env_rust_path.contains(&cwd) {
|
|
|
|
env_rust_path.push(cwd.clone());
|
|
|
|
}
|
2013-10-07 21:16:58 -05:00
|
|
|
loop {
|
2014-06-18 13:25:36 -05:00
|
|
|
if { let f = cwd.filename(); f.is_none() || f.unwrap() == b".." } {
|
2013-10-10 00:05:14 -05:00
|
|
|
break
|
2012-02-05 03:30:03 -06:00
|
|
|
}
|
2013-10-10 00:05:14 -05:00
|
|
|
cwd.set_filename(".rust");
|
2013-10-25 19:04:37 -05:00
|
|
|
if !env_rust_path.contains(&cwd) && cwd.exists() {
|
2013-10-07 21:16:58 -05:00
|
|
|
env_rust_path.push(cwd.clone());
|
|
|
|
}
|
|
|
|
cwd.pop();
|
|
|
|
}
|
2015-02-02 13:01:12 -06:00
|
|
|
if let Some(h) = env::home_dir() {
|
2013-10-07 21:16:58 -05:00
|
|
|
let p = h.join(".rust");
|
2013-10-25 19:04:37 -05:00
|
|
|
if !env_rust_path.contains(&p) && p.exists() {
|
2013-10-07 21:16:58 -05:00
|
|
|
env_rust_path.push(p);
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
env_rust_path
|
2012-01-10 19:45:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The name of the directory rustc expects libraries to be located.
|
|
|
|
// On Unix should be "lib", on windows "bin"
|
2014-03-25 21:17:02 -05:00
|
|
|
#[cfg(unix)]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn find_libdir(sysroot: &Path) -> String {
|
2014-03-25 23:25:43 -05: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.
|
2014-08-17 01:23:36 -05:00
|
|
|
//If --libdir is set during configuration to the value other than
|
|
|
|
// "lib" (i.e. non-default), this value is used (see issue #16552).
|
|
|
|
|
|
|
|
match option_env!("CFG_LIBDIR_RELATIVE") {
|
|
|
|
Some(libdir) if libdir != "lib" => return libdir.to_string(),
|
|
|
|
_ => if sysroot.join(primary_libdir_name()).join(rustlibdir()).exists() {
|
|
|
|
return primary_libdir_name();
|
|
|
|
} else {
|
|
|
|
return secondary_libdir_name();
|
|
|
|
}
|
2014-03-25 23:25:43 -05:00
|
|
|
}
|
|
|
|
|
2015-01-16 09:01:02 -06:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn primary_libdir_name() -> String {
|
2014-05-25 05:17:19 -05:00
|
|
|
"lib64".to_string()
|
2014-05-09 20:45:36 -05:00
|
|
|
}
|
2014-03-25 23:25:43 -05:00
|
|
|
|
2015-01-16 09:01:02 -06:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn primary_libdir_name() -> String {
|
2014-05-25 05:17:19 -05:00
|
|
|
"lib32".to_string()
|
2014-05-09 20:45:36 -05:00
|
|
|
}
|
2014-03-25 23:25:43 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
fn secondary_libdir_name() -> String {
|
2014-05-25 05:17:19 -05:00
|
|
|
"lib".to_string()
|
2014-05-09 20:45:36 -05:00
|
|
|
}
|
2014-03-25 21:17:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn find_libdir(_sysroot: &Path) -> String {
|
2014-05-25 05:17:19 -05:00
|
|
|
"bin".to_string()
|
2013-08-06 23:50:23 -05:00
|
|
|
}
|
2014-01-04 19:55:20 -06:00
|
|
|
|
|
|
|
// The name of rustc's own place to organize libraries.
|
|
|
|
// Used to be "rustc", now the default is "rustlib"
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn rustlibdir() -> String {
|
2014-05-25 05:17:19 -05:00
|
|
|
"rustlib".to_string()
|
2014-01-04 19:55:20 -06:00
|
|
|
}
|