]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_session/src/utils.rs
Rollup merge of #103110 - RalfJung:manual-send, r=thomcc
[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     pub fn is_statically_included(&self) -> bool {
58         matches!(self, NativeLibKind::Static { .. })
59     }
60
61     pub fn is_dllimport(&self) -> bool {
62         matches!(
63             self,
64             NativeLibKind::Dylib { .. } | NativeLibKind::RawDylib | NativeLibKind::Unspecified
65         )
66     }
67 }
68
69 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
70 #[derive(HashStable_Generic)]
71 pub struct NativeLib {
72     pub name: String,
73     pub new_name: Option<String>,
74     pub kind: NativeLibKind,
75     pub verbatim: Option<bool>,
76 }
77
78 impl NativeLib {
79     pub fn has_modifiers(&self) -> bool {
80         self.verbatim.is_some() || self.kind.has_modifiers()
81     }
82 }
83
84 /// A path that has been canonicalized along with its original, non-canonicalized form
85 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
86 pub struct CanonicalizedPath {
87     // Optional since canonicalization can sometimes fail
88     canonicalized: Option<PathBuf>,
89     original: PathBuf,
90 }
91
92 impl CanonicalizedPath {
93     pub fn new(path: &Path) -> Self {
94         Self { original: path.to_owned(), canonicalized: std::fs::canonicalize(path).ok() }
95     }
96
97     pub fn canonicalized(&self) -> &PathBuf {
98         self.canonicalized.as_ref().unwrap_or(self.original())
99     }
100
101     pub fn original(&self) -> &PathBuf {
102         &self.original
103     }
104 }