Skip to main content

rustc_session/
search_paths.rs

1use std::path::{Path, PathBuf};
2use std::sync::Arc;
3
4use rustc_macros::{Decodable, Encodable, StableHash};
5use rustc_target::spec::TargetTuple;
6
7use crate::EarlyDiagCtxt;
8use crate::filesearch::make_target_lib_path;
9
10/// Directory containing object/library files, passed through the command-line `-L` flag.
11#[derive(#[automatically_derived]
impl ::core::clone::Clone for SearchPath {
    #[inline]
    fn clone(&self) -> SearchPath {
        SearchPath {
            kind: ::core::clone::Clone::clone(&self.kind),
            dir: ::core::clone::Clone::clone(&self.dir),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for SearchPath {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "SearchPath",
            "kind", &self.kind, "dir", &&self.dir)
    }
}Debug)]
12pub struct SearchPath {
13    pub kind: PathKind,
14    pub dir: Arc<Path>,
15}
16
17#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for PathKind {
    #[inline]
    fn eq(&self, other: &PathKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::clone::Clone for PathKind {
    #[inline]
    fn clone(&self) -> PathKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PathKind { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for PathKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                PathKind::Native => "Native",
                PathKind::Crate => "Crate",
                PathKind::Dependency => "Dependency",
                PathKind::Framework => "Framework",
                PathKind::All => "All",
            })
    }
}Debug, #[automatically_derived]
impl ::core::hash::Hash for PathKind {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash, #[automatically_derived]
impl ::core::cmp::Eq for PathKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for PathKind {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        PathKind::Native => { 0usize }
                        PathKind::Crate => { 1usize }
                        PathKind::Dependency => { 2usize }
                        PathKind::Framework => { 3usize }
                        PathKind::All => { 4usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for PathKind {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { PathKind::Native }
                    1usize => { PathKind::Crate }
                    2usize => { PathKind::Dependency }
                    3usize => { PathKind::Framework }
                    4usize => { PathKind::All }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `PathKind`, expected 0..5, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for PathKind {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    PathKind::Native => {}
                    PathKind::Crate => {}
                    PathKind::Dependency => {}
                    PathKind::Framework => {}
                    PathKind::All => {}
                }
            }
        }
    };StableHash)]
18pub enum PathKind {
19    Native,
20    Crate,
21    Dependency,
22    Framework,
23    All,
24}
25
26impl PathKind {
27    pub fn matches(&self, kind: PathKind) -> bool {
28        match (self, kind) {
29            (PathKind::All, _) | (_, PathKind::All) => true,
30            _ => *self == kind,
31        }
32    }
33}
34
35impl SearchPath {
36    pub fn from_cli_opt(
37        sysroot: &Path,
38        triple: &TargetTuple,
39        early_dcx: &EarlyDiagCtxt,
40        path: &str,
41        is_unstable_enabled: bool,
42    ) -> Self {
43        let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
44            (PathKind::Native, stripped)
45        } else if let Some(stripped) = path.strip_prefix("crate=") {
46            (PathKind::Crate, stripped)
47        } else if let Some(stripped) = path.strip_prefix("dependency=") {
48            (PathKind::Dependency, stripped)
49        } else if let Some(stripped) = path.strip_prefix("framework=") {
50            (PathKind::Framework, stripped)
51        } else if let Some(stripped) = path.strip_prefix("all=") {
52            (PathKind::All, stripped)
53        } else {
54            (PathKind::All, path)
55        };
56        let dir = match path.strip_prefix("@RUSTC_BUILTIN") {
57            Some(stripped) => {
58                if !is_unstable_enabled {
59                    early_dcx.early_fatal(
60                        "the `-Z unstable-options` flag must also be passed to \
61                         enable the use of `@RUSTC_BUILTIN`",
62                    );
63                }
64
65                make_target_lib_path(sysroot, triple.tuple()).join("builtin").join(stripped)
66            }
67            None => PathBuf::from(path),
68        };
69        if dir.as_os_str().is_empty() {
70            early_dcx.early_fatal("empty search path given via `-L`");
71        }
72
73        Self::new(kind, dir)
74    }
75
76    pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
77        Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
78    }
79
80    fn new(kind: PathKind, dir: PathBuf) -> Self {
81        SearchPath { kind, dir: dir.into() }
82    }
83}