]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_session/src/utils.rs
Rollup merge of #99709 - lcnr:rm-MaybeTypeckResults, r=compiler-errors
[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     /// The library kind wasn't specified, `Dylib` is currently used as a default.
38     Unspecified,
39 }
40
41 impl NativeLibKind {
42     pub fn has_modifiers(&self) -> bool {
43         match self {
44             NativeLibKind::Static { bundle, whole_archive } => {
45                 bundle.is_some() || whole_archive.is_some()
46             }
47             NativeLibKind::Dylib { as_needed } | NativeLibKind::Framework { as_needed } => {
48                 as_needed.is_some()
49             }
50             NativeLibKind::RawDylib | NativeLibKind::Unspecified => false,
51         }
52     }
53 }
54
55 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
56 #[derive(HashStable_Generic)]
57 pub struct NativeLib {
58     pub name: String,
59     pub new_name: Option<String>,
60     pub kind: NativeLibKind,
61     pub verbatim: Option<bool>,
62 }
63
64 impl NativeLib {
65     pub fn has_modifiers(&self) -> bool {
66         self.verbatim.is_some() || self.kind.has_modifiers()
67     }
68 }
69
70 /// A path that has been canonicalized along with its original, non-canonicalized form
71 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
72 pub struct CanonicalizedPath {
73     // Optional since canonicalization can sometimes fail
74     canonicalized: Option<PathBuf>,
75     original: PathBuf,
76 }
77
78 impl CanonicalizedPath {
79     pub fn new(path: &Path) -> Self {
80         Self { original: path.to_owned(), canonicalized: std::fs::canonicalize(path).ok() }
81     }
82
83     pub fn canonicalized(&self) -> &PathBuf {
84         self.canonicalized.as_ref().unwrap_or(self.original())
85     }
86
87     pub fn original(&self) -> &PathBuf {
88         &self.original
89     }
90 }