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