rustc_session/
utils.rs

1use std::path::PathBuf;
2use std::sync::OnceLock;
3
4use rustc_data_structures::profiling::VerboseTimingGuard;
5use rustc_fs_util::try_canonicalize;
6use rustc_hir::attrs::NativeLibKind;
7use rustc_macros::{Decodable, Encodable, HashStable_Generic};
8
9use crate::session::Session;
10
11impl Session {
12    pub fn timer(&self, what: &'static str) -> VerboseTimingGuard<'_> {
13        self.prof.verbose_generic_activity(what)
14    }
15    /// Used by `-Z self-profile`.
16    pub fn time<R>(&self, what: &'static str, f: impl FnOnce() -> R) -> R {
17        self.prof.verbose_generic_activity(what).run(f)
18    }
19}
20
21#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
22#[derive(HashStable_Generic)]
23pub struct NativeLib {
24    pub name: String,
25    pub new_name: Option<String>,
26    pub kind: NativeLibKind,
27    pub verbatim: Option<bool>,
28}
29
30impl NativeLib {
31    pub fn has_modifiers(&self) -> bool {
32        self.verbatim.is_some() || self.kind.has_modifiers()
33    }
34}
35
36/// A path that has been canonicalized along with its original, non-canonicalized form
37#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
38pub struct CanonicalizedPath {
39    // Optional since canonicalization can sometimes fail
40    canonicalized: Option<PathBuf>,
41    original: PathBuf,
42}
43
44impl CanonicalizedPath {
45    pub fn new(path: PathBuf) -> Self {
46        Self { canonicalized: try_canonicalize(&path).ok(), original: path }
47    }
48
49    pub fn canonicalized(&self) -> &PathBuf {
50        self.canonicalized.as_ref().unwrap_or(self.original())
51    }
52
53    pub fn original(&self) -> &PathBuf {
54        &self.original
55    }
56}
57
58/// Gets a list of extra command-line flags provided by the user, as strings.
59///
60/// This function is used during ICEs to show more information useful for
61/// debugging, since some ICEs only happens with non-default compiler flags
62/// (and the users don't always report them).
63pub fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
64    const ICE_REPORT_COMPILER_FLAGS: &[&str] = &["-Z", "-C", "--crate-type"];
65
66    const ICE_REPORT_COMPILER_FLAGS_EXCLUDE: &[&str] = &["metadata", "extra-filename"];
67
68    const ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE: &[&str] = &["incremental"];
69
70    let mut args = std::env::args_os().map(|arg| arg.to_string_lossy().to_string());
71
72    let mut result = Vec::new();
73    let mut excluded_cargo_defaults = false;
74    while let Some(arg) = args.next() {
75        if let Some(a) = ICE_REPORT_COMPILER_FLAGS.iter().find(|a| arg.starts_with(*a)) {
76            let content = if arg.len() == a.len() {
77                // A space-separated option, like `-C incremental=foo` or `--crate-type rlib`
78                match args.next() {
79                    Some(arg) => arg,
80                    None => continue,
81                }
82            } else if arg.get(a.len()..a.len() + 1) == Some("=") {
83                // An equals option, like `--crate-type=rlib`
84                arg[a.len() + 1..].to_string()
85            } else {
86                // A non-space option, like `-Cincremental=foo`
87                arg[a.len()..].to_string()
88            };
89            let option = content.split_once('=').map(|s| s.0).unwrap_or(&content);
90            if ICE_REPORT_COMPILER_FLAGS_EXCLUDE.contains(&option) {
91                excluded_cargo_defaults = true;
92            } else {
93                result.push(a.to_string());
94                result.push(if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&option) {
95                    format!("{option}=[REDACTED]")
96                } else {
97                    content
98                });
99            }
100        }
101    }
102
103    if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
104}
105
106/// Returns whenever rustc was launched by Cargo as opposed to another build system.
107///
108/// To be used in diagnostics to avoid printing Cargo specific suggestions to other
109/// build systems (like Bazel, Buck2, Makefile, ...).
110pub fn was_invoked_from_cargo() -> bool {
111    static FROM_CARGO: OnceLock<bool> = OnceLock::new();
112
113    // To be able to detect Cargo, we use the simplest and least intrusive
114    // way: we check whenever the `CARGO_CRATE_NAME` env is set.
115    //
116    // Note that it is common in Makefiles to define the `CARGO` env even
117    // though we may not have been called by Cargo, so we avoid using it.
118    *FROM_CARGO.get_or_init(|| std::env::var_os("CARGO_CRATE_NAME").is_some())
119}