]> git.proxmox.com Git - rustc.git/blame - src/librustc/session/search_paths.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / src / librustc / session / search_paths.rs
CommitLineData
c34b1796 1use std::path::{Path, PathBuf};
9fa01778
XL
2use crate::session::{early_error, config};
3use crate::session::filesearch::make_target_lib_path;
1a4d82fc 4
85aaf69f 5#[derive(Clone, Debug)]
0731742a
XL
6pub struct SearchPath {
7 pub kind: PathKind,
8 pub dir: PathBuf,
9 pub files: Vec<PathBuf>,
1a4d82fc
JJ
10}
11
e74abb32 12#[derive(PartialEq, Clone, Copy, Debug, HashStable)]
1a4d82fc
JJ
13pub enum PathKind {
14 Native,
15 Crate,
16 Dependency,
85aaf69f
SL
17 Framework,
18 ExternFlag,
1a4d82fc
JJ
19 All,
20}
21
0731742a
XL
22impl PathKind {
23 pub fn matches(&self, kind: PathKind) -> bool {
24 match (self, kind) {
25 (PathKind::All, _) | (_, PathKind::All) => true,
26 _ => *self == kind,
27 }
1a4d82fc 28 }
0731742a 29}
1a4d82fc 30
0731742a
XL
31impl SearchPath {
32 pub fn from_cli_opt(path: &str, output: config::ErrorOutputType) -> Self {
1a4d82fc 33 let (kind, path) = if path.starts_with("native=") {
85aaf69f 34 (PathKind::Native, &path["native=".len()..])
1a4d82fc 35 } else if path.starts_with("crate=") {
85aaf69f 36 (PathKind::Crate, &path["crate=".len()..])
1a4d82fc 37 } else if path.starts_with("dependency=") {
85aaf69f
SL
38 (PathKind::Dependency, &path["dependency=".len()..])
39 } else if path.starts_with("framework=") {
40 (PathKind::Framework, &path["framework=".len()..])
1a4d82fc 41 } else if path.starts_with("all=") {
85aaf69f 42 (PathKind::All, &path["all=".len()..])
1a4d82fc
JJ
43 } else {
44 (PathKind::All, path)
45 };
c34b1796 46 if path.is_empty() {
9cc50fc6 47 early_error(output, "empty search path given via `-L`");
c34b1796 48 }
1a4d82fc 49
0731742a
XL
50 let dir = PathBuf::from(path);
51 Self::new(kind, dir)
1a4d82fc 52 }
1a4d82fc 53
0731742a
XL
54 pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
55 Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
56 }
1a4d82fc 57
0731742a
XL
58 fn new(kind: PathKind, dir: PathBuf) -> Self {
59 // Get the files within the directory.
60 let files = match std::fs::read_dir(&dir) {
61 Ok(files) => {
62 files.filter_map(|p| {
63 p.ok().map(|s| s.path())
64 })
65 .collect::<Vec<_>>()
1a4d82fc 66 }
0731742a
XL
67 Err(..) => vec![],
68 };
0531ce1d 69
0731742a 70 SearchPath { kind, dir, files }
0531ce1d 71 }
1a4d82fc 72}