1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_camel_case_types)]
13 pub use self::FileMatch
::*;
16 use std
::collections
::HashSet
;
19 use std
::path
::{Path, PathBuf}
;
21 use session
::search_paths
::{SearchPaths, PathKind}
;
22 use util
::fs
as rustcfs
;
24 #[derive(Copy, Clone)]
30 // A module for searching for libraries
31 // FIXME (#2658): I'm not happy how this module turned out. Should
32 // probably just be folded into cstore.
34 pub struct FileSearch
<'a
> {
35 pub sysroot
: &'a Path
,
36 pub search_paths
: &'a SearchPaths
,
41 impl<'a
> FileSearch
<'a
> {
42 pub fn for_each_lib_search_path
<F
>(&self, mut f
: F
) where
43 F
: FnMut(&Path
, PathKind
)
45 let mut visited_dirs
= HashSet
::new();
47 for (path
, kind
) in self.search_paths
.iter(self.kind
) {
49 visited_dirs
.insert(path
.to_path_buf());
52 debug
!("filesearch: searching lib path");
53 let tlib_path
= make_target_lib_path(self.sysroot
,
55 if !visited_dirs
.contains(&tlib_path
) {
56 f(&tlib_path
, PathKind
::All
);
59 visited_dirs
.insert(tlib_path
);
62 pub fn get_lib_path(&self) -> PathBuf
{
63 make_target_lib_path(self.sysroot
, self.triple
)
66 pub fn search
<F
>(&self, mut pick
: F
)
67 where F
: FnMut(&Path
, PathKind
) -> FileMatch
69 self.for_each_lib_search_path(|lib_search_path
, kind
| {
70 debug
!("searching {}", lib_search_path
.display());
71 let files
= match fs
::read_dir(lib_search_path
) {
75 let files
= files
.filter_map(|p
| p
.ok().map(|s
| s
.path()))
77 fn is_rlib(p
: &Path
) -> bool
{
78 p
.extension() == Some("rlib".as_ref())
80 // Reading metadata out of rlibs is faster, and if we find both
81 // an rlib and a dylib we only read one of the files of
82 // metadata, so in the name of speed, bring all rlib files to
83 // the front of the search list.
84 let files1
= files
.iter().filter(|p
| is_rlib(p
));
85 let files2
= files
.iter().filter(|p
| !is_rlib(p
));
86 for path
in files1
.chain(files2
) {
87 debug
!("testing {}", path
.display());
88 let maybe_picked
= pick(path
, kind
);
91 debug
!("picked {}", path
.display());
94 debug
!("rejected {}", path
.display());
101 pub fn new(sysroot
: &'a Path
,
103 search_paths
: &'a SearchPaths
,
104 kind
: PathKind
) -> FileSearch
<'a
> {
105 debug
!("using sysroot = {}, triple = {}", sysroot
.display(), triple
);
108 search_paths
: search_paths
,
114 // Returns a list of directories where target-specific dylibs might be located.
115 pub fn get_dylib_search_paths(&self) -> Vec
<PathBuf
> {
116 let mut paths
= Vec
::new();
117 self.for_each_lib_search_path(|lib_search_path
, _
| {
118 paths
.push(lib_search_path
.to_path_buf());
123 // Returns a list of directories where target-specific tool binaries are located.
124 pub fn get_tools_search_paths(&self) -> Vec
<PathBuf
> {
125 let mut p
= PathBuf
::from(self.sysroot
);
126 p
.push(find_libdir(self.sysroot
).as_ref());
127 p
.push(RUST_LIB_DIR
);
128 p
.push(&self.triple
);
134 pub fn relative_target_lib_path(sysroot
: &Path
, target_triple
: &str) -> PathBuf
{
135 let mut p
= PathBuf
::from(find_libdir(sysroot
).as_ref());
136 assert
!(p
.is_relative());
137 p
.push(RUST_LIB_DIR
);
138 p
.push(target_triple
);
143 fn make_target_lib_path(sysroot
: &Path
,
144 target_triple
: &str) -> PathBuf
{
145 sysroot
.join(&relative_target_lib_path(sysroot
, target_triple
))
148 pub fn get_or_default_sysroot() -> PathBuf
{
149 // Follow symlinks. If the resolved path is relative, make it absolute.
150 fn canonicalize(path
: Option
<PathBuf
>) -> Option
<PathBuf
> {
151 path
.and_then(|path
| {
152 match fs
::canonicalize(&path
) {
153 // See comments on this target function, but the gist is that
154 // gcc chokes on verbatim paths which fs::canonicalize generates
155 // so we try to avoid those kinds of paths.
156 Ok(canon
) => Some(rustcfs
::fix_windows_verbatim_for_gcc(&canon
)),
157 Err(e
) => bug
!("failed to get realpath: {}", e
),
162 match env
::current_exe() {
164 match canonicalize(Some(exe
)) {
165 Some(mut p
) => { p.pop(); p.pop(); return p; }
,
166 None
=> bug
!("can't determine value for sysroot")
169 Err(ref e
) => panic
!(format
!("failed to get current_exe: {}", e
))
173 // The name of the directory rustc expects libraries to be located.
174 fn find_libdir(sysroot
: &Path
) -> Cow
<'
static, str> {
175 // FIXME: This is a quick hack to make the rustc binary able to locate
176 // Rust libraries in Linux environments where libraries might be installed
177 // to lib64/lib32. This would be more foolproof by basing the sysroot off
178 // of the directory where librustc is located, rather than where the rustc
180 //If --libdir is set during configuration to the value other than
181 // "lib" (i.e. non-default), this value is used (see issue #16552).
183 match option_env
!("CFG_LIBDIR_RELATIVE") {
184 Some(libdir
) if libdir
!= "lib" => return libdir
.into(),
185 _
=> if sysroot
.join(PRIMARY_LIB_DIR
).join(RUST_LIB_DIR
).exists() {
186 return PRIMARY_LIB_DIR
.into();
188 return SECONDARY_LIB_DIR
.into();
192 #[cfg(target_pointer_width = "64")]
193 const PRIMARY_LIB_DIR
: &'
static str = "lib64";
195 #[cfg(target_pointer_width = "32")]
196 const PRIMARY_LIB_DIR
: &'
static str = "lib32";
198 const SECONDARY_LIB_DIR
: &'
static str = "lib";
201 // The name of rustc's own place to organize libraries.
202 // Used to be "rustc", now the default is "rustlib"
203 const RUST_LIB_DIR
: &'
static str = "rustlib";