rust/compiler/rustc_session/src/search_paths.rs

96 lines
3.1 KiB
Rust
Raw Normal View History

2019-11-29 21:05:28 +00:00
use crate::filesearch::make_target_lib_path;
2019-12-22 22:42:04 +00:00
use crate::{config, early_error};
use std::path::{Path, PathBuf};
2015-01-28 13:34:18 +00:00
#[derive(Clone, Debug)]
pub struct SearchPath {
pub kind: PathKind,
pub dir: PathBuf,
pub files: Vec<SearchPathFile>,
}
// The obvious implementation of `SearchPath::files` is a `Vec<PathBuf>`. But
// it is searched repeatedly by `find_library_crate`, and the searches involve
// checking the prefix and suffix of the filename of each `PathBuf`. This is
// doable, but very slow, because it involves calls to `file_name` and
// `extension` that are themselves slow.
//
// This type augments the `PathBuf` with an `Option<String>` containing the
// `PathBuf`'s filename. The prefix and suffix checking is much faster on the
// `Option<String>` than the `PathBuf`. (It's an `Option` because
// `Path::file_name` can fail; if that happens then all subsequent checking
// will also fail, which is fine.)
#[derive(Clone, Debug)]
pub struct SearchPathFile {
pub path: PathBuf,
pub file_name_str: Option<String>,
}
impl SearchPathFile {
fn new(path: PathBuf) -> SearchPathFile {
let file_name_str = path.file_name().and_then(|f| f.to_str()).map(|s| s.to_string());
SearchPathFile { path, file_name_str }
}
}
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable)]
pub enum PathKind {
Native,
Crate,
Dependency,
Framework,
rustc: Fix a leak in dependency= paths With the addition of separate search paths to the compiler, it was intended that applications such as Cargo could require a `--extern` flag per `extern crate` directive in the source. The system can currently be subverted, however, due to the `existing_match()` logic in the crate loader. When loading crates we first attempt to match an `extern crate` directive against all previously loaded crates to avoid reading metadata twice. This "hit the cache if possible" step was erroneously leaking crates across the search path boundaries, however. For example: extern crate b; extern crate a; If `b` depends on `a`, then it will load crate `a` when the `extern crate b` directive is being processed. When the compiler reaches `extern crate a` it will use the previously loaded version no matter what. If the compiler was not invoked with `-L crate=path/to/a`, it will still succeed. This behavior is allowing `extern crate` declarations in Cargo without a corresponding declaration in the manifest of a dependency, which is considered a bug. This commit fixes this problem by keeping track of the origin search path for a crate. Crates loaded from the dependency search path are not candidates for crates which are loaded from the crate search path. As a result of this fix, this is a likely a breaking change for a number of Cargo packages. If the compiler starts informing that a crate can no longer be found, it likely means that the dependency was forgotten in your Cargo.toml. [breaking-change]
2015-01-06 16:46:07 +00:00
ExternFlag,
All,
}
2019-11-29 21:05:28 +00:00
rustc_data_structures::impl_stable_hash_via_hash!(PathKind);
impl PathKind {
pub fn matches(&self, kind: PathKind) -> bool {
match (self, kind) {
(PathKind::All, _) | (_, PathKind::All) => true,
_ => *self == kind,
}
}
}
impl SearchPath {
pub fn from_cli_opt(path: &str, output: config::ErrorOutputType) -> Self {
let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
(PathKind::Native, stripped)
} else if let Some(stripped) = path.strip_prefix("crate=") {
(PathKind::Crate, stripped)
} else if let Some(stripped) = path.strip_prefix("dependency=") {
(PathKind::Dependency, stripped)
} else if let Some(stripped) = path.strip_prefix("framework=") {
(PathKind::Framework, stripped)
} else if let Some(stripped) = path.strip_prefix("all=") {
(PathKind::All, stripped)
} else {
(PathKind::All, path)
};
if path.is_empty() {
early_error(output, "empty search path given via `-L`");
}
let dir = PathBuf::from(path);
Self::new(kind, dir)
}
2015-01-02 04:26:38 +00:00
pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
}
fn new(kind: PathKind, dir: PathBuf) -> Self {
// Get the files within the directory.
let files = match std::fs::read_dir(&dir) {
Ok(files) => files
.filter_map(|e| e.ok().map(|e| SearchPathFile::new(e.path())))
.collect::<Vec<_>>(),
Err(..) => vec![],
};
SearchPath { kind, dir, files }
}
}