]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_session/src/utils.rs
Rollup merge of #102736 - GuillaumeGomez:search-input-color, r=notriddle
[rust.git] / compiler / rustc_session / src / utils.rs
1 use crate::session::Session;
2 use rustc_data_structures::profiling::VerboseTimingGuard;
3 use std::path::{Path, PathBuf};
4
5 impl Session {
6     pub fn timer<'a>(&'a self, what: &'static str) -> VerboseTimingGuard<'a> {
7         self.prof.verbose_generic_activity(what)
8     }
9     pub fn time<R>(&self, what: &'static str, f: impl FnOnce() -> R) -> R {
10         self.prof.verbose_generic_activity(what).run(f)
11     }
12 }
13
14 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
15 #[derive(HashStable_Generic)]
16 pub enum NativeLibKind {
17     /// Static library (e.g. `libfoo.a` on Linux or `foo.lib` on Windows/MSVC)
18     Static {
19         /// Whether to bundle objects from static library into produced rlib
20         bundle: Option<bool>,
21         /// Whether to link static library without throwing any object files away
22         whole_archive: Option<bool>,
23     },
24     /// Dynamic library (e.g. `libfoo.so` on Linux)
25     /// or an import library corresponding to a dynamic library (e.g. `foo.lib` on Windows/MSVC).
26     Dylib {
27         /// Whether the dynamic library will be linked only if it satisfies some undefined symbols
28         as_needed: Option<bool>,
29     },
30     /// Dynamic library (e.g. `foo.dll` on Windows) without a corresponding import library.
31     RawDylib,
32     /// A macOS-specific kind of dynamic libraries.
33     Framework {
34         /// Whether the framework will be linked only if it satisfies some undefined symbols
35         as_needed: Option<bool>,
36     },
37     /// Argument which is passed to linker, relative order with libraries and other arguments
38     /// is preserved
39     LinkArg,
40     /// The library kind wasn't specified, `Dylib` is currently used as a default.
41     Unspecified,
42 }
43
44 impl NativeLibKind {
45     pub fn has_modifiers(&self) -> bool {
46         match self {
47             NativeLibKind::Static { bundle, whole_archive } => {
48                 bundle.is_some() || whole_archive.is_some()
49             }
50             NativeLibKind::Dylib { as_needed } | NativeLibKind::Framework { as_needed } => {
51                 as_needed.is_some()
52             }
53             NativeLibKind::RawDylib | NativeLibKind::Unspecified | NativeLibKind::LinkArg => false,
54         }
55     }
56 }
57
58 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
59 #[derive(HashStable_Generic)]
60 pub struct NativeLib {
61     pub name: String,
62     pub new_name: Option<String>,
63     pub kind: NativeLibKind,
64     pub verbatim: Option<bool>,
65 }
66
67 impl NativeLib {
68     pub fn has_modifiers(&self) -> bool {
69         self.verbatim.is_some() || self.kind.has_modifiers()
70     }
71 }
72
73 /// A path that has been canonicalized along with its original, non-canonicalized form
74 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
75 pub struct CanonicalizedPath {
76     // Optional since canonicalization can sometimes fail
77     canonicalized: Option<PathBuf>,
78     original: PathBuf,
79 }
80
81 impl CanonicalizedPath {
82     pub fn new(path: &Path) -> Self {
83         Self { original: path.to_owned(), canonicalized: std::fs::canonicalize(path).ok() }
84     }
85
86     pub fn canonicalized(&self) -> &PathBuf {
87         self.canonicalized.as_ref().unwrap_or(self.original())
88     }
89
90     pub fn original(&self) -> &PathBuf {
91         &self.original
92     }
93 }