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