2019-11-29 21:05:28 +00:00
|
|
|
use crate::filesearch::make_target_lib_path;
|
|
|
|
use crate::{config, early_error};
|
2015-02-27 05:00:43 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2014-12-16 22:32:02 +00:00
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2018-11-22 05:33:07 +00:00
|
|
|
pub struct SearchPath {
|
|
|
|
pub kind: PathKind,
|
|
|
|
pub dir: PathBuf,
|
2020-04-06 07:44:30 +00:00
|
|
|
pub files: Vec<SearchPathFile>,
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:31:29 +00:00
|
|
|
/// 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.
|
|
|
|
///
|
2022-02-02 02:16:25 +00:00
|
|
|
/// This type augments the `PathBuf` with an `String` containing the
|
2021-08-31 16:31:29 +00:00
|
|
|
/// `PathBuf`'s filename. The prefix and suffix checking is much faster on the
|
2022-02-02 02:16:25 +00:00
|
|
|
/// `String` than the `PathBuf`. (The filename must be valid UTF-8. If it's
|
|
|
|
/// not, the entry should be skipped, because all Rust output files are valid
|
|
|
|
/// UTF-8, and so a non-UTF-8 filename couldn't be one we're looking for.)
|
2020-04-06 07:44:30 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct SearchPathFile {
|
|
|
|
pub path: PathBuf,
|
2022-02-02 02:16:25 +00:00
|
|
|
pub file_name_str: String,
|
2014-12-16 22:32:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable)]
|
2014-12-16 22:32:02 +00:00
|
|
|
pub enum PathKind {
|
|
|
|
Native,
|
|
|
|
Crate,
|
|
|
|
Dependency,
|
2015-02-04 21:47:06 +00:00
|
|
|
Framework,
|
2015-01-06 16:46:07 +00:00
|
|
|
ExternFlag,
|
2014-12-16 22:32:02 +00:00
|
|
|
All,
|
|
|
|
}
|
|
|
|
|
2019-11-29 21:05:28 +00:00
|
|
|
rustc_data_structures::impl_stable_hash_via_hash!(PathKind);
|
|
|
|
|
2018-11-22 05:33:07 +00:00
|
|
|
impl PathKind {
|
|
|
|
pub fn matches(&self, kind: PathKind) -> bool {
|
|
|
|
match (self, kind) {
|
|
|
|
(PathKind::All, _) | (_, PathKind::All) => true,
|
|
|
|
_ => *self == kind,
|
|
|
|
}
|
2014-12-16 22:32:02 +00:00
|
|
|
}
|
2018-11-22 05:33:07 +00:00
|
|
|
}
|
2014-12-16 22:32:02 +00:00
|
|
|
|
2018-11-22 05:33:07 +00:00
|
|
|
impl SearchPath {
|
|
|
|
pub fn from_cli_opt(path: &str, output: config::ErrorOutputType) -> Self {
|
2020-09-17 08:13:16 +00:00
|
|
|
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)
|
2014-12-16 22:32:02 +00:00
|
|
|
} else {
|
|
|
|
(PathKind::All, path)
|
|
|
|
};
|
Reject `-L ""`, `-L native=`, and other empty search paths.
It wasn't clear to me that early_error was correct here, but it seems to
work. This code is reachable from `rustdoc`, which is problematic, because
early_error panics. rustc handles the panics gracefully (without ICEing or
crashing), but rustdoc does not. It's not the first such rustdoc problem,
though:
$ rustdoc hello.rs --extern std=bad-std
error: extern location for std does not exist: bad-std
hello.rs:1:1: 1:1 error: can't find crate for `std`
hello.rs:1
^
error: aborting due to 2 previous errors
thread '<unnamed>' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:151
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "rustc failed"', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcore/result.rs:744
thread '<main>' panicked at 'child thread None panicked', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thread.rs:661
2015-03-14 06:49:44 +00:00
|
|
|
if path.is_empty() {
|
2015-12-31 03:50:06 +00:00
|
|
|
early_error(output, "empty search path given via `-L`");
|
Reject `-L ""`, `-L native=`, and other empty search paths.
It wasn't clear to me that early_error was correct here, but it seems to
work. This code is reachable from `rustdoc`, which is problematic, because
early_error panics. rustc handles the panics gracefully (without ICEing or
crashing), but rustdoc does not. It's not the first such rustdoc problem,
though:
$ rustdoc hello.rs --extern std=bad-std
error: extern location for std does not exist: bad-std
hello.rs:1:1: 1:1 error: can't find crate for `std`
hello.rs:1
^
error: aborting due to 2 previous errors
thread '<unnamed>' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:151
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: "rustc failed"', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcore/result.rs:744
thread '<main>' panicked at 'child thread None panicked', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thread.rs:661
2015-03-14 06:49:44 +00:00
|
|
|
}
|
2014-12-16 22:32:02 +00:00
|
|
|
|
2018-11-22 05:33:07 +00:00
|
|
|
let dir = PathBuf::from(path);
|
|
|
|
Self::new(kind, dir)
|
2014-12-16 22:32:02 +00:00
|
|
|
}
|
2015-01-02 04:26:38 +00:00
|
|
|
|
2018-11-22 05:33:07 +00:00
|
|
|
pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
|
|
|
|
Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
|
2014-12-16 22:32:02 +00:00
|
|
|
}
|
2018-03-20 09:33:59 +00:00
|
|
|
|
2018-11-22 05:33:07 +00:00
|
|
|
fn new(kind: PathKind, dir: PathBuf) -> Self {
|
2018-11-20 00:06:45 +00:00
|
|
|
// Get the files within the directory.
|
|
|
|
let files = match std::fs::read_dir(&dir) {
|
2020-04-06 07:44:30 +00:00
|
|
|
Ok(files) => files
|
2022-02-02 02:16:25 +00:00
|
|
|
.filter_map(|e| {
|
|
|
|
e.ok().and_then(|e| {
|
|
|
|
e.file_name().to_str().map(|s| SearchPathFile {
|
|
|
|
path: e.path(),
|
|
|
|
file_name_str: s.to_string(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-04-06 07:44:30 +00:00
|
|
|
.collect::<Vec<_>>(),
|
2018-11-20 00:06:45 +00:00
|
|
|
Err(..) => vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
SearchPath { kind, dir, files }
|
2018-03-20 09:33:59 +00:00
|
|
|
}
|
2014-12-16 22:32:02 +00:00
|
|
|
}
|