Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1 | //! Contains infrastructure for configuring the compiler, including parsing |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 2 | //! command-line options. |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 3 | |
Nicholas Nethercote | b7d58ee | 2024-02-20 03:12:50 | [diff] [blame] | 4 | #![allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable |
| 5 | |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 6 | use std::collections::btree_map::{ |
| 7 | Iter as BTreeMapIter, Keys as BTreeMapKeysIter, Values as BTreeMapValuesIter, |
| 8 | }; |
| 9 | use std::collections::{BTreeMap, BTreeSet}; |
| 10 | use std::ffi::OsStr; |
| 11 | use std::hash::Hash; |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | use std::str::{self, FromStr}; |
| 14 | use std::sync::LazyLock; |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 15 | use std::{cmp, fmt, fs, iter}; |
Victor Ding | 83fc600 | 2019-12-17 12:22:55 | [diff] [blame] | 16 | |
Urgau | 5b14497 | 2024-04-05 21:01:40 | [diff] [blame] | 17 | use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; |
Michael Woerister | 3a58309 | 2022-12-02 14:14:49 | [diff] [blame] | 18 | use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey}; |
Nicholas Nethercote | 3521abd | 2023-11-28 04:29:39 | [diff] [blame] | 19 | use rustc_errors::emitter::HumanReadableErrorType; |
Nicholas Nethercote | a09b1d3 | 2024-03-05 05:53:24 | [diff] [blame] | 20 | use rustc_errors::{ColorConfig, DiagArgValue, DiagCtxtFlags, IntoDiagArg}; |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 21 | use rustc_feature::UnstableFeatures; |
Nicholas Nethercote | 4814fd0 | 2024-04-28 22:53:45 | [diff] [blame] | 22 | use rustc_macros::{Decodable, Encodable, HashStable_Generic}; |
Michael Goulet | c682aa1 | 2024-09-22 23:05:04 | [diff] [blame] | 23 | use rustc_span::edition::{DEFAULT_EDITION, EDITION_NAME_LIST, Edition, LATEST_STABLE_EDITION}; |
Nicholas Nethercote | f405ce8 | 2023-11-02 03:10:12 | [diff] [blame] | 24 | use rustc_span::source_map::FilePathMapping; |
Kornel | 88b9edc | 2024-04-14 12:52:58 | [diff] [blame] | 25 | use rustc_span::{ |
Michael Goulet | c682aa1 | 2024-09-22 23:05:04 | [diff] [blame] | 26 | FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm, Symbol, sym, |
Kornel | 88b9edc | 2024-04-14 12:52:58 | [diff] [blame] | 27 | }; |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 28 | use rustc_target::spec::{ |
Noratrieb | a26450c | 2024-10-17 17:02:32 | [diff] [blame] | 29 | FramePointer, LinkSelfContainedComponents, LinkerFeatures, SplitDebuginfo, Target, TargetTuple, |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 30 | }; |
Nicholas Nethercote | 6341935 | 2024-04-29 06:24:06 | [diff] [blame] | 31 | use tracing::debug; |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 32 | |
Zalathar | 478db48 | 2024-11-11 10:39:52 | [diff] [blame] | 33 | pub use crate::config::cfg::{Cfg, CheckCfg, ExpectedValues}; |
Zalathar | 78edefe | 2024-11-11 10:42:42 | [diff] [blame] | 34 | use crate::config::native_libs::parse_native_libs; |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 35 | use crate::errors::FileWriteFail; |
| 36 | pub use crate::options::*; |
| 37 | use crate::search_paths::SearchPath; |
Zalathar | 478db48 | 2024-11-11 10:39:52 | [diff] [blame] | 38 | use crate::utils::CanonicalizedPath; |
Michael Goulet | c682aa1 | 2024-09-22 23:05:04 | [diff] [blame] | 39 | use crate::{EarlyDiagCtxt, HashStableContext, Session, filesearch, lint}; |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 40 | |
Urgau | 5b14497 | 2024-04-05 21:01:40 | [diff] [blame] | 41 | mod cfg; |
Zalathar | 478db48 | 2024-11-11 10:39:52 | [diff] [blame] | 42 | mod native_libs; |
Martin Nordholts | ddee45e | 2022-07-05 17:56:22 | [diff] [blame] | 43 | pub mod sigpipe; |
| 44 | |
Josh Triplett | e35b7bb | 2021-10-21 11:19:46 | [diff] [blame] | 45 | /// The different settings that the `-C strip` flag can have. |
YI | a6c2f73 | 2020-05-03 04:36:12 | [diff] [blame] | 46 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 47 | pub enum Strip { |
| 48 | /// Do not strip at all. |
| 49 | None, |
| 50 | |
| 51 | /// Strip debuginfo. |
| 52 | Debuginfo, |
| 53 | |
| 54 | /// Strip all symbols. |
| 55 | Symbols, |
| 56 | } |
| 57 | |
Andrew Paverd | 31c7aae | 2020-07-14 14:27:42 | [diff] [blame] | 58 | /// The different settings that the `-C control-flow-guard` flag can have. |
Andrew Paverd | c0744e1 | 2020-01-13 13:25:39 | [diff] [blame] | 59 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 60 | pub enum CFGuard { |
| 61 | /// Do not emit Control Flow Guard metadata or checks. |
| 62 | Disabled, |
| 63 | |
| 64 | /// Emit Control Flow Guard metadata but no checks. |
| 65 | NoChecks, |
| 66 | |
| 67 | /// Emit Control Flow Guard metadata and checks. |
| 68 | Checks, |
| 69 | } |
| 70 | |
Andrew Brown | 8d6c973 | 2022-01-28 17:48:59 | [diff] [blame] | 71 | /// The different settings that the `-Z cf-protection` flag can have. |
| 72 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 73 | pub enum CFProtection { |
| 74 | /// Do not enable control-flow protection |
| 75 | None, |
| 76 | |
| 77 | /// Emit control-flow protection for branches (enables indirect branch tracking). |
| 78 | Branch, |
| 79 | |
| 80 | /// Emit control-flow protection for returns. |
| 81 | Return, |
| 82 | |
| 83 | /// Emit control-flow protection for both branches and returns. |
| 84 | Full, |
| 85 | } |
| 86 | |
Michael Woerister | c0be619 | 2022-04-19 08:43:09 | [diff] [blame] | 87 | #[derive(Clone, Copy, Debug, PartialEq, Hash, HashStable_Generic)] |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 88 | pub enum OptLevel { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 89 | No, // -O0 |
| 90 | Less, // -O1 |
| 91 | Default, // -O2 |
Brandon Edens | b1337d3 | 2016-03-27 19:42:47 | [diff] [blame] | 92 | Aggressive, // -O3 |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 93 | Size, // -Os |
| 94 | SizeMin, // -Oz |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 95 | } |
| 96 | |
Michael Woerister | 24093a6 | 2018-09-04 15:57:17 | [diff] [blame] | 97 | /// This is what the `LtoCli` values get mapped to after resolving defaults and |
| 98 | /// and taking other command line options into account. |
Edd Barrett | 8cc918a | 2021-04-20 09:19:25 | [diff] [blame] | 99 | /// |
| 100 | /// Note that linker plugin-based LTO is a different mechanism entirely. |
Nicholas Nethercote | ac6daed | 2019-10-20 04:54:53 | [diff] [blame] | 101 | #[derive(Clone, PartialEq)] |
Alex Crichton | 8bde2ac | 2018-01-16 23:02:31 | [diff] [blame] | 102 | pub enum Lto { |
Edd Barrett | 8cc918a | 2021-04-20 09:19:25 | [diff] [blame] | 103 | /// Don't do any LTO whatsoever. |
Alex Crichton | 8bde2ac | 2018-01-16 23:02:31 | [diff] [blame] | 104 | No, |
| 105 | |
Edd Barrett | 8cc918a | 2021-04-20 09:19:25 | [diff] [blame] | 106 | /// Do a full-crate-graph (inter-crate) LTO with ThinLTO. |
Alex Crichton | 8bde2ac | 2018-01-16 23:02:31 | [diff] [blame] | 107 | Thin, |
| 108 | |
Edd Barrett | 8cc918a | 2021-04-20 09:19:25 | [diff] [blame] | 109 | /// Do a local ThinLTO (intra-crate, over the CodeGen Units of the local crate only). This is |
| 110 | /// only relevant if multiple CGUs are used. |
Alex Crichton | 8bde2ac | 2018-01-16 23:02:31 | [diff] [blame] | 111 | ThinLocal, |
| 112 | |
Edd Barrett | 8cc918a | 2021-04-20 09:19:25 | [diff] [blame] | 113 | /// Do a full-crate-graph (inter-crate) LTO with "fat" LTO. |
Alex Crichton | 8bde2ac | 2018-01-16 23:02:31 | [diff] [blame] | 114 | Fat, |
| 115 | } |
| 116 | |
Michael Woerister | 24093a6 | 2018-09-04 15:57:17 | [diff] [blame] | 117 | /// The different settings that the `-C lto` flag can have. |
| 118 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 119 | pub enum LtoCli { |
| 120 | /// `-C lto=no` |
| 121 | No, |
| 122 | /// `-C lto=yes` |
| 123 | Yes, |
| 124 | /// `-C lto` |
| 125 | NoParam, |
| 126 | /// `-C lto=thin` |
| 127 | Thin, |
| 128 | /// `-C lto=fat` |
| 129 | Fat, |
| 130 | /// No `-C lto` flag passed |
| 131 | Unspecified, |
| 132 | } |
| 133 | |
Josh Triplett | 34106f8 | 2021-10-21 14:04:22 | [diff] [blame] | 134 | /// The different settings that the `-C instrument-coverage` flag can have. |
Rich Kadel | bcf7555 | 2021-03-15 23:32:45 | [diff] [blame] | 135 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 136 | pub enum InstrumentCoverage { |
Zalathar | 9f287dd | 2023-10-25 03:57:25 | [diff] [blame] | 137 | /// `-C instrument-coverage=no` (or `off`, `false` etc.) |
| 138 | No, |
| 139 | /// `-C instrument-coverage` or `-C instrument-coverage=yes` |
| 140 | Yes, |
Rich Kadel | bcf7555 | 2021-03-15 23:32:45 | [diff] [blame] | 141 | } |
| 142 | |
Zalathar | aced4dc | 2024-12-19 10:53:10 | [diff] [blame] | 143 | /// Individual flag values controlled by `-Zcoverage-options`. |
Matthias Krüger | 0437a0c | 2024-03-17 12:37:54 | [diff] [blame] | 144 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)] |
Zalathar | 3407fcc | 2024-03-08 07:07:04 | [diff] [blame] | 145 | pub struct CoverageOptions { |
Zalathar | f926337 | 2024-04-29 06:19:55 | [diff] [blame] | 146 | pub level: CoverageLevel, |
Zalathar | abc2c70 | 2024-06-17 10:09:45 | [diff] [blame] | 147 | |
Zalathar | aced4dc | 2024-12-19 10:53:10 | [diff] [blame] | 148 | /// `-Zcoverage-options=no-mir-spans`: Don't extract block coverage spans |
Zalathar | abc2c70 | 2024-06-17 10:09:45 | [diff] [blame] | 149 | /// from MIR statements/terminators, making it easier to inspect/debug |
| 150 | /// branch and MC/DC coverage mappings. |
| 151 | /// |
| 152 | /// For internal debugging only. If other code changes would make it hard |
| 153 | /// to keep supporting this flag, remove it. |
| 154 | pub no_mir_spans: bool, |
Zalathar | aced4dc | 2024-12-19 10:53:10 | [diff] [blame] | 155 | |
| 156 | /// `-Zcoverage-options=discard-all-spans-in-codegen`: During codgen, |
| 157 | /// discard all coverage spans as though they were invalid. Needed by |
| 158 | /// regression tests for #133606, because we don't have an easy way to |
| 159 | /// reproduce it from actual source code. |
| 160 | pub discard_all_spans_in_codegen: bool, |
Zalathar | f926337 | 2024-04-29 06:19:55 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /// Controls whether branch coverage or MC/DC coverage is enabled. |
Esteban Küber | 1f82b45 | 2024-12-11 01:37:17 | [diff] [blame] | 164 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)] |
Zalathar | f926337 | 2024-04-29 06:19:55 | [diff] [blame] | 165 | pub enum CoverageLevel { |
| 166 | /// Instrument for coverage at the MIR block level. |
Esteban Küber | 1f82b45 | 2024-12-11 01:37:17 | [diff] [blame] | 167 | #[default] |
Zalathar | f926337 | 2024-04-29 06:19:55 | [diff] [blame] | 168 | Block, |
| 169 | /// Also instrument branch points (includes block coverage). |
| 170 | Branch, |
Dorian Péron | fa563c1 | 2024-04-26 13:01:06 | [diff] [blame] | 171 | /// Same as branch coverage, but also adds branch instrumentation for |
| 172 | /// certain boolean expressions that are not directly used for branching. |
| 173 | /// |
| 174 | /// For example, in the following code, `b` does not directly participate |
| 175 | /// in a branch, but condition coverage will instrument it as its own |
| 176 | /// artificial branch: |
| 177 | /// ``` |
| 178 | /// # let (a, b) = (false, true); |
| 179 | /// let x = a && b; |
| 180 | /// // ^ last operand |
| 181 | /// ``` |
| 182 | /// |
| 183 | /// This level is mainly intended to be a stepping-stone towards full MC/DC |
| 184 | /// instrumentation, so it might be removed in the future when MC/DC is |
| 185 | /// sufficiently complete, or if it is making MC/DC changes difficult. |
| 186 | Condition, |
| 187 | /// Instrument for MC/DC. Mostly a superset of condition coverage, but might |
Zalathar | f926337 | 2024-04-29 06:19:55 | [diff] [blame] | 188 | /// differ in some corner cases. |
| 189 | Mcdc, |
| 190 | } |
| 191 | |
Oleksii Lozovskyi | 0e60df9 | 2022-09-24 11:02:44 | [diff] [blame] | 192 | /// Settings for `-Z instrument-xray` flag. |
| 193 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] |
| 194 | pub struct InstrumentXRay { |
| 195 | /// `-Z instrument-xray=always`, force instrumentation |
| 196 | pub always: bool, |
| 197 | /// `-Z instrument-xray=never`, disable instrumentation |
| 198 | pub never: bool, |
| 199 | /// `-Z instrument-xray=ignore-loops`, ignore presence of loops, |
| 200 | /// instrument functions based only on instruction count |
| 201 | pub ignore_loops: bool, |
| 202 | /// `-Z instrument-xray=instruction-threshold=N`, explicitly set instruction threshold |
| 203 | /// for instrumentation, or `None` to use compiler's default |
| 204 | pub instruction_threshold: Option<usize>, |
| 205 | /// `-Z instrument-xray=skip-entry`, do not instrument function entry |
| 206 | pub skip_entry: bool, |
| 207 | /// `-Z instrument-xray=skip-exit`, do not instrument function exit |
| 208 | pub skip_exit: bool, |
| 209 | } |
| 210 | |
Joshua Nelson | fb7018b | 2021-04-16 03:06:32 | [diff] [blame] | 211 | #[derive(Clone, PartialEq, Hash, Debug)] |
Michael Woerister | 04f425d | 2019-02-01 14:15:43 | [diff] [blame] | 212 | pub enum LinkerPluginLto { |
Michael Woerister | a981089 | 2018-04-25 13:45:04 | [diff] [blame] | 213 | LinkerPlugin(PathBuf), |
Michael Woerister | 65ff414 | 2018-07-03 14:33:11 | [diff] [blame] | 214 | LinkerPluginAuto, |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 215 | Disabled, |
Michael Woerister | a981089 | 2018-04-25 13:45:04 | [diff] [blame] | 216 | } |
| 217 | |
Rémy Rakic | 5bc8870 | 2023-06-21 17:54:27 | [diff] [blame] | 218 | impl LinkerPluginLto { |
| 219 | pub fn enabled(&self) -> bool { |
| 220 | match *self { |
| 221 | LinkerPluginLto::LinkerPlugin(_) | LinkerPluginLto::LinkerPluginAuto => true, |
| 222 | LinkerPluginLto::Disabled => false, |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Rémy Rakic | 5d91c1c | 2023-06-21 21:45:13 | [diff] [blame] | 227 | /// The different values `-C link-self-contained` can take: a list of individually enabled or |
| 228 | /// disabled components used during linking, coming from the rustc distribution, instead of being |
| 229 | /// found somewhere on the host system. |
| 230 | /// |
| 231 | /// They can be set in bulk via `-C link-self-contained=yes|y|on` or `-C |
| 232 | /// link-self-contained=no|n|off`, and those boolean values are the historical defaults. |
| 233 | /// |
| 234 | /// But each component is fine-grained, and can be unstably targeted, to use: |
| 235 | /// - some CRT objects |
| 236 | /// - the libc static library |
| 237 | /// - libgcc/libunwind libraries |
| 238 | /// - a linker we distribute |
| 239 | /// - some sanitizer runtime libraries |
| 240 | /// - all other MinGW libraries and Windows import libs |
| 241 | /// |
| 242 | #[derive(Default, Clone, PartialEq, Debug)] |
| 243 | pub struct LinkSelfContained { |
| 244 | /// Whether the user explicitly set `-C link-self-contained` on or off, the historical values. |
| 245 | /// Used for compatibility with the existing opt-in and target inference. |
| 246 | pub explicitly_set: Option<bool>, |
| 247 | |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 248 | /// The components that are enabled on the CLI, using the `+component` syntax or one of the |
Alexander Cyon | 00de006 | 2024-09-02 05:50:22 | [diff] [blame] | 249 | /// `true` shortcuts. |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 250 | enabled_components: LinkSelfContainedComponents, |
| 251 | |
| 252 | /// The components that are disabled on the CLI, using the `-component` syntax or one of the |
| 253 | /// `false` shortcuts. |
| 254 | disabled_components: LinkSelfContainedComponents, |
Rémy Rakic | 5d91c1c | 2023-06-21 21:45:13 | [diff] [blame] | 255 | } |
| 256 | |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 257 | impl LinkSelfContained { |
| 258 | /// Incorporates an enabled or disabled component as specified on the CLI, if possible. |
| 259 | /// For example: `+linker`, and `-crto`. |
Rémy Rakic | 2ce46f8 | 2023-09-20 20:09:06 | [diff] [blame] | 260 | pub(crate) fn handle_cli_component(&mut self, component: &str) -> Option<()> { |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 261 | // Note that for example `-Cself-contained=y -Cself-contained=-linker` is not an explicit |
| 262 | // set of all values like `y` or `n` used to be. Therefore, if this flag had previously been |
| 263 | // set in bulk with its historical values, then manually setting a component clears that |
| 264 | // `explicitly_set` state. |
Matthias Krüger | 7a77089 | 2023-07-23 08:12:40 | [diff] [blame] | 265 | if let Some(component_to_enable) = component.strip_prefix('+') { |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 266 | self.explicitly_set = None; |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 267 | self.enabled_components |
| 268 | .insert(LinkSelfContainedComponents::from_str(component_to_enable)?); |
Rémy Rakic | 2ce46f8 | 2023-09-20 20:09:06 | [diff] [blame] | 269 | Some(()) |
Matthias Krüger | 7a77089 | 2023-07-23 08:12:40 | [diff] [blame] | 270 | } else if let Some(component_to_disable) = component.strip_prefix('-') { |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 271 | self.explicitly_set = None; |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 272 | self.disabled_components |
| 273 | .insert(LinkSelfContainedComponents::from_str(component_to_disable)?); |
Rémy Rakic | 2ce46f8 | 2023-09-20 20:09:06 | [diff] [blame] | 274 | Some(()) |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 275 | } else { |
Rémy Rakic | 2ce46f8 | 2023-09-20 20:09:06 | [diff] [blame] | 276 | None |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | /// Turns all components on or off and records that this was done explicitly for compatibility |
| 281 | /// purposes. |
| 282 | pub(crate) fn set_all_explicitly(&mut self, enabled: bool) { |
| 283 | self.explicitly_set = Some(enabled); |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 284 | |
| 285 | if enabled { |
| 286 | self.enabled_components = LinkSelfContainedComponents::all(); |
| 287 | self.disabled_components = LinkSelfContainedComponents::empty(); |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 288 | } else { |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 289 | self.enabled_components = LinkSelfContainedComponents::empty(); |
| 290 | self.disabled_components = LinkSelfContainedComponents::all(); |
| 291 | } |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /// Helper creating a fully enabled `LinkSelfContained` instance. Used in tests. |
| 295 | pub fn on() -> Self { |
| 296 | let mut on = LinkSelfContained::default(); |
| 297 | on.set_all_explicitly(true); |
| 298 | on |
| 299 | } |
Rémy Rakic | 1da271b | 2023-06-21 15:49:10 | [diff] [blame] | 300 | |
Rémy Rakic | 38dca73 | 2023-06-21 21:49:41 | [diff] [blame] | 301 | /// To help checking CLI usage while some of the values are unstable: returns whether one of the |
| 302 | /// components was set individually. This would also require the `-Zunstable-options` flag, to |
| 303 | /// be allowed. |
| 304 | fn are_unstable_variants_set(&self) -> bool { |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 305 | let any_component_set = |
| 306 | !self.enabled_components.is_empty() || !self.disabled_components.is_empty(); |
Rémy Rakic | 38dca73 | 2023-06-21 21:49:41 | [diff] [blame] | 307 | self.explicitly_set.is_none() && any_component_set |
| 308 | } |
| 309 | |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 310 | /// Returns whether the self-contained linker component was enabled on the CLI, using the |
Alexander Cyon | 00de006 | 2024-09-02 05:50:22 | [diff] [blame] | 311 | /// `-C link-self-contained=+linker` syntax, or one of the `true` shortcuts. |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 312 | pub fn is_linker_enabled(&self) -> bool { |
| 313 | self.enabled_components.contains(LinkSelfContainedComponents::LINKER) |
| 314 | } |
| 315 | |
| 316 | /// Returns whether the self-contained linker component was disabled on the CLI, using the |
Alexander Cyon | 00de006 | 2024-09-02 05:50:22 | [diff] [blame] | 317 | /// `-C link-self-contained=-linker` syntax, or one of the `false` shortcuts. |
Rémy Rakic | 5b9aa26 | 2023-09-20 20:25:17 | [diff] [blame] | 318 | pub fn is_linker_disabled(&self) -> bool { |
| 319 | self.disabled_components.contains(LinkSelfContainedComponents::LINKER) |
Rémy Rakic | 1da271b | 2023-06-21 15:49:10 | [diff] [blame] | 320 | } |
Rémy Rakic | 1394874 | 2023-09-21 13:27:45 | [diff] [blame] | 321 | |
| 322 | /// Returns CLI inconsistencies to emit errors: individual components were both enabled and |
| 323 | /// disabled. |
| 324 | fn check_consistency(&self) -> Option<LinkSelfContainedComponents> { |
| 325 | if self.explicitly_set.is_some() { |
| 326 | None |
| 327 | } else { |
| 328 | let common = self.enabled_components.intersection(self.disabled_components); |
| 329 | if common.is_empty() { None } else { Some(common) } |
| 330 | } |
| 331 | } |
Rémy Rakic | 0fb8071 | 2023-06-21 21:46:36 | [diff] [blame] | 332 | } |
| 333 | |
Rémy Rakic | 2398d8c | 2024-04-08 21:40:44 | [diff] [blame] | 334 | /// The different values that `-Z linker-features` can take on the CLI: a list of individually |
| 335 | /// enabled or disabled features used during linking. |
| 336 | /// |
| 337 | /// There is no need to enable or disable them in bulk. Each feature is fine-grained, and can be |
| 338 | /// used to turn `LinkerFeatures` on or off, without needing to change the linker flavor: |
| 339 | /// - using the system lld, or the self-contained `rust-lld` linker |
| 340 | /// - using a C/C++ compiler to drive the linker (not yet exposed on the CLI) |
| 341 | /// - etc. |
| 342 | #[derive(Default, Copy, Clone, PartialEq, Debug)] |
| 343 | pub struct LinkerFeaturesCli { |
| 344 | /// The linker features that are enabled on the CLI, using the `+feature` syntax. |
| 345 | pub enabled: LinkerFeatures, |
| 346 | |
| 347 | /// The linker features that are disabled on the CLI, using the `-feature` syntax. |
| 348 | pub disabled: LinkerFeatures, |
| 349 | } |
| 350 | |
| 351 | impl LinkerFeaturesCli { |
| 352 | /// Accumulates an enabled or disabled feature as specified on the CLI, if possible. |
| 353 | /// For example: `+lld`, and `-lld`. |
| 354 | pub(crate) fn handle_cli_feature(&mut self, feature: &str) -> Option<()> { |
| 355 | // Duplicate flags are reduced as we go, the last occurrence wins: |
| 356 | // `+feature,-feature,+feature` only enables the feature, and does not record it as both |
| 357 | // enabled and disabled on the CLI. |
Alexander Cyon | 00de006 | 2024-09-02 05:50:22 | [diff] [blame] | 358 | // We also only expose `+/-lld` at the moment, as it's currently the only implemented linker |
Rémy Rakic | 2398d8c | 2024-04-08 21:40:44 | [diff] [blame] | 359 | // feature and toggling `LinkerFeatures::CC` would be a noop. |
| 360 | match feature { |
| 361 | "+lld" => { |
| 362 | self.enabled.insert(LinkerFeatures::LLD); |
| 363 | self.disabled.remove(LinkerFeatures::LLD); |
| 364 | Some(()) |
| 365 | } |
| 366 | "-lld" => { |
| 367 | self.disabled.insert(LinkerFeatures::LLD); |
| 368 | self.enabled.remove(LinkerFeatures::LLD); |
| 369 | Some(()) |
| 370 | } |
| 371 | _ => None, |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
pierwill | 1642fdf | 2021-10-31 22:05:48 | [diff] [blame] | 376 | /// Used with `-Z assert-incr-state`. |
| 377 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 378 | pub enum IncrementalStateAssertion { |
| 379 | /// Found and loaded an existing session directory. |
| 380 | /// |
| 381 | /// Note that this says nothing about whether any particular query |
| 382 | /// will be found to be red or green. |
| 383 | Loaded, |
| 384 | /// Did not load an existing session directory. |
| 385 | NotLoaded, |
| 386 | } |
| 387 | |
Hudson Ayers | a9a1393 | 2021-10-14 00:01:31 | [diff] [blame] | 388 | /// The different settings that can be enabled via the `-Z location-detail` flag. |
Maybe Waffle | 09a8791 | 2023-04-13 18:04:30 | [diff] [blame] | 389 | #[derive(Copy, Clone, PartialEq, Hash, Debug)] |
Hudson Ayers | a9a1393 | 2021-10-14 00:01:31 | [diff] [blame] | 390 | pub struct LocationDetail { |
| 391 | pub file: bool, |
| 392 | pub line: bool, |
| 393 | pub column: bool, |
| 394 | } |
| 395 | |
| 396 | impl LocationDetail { |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 397 | pub(crate) fn all() -> Self { |
Hudson Ayers | a9a1393 | 2021-10-14 00:01:31 | [diff] [blame] | 398 | Self { file: true, line: true, column: true } |
| 399 | } |
| 400 | } |
| 401 | |
Kornel | 88b9edc | 2024-04-14 12:52:58 | [diff] [blame] | 402 | /// Values for the `-Z fmt-debug` flag. |
| 403 | #[derive(Copy, Clone, PartialEq, Hash, Debug)] |
| 404 | pub enum FmtDebug { |
| 405 | /// Derive fully-featured implementation |
| 406 | Full, |
| 407 | /// Print only type name, without fields |
| 408 | Shallow, |
| 409 | /// `#[derive(Debug)]` and `{:?}` are no-ops |
| 410 | None, |
| 411 | } |
| 412 | |
| 413 | impl FmtDebug { |
| 414 | pub(crate) fn all() -> [Symbol; 3] { |
| 415 | [sym::full, sym::none, sym::shallow] |
| 416 | } |
| 417 | } |
| 418 | |
Joshua Nelson | fb7018b | 2021-04-16 03:06:32 | [diff] [blame] | 419 | #[derive(Clone, PartialEq, Hash, Debug)] |
Michael Woerister | 64ee32e | 2019-05-28 14:13:59 | [diff] [blame] | 420 | pub enum SwitchWithOptPath { |
Michael Woerister | 7b1df42 | 2019-04-10 11:46:37 | [diff] [blame] | 421 | Enabled(Option<PathBuf>), |
| 422 | Disabled, |
| 423 | } |
| 424 | |
Michael Woerister | 64ee32e | 2019-05-28 14:13:59 | [diff] [blame] | 425 | impl SwitchWithOptPath { |
Michael Woerister | 7b1df42 | 2019-04-10 11:46:37 | [diff] [blame] | 426 | pub fn enabled(&self) -> bool { |
| 427 | match *self { |
Michael Woerister | 64ee32e | 2019-05-28 14:13:59 | [diff] [blame] | 428 | SwitchWithOptPath::Enabled(_) => true, |
| 429 | SwitchWithOptPath::Disabled => false, |
Michael Woerister | 7b1df42 | 2019-04-10 11:46:37 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
Michael Woerister | c0be619 | 2022-04-19 08:43:09 | [diff] [blame] | 434 | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable_Generic)] |
Matthew Jasper | cbcef3e | 2020-06-11 14:49:57 | [diff] [blame] | 435 | #[derive(Encodable, Decodable)] |
Eduard-Mihai Burtescu | 2092963 | 2019-01-29 05:24:32 | [diff] [blame] | 436 | pub enum SymbolManglingVersion { |
| 437 | Legacy, |
| 438 | V0, |
h1467792822 | 6e53e66 | 2023-12-05 04:42:57 | [diff] [blame] | 439 | Hashed, |
Eduard-Mihai Burtescu | 2092963 | 2019-01-29 05:24:32 | [diff] [blame] | 440 | } |
| 441 | |
J. Ryan Stinnett | 8b18f41 | 2020-11-28 15:07:51 | [diff] [blame] | 442 | #[derive(Clone, Copy, Debug, PartialEq, Hash)] |
Mark Rousskov | 2bc7197 | 2018-07-26 17:41:10 | [diff] [blame] | 443 | pub enum DebugInfo { |
| 444 | None, |
Julia Tatz | 0504a33 | 2021-04-06 20:00:35 | [diff] [blame] | 445 | LineDirectivesOnly, |
| 446 | LineTablesOnly, |
Mark Rousskov | 2bc7197 | 2018-07-26 17:41:10 | [diff] [blame] | 447 | Limited, |
| 448 | Full, |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 449 | } |
| 450 | |
Augie Fackler | af9e550 | 2023-07-12 21:07:34 | [diff] [blame] | 451 | #[derive(Clone, Copy, Debug, PartialEq, Hash)] |
| 452 | pub enum DebugInfoCompression { |
| 453 | None, |
| 454 | Zlib, |
| 455 | Zstd, |
| 456 | } |
| 457 | |
| 458 | impl ToString for DebugInfoCompression { |
| 459 | fn to_string(&self) -> String { |
| 460 | match self { |
| 461 | DebugInfoCompression::None => "none", |
| 462 | DebugInfoCompression::Zlib => "zlib", |
| 463 | DebugInfoCompression::Zstd => "zstd", |
| 464 | } |
| 465 | .to_owned() |
| 466 | } |
| 467 | } |
| 468 | |
Scott McMurray | a7fc76a | 2024-12-05 08:47:36 | [diff] [blame] | 469 | #[derive(Clone, Copy, Debug, PartialEq, Hash)] |
| 470 | pub enum MirStripDebugInfo { |
| 471 | None, |
| 472 | LocalsInTinyFunctions, |
| 473 | AllLocals, |
| 474 | } |
| 475 | |
David Wood | 08ed338 | 2021-10-08 16:10:17 | [diff] [blame] | 476 | /// Split debug-information is enabled by `-C split-debuginfo`, this enum is only used if split |
| 477 | /// debug-information is enabled (in either `Packed` or `Unpacked` modes), and the platform |
| 478 | /// uses DWARF for debug-information. |
| 479 | /// |
| 480 | /// Some debug-information requires link-time relocation and some does not. LLVM can partition |
| 481 | /// the debuginfo into sections depending on whether or not it requires link-time relocation. Split |
| 482 | /// DWARF provides a mechanism which allows the linker to skip the sections which don't require |
| 483 | /// link-time relocation - either by putting those sections in DWARF object files, or by keeping |
| 484 | /// them in the object file in such a way that the linker will skip them. |
| 485 | #[derive(Clone, Copy, Debug, PartialEq, Hash)] |
| 486 | pub enum SplitDwarfKind { |
| 487 | /// Sections which do not require relocation are written into object file but ignored by the |
| 488 | /// linker. |
| 489 | Single, |
| 490 | /// Sections which do not require relocation are written into a DWARF object (`.dwo`) file |
| 491 | /// which is ignored by the linker. |
| 492 | Split, |
| 493 | } |
| 494 | |
| 495 | impl FromStr for SplitDwarfKind { |
| 496 | type Err = (); |
| 497 | |
| 498 | fn from_str(s: &str) -> Result<Self, ()> { |
| 499 | Ok(match s { |
| 500 | "single" => SplitDwarfKind::Single, |
| 501 | "split" => SplitDwarfKind::Split, |
| 502 | _ => return Err(()), |
| 503 | }) |
| 504 | } |
| 505 | } |
| 506 | |
Michael Woerister | c0be619 | 2022-04-19 08:43:09 | [diff] [blame] | 507 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, HashStable_Generic)] |
Matthew Jasper | cbcef3e | 2020-06-11 14:49:57 | [diff] [blame] | 508 | #[derive(Encodable, Decodable)] |
Niko Matsakis | dc6e414 | 2014-11-16 01:30:33 | [diff] [blame] | 509 | pub enum OutputType { |
Alex Crichton | 8c963c0 | 2015-09-30 17:08:37 | [diff] [blame] | 510 | Bitcode, |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 511 | ThinLinkBitcode, |
Alex Crichton | 8c963c0 | 2015-09-30 17:08:37 | [diff] [blame] | 512 | Assembly, |
| 513 | LlvmAssembly, |
Jake Goulding | 9218f97 | 2017-02-16 21:59:09 | [diff] [blame] | 514 | Mir, |
Nick Cameron | 7720cf0 | 2016-12-23 06:39:20 | [diff] [blame] | 515 | Metadata, |
Alex Crichton | 8c963c0 | 2015-09-30 17:08:37 | [diff] [blame] | 516 | Object, |
| 517 | Exe, |
| 518 | DepInfo, |
Niko Matsakis | dc6e414 | 2014-11-16 01:30:33 | [diff] [blame] | 519 | } |
| 520 | |
Alan Egerton | 114dd20 | 2024-06-12 12:01:22 | [diff] [blame] | 521 | impl StableOrd for OutputType { |
Andrew Xie | 54d7b32 | 2023-06-08 04:38:50 | [diff] [blame] | 522 | const CAN_USE_UNSTABLE_SORT: bool = true; |
Alan Egerton | 0e73e70 | 2024-06-22 06:11:42 | [diff] [blame] | 523 | |
| 524 | // Trivial C-Style enums have a stable sort order across compilation sessions. |
| 525 | const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = (); |
Andrew Xie | 54d7b32 | 2023-06-08 04:38:50 | [diff] [blame] | 526 | } |
Michael Woerister | 3a58309 | 2022-12-02 14:14:49 | [diff] [blame] | 527 | |
Michael Woerister | c0be619 | 2022-04-19 08:43:09 | [diff] [blame] | 528 | impl<HCX: HashStableContext> ToStableHashKey<HCX> for OutputType { |
| 529 | type KeyType = Self; |
| 530 | |
| 531 | fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType { |
| 532 | *self |
| 533 | } |
| 534 | } |
Michael Woerister | 74d6b85 | 2017-09-18 10:14:52 | [diff] [blame] | 535 | |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 536 | impl OutputType { |
| 537 | fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool { |
| 538 | match *self { |
Alex Crichton | 955f283 | 2019-04-25 16:06:38 | [diff] [blame] | 539 | OutputType::Exe | OutputType::DepInfo | OutputType::Metadata => true, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 540 | OutputType::Bitcode |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 541 | | OutputType::ThinLinkBitcode |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 542 | | OutputType::Assembly |
| 543 | | OutputType::LlvmAssembly |
| 544 | | OutputType::Mir |
Alex Crichton | 955f283 | 2019-04-25 16:06:38 | [diff] [blame] | 545 | | OutputType::Object => false, |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 549 | pub fn shorthand(&self) -> &'static str { |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 550 | match *self { |
| 551 | OutputType::Bitcode => "llvm-bc", |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 552 | OutputType::ThinLinkBitcode => "thin-link-bitcode", |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 553 | OutputType::Assembly => "asm", |
| 554 | OutputType::LlvmAssembly => "llvm-ir", |
Jake Goulding | 9218f97 | 2017-02-16 21:59:09 | [diff] [blame] | 555 | OutputType::Mir => "mir", |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 556 | OutputType::Object => "obj", |
Nick Cameron | 7720cf0 | 2016-12-23 06:39:20 | [diff] [blame] | 557 | OutputType::Metadata => "metadata", |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 558 | OutputType::Exe => "link", |
| 559 | OutputType::DepInfo => "dep-info", |
| 560 | } |
| 561 | } |
Niko Matsakis | 2f9fff21 | 2016-07-25 14:51:14 | [diff] [blame] | 562 | |
Corey Farwell | c3ea358 | 2017-11-05 14:20:59 | [diff] [blame] | 563 | fn from_shorthand(shorthand: &str) -> Option<Self> { |
| 564 | Some(match shorthand { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 565 | "asm" => OutputType::Assembly, |
| 566 | "llvm-ir" => OutputType::LlvmAssembly, |
| 567 | "mir" => OutputType::Mir, |
| 568 | "llvm-bc" => OutputType::Bitcode, |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 569 | "thin-link-bitcode" => OutputType::ThinLinkBitcode, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 570 | "obj" => OutputType::Object, |
| 571 | "metadata" => OutputType::Metadata, |
| 572 | "link" => OutputType::Exe, |
| 573 | "dep-info" => OutputType::DepInfo, |
Corey Farwell | c3ea358 | 2017-11-05 14:20:59 | [diff] [blame] | 574 | _ => return None, |
| 575 | }) |
| 576 | } |
| 577 | |
| 578 | fn shorthands_display() -> String { |
| 579 | format!( |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 580 | "`{}`, `{}`, `{}`, `{}`, `{}`, `{}`, `{}`, `{}`, `{}`", |
Corey Farwell | c3ea358 | 2017-11-05 14:20:59 | [diff] [blame] | 581 | OutputType::Bitcode.shorthand(), |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 582 | OutputType::ThinLinkBitcode.shorthand(), |
Corey Farwell | c3ea358 | 2017-11-05 14:20:59 | [diff] [blame] | 583 | OutputType::Assembly.shorthand(), |
| 584 | OutputType::LlvmAssembly.shorthand(), |
| 585 | OutputType::Mir.shorthand(), |
| 586 | OutputType::Object.shorthand(), |
| 587 | OutputType::Metadata.shorthand(), |
| 588 | OutputType::Exe.shorthand(), |
| 589 | OutputType::DepInfo.shorthand(), |
| 590 | ) |
| 591 | } |
| 592 | |
Niko Matsakis | 2f9fff21 | 2016-07-25 14:51:14 | [diff] [blame] | 593 | pub fn extension(&self) -> &'static str { |
| 594 | match *self { |
| 595 | OutputType::Bitcode => "bc", |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 596 | OutputType::ThinLinkBitcode => "indexing.o", |
Niko Matsakis | 2f9fff21 | 2016-07-25 14:51:14 | [diff] [blame] | 597 | OutputType::Assembly => "s", |
| 598 | OutputType::LlvmAssembly => "ll", |
Jake Goulding | 9218f97 | 2017-02-16 21:59:09 | [diff] [blame] | 599 | OutputType::Mir => "mir", |
Niko Matsakis | 2f9fff21 | 2016-07-25 14:51:14 | [diff] [blame] | 600 | OutputType::Object => "o", |
Nick Cameron | 7720cf0 | 2016-12-23 06:39:20 | [diff] [blame] | 601 | OutputType::Metadata => "rmeta", |
Niko Matsakis | 2f9fff21 | 2016-07-25 14:51:14 | [diff] [blame] | 602 | OutputType::DepInfo => "d", |
| 603 | OutputType::Exe => "", |
| 604 | } |
| 605 | } |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 606 | |
| 607 | pub fn is_text_output(&self) -> bool { |
| 608 | match *self { |
| 609 | OutputType::Assembly |
| 610 | | OutputType::LlvmAssembly |
| 611 | | OutputType::Mir |
| 612 | | OutputType::DepInfo => true, |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 613 | OutputType::Bitcode |
| 614 | | OutputType::ThinLinkBitcode |
| 615 | | OutputType::Object |
| 616 | | OutputType::Metadata |
| 617 | | OutputType::Exe => false, |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 618 | } |
| 619 | } |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 620 | } |
| 621 | |
Philipp Hansch | 33137ff | 2019-06-10 08:59:03 | [diff] [blame] | 622 | /// The type of diagnostics output to generate. |
Nick Cameron | b286a2f | 2016-10-25 22:14:02 | [diff] [blame] | 623 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 624 | pub enum ErrorOutputType { |
Philipp Hansch | e3516a1 | 2019-06-09 10:04:40 | [diff] [blame] | 625 | /// Output meant for the consumption of humans. |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 626 | HumanReadable(HumanReadableErrorType, ColorConfig), |
Philipp Hansch | e3516a1 | 2019-06-09 10:04:40 | [diff] [blame] | 627 | /// Output that's consumed by other tools such as `rustfix` or the `RLS`. |
Oliver Scherer | 96404ee | 2019-03-12 12:06:43 | [diff] [blame] | 628 | Json { |
Philipp Hansch | 33137ff | 2019-06-10 08:59:03 | [diff] [blame] | 629 | /// Render the JSON in a human readable way (with indents and newlines). |
Oliver Scherer | 96404ee | 2019-03-12 12:06:43 | [diff] [blame] | 630 | pretty: bool, |
Philipp Hansch | e3516a1 | 2019-06-09 10:04:40 | [diff] [blame] | 631 | /// The JSON output includes a `rendered` field that includes the rendered |
| 632 | /// human output. |
Oliver Scherer | 39b2137 | 2019-03-25 10:16:58 | [diff] [blame] | 633 | json_rendered: HumanReadableErrorType, |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 634 | color_config: ColorConfig, |
Oliver Scherer | 96404ee | 2019-03-12 12:06:43 | [diff] [blame] | 635 | }, |
Nick Cameron | b286a2f | 2016-10-25 22:14:02 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | impl Default for ErrorOutputType { |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 639 | fn default() -> Self { |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 640 | Self::HumanReadable(HumanReadableErrorType::Default, ColorConfig::Auto) |
Nick Cameron | b286a2f | 2016-10-25 22:14:02 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
bohan | f34678c | 2023-05-11 18:09:46 | [diff] [blame] | 644 | #[derive(Clone, Hash, Debug)] |
Vadim Petrochenkov | da4ce6b | 2023-02-06 17:57:45 | [diff] [blame] | 645 | pub enum ResolveDocLinks { |
| 646 | /// Do not resolve doc links. |
| 647 | None, |
| 648 | /// Resolve doc links on exported items only for crate types that have metadata. |
| 649 | ExportedMetadata, |
| 650 | /// Resolve doc links on exported items. |
| 651 | Exported, |
| 652 | /// Resolve doc links on all items. |
| 653 | All, |
| 654 | } |
| 655 | |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 656 | /// Use tree-based collections to cheaply get a deterministic `Hash` implementation. |
| 657 | /// *Do not* switch `BTreeMap` out for an unsorted container type! That would break |
Jeremy Fitzhardinge | a26d99f | 2021-06-05 22:43:12 | [diff] [blame] | 658 | /// dependency tracking for command-line arguments. Also only hash keys, since tracking |
| 659 | /// should only depend on the output types, not the paths they're written to. |
bjorn3 | 98a6eaa | 2023-11-04 15:49:57 | [diff] [blame] | 660 | #[derive(Clone, Debug, Hash, HashStable_Generic, Encodable, Decodable)] |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 661 | pub struct OutputTypes(BTreeMap<OutputType, Option<OutFileName>>); |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 662 | |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 663 | impl OutputTypes { |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 664 | pub fn new(entries: &[(OutputType, Option<OutFileName>)]) -> OutputTypes { |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 665 | OutputTypes(BTreeMap::from_iter(entries.iter().map(|&(k, ref v)| (k, v.clone())))) |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 666 | } |
Niko Matsakis | d09fd1a | 2016-01-29 20:07:04 | [diff] [blame] | 667 | |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 668 | pub(crate) fn get(&self, key: &OutputType) -> Option<&Option<OutFileName>> { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 669 | self.0.get(key) |
| 670 | } |
Niko Matsakis | d09fd1a | 2016-01-29 20:07:04 | [diff] [blame] | 671 | |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 672 | pub fn contains_key(&self, key: &OutputType) -> bool { |
| 673 | self.0.contains_key(key) |
| 674 | } |
| 675 | |
Michael Baikov | bf12aa4 | 2024-03-21 20:13:46 | [diff] [blame] | 676 | /// Returns `true` if user specified a name and not just produced type |
| 677 | pub fn contains_explicit_name(&self, key: &OutputType) -> bool { |
Yotam Ofek | 264fa0f | 2025-01-19 19:15:00 | [diff] [blame^] | 678 | self.0.get(key).is_some_and(|f| f.is_some()) |
Michael Baikov | bf12aa4 | 2024-03-21 20:13:46 | [diff] [blame] | 679 | } |
| 680 | |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 681 | pub fn iter(&self) -> BTreeMapIter<'_, OutputType, Option<OutFileName>> { |
| 682 | self.0.iter() |
| 683 | } |
| 684 | |
| 685 | pub fn keys(&self) -> BTreeMapKeysIter<'_, OutputType, Option<OutFileName>> { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 686 | self.0.keys() |
| 687 | } |
| 688 | |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 689 | pub fn values(&self) -> BTreeMapValuesIter<'_, OutputType, Option<OutFileName>> { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 690 | self.0.values() |
| 691 | } |
Nick Cameron | b059a80 | 2016-12-29 00:23:38 | [diff] [blame] | 692 | |
varkor | 8414520 | 2018-03-27 23:13:34 | [diff] [blame] | 693 | pub fn len(&self) -> usize { |
| 694 | self.0.len() |
| 695 | } |
| 696 | |
David Wood | 08ed338 | 2021-10-08 16:10:17 | [diff] [blame] | 697 | /// Returns `true` if any of the output types require codegen or linking. |
Irina Popa | b63d7e2 | 2018-05-08 13:10:16 | [diff] [blame] | 698 | pub fn should_codegen(&self) -> bool { |
Nick Cameron | b059a80 | 2016-12-29 00:23:38 | [diff] [blame] | 699 | self.0.keys().any(|k| match *k { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 700 | OutputType::Bitcode |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 701 | | OutputType::ThinLinkBitcode |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 702 | | OutputType::Assembly |
| 703 | | OutputType::LlvmAssembly |
| 704 | | OutputType::Mir |
| 705 | | OutputType::Object |
| 706 | | OutputType::Exe => true, |
| 707 | OutputType::Metadata | OutputType::DepInfo => false, |
Nick Cameron | b059a80 | 2016-12-29 00:23:38 | [diff] [blame] | 708 | }) |
| 709 | } |
Miguel Ojeda | f9275e1 | 2021-01-17 14:06:47 | [diff] [blame] | 710 | |
David Wood | 08ed338 | 2021-10-08 16:10:17 | [diff] [blame] | 711 | /// Returns `true` if any of the output types require linking. |
Miguel Ojeda | f9275e1 | 2021-01-17 14:06:47 | [diff] [blame] | 712 | pub fn should_link(&self) -> bool { |
| 713 | self.0.keys().any(|k| match *k { |
| 714 | OutputType::Bitcode |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 715 | | OutputType::ThinLinkBitcode |
Miguel Ojeda | f9275e1 | 2021-01-17 14:06:47 | [diff] [blame] | 716 | | OutputType::Assembly |
| 717 | | OutputType::LlvmAssembly |
| 718 | | OutputType::Mir |
| 719 | | OutputType::Metadata |
| 720 | | OutputType::Object |
| 721 | | OutputType::DepInfo => false, |
| 722 | OutputType::Exe => true, |
| 723 | }) |
| 724 | } |
Brian Anderson | c27133e | 2015-01-06 14:26:08 | [diff] [blame] | 725 | } |
| 726 | |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 727 | /// Use tree-based collections to cheaply get a deterministic `Hash` implementation. |
| 728 | /// *Do not* switch `BTreeMap` or `BTreeSet` out for an unsorted container type! That |
| 729 | /// would break dependency tracking for command-line arguments. |
Nicholas Nethercote | ac6daed | 2019-10-20 04:54:53 | [diff] [blame] | 730 | #[derive(Clone)] |
Aaron Hill | 482b77a | 2019-04-07 22:48:40 | [diff] [blame] | 731 | pub struct Externs(BTreeMap<String, ExternEntry>); |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 732 | |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 733 | #[derive(Clone, Debug)] |
Aaron Hill | 7cc3ce3 | 2019-03-25 03:06:32 | [diff] [blame] | 734 | pub struct ExternEntry { |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 735 | pub location: ExternLocation, |
| 736 | /// Indicates this is a "private" dependency for the |
| 737 | /// `exported_private_dependencies` lint. |
| 738 | /// |
| 739 | /// This can be set with the `priv` option like |
| 740 | /// `--extern priv:name=foo.rlib`. |
| 741 | pub is_private_dep: bool, |
| 742 | /// Add the extern entry to the extern prelude. |
| 743 | /// |
| 744 | /// This can be disabled with the `noprelude` option like |
| 745 | /// `--extern noprelude:name`. |
| 746 | pub add_prelude: bool, |
Jeremy Fitzhardinge | 9102edf | 2022-04-14 22:09:00 | [diff] [blame] | 747 | /// The extern entry shouldn't be considered for unused dependency warnings. |
| 748 | /// |
| 749 | /// `--extern nounused:std=/path/to/lib/libstd.rlib`. This is used to |
| 750 | /// suppress `unused-crate-dependencies` warnings. |
| 751 | pub nounused_dep: bool, |
Matt Hammerly | 812f2d7 | 2023-03-14 03:55:43 | [diff] [blame] | 752 | /// If the extern entry is not referenced in the crate, force it to be resolved anyway. |
| 753 | /// |
| 754 | /// Allows a dependency satisfying, for instance, a missing panic handler to be injected |
| 755 | /// without modifying source: |
| 756 | /// `--extern force:extras=/path/to/lib/libstd.rlib` |
| 757 | pub force: bool, |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | #[derive(Clone, Debug)] |
| 761 | pub enum ExternLocation { |
| 762 | /// Indicates to look for the library in the search paths. |
| 763 | /// |
| 764 | /// Added via `--extern name`. |
| 765 | FoundInLibrarySearchDirectories, |
| 766 | /// The locations where this extern entry must be found. |
| 767 | /// |
| 768 | /// The `CrateLoader` is responsible for loading these and figuring out |
| 769 | /// which one to use. |
| 770 | /// |
| 771 | /// Added via `--extern prelude_name=some_file.rlib` |
Ryan Levick | 6c7ecd0 | 2021-01-26 21:27:42 | [diff] [blame] | 772 | ExactPaths(BTreeSet<CanonicalizedPath>), |
Aaron Hill | 7cc3ce3 | 2019-03-25 03:06:32 | [diff] [blame] | 773 | } |
Aaron Hill | 21491dc | 2019-03-21 03:27:08 | [diff] [blame] | 774 | |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 775 | impl Externs { |
Joshua Nelson | 441dc36 | 2021-03-16 05:50:34 | [diff] [blame] | 776 | /// Used for testing. |
Aaron Hill | 482b77a | 2019-04-07 22:48:40 | [diff] [blame] | 777 | pub fn new(data: BTreeMap<String, ExternEntry>) -> Externs { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 778 | Externs(data) |
| 779 | } |
| 780 | |
Aaron Hill | 482b77a | 2019-04-07 22:48:40 | [diff] [blame] | 781 | pub fn get(&self, key: &str) -> Option<&ExternEntry> { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 782 | self.0.get(key) |
| 783 | } |
| 784 | |
Jeremy Stucki | d28832d | 2019-06-21 21:49:03 | [diff] [blame] | 785 | pub fn iter(&self) -> BTreeMapIter<'_, String, ExternEntry> { |
Aaron Hill | 21491dc | 2019-03-21 03:27:08 | [diff] [blame] | 786 | self.0.iter() |
| 787 | } |
| 788 | } |
| 789 | |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 790 | impl ExternEntry { |
| 791 | fn new(location: ExternLocation) -> ExternEntry { |
Matt Hammerly | 812f2d7 | 2023-03-14 03:55:43 | [diff] [blame] | 792 | ExternEntry { |
| 793 | location, |
| 794 | is_private_dep: false, |
| 795 | add_prelude: false, |
| 796 | nounused_dep: false, |
| 797 | force: false, |
| 798 | } |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 799 | } |
| 800 | |
Ryan Levick | 6c7ecd0 | 2021-01-26 21:27:42 | [diff] [blame] | 801 | pub fn files(&self) -> Option<impl Iterator<Item = &CanonicalizedPath>> { |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 802 | match &self.location { |
| 803 | ExternLocation::ExactPaths(set) => Some(set.iter()), |
| 804 | _ => None, |
| 805 | } |
| 806 | } |
| 807 | } |
Aaron Hill | 21491dc | 2019-03-21 03:27:08 | [diff] [blame] | 808 | |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 809 | #[derive(Clone, PartialEq, Debug)] |
| 810 | pub struct PrintRequest { |
| 811 | pub kind: PrintKind, |
| 812 | pub out: OutFileName, |
| 813 | } |
| 814 | |
Robin Kruppe | e3f6e68 | 2017-04-30 18:33:25 | [diff] [blame] | 815 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 816 | pub enum PrintKind { |
Alex Crichton | 117984b | 2014-12-16 00:03:39 | [diff] [blame] | 817 | FileNames, |
Noratrieb | ba48151 | 2024-10-17 17:03:06 | [diff] [blame] | 818 | HostTuple, |
Alex Crichton | 117984b | 2014-12-16 00:03:39 | [diff] [blame] | 819 | Sysroot, |
O01eg | 4023e5df | 2020-03-01 11:33:52 | [diff] [blame] | 820 | TargetLibdir, |
Alex Crichton | 117984b | 2014-12-16 00:03:39 | [diff] [blame] | 821 | CrateName, |
Alex Crichton | a1ffe6b | 2016-01-25 19:36:18 | [diff] [blame] | 822 | Cfg, |
Urgau | ac59bdc | 2024-04-22 21:47:48 | [diff] [blame] | 823 | CheckCfg, |
khyperia | 9a206a7 | 2022-09-08 13:37:15 | [diff] [blame] | 824 | CallingConventions, |
Jorge Aparicio | 0bb4209 | 2016-02-12 15:11:58 | [diff] [blame] | 825 | TargetList, |
Cameron Hart | e1efa32 | 2016-07-10 14:22:13 | [diff] [blame] | 826 | TargetCPUs, |
| 827 | TargetFeatures, |
| 828 | RelocationModels, |
| 829 | CodeModels, |
Amanieu d'Antras | b233a6e | 2017-10-31 18:24:04 | [diff] [blame] | 830 | TlsModels, |
Doug Goldstein | ff11264 | 2016-04-07 21:36:35 | [diff] [blame] | 831 | TargetSpec, |
Pietro Albini | ef2bf6d | 2023-03-09 13:52:45 | [diff] [blame] | 832 | AllTargetSpecs, |
Kornel | 2920658 | 2017-08-22 20:20:42 | [diff] [blame] | 833 | NativeStaticLibs, |
Benjamin A. Bjørnseth | bb9dee9 | 2021-04-06 19:37:49 | [diff] [blame] | 834 | StackProtectorStrategies, |
Josh Triplett | cd626fe | 2021-12-06 22:09:24 | [diff] [blame] | 835 | LinkArgs, |
Kamil Koczurek | 4c3cad0 | 2022-11-07 14:21:35 | [diff] [blame] | 836 | SplitDebuginfo, |
BlackHoleFox | a427d41 | 2022-12-06 05:15:16 | [diff] [blame] | 837 | DeploymentTarget, |
Alex Crichton | 117984b | 2014-12-16 00:03:39 | [diff] [blame] | 838 | } |
| 839 | |
Michael Goulet | a4974fa | 2023-01-02 23:12:47 | [diff] [blame] | 840 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] |
lcnr | 5d97ada | 2023-12-14 12:00:23 | [diff] [blame] | 841 | pub struct NextSolverConfig { |
| 842 | /// Whether the new trait solver should be enabled in coherence. |
| 843 | pub coherence: bool, |
| 844 | /// Whether the new trait solver should be enabled everywhere. |
| 845 | /// This is only `true` if `coherence` is also enabled. |
| 846 | pub globally: bool, |
Boxy | 040aa58 | 2023-07-03 20:00:10 | [diff] [blame] | 847 | } |
lcnr | 1a9d2d8 | 2024-09-21 07:02:51 | [diff] [blame] | 848 | impl Default for NextSolverConfig { |
| 849 | fn default() -> Self { |
| 850 | NextSolverConfig { coherence: true, globally: false } |
| 851 | } |
| 852 | } |
Boxy | 040aa58 | 2023-07-03 20:00:10 | [diff] [blame] | 853 | |
Urgau | c773b19 | 2024-05-03 17:17:29 | [diff] [blame] | 854 | #[derive(Clone)] |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 855 | pub enum Input { |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 856 | /// Load source code from a file. |
Alex Crichton | 95d9046 | 2015-02-27 05:00:43 | [diff] [blame] | 857 | File(PathBuf), |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 858 | /// Load source code from a string. |
mitaa | ea7cf90 | 2016-03-10 03:49:40 | [diff] [blame] | 859 | Str { |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 860 | /// A string that is shown in place of a filename. |
Oliver Schneider | d732da8 | 2017-12-14 07:09:19 | [diff] [blame] | 861 | name: FileName, |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 862 | /// An anonymous string containing the source code. |
mitaa | ea7cf90 | 2016-03-10 03:49:40 | [diff] [blame] | 863 | input: String, |
| 864 | }, |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | impl Input { |
ljedrz | 675f00b | 2018-10-10 13:30:53 | [diff] [blame] | 868 | pub fn filestem(&self) -> &str { |
Chris Denton | c6d94821 | 2024-08-05 22:30:13 | [diff] [blame] | 869 | if let Input::File(ifile) = self { |
| 870 | // If for some reason getting the file stem as a UTF-8 string fails, |
| 871 | // then fallback to a fixed name. |
| 872 | if let Some(name) = ifile.file_stem().and_then(OsStr::to_str) { |
| 873 | return name; |
| 874 | } |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 875 | } |
Chris Denton | c6d94821 | 2024-08-05 22:30:13 | [diff] [blame] | 876 | "rust_out" |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 877 | } |
Guillaume Gomez | dadfa13 | 2018-05-10 18:13:25 | [diff] [blame] | 878 | |
John Kåre Alsaker | 23a51f9 | 2018-12-08 19:30:23 | [diff] [blame] | 879 | pub fn source_name(&self) -> FileName { |
| 880 | match *self { |
| 881 | Input::File(ref ifile) => ifile.clone().into(), |
| 882 | Input::Str { ref name, .. } => name.clone(), |
| 883 | } |
| 884 | } |
Oli Scherer | f5c6014 | 2022-12-06 18:56:28 | [diff] [blame] | 885 | |
Oli Scherer | 1355559 | 2023-01-16 14:27:33 | [diff] [blame] | 886 | pub fn opt_path(&self) -> Option<&Path> { |
Oli Scherer | f5c6014 | 2022-12-06 18:56:28 | [diff] [blame] | 887 | match self { |
Oli Scherer | 1355559 | 2023-01-16 14:27:33 | [diff] [blame] | 888 | Input::File(file) => Some(file), |
Oli Scherer | f5c6014 | 2022-12-06 18:56:28 | [diff] [blame] | 889 | Input::Str { name, .. } => match name { |
Oli Scherer | 1355559 | 2023-01-16 14:27:33 | [diff] [blame] | 890 | FileName::Real(real) => real.local_path(), |
Oli Scherer | f5c6014 | 2022-12-06 18:56:28 | [diff] [blame] | 891 | FileName::QuoteExpansion(_) => None, |
| 892 | FileName::Anon(_) => None, |
| 893 | FileName::MacroExpansion(_) => None, |
| 894 | FileName::ProcMacroSourceCode(_) => None, |
Oli Scherer | f5c6014 | 2022-12-06 18:56:28 | [diff] [blame] | 895 | FileName::CliCrateAttr(_) => None, |
| 896 | FileName::Custom(_) => None, |
Oli Scherer | 1355559 | 2023-01-16 14:27:33 | [diff] [blame] | 897 | FileName::DocTest(path, _) => Some(path), |
Oli Scherer | f5c6014 | 2022-12-06 18:56:28 | [diff] [blame] | 898 | FileName::InlineAsm(_) => None, |
| 899 | }, |
| 900 | } |
| 901 | } |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 902 | } |
| 903 | |
bjorn3 | 98a6eaa | 2023-11-04 15:49:57 | [diff] [blame] | 904 | #[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq, Encodable, Decodable)] |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 905 | pub enum OutFileName { |
| 906 | Real(PathBuf), |
| 907 | Stdout, |
| 908 | } |
| 909 | |
| 910 | impl OutFileName { |
| 911 | pub fn parent(&self) -> Option<&Path> { |
| 912 | match *self { |
| 913 | OutFileName::Real(ref path) => path.parent(), |
| 914 | OutFileName::Stdout => None, |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | pub fn filestem(&self) -> Option<&OsStr> { |
| 919 | match *self { |
| 920 | OutFileName::Real(ref path) => path.file_stem(), |
| 921 | OutFileName::Stdout => Some(OsStr::new("stdout")), |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | pub fn is_stdout(&self) -> bool { |
| 926 | match *self { |
| 927 | OutFileName::Real(_) => false, |
| 928 | OutFileName::Stdout => true, |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | pub fn is_tty(&self) -> bool { |
klensy | 3163085 | 2023-07-26 15:09:50 | [diff] [blame] | 933 | use std::io::IsTerminal; |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 934 | match *self { |
| 935 | OutFileName::Real(_) => false, |
klensy | 3163085 | 2023-07-26 15:09:50 | [diff] [blame] | 936 | OutFileName::Stdout => std::io::stdout().is_terminal(), |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
| 940 | pub fn as_path(&self) -> &Path { |
| 941 | match *self { |
| 942 | OutFileName::Real(ref path) => path.as_ref(), |
Nilstrieb | 21a8705 | 2023-11-21 19:07:32 | [diff] [blame] | 943 | OutFileName::Stdout => Path::new("stdout"), |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
| 947 | /// For a given output filename, return the actual name of the file that |
| 948 | /// can be used to write codegen data of type `flavor`. For real-path |
| 949 | /// output filenames, this would be trivial as we can just use the path. |
| 950 | /// Otherwise for stdout, return a temporary path so that the codegen data |
| 951 | /// may be later copied to stdout. |
| 952 | pub fn file_for_writing( |
| 953 | &self, |
| 954 | outputs: &OutputFilenames, |
| 955 | flavor: OutputType, |
| 956 | codegen_unit_name: Option<&str>, |
| 957 | ) -> PathBuf { |
| 958 | match *self { |
| 959 | OutFileName::Real(ref path) => path.clone(), |
| 960 | OutFileName::Stdout => outputs.temp_path(flavor, codegen_unit_name), |
| 961 | } |
| 962 | } |
David Tolnay | f2e3d3f | 2023-07-17 05:13:08 | [diff] [blame] | 963 | |
| 964 | pub fn overwrite(&self, content: &str, sess: &Session) { |
| 965 | match self { |
| 966 | OutFileName::Stdout => print!("{content}"), |
| 967 | OutFileName::Real(path) => { |
| 968 | if let Err(e) = fs::write(path, content) { |
Nicholas Nethercote | 99472c7 | 2023-12-18 11:21:37 | [diff] [blame] | 969 | sess.dcx().emit_fatal(FileWriteFail { path, err: e.to_string() }); |
David Tolnay | f2e3d3f | 2023-07-17 05:13:08 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | } |
| 973 | } |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 974 | } |
| 975 | |
bjorn3 | 98a6eaa | 2023-11-04 15:49:57 | [diff] [blame] | 976 | #[derive(Clone, Hash, Debug, HashStable_Generic, Encodable, Decodable)] |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 977 | pub struct OutputFilenames { |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 978 | pub(crate) out_directory: PathBuf, |
Martin Nordholts | 04d81ba | 2023-08-11 04:20:35 | [diff] [blame] | 979 | /// Crate name. Never contains '-'. |
| 980 | crate_stem: String, |
| 981 | /// Typically based on `.rs` input file name. Any '-' is preserved. |
Mark Rousskov | 8c6067c | 2020-01-21 14:54:58 | [diff] [blame] | 982 | filestem: String, |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 983 | pub single_output_file: Option<OutFileName>, |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 984 | temps_directory: Option<PathBuf>, |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 985 | pub outputs: OutputTypes, |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 986 | } |
| 987 | |
Victor Ding | a47fdb9 | 2020-01-23 10:48:48 | [diff] [blame] | 988 | pub const RLINK_EXT: &str = "rlink"; |
Vadim Petrochenkov | d588f93 | 2017-11-03 19:41:15 | [diff] [blame] | 989 | pub const RUST_CGU_EXT: &str = "rcgu"; |
David Wood | e3fdae9 | 2020-09-23 16:33:54 | [diff] [blame] | 990 | pub const DWARF_OBJECT_EXT: &str = "dwo"; |
Michael Woerister | 65e8a13 | 2016-05-14 00:48:32 | [diff] [blame] | 991 | |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 992 | impl OutputFilenames { |
Mark Rousskov | d1bb7e1 | 2020-01-21 14:50:22 | [diff] [blame] | 993 | pub fn new( |
| 994 | out_directory: PathBuf, |
Martin Nordholts | 04d81ba | 2023-08-11 04:20:35 | [diff] [blame] | 995 | out_crate_name: String, |
Mark Rousskov | d1bb7e1 | 2020-01-21 14:50:22 | [diff] [blame] | 996 | out_filestem: String, |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 997 | single_output_file: Option<OutFileName>, |
Tor Hovland | 5d1e09f | 2021-11-02 21:41:34 | [diff] [blame] | 998 | temps_directory: Option<PathBuf>, |
Mark Rousskov | d1bb7e1 | 2020-01-21 14:50:22 | [diff] [blame] | 999 | extra: String, |
| 1000 | outputs: OutputTypes, |
| 1001 | ) -> Self { |
Mark Rousskov | 8c6067c | 2020-01-21 14:54:58 | [diff] [blame] | 1002 | OutputFilenames { |
| 1003 | out_directory, |
| 1004 | single_output_file, |
Tor Hovland | 5d1e09f | 2021-11-02 21:41:34 | [diff] [blame] | 1005 | temps_directory, |
Mark Rousskov | 8c6067c | 2020-01-21 14:54:58 | [diff] [blame] | 1006 | outputs, |
Martin Nordholts | 04d81ba | 2023-08-11 04:20:35 | [diff] [blame] | 1007 | crate_stem: format!("{out_crate_name}{extra}"), |
Jubilee Young | de66e08 | 2022-03-25 05:11:05 | [diff] [blame] | 1008 | filestem: format!("{out_filestem}{extra}"), |
Mark Rousskov | 8c6067c | 2020-01-21 14:54:58 | [diff] [blame] | 1009 | } |
Mark Rousskov | d1bb7e1 | 2020-01-21 14:50:22 | [diff] [blame] | 1010 | } |
| 1011 | |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 1012 | pub fn path(&self, flavor: OutputType) -> OutFileName { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1013 | self.outputs |
| 1014 | .get(&flavor) |
| 1015 | .and_then(|p| p.to_owned()) |
Alex Crichton | 8c963c0 | 2015-09-30 17:08:37 | [diff] [blame] | 1016 | .or_else(|| self.single_output_file.clone()) |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 1017 | .unwrap_or_else(|| OutFileName::Real(self.output_path(flavor))) |
Tor Hovland | 0132adc | 2021-04-04 11:35:04 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /// Gets the output path where a compilation artifact of the given type |
| 1021 | /// should be placed on disk. |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1022 | fn output_path(&self, flavor: OutputType) -> PathBuf { |
Tor Hovland | 0132adc | 2021-04-04 11:35:04 | [diff] [blame] | 1023 | let extension = flavor.extension(); |
Martin Nordholts | 04d81ba | 2023-08-11 04:20:35 | [diff] [blame] | 1024 | match flavor { |
| 1025 | OutputType::Metadata => { |
| 1026 | self.out_directory.join(format!("lib{}.{}", self.crate_stem, extension)) |
| 1027 | } |
| 1028 | _ => self.with_directory_and_extension(&self.out_directory, extension), |
| 1029 | } |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 1030 | } |
| 1031 | |
Alexander Regueiro | c3e182c | 2019-02-08 13:53:55 | [diff] [blame] | 1032 | /// Gets the path where a compilation artifact of the given type for the |
Michael Woerister | 65e8a13 | 2016-05-14 00:48:32 | [diff] [blame] | 1033 | /// given codegen unit should be placed on disk. If codegen_unit_name is |
| 1034 | /// None, a path distinct from those of any codegen unit will be generated. |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1035 | pub fn temp_path(&self, flavor: OutputType, codegen_unit_name: Option<&str>) -> PathBuf { |
Niko Matsakis | 2f9fff21 | 2016-07-25 14:51:14 | [diff] [blame] | 1036 | let extension = flavor.extension(); |
Michael Woerister | 65e8a13 | 2016-05-14 00:48:32 | [diff] [blame] | 1037 | self.temp_path_ext(extension, codegen_unit_name) |
| 1038 | } |
| 1039 | |
David Wood | e3fdae9 | 2020-09-23 16:33:54 | [diff] [blame] | 1040 | /// Like `temp_path`, but specifically for dwarf objects. |
| 1041 | pub fn temp_path_dwo(&self, codegen_unit_name: Option<&str>) -> PathBuf { |
| 1042 | self.temp_path_ext(DWARF_OBJECT_EXT, codegen_unit_name) |
| 1043 | } |
| 1044 | |
| 1045 | /// Like `temp_path`, but also supports things where there is no corresponding |
Alexander Regueiro | c3e182c | 2019-02-08 13:53:55 | [diff] [blame] | 1046 | /// OutputType, like noopt-bitcode or lto-bitcode. |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1047 | pub fn temp_path_ext(&self, ext: &str, codegen_unit_name: Option<&str>) -> PathBuf { |
Michael Woerister | 65e8a13 | 2016-05-14 00:48:32 | [diff] [blame] | 1048 | let mut extension = String::new(); |
| 1049 | |
| 1050 | if let Some(codegen_unit_name) = codegen_unit_name { |
Alex Crichton | 4ca1b19 | 2017-07-23 15:14:38 | [diff] [blame] | 1051 | extension.push_str(codegen_unit_name); |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 1052 | } |
Michael Woerister | 65e8a13 | 2016-05-14 00:48:32 | [diff] [blame] | 1053 | |
| 1054 | if !ext.is_empty() { |
| 1055 | if !extension.is_empty() { |
Matthias Krüger | 9bb10cc | 2020-09-10 11:57:40 | [diff] [blame] | 1056 | extension.push('.'); |
Alex Crichton | 4ca1b19 | 2017-07-23 15:14:38 | [diff] [blame] | 1057 | extension.push_str(RUST_CGU_EXT); |
Matthias Krüger | 9bb10cc | 2020-09-10 11:57:40 | [diff] [blame] | 1058 | extension.push('.'); |
Michael Woerister | 65e8a13 | 2016-05-14 00:48:32 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | extension.push_str(ext); |
| 1062 | } |
| 1063 | |
Tor Hovland | 5d1e09f | 2021-11-02 21:41:34 | [diff] [blame] | 1064 | let temps_directory = self.temps_directory.as_ref().unwrap_or(&self.out_directory); |
| 1065 | |
Maybe Waffle | f2b97a8 | 2022-11-29 11:01:17 | [diff] [blame] | 1066 | self.with_directory_and_extension(temps_directory, &extension) |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 1067 | } |
| 1068 | |
Alex Crichton | 95d9046 | 2015-02-27 05:00:43 | [diff] [blame] | 1069 | pub fn with_extension(&self, extension: &str) -> PathBuf { |
Tor Hovland | 5d1e09f | 2021-11-02 21:41:34 | [diff] [blame] | 1070 | self.with_directory_and_extension(&self.out_directory, extension) |
| 1071 | } |
| 1072 | |
Integral | 7eb0d84 | 2024-12-17 16:28:34 | [diff] [blame] | 1073 | pub fn with_directory_and_extension(&self, directory: &Path, extension: &str) -> PathBuf { |
Tor Hovland | 5d1e09f | 2021-11-02 21:41:34 | [diff] [blame] | 1074 | let mut path = directory.join(&self.filestem); |
Mark Rousskov | dc97181 | 2020-01-21 14:57:50 | [diff] [blame] | 1075 | path.set_extension(extension); |
| 1076 | path |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 1077 | } |
David Wood | e3fdae9 | 2020-09-23 16:33:54 | [diff] [blame] | 1078 | |
| 1079 | /// Returns the path for the Split DWARF file - this can differ depending on which Split DWARF |
| 1080 | /// mode is being used, which is the logic that this function is intended to encapsulate. |
David Wood | ee073b5 | 2020-11-08 17:17:37 | [diff] [blame] | 1081 | pub fn split_dwarf_path( |
David Wood | e3fdae9 | 2020-09-23 16:33:54 | [diff] [blame] | 1082 | &self, |
Alex Crichton | a124043 | 2020-11-30 16:39:08 | [diff] [blame] | 1083 | split_debuginfo_kind: SplitDebuginfo, |
David Wood | 08ed338 | 2021-10-08 16:10:17 | [diff] [blame] | 1084 | split_dwarf_kind: SplitDwarfKind, |
David Wood | e3fdae9 | 2020-09-23 16:33:54 | [diff] [blame] | 1085 | cgu_name: Option<&str>, |
| 1086 | ) -> Option<PathBuf> { |
| 1087 | let obj_out = self.temp_path(OutputType::Object, cgu_name); |
| 1088 | let dwo_out = self.temp_path_dwo(cgu_name); |
David Wood | 08ed338 | 2021-10-08 16:10:17 | [diff] [blame] | 1089 | match (split_debuginfo_kind, split_dwarf_kind) { |
| 1090 | (SplitDebuginfo::Off, SplitDwarfKind::Single | SplitDwarfKind::Split) => None, |
David Wood | e3fdae9 | 2020-09-23 16:33:54 | [diff] [blame] | 1091 | // Single mode doesn't change how DWARF is emitted, but does add Split DWARF attributes |
| 1092 | // (pointing at the path which is being determined here). Use the path to the current |
| 1093 | // object file. |
David Wood | 08ed338 | 2021-10-08 16:10:17 | [diff] [blame] | 1094 | (SplitDebuginfo::Packed | SplitDebuginfo::Unpacked, SplitDwarfKind::Single) => { |
| 1095 | Some(obj_out) |
| 1096 | } |
David Wood | e3fdae9 | 2020-09-23 16:33:54 | [diff] [blame] | 1097 | // Split mode emits the DWARF into a different file, use that path. |
David Wood | 08ed338 | 2021-10-08 16:10:17 | [diff] [blame] | 1098 | (SplitDebuginfo::Packed | SplitDebuginfo::Unpacked, SplitDwarfKind::Split) => { |
| 1099 | Some(dwo_out) |
| 1100 | } |
David Wood | e3fdae9 | 2020-09-23 16:33:54 | [diff] [blame] | 1101 | } |
| 1102 | } |
Niko Matsakis | e135fa5 | 2014-11-27 12:21:26 | [diff] [blame] | 1103 | } |
| 1104 | |
Urgau | 30f9471 | 2023-08-23 09:18:20 | [diff] [blame] | 1105 | bitflags::bitflags! { |
| 1106 | /// Scopes used to determined if it need to apply to --remap-path-prefix |
Nilstrieb | ffafcd8 | 2023-12-30 16:09:02 | [diff] [blame] | 1107 | #[derive(Clone, Copy, PartialEq, Eq, Hash)] |
Urgau | 30f9471 | 2023-08-23 09:18:20 | [diff] [blame] | 1108 | pub struct RemapPathScopeComponents: u8 { |
| 1109 | /// Apply remappings to the expansion of std::file!() macro |
| 1110 | const MACRO = 1 << 0; |
| 1111 | /// Apply remappings to printed compiler diagnostics |
| 1112 | const DIAGNOSTICS = 1 << 1; |
Alexander Cyon | 00de006 | 2024-09-02 05:50:22 | [diff] [blame] | 1113 | /// Apply remappings to debug information |
Urgau | 777c6b4 | 2024-03-19 12:51:22 | [diff] [blame] | 1114 | const DEBUGINFO = 1 << 3; |
Urgau | 30f9471 | 2023-08-23 09:18:20 | [diff] [blame] | 1115 | |
Urgau | 777c6b4 | 2024-03-19 12:51:22 | [diff] [blame] | 1116 | /// An alias for `macro` and `debuginfo`. This ensures all paths in compiled |
| 1117 | /// executables or libraries are remapped but not elsewhere. |
| 1118 | const OBJECT = Self::MACRO.bits() | Self::DEBUGINFO.bits(); |
Urgau | 30f9471 | 2023-08-23 09:18:20 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
Noratrieb | a26450c | 2024-10-17 17:02:32 | [diff] [blame] | 1122 | pub fn host_tuple() -> &'static str { |
Niko Matsakis | dc6e414 | 2014-11-16 01:30:33 | [diff] [blame] | 1123 | // Get the host triple out of the build environment. This ensures that our |
| 1124 | // idea of the host triple is the same as for the set of libraries we've |
Maybe Waffle | 6a28fb4 | 2022-11-16 20:34:16 | [diff] [blame] | 1125 | // actually built. We can't just take LLVM's host triple because they |
Niko Matsakis | dc6e414 | 2014-11-16 01:30:33 | [diff] [blame] | 1126 | // normalize all ix86 architectures to i386. |
| 1127 | // |
| 1128 | // Instead of grabbing the host triple (for the current host), we grab (at |
| 1129 | // compile time) the target triple that this rustc is built with and |
| 1130 | // calling that (at runtime) the host triple. |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1131 | (option_env!("CFG_COMPILER_HOST_TRIPLE")).expect("CFG_COMPILER_HOST_TRIPLE") |
Niko Matsakis | dc6e414 | 2014-11-16 01:30:33 | [diff] [blame] | 1132 | } |
| 1133 | |
Urgau | eccc9e6 | 2023-08-23 13:46:58 | [diff] [blame] | 1134 | fn file_path_mapping( |
| 1135 | remap_path_prefix: Vec<(PathBuf, PathBuf)>, |
| 1136 | unstable_opts: &UnstableOptions, |
| 1137 | ) -> FilePathMapping { |
| 1138 | FilePathMapping::new( |
| 1139 | remap_path_prefix.clone(), |
| 1140 | if unstable_opts.remap_path_scope.contains(RemapPathScopeComponents::DIAGNOSTICS) |
| 1141 | && !remap_path_prefix.is_empty() |
| 1142 | { |
| 1143 | FileNameDisplayPreference::Remapped |
| 1144 | } else { |
| 1145 | FileNameDisplayPreference::Local |
| 1146 | }, |
| 1147 | ) |
| 1148 | } |
| 1149 | |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1150 | impl Default for Options { |
| 1151 | fn default() -> Options { |
| 1152 | Options { |
pierwill | 1642fdf | 2021-10-31 22:05:48 | [diff] [blame] | 1153 | assert_incr_state: None, |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1154 | crate_types: Vec::new(), |
| 1155 | optimize: OptLevel::No, |
| 1156 | debuginfo: DebugInfo::None, |
Augie Fackler | af9e550 | 2023-07-12 21:07:34 | [diff] [blame] | 1157 | debuginfo_compression: DebugInfoCompression::None, |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1158 | lint_opts: Vec::new(), |
| 1159 | lint_cap: None, |
| 1160 | describe_lints: false, |
| 1161 | output_types: OutputTypes(BTreeMap::new()), |
Nicholas Nethercote | f130061 | 2018-11-22 05:33:07 | [diff] [blame] | 1162 | search_paths: vec![], |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1163 | maybe_sysroot: None, |
Noratrieb | a26450c | 2024-10-17 17:02:32 | [diff] [blame] | 1164 | target_triple: TargetTuple::from_tuple(host_tuple()), |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1165 | test: false, |
| 1166 | incremental: None, |
Alex Macleod | 59f6f04 | 2023-10-13 17:28:34 | [diff] [blame] | 1167 | untracked_state_hash: Default::default(), |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 1168 | unstable_opts: Default::default(), |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1169 | prints: Vec::new(), |
Vadim Petrochenkov | 9d18d4d | 2021-05-07 12:18:19 | [diff] [blame] | 1170 | cg: Default::default(), |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1171 | error_format: ErrorOutputType::default(), |
David Wood | 44c1fcc | 2022-07-06 10:57:41 | [diff] [blame] | 1172 | diagnostic_width: None, |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1173 | externs: Externs(BTreeMap::new()), |
| 1174 | crate_name: None, |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1175 | libs: Vec::new(), |
| 1176 | unstable_features: UnstableFeatures::Disallow, |
| 1177 | debug_assertions: true, |
| 1178 | actually_rustdoc: false, |
Vadim Petrochenkov | da4ce6b | 2023-02-06 17:57:45 | [diff] [blame] | 1179 | resolve_doc_links: ResolveDocLinks::None, |
Nicholas Nethercote | 32de78c | 2024-01-10 01:47:22 | [diff] [blame] | 1180 | trimmed_def_paths: false, |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1181 | cli_forced_codegen_units: None, |
Wesley Wiser | 7c6345d | 2022-10-27 00:28:25 | [diff] [blame] | 1182 | cli_forced_local_thinlto_off: false, |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1183 | remap_path_prefix: Vec::new(), |
Joshua Nelson | 39648ea | 2021-04-27 16:25:12 | [diff] [blame] | 1184 | real_rust_source_base_dir: None, |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1185 | edition: DEFAULT_EDITION, |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1186 | json_artifact_notifications: false, |
Jeremy Fitzhardinge | c6bafa7 | 2022-04-17 00:11:33 | [diff] [blame] | 1187 | json_unused_externs: JsonUnusedExterns::No, |
Aaron Hill | 63523e4 | 2021-12-04 19:34:20 | [diff] [blame] | 1188 | json_future_incompat: false, |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 1189 | pretty: None, |
Aaron Hill | a895069 | 2021-08-12 20:30:40 | [diff] [blame] | 1190 | working_dir: RealFileName::LocalPath(std::env::current_dir().unwrap()), |
Trevor Gross | 6a1c10b | 2022-12-19 18:09:40 | [diff] [blame] | 1191 | color: ColorConfig::Auto, |
Guillaume Gomez | 486e55e | 2023-11-27 12:47:26 | [diff] [blame] | 1192 | logical_env: FxIndexMap::default(), |
jyn | cb6d033 | 2023-12-19 18:24:05 | [diff] [blame] | 1193 | verbose: false, |
Mark Rousskov | 5fcef25 | 2018-07-26 18:36:11 | [diff] [blame] | 1194 | } |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
Niko Matsakis | fe47ca0 | 2016-03-28 21:43:36 | [diff] [blame] | 1198 | impl Options { |
Alexander Regueiro | c3e182c | 2019-02-08 13:53:55 | [diff] [blame] | 1199 | /// Returns `true` if there is a reason to build the dep graph. |
Niko Matsakis | fe47ca0 | 2016-03-28 21:43:36 | [diff] [blame] | 1200 | pub fn build_dep_graph(&self) -> bool { |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1201 | self.incremental.is_some() |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 1202 | || self.unstable_opts.dump_dep_graph |
| 1203 | || self.unstable_opts.query_dep_graph |
Niko Matsakis | fe47ca0 | 2016-03-28 21:43:36 | [diff] [blame] | 1204 | } |
Michael Woerister | 59cfe90 | 2016-07-13 21:03:02 | [diff] [blame] | 1205 | |
Nicholas Nethercote | 62c32ae | 2024-03-21 03:17:00 | [diff] [blame] | 1206 | pub fn file_path_mapping(&self) -> FilePathMapping { |
Urgau | eccc9e6 | 2023-08-23 13:46:58 | [diff] [blame] | 1207 | file_path_mapping(self.remap_path_prefix.clone(), &self.unstable_opts) |
Michael Woerister | 39ffea3 | 2017-04-24 17:01:19 | [diff] [blame] | 1208 | } |
varkor | c76cdce | 2017-12-18 15:35:45 | [diff] [blame] | 1209 | |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 1210 | /// Returns `true` if there will be an output file generated. |
varkor | c76cdce | 2017-12-18 15:35:45 | [diff] [blame] | 1211 | pub fn will_create_output_file(&self) -> bool { |
Nicholas Nethercote | 76adf05 | 2024-11-28 19:05:56 | [diff] [blame] | 1212 | !self.unstable_opts.parse_crate_root_only && // The file is just being parsed |
bjorn3 | ff00763 | 2023-09-10 13:24:20 | [diff] [blame] | 1213 | self.unstable_opts.ls.is_empty() // The file is just being queried |
varkor | c76cdce | 2017-12-18 15:35:45 | [diff] [blame] | 1214 | } |
Mark Rousskov | a9093a4 | 2018-07-26 19:20:47 | [diff] [blame] | 1215 | |
| 1216 | #[inline] |
| 1217 | pub fn share_generics(&self) -> bool { |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 1218 | match self.unstable_opts.share_generics { |
Mark Rousskov | a9093a4 | 2018-07-26 19:20:47 | [diff] [blame] | 1219 | Some(setting) => setting, |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1220 | None => match self.optimize { |
| 1221 | OptLevel::No | OptLevel::Less | OptLevel::Size | OptLevel::SizeMin => true, |
| 1222 | OptLevel::Default | OptLevel::Aggressive => false, |
| 1223 | }, |
Mark Rousskov | a9093a4 | 2018-07-26 19:20:47 | [diff] [blame] | 1224 | } |
| 1225 | } |
Josh Triplett | bbf4b66 | 2021-10-21 12:02:59 | [diff] [blame] | 1226 | |
| 1227 | pub fn get_symbol_mangling_version(&self) -> SymbolManglingVersion { |
| 1228 | self.cg.symbol_mangling_version.unwrap_or(SymbolManglingVersion::Legacy) |
| 1229 | } |
Niko Matsakis | fe47ca0 | 2016-03-28 21:43:36 | [diff] [blame] | 1230 | } |
| 1231 | |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 1232 | impl UnstableOptions { |
Nicholas Nethercote | 55bafab | 2023-12-17 21:59:22 | [diff] [blame] | 1233 | pub fn dcx_flags(&self, can_emit_warnings: bool) -> DiagCtxtFlags { |
Nicholas Nethercote | 9b1f87c | 2023-12-17 21:47:03 | [diff] [blame] | 1234 | DiagCtxtFlags { |
Vadim Petrochenkov | 1370bbc | 2019-12-29 20:07:23 | [diff] [blame] | 1235 | can_emit_warnings, |
| 1236 | treat_err_as_bug: self.treat_err_as_bug, |
Michael Goulet | 7df43d3 | 2024-01-12 00:30:04 | [diff] [blame] | 1237 | eagerly_emit_delayed_bugs: self.eagerly_emit_delayed_bugs, |
Eduard-Mihai Burtescu | f6fc802 | 2019-12-15 15:12:30 | [diff] [blame] | 1238 | macro_backtrace: self.macro_backtrace, |
Nicholas Nethercote | 58217bc | 2020-04-02 05:44:47 | [diff] [blame] | 1239 | deduplicate_diagnostics: self.deduplicate_diagnostics, |
mejrs | 406e1dc | 2022-10-18 22:08:20 | [diff] [blame] | 1240 | track_diagnostics: self.track_diagnostics, |
Vadim Petrochenkov | 1370bbc | 2019-12-29 20:07:23 | [diff] [blame] | 1241 | } |
| 1242 | } |
Nicholas Nethercote | 62c32ae | 2024-03-21 03:17:00 | [diff] [blame] | 1243 | |
| 1244 | pub fn src_hash_algorithm(&self, target: &Target) -> SourceFileHashAlgorithm { |
| 1245 | self.src_hash_algorithm.unwrap_or_else(|| { |
| 1246 | if target.is_like_msvc { |
| 1247 | SourceFileHashAlgorithm::Sha256 |
| 1248 | } else { |
| 1249 | SourceFileHashAlgorithm::Md5 |
| 1250 | } |
| 1251 | }) |
| 1252 | } |
Jacob Kiesel | bb5a827 | 2024-06-22 07:27:59 | [diff] [blame] | 1253 | |
| 1254 | pub fn checksum_hash_algorithm(&self) -> Option<SourceFileHashAlgorithm> { |
| 1255 | self.checksum_hash_algorithm |
| 1256 | } |
Vadim Petrochenkov | 208c1bf | 2019-12-29 21:23:19 | [diff] [blame] | 1257 | } |
| 1258 | |
Igor Matuszewski | ff19a53 | 2019-01-13 12:06:26 | [diff] [blame] | 1259 | // The type of entry function, so users can have their own entry functions |
Michael Woerister | c0be619 | 2022-04-19 08:43:09 | [diff] [blame] | 1260 | #[derive(Copy, Clone, PartialEq, Hash, Debug, HashStable_Generic)] |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1261 | pub enum EntryFnType { |
Martin Nordholts | ddee45e | 2022-07-05 17:56:22 | [diff] [blame] | 1262 | Main { |
| 1263 | /// Specifies what to do with `SIGPIPE` before calling `fn main()`. |
| 1264 | /// |
| 1265 | /// What values that are valid and what they mean must be in sync |
| 1266 | /// across rustc and libstd, but we don't want it public in libstd, |
| 1267 | /// so we take a bit of an unusual approach with simple constants |
| 1268 | /// and an `include!()`. |
| 1269 | sigpipe: u8, |
| 1270 | }, |
Mark Rousskov | 442a474 | 2018-07-26 17:29:45 | [diff] [blame] | 1271 | Start, |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1272 | } |
| 1273 | |
Matthew Jasper | cbcef3e | 2020-06-11 14:49:57 | [diff] [blame] | 1274 | #[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug, Encodable, Decodable)] |
Michael Woerister | c0be619 | 2022-04-19 08:43:09 | [diff] [blame] | 1275 | #[derive(HashStable_Generic)] |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1276 | pub enum CrateType { |
Mark Rousskov | 2a93442 | 2018-07-26 17:13:11 | [diff] [blame] | 1277 | Executable, |
| 1278 | Dylib, |
| 1279 | Rlib, |
| 1280 | Staticlib, |
| 1281 | Cdylib, |
| 1282 | ProcMacro, |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1283 | } |
| 1284 | |
Vadim Petrochenkov | da4ce6b | 2023-02-06 17:57:45 | [diff] [blame] | 1285 | impl CrateType { |
| 1286 | pub fn has_metadata(self) -> bool { |
| 1287 | match self { |
| 1288 | CrateType::Rlib | CrateType::Dylib | CrateType::ProcMacro => true, |
| 1289 | CrateType::Executable | CrateType::Cdylib | CrateType::Staticlib => false, |
| 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | |
Joshua Nelson | fb7018b | 2021-04-16 03:06:32 | [diff] [blame] | 1294 | #[derive(Clone, Hash, Debug, PartialEq, Eq)] |
Keegan McAllister | ad9a1da | 2014-09-12 15:17:58 | [diff] [blame] | 1295 | pub enum Passes { |
Mark Rousskov | b3267dc | 2018-07-26 17:22:14 | [diff] [blame] | 1296 | Some(Vec<String>), |
| 1297 | All, |
Keegan McAllister | ad9a1da | 2014-09-12 15:17:58 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | impl Passes { |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1301 | fn is_empty(&self) -> bool { |
Keegan McAllister | ad9a1da | 2014-09-12 15:17:58 | [diff] [blame] | 1302 | match *self { |
Mark Rousskov | b3267dc | 2018-07-26 17:22:14 | [diff] [blame] | 1303 | Passes::Some(ref v) => v.is_empty(), |
| 1304 | Passes::All => false, |
Keegan McAllister | ad9a1da | 2014-09-12 15:17:58 | [diff] [blame] | 1305 | } |
| 1306 | } |
Tomasz Miąsko | e74e39a | 2021-11-13 00:00:00 | [diff] [blame] | 1307 | |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1308 | pub(crate) fn extend(&mut self, passes: impl IntoIterator<Item = String>) { |
Tomasz Miąsko | e74e39a | 2021-11-13 00:00:00 | [diff] [blame] | 1309 | match *self { |
| 1310 | Passes::Some(ref mut v) => v.extend(passes), |
| 1311 | Passes::All => {} |
| 1312 | } |
| 1313 | } |
Keegan McAllister | ad9a1da | 2014-09-12 15:17:58 | [diff] [blame] | 1314 | } |
| 1315 | |
James McGregor | 837cc16 | 2021-07-13 11:14:26 | [diff] [blame] | 1316 | #[derive(Clone, Copy, Hash, Debug, PartialEq)] |
| 1317 | pub enum PAuthKey { |
| 1318 | A, |
| 1319 | B, |
| 1320 | } |
| 1321 | |
| 1322 | #[derive(Clone, Copy, Hash, Debug, PartialEq)] |
| 1323 | pub struct PacRet { |
| 1324 | pub leaf: bool, |
Kajetan Puchalski | 10edeea | 2024-10-16 14:39:58 | [diff] [blame] | 1325 | pub pc: bool, |
James McGregor | 837cc16 | 2021-07-13 11:14:26 | [diff] [blame] | 1326 | pub key: PAuthKey, |
| 1327 | } |
| 1328 | |
Matthias Krüger | de59844 | 2022-12-14 23:06:34 | [diff] [blame] | 1329 | #[derive(Clone, Copy, Hash, Debug, PartialEq, Default)] |
James McGregor | 837cc16 | 2021-07-13 11:14:26 | [diff] [blame] | 1330 | pub struct BranchProtection { |
| 1331 | pub bti: bool, |
| 1332 | pub pac_ret: Option<PacRet>, |
| 1333 | } |
| 1334 | |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1335 | pub(crate) const fn default_lib_output() -> CrateType { |
Mark Rousskov | 2a93442 | 2018-07-26 17:13:11 | [diff] [blame] | 1336 | CrateType::Rlib |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1337 | } |
| 1338 | |
Nicholas Nethercote | 5c6a12c | 2023-10-30 03:05:06 | [diff] [blame] | 1339 | pub fn build_configuration(sess: &Session, mut user_cfg: Cfg) -> Cfg { |
Urgau | c0c57b3 | 2024-06-08 11:26:37 | [diff] [blame] | 1340 | // First disallow some configuration given on the command line |
| 1341 | cfg::disallow_cfgs(sess, &user_cfg); |
| 1342 | |
| 1343 | // Then combine the configuration requested by the session (command line) with |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 1344 | // some default and generated configuration items. |
Urgau | 5b14497 | 2024-04-05 21:01:40 | [diff] [blame] | 1345 | user_cfg.extend(cfg::default_configuration(sess)); |
Jeffrey Seyfried | 4b9b0d3 | 2016-11-15 08:54:27 | [diff] [blame] | 1346 | user_cfg |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1347 | } |
| 1348 | |
Nicholas Nethercote | 23ee523e | 2024-03-20 23:35:19 | [diff] [blame] | 1349 | pub fn build_target_config(early_dcx: &EarlyDiagCtxt, opts: &Options, sysroot: &Path) -> Target { |
| 1350 | match Target::search(&opts.target_triple, sysroot) { |
| 1351 | Ok((target, warnings)) => { |
| 1352 | for warning in warnings.warning_messages() { |
| 1353 | early_dcx.early_warn(warning) |
| 1354 | } |
Alex Crichton | e003a12 | 2024-06-19 03:54:11 | [diff] [blame] | 1355 | |
Nicholas Nethercote | 23ee523e | 2024-03-20 23:35:19 | [diff] [blame] | 1356 | if !matches!(target.pointer_width, 16 | 32 | 64) { |
| 1357 | early_dcx.early_fatal(format!( |
| 1358 | "target specification was invalid: unrecognized target-pointer-width {}", |
| 1359 | target.pointer_width |
| 1360 | )) |
| 1361 | } |
| 1362 | target |
| 1363 | } |
| 1364 | Err(e) => early_dcx.early_fatal(format!( |
Matthias Krüger | 2381546 | 2023-07-25 20:00:13 | [diff] [blame] | 1365 | "Error loading target specification: {e}. \ |
Nicholas Nethercote | 23ee523e | 2024-03-20 23:35:19 | [diff] [blame] | 1366 | Run `rustc --print target-list` for a list of built-in targets" |
| 1367 | )), |
Adam Bratschi-Kaye | 88b01f1 | 2021-05-27 08:21:53 | [diff] [blame] | 1368 | } |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1369 | } |
| 1370 | |
Jorge Aparicio | 788181d | 2015-01-28 13:34:18 | [diff] [blame] | 1371 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1372 | pub enum OptionStability { |
Alex Crichton | 1282833 | 2016-02-20 06:03:54 | [diff] [blame] | 1373 | Stable, |
Alex Crichton | 1282833 | 2016-02-20 06:03:54 | [diff] [blame] | 1374 | Unstable, |
| 1375 | } |
Felix S. Klock II | e711e2d | 2014-12-17 13:42:50 | [diff] [blame] | 1376 | |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1377 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 1378 | pub enum OptionKind { |
| 1379 | /// An option that takes a value, and cannot appear more than once (e.g. `--out-dir`). |
| 1380 | /// |
| 1381 | /// Corresponds to [`getopts::Options::optopt`]. |
| 1382 | Opt, |
| 1383 | |
| 1384 | /// An option that takes a value, and can appear multiple times (e.g. `--emit`). |
| 1385 | /// |
| 1386 | /// Corresponds to [`getopts::Options::optmulti`]. |
| 1387 | Multi, |
| 1388 | |
| 1389 | /// An option that does not take a value, and cannot appear more than once (e.g. `--help`). |
| 1390 | /// |
| 1391 | /// Corresponds to [`getopts::Options::optflag`]. |
| 1392 | /// The `hint` string must be empty. |
| 1393 | Flag, |
| 1394 | |
| 1395 | /// An option that does not take a value, and can appear multiple times (e.g. `-O`). |
| 1396 | /// |
| 1397 | /// Corresponds to [`getopts::Options::optflagmulti`]. |
| 1398 | /// The `hint` string must be empty. |
| 1399 | FlagMulti, |
| 1400 | } |
| 1401 | |
Felix S. Klock II | e711e2d | 2014-12-17 13:42:50 | [diff] [blame] | 1402 | pub struct RustcOptGroup { |
Zalathar | 3250c1c | 2024-11-11 02:05:53 | [diff] [blame] | 1403 | /// The "primary" name for this option. Normally equal to `long_name`, |
| 1404 | /// except for options that don't have a long name, in which case |
| 1405 | /// `short_name` is used. |
| 1406 | /// |
| 1407 | /// This is needed when interacting with `getopts` in some situations, |
| 1408 | /// because if an option has both forms, that library treats the long name |
| 1409 | /// as primary and the short name as an alias. |
George Bateman | 6360287 | 2024-08-11 08:07:52 | [diff] [blame] | 1410 | pub name: &'static str, |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1411 | stability: OptionStability, |
Zalathar | 3250c1c | 2024-11-11 02:05:53 | [diff] [blame] | 1412 | kind: OptionKind, |
| 1413 | |
| 1414 | short_name: &'static str, |
| 1415 | long_name: &'static str, |
| 1416 | desc: &'static str, |
| 1417 | value_hint: &'static str, |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1418 | |
| 1419 | /// If true, this option should not be printed by `rustc --help`, but |
| 1420 | /// should still be printed by `rustc --help -v`. |
| 1421 | pub is_verbose_help_only: bool, |
Felix S. Klock II | e711e2d | 2014-12-17 13:42:50 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | impl RustcOptGroup { |
| 1425 | pub fn is_stable(&self) -> bool { |
| 1426 | self.stability == OptionStability::Stable |
| 1427 | } |
| 1428 | |
Zalathar | 584c820 | 2024-11-08 01:20:51 | [diff] [blame] | 1429 | pub fn apply(&self, options: &mut getopts::Options) { |
Zalathar | 3250c1c | 2024-11-11 02:05:53 | [diff] [blame] | 1430 | let &Self { short_name, long_name, desc, value_hint, .. } = self; |
| 1431 | match self.kind { |
| 1432 | OptionKind::Opt => options.optopt(short_name, long_name, desc, value_hint), |
| 1433 | OptionKind::Multi => options.optmulti(short_name, long_name, desc, value_hint), |
| 1434 | OptionKind::Flag => options.optflag(short_name, long_name, desc), |
| 1435 | OptionKind::FlagMulti => options.optflagmulti(short_name, long_name, desc), |
| 1436 | }; |
Zalathar | 584c820 | 2024-11-08 01:20:51 | [diff] [blame] | 1437 | } |
Felix S. Klock II | e711e2d | 2014-12-17 13:42:50 | [diff] [blame] | 1438 | } |
| 1439 | |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1440 | pub fn make_opt( |
| 1441 | stability: OptionStability, |
| 1442 | kind: OptionKind, |
| 1443 | short_name: &'static str, |
| 1444 | long_name: &'static str, |
| 1445 | desc: &'static str, |
Zalathar | 3250c1c | 2024-11-11 02:05:53 | [diff] [blame] | 1446 | value_hint: &'static str, |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1447 | ) -> RustcOptGroup { |
Zalathar | 3250c1c | 2024-11-11 02:05:53 | [diff] [blame] | 1448 | // "Flag" options don't have a value, and therefore don't have a value hint. |
| 1449 | match kind { |
| 1450 | OptionKind::Opt | OptionKind::Multi => {} |
| 1451 | OptionKind::Flag | OptionKind::FlagMulti => assert_eq!(value_hint, ""), |
| 1452 | } |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1453 | RustcOptGroup { |
| 1454 | name: cmp::max_by_key(short_name, long_name, |s| s.len()), |
| 1455 | stability, |
Zalathar | 3250c1c | 2024-11-11 02:05:53 | [diff] [blame] | 1456 | kind, |
| 1457 | short_name, |
| 1458 | long_name, |
| 1459 | desc, |
| 1460 | value_hint, |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1461 | is_verbose_help_only: false, |
Alex Crichton | 1282833 | 2016-02-20 06:03:54 | [diff] [blame] | 1462 | } |
Felix S. Klock II | e711e2d | 2014-12-17 13:42:50 | [diff] [blame] | 1463 | } |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1464 | |
Matthew Esposito | 5cda0a2 | 2023-01-06 19:07:12 | [diff] [blame] | 1465 | static EDITION_STRING: LazyLock<String> = LazyLock::new(|| { |
| 1466 | format!( |
Matthew E | 893938f | 2023-01-06 19:36:52 | [diff] [blame] | 1467 | "Specify which edition of the compiler to use when compiling code. \ |
Matthew Esposito | 5cda0a2 | 2023-01-06 19:07:12 | [diff] [blame] | 1468 | The default is {DEFAULT_EDITION} and the latest stable edition is {LATEST_STABLE_EDITION}." |
| 1469 | ) |
| 1470 | }); |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1471 | |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1472 | /// Returns all rustc command line options, including metadata for |
| 1473 | /// each option, such as whether the option is stable. |
| 1474 | pub fn rustc_optgroups() -> Vec<RustcOptGroup> { |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1475 | use OptionKind::{Flag, FlagMulti, Multi, Opt}; |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1476 | use OptionStability::{Stable, Unstable}; |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1477 | |
| 1478 | use self::make_opt as opt; |
| 1479 | |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1480 | let mut options = vec![ |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1481 | opt(Stable, Flag, "h", "help", "Display this message", ""), |
| 1482 | opt( |
| 1483 | Stable, |
| 1484 | Multi, |
| 1485 | "", |
| 1486 | "cfg", |
| 1487 | "Configure the compilation environment.\n\ |
| 1488 | SPEC supports the syntax `NAME[=\"VALUE\"]`.", |
| 1489 | "SPEC", |
| 1490 | ), |
| 1491 | opt(Stable, Multi, "", "check-cfg", "Provide list of expected cfgs for checking", "SPEC"), |
| 1492 | opt( |
| 1493 | Stable, |
| 1494 | Multi, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1495 | "L", |
| 1496 | "", |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1497 | "Add a directory to the library search path. \ |
| 1498 | The optional KIND can be one of dependency, crate, native, framework, or all (the default).", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1499 | "[KIND=]PATH", |
| 1500 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1501 | opt( |
| 1502 | Stable, |
| 1503 | Multi, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1504 | "l", |
| 1505 | "", |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1506 | "Link the generated crate(s) to the specified native\n\ |
| 1507 | library NAME. The optional KIND can be one of\n\ |
| 1508 | static, framework, or dylib (the default).\n\ |
| 1509 | Optional comma separated MODIFIERS\n\ |
| 1510 | (bundle|verbatim|whole-archive|as-needed)\n\ |
| 1511 | may be specified each with a prefix of either '+' to\n\ |
| 1512 | enable or '-' to disable.", |
Luqman Aden | db555e1 | 2021-03-25 04:45:09 | [diff] [blame] | 1513 | "[KIND[:MODIFIERS]=]NAME[:RENAME]", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1514 | ), |
Aaron Hill | 1498608 | 2019-07-20 20:34:41 | [diff] [blame] | 1515 | make_crate_type_option(), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1516 | opt(Stable, Opt, "", "crate-name", "Specify the name of the crate being built", "NAME"), |
| 1517 | opt(Stable, Opt, "", "edition", &EDITION_STRING, EDITION_NAME_LIST), |
| 1518 | opt( |
| 1519 | Stable, |
| 1520 | Multi, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1521 | "", |
| 1522 | "emit", |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1523 | "Comma separated list of types of output for the compiler to emit", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1524 | "[asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]", |
| 1525 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1526 | opt( |
| 1527 | Stable, |
| 1528 | Multi, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1529 | "", |
| 1530 | "print", |
Eric Huss | e392db6 | 2019-05-12 21:16:50 | [diff] [blame] | 1531 | "Compiler information to print on stdout", |
Urgau | ac59bdc | 2024-04-22 21:47:48 | [diff] [blame] | 1532 | "[crate-name|file-names|sysroot|target-libdir|cfg|check-cfg|calling-conventions|\ |
khyperia | 9a206a7 | 2022-09-08 13:37:15 | [diff] [blame] | 1533 | target-list|target-cpus|target-features|relocation-models|code-models|\ |
Pietro Albini | ef2bf6d | 2023-03-09 13:52:45 | [diff] [blame] | 1534 | tls-models|target-spec-json|all-target-specs-json|native-static-libs|\ |
BlackHoleFox | a427d41 | 2022-12-06 05:15:16 | [diff] [blame] | 1535 | stack-protector-strategies|link-args|deployment-target]", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1536 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1537 | opt(Stable, FlagMulti, "g", "", "Equivalent to -C debuginfo=2", ""), |
| 1538 | opt(Stable, FlagMulti, "O", "", "Equivalent to -C opt-level=2", ""), |
| 1539 | opt(Stable, Opt, "o", "", "Write output to <filename>", "FILENAME"), |
| 1540 | opt(Stable, Opt, "", "out-dir", "Write output to compiler-chosen filename in <dir>", "DIR"), |
| 1541 | opt( |
| 1542 | Stable, |
| 1543 | Opt, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1544 | "", |
| 1545 | "explain", |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1546 | "Provide a detailed explanation of an error message", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1547 | "OPT", |
| 1548 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1549 | opt(Stable, Flag, "", "test", "Build a test harness", ""), |
| 1550 | opt(Stable, Opt, "", "target", "Target triple for which the code is compiled", "TARGET"), |
| 1551 | opt(Stable, Multi, "A", "allow", "Set lint allowed", "LINT"), |
| 1552 | opt(Stable, Multi, "W", "warn", "Set lint warnings", "LINT"), |
| 1553 | opt(Stable, Multi, "", "force-warn", "Set lint force-warn", "LINT"), |
| 1554 | opt(Stable, Multi, "D", "deny", "Set lint denied", "LINT"), |
| 1555 | opt(Stable, Multi, "F", "forbid", "Set lint forbidden", "LINT"), |
| 1556 | opt( |
| 1557 | Stable, |
| 1558 | Multi, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1559 | "", |
| 1560 | "cap-lints", |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1561 | "Set the most restrictive lint level. More restrictive lints are capped at this level", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1562 | "LEVEL", |
| 1563 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1564 | opt(Stable, Multi, "C", "codegen", "Set a codegen option", "OPT[=VALUE]"), |
| 1565 | opt(Stable, Flag, "V", "version", "Print version info and exit", ""), |
| 1566 | opt(Stable, Flag, "v", "verbose", "Use verbose output", ""), |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1567 | ]; |
Alex Crichton | 117984b | 2014-12-16 00:03:39 | [diff] [blame] | 1568 | |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1569 | // Options in this list are hidden from `rustc --help` by default, but are |
| 1570 | // shown by `rustc --help -v`. |
| 1571 | let verbose_only = [ |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1572 | opt( |
| 1573 | Stable, |
| 1574 | Multi, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1575 | "", |
| 1576 | "extern", |
| 1577 | "Specify where an external rust library is located", |
Eric Huss | 4bf411e | 2019-09-30 01:17:48 | [diff] [blame] | 1578 | "NAME[=PATH]", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1579 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1580 | opt(Stable, Opt, "", "sysroot", "Override the system root", "PATH"), |
| 1581 | opt(Unstable, Multi, "Z", "", "Set unstable / perma-unstable options", "FLAG"), |
| 1582 | opt( |
| 1583 | Stable, |
| 1584 | Opt, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1585 | "", |
| 1586 | "error-format", |
| 1587 | "How errors and other messages are produced", |
| 1588 | "human|json|short", |
| 1589 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1590 | opt(Stable, Multi, "", "json", "Configure the JSON output of the compiler", "CONFIG"), |
| 1591 | opt( |
| 1592 | Stable, |
| 1593 | Opt, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1594 | "", |
| 1595 | "color", |
| 1596 | "Configure coloring of output: |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1597 | auto = colorize, if output goes to a tty (default); |
| 1598 | always = always colorize output; |
| 1599 | never = never colorize output", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1600 | "auto|always|never", |
| 1601 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1602 | opt( |
| 1603 | Stable, |
| 1604 | Opt, |
David Wood | e528884 | 2022-02-14 06:01:38 | [diff] [blame] | 1605 | "", |
David Wood | 44c1fcc | 2022-07-06 10:57:41 | [diff] [blame] | 1606 | "diagnostic-width", |
| 1607 | "Inform rustc of the width of the output so that diagnostics can be truncated to fit", |
David Wood | e528884 | 2022-02-14 06:01:38 | [diff] [blame] | 1608 | "WIDTH", |
| 1609 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1610 | opt( |
| 1611 | Stable, |
| 1612 | Multi, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1613 | "", |
| 1614 | "remap-path-prefix", |
Jeremy Fitzhardinge | 6d534d5 | 2018-03-13 20:26:07 | [diff] [blame] | 1615 | "Remap source names in all output (compiler messages and output files)", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1616 | "FROM=TO", |
| 1617 | ), |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 1618 | opt(Unstable, Multi, "", "env-set", "Inject an environment variable", "VAR=VALUE"), |
Zalathar | 8b4701d | 2024-11-11 02:17:39 | [diff] [blame] | 1619 | ]; |
| 1620 | options.extend(verbose_only.into_iter().map(|mut opt| { |
| 1621 | opt.is_verbose_help_only = true; |
| 1622 | opt |
| 1623 | })); |
| 1624 | |
| 1625 | options |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1626 | } |
| 1627 | |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1628 | pub fn get_cmd_lint_options( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1629 | early_dcx: &EarlyDiagCtxt, |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1630 | matches: &getopts::Matches, |
inquisitivecrystal | 2f2db99 | 2021-07-08 07:05:38 | [diff] [blame] | 1631 | ) -> (Vec<(String, lint::Level)>, bool, Option<lint::Level>) { |
Tobias Thiel | 51021b1 | 2020-01-05 02:04:30 | [diff] [blame] | 1632 | let mut lint_opts_with_position = vec![]; |
Guillaume Gomez | e221be8 | 2018-06-23 13:09:21 | [diff] [blame] | 1633 | let mut describe_lints = false; |
| 1634 | |
xFrednet | 8527a3d | 2022-06-05 10:33:45 | [diff] [blame] | 1635 | for level in [lint::Allow, lint::Warn, lint::ForceWarn(None), lint::Deny, lint::Forbid] { |
inquisitivecrystal | 2f2db99 | 2021-07-08 07:05:38 | [diff] [blame] | 1636 | for (arg_pos, lint_name) in matches.opt_strs_pos(level.as_str()) { |
Guillaume Gomez | e221be8 | 2018-06-23 13:09:21 | [diff] [blame] | 1637 | if lint_name == "help" { |
| 1638 | describe_lints = true; |
| 1639 | } else { |
Matthias Krüger | 97e844a | 2021-12-13 21:58:58 | [diff] [blame] | 1640 | lint_opts_with_position.push((arg_pos, lint_name.replace('-', "_"), level)); |
Guillaume Gomez | e221be8 | 2018-06-23 13:09:21 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | |
Tobias Thiel | 51021b1 | 2020-01-05 02:04:30 | [diff] [blame] | 1645 | lint_opts_with_position.sort_by_key(|x| x.0); |
| 1646 | let lint_opts = lint_opts_with_position |
| 1647 | .iter() |
| 1648 | .cloned() |
| 1649 | .map(|(_, lint_name, level)| (lint_name, level)) |
| 1650 | .collect(); |
| 1651 | |
Guillaume Gomez | e221be8 | 2018-06-23 13:09:21 | [diff] [blame] | 1652 | let lint_cap = matches.opt_str("cap-lints").map(|cap| { |
| 1653 | lint::Level::from_str(&cap) |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1654 | .unwrap_or_else(|| early_dcx.early_fatal(format!("unknown lint level: `{cap}`"))) |
Guillaume Gomez | e221be8 | 2018-06-23 13:09:21 | [diff] [blame] | 1655 | }); |
Ryan Levick | 69a19bf | 2021-05-27 17:19:39 | [diff] [blame] | 1656 | |
inquisitivecrystal | 2f2db99 | 2021-07-08 07:05:38 | [diff] [blame] | 1657 | (lint_opts, describe_lints, lint_cap) |
Guillaume Gomez | e221be8 | 2018-06-23 13:09:21 | [diff] [blame] | 1658 | } |
| 1659 | |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 1660 | /// Parses the `--color` flag. |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1661 | pub fn parse_color(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> ColorConfig { |
Maybe Waffle | 94470f4 | 2022-11-16 21:58:58 | [diff] [blame] | 1662 | match matches.opt_str("color").as_deref() { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1663 | Some("auto") => ColorConfig::Auto, |
Nick Cameron | 6309b0f | 2015-12-13 22:17:55 | [diff] [blame] | 1664 | Some("always") => ColorConfig::Always, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1665 | Some("never") => ColorConfig::Never, |
Barosl Lee | 71f39c1a | 2015-08-22 14:51:53 | [diff] [blame] | 1666 | |
Nick Cameron | 6309b0f | 2015-12-13 22:17:55 | [diff] [blame] | 1667 | None => ColorConfig::Auto, |
Barosl Lee | 71f39c1a | 2015-08-22 14:51:53 | [diff] [blame] | 1668 | |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1669 | Some(arg) => early_dcx.early_fatal(format!( |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1670 | "argument for `--color` must be auto, \ |
Jubilee Young | de66e08 | 2022-03-25 05:11:05 | [diff] [blame] | 1671 | always or never (instead was `{arg}`)" |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1672 | )), |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1673 | } |
| 1674 | } |
Barosl Lee | 71f39c1a | 2015-08-22 14:51:53 | [diff] [blame] | 1675 | |
est31 | 2d52006 | 2020-07-05 17:35:46 | [diff] [blame] | 1676 | /// Possible json config files |
| 1677 | pub struct JsonConfig { |
| 1678 | pub json_rendered: HumanReadableErrorType, |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1679 | pub json_color: ColorConfig, |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1680 | json_artifact_notifications: bool, |
Jeremy Fitzhardinge | c6bafa7 | 2022-04-17 00:11:33 | [diff] [blame] | 1681 | pub json_unused_externs: JsonUnusedExterns, |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 1682 | json_future_incompat: bool, |
est31 | 2d52006 | 2020-07-05 17:35:46 | [diff] [blame] | 1683 | } |
| 1684 | |
Jeremy Fitzhardinge | c6bafa7 | 2022-04-17 00:11:33 | [diff] [blame] | 1685 | /// Report unused externs in event stream |
| 1686 | #[derive(Copy, Clone)] |
| 1687 | pub enum JsonUnusedExterns { |
| 1688 | /// Do not |
| 1689 | No, |
| 1690 | /// Report, but do not exit with failure status for deny/forbid |
| 1691 | Silent, |
| 1692 | /// Report, and also exit with failure status for deny/forbid |
| 1693 | Loud, |
| 1694 | } |
| 1695 | |
| 1696 | impl JsonUnusedExterns { |
| 1697 | pub fn is_enabled(&self) -> bool { |
| 1698 | match self { |
| 1699 | JsonUnusedExterns::No => false, |
| 1700 | JsonUnusedExterns::Loud | JsonUnusedExterns::Silent => true, |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | pub fn is_loud(&self) -> bool { |
| 1705 | match self { |
| 1706 | JsonUnusedExterns::No | JsonUnusedExterns::Silent => false, |
| 1707 | JsonUnusedExterns::Loud => true, |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1712 | /// Parse the `--json` flag. |
| 1713 | /// |
| 1714 | /// The first value returned is how to render JSON diagnostics, and the second |
| 1715 | /// is whether or not artifact notifications are enabled. |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1716 | pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> JsonConfig { |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1717 | let mut json_rendered = HumanReadableErrorType::Default; |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1718 | let mut json_color = ColorConfig::Never; |
| 1719 | let mut json_artifact_notifications = false; |
Jeremy Fitzhardinge | c6bafa7 | 2022-04-17 00:11:33 | [diff] [blame] | 1720 | let mut json_unused_externs = JsonUnusedExterns::No; |
Aaron Hill | 63523e4 | 2021-12-04 19:34:20 | [diff] [blame] | 1721 | let mut json_future_incompat = false; |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1722 | for option in matches.opt_strs("json") { |
| 1723 | // For now conservatively forbid `--color` with `--json` since `--json` |
| 1724 | // won't actually be emitting any colors and anything colorized is |
| 1725 | // embedded in a diagnostic message anyway. |
| 1726 | if matches.opt_str("color").is_some() { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1727 | early_dcx.early_fatal("cannot specify the `--color` option with `--json`"); |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1728 | } |
Kurtis Nusbaum | 51f5110 | 2018-04-19 20:56:26 | [diff] [blame] | 1729 | |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1730 | for sub_option in option.split(',') { |
| 1731 | match sub_option { |
| 1732 | "diagnostic-short" => json_rendered = HumanReadableErrorType::Short, |
Esteban Küber | 1d78004 | 2024-06-17 15:14:07 | [diff] [blame] | 1733 | "diagnostic-unicode" => { |
| 1734 | json_rendered = HumanReadableErrorType::Unicode; |
| 1735 | } |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1736 | "diagnostic-rendered-ansi" => json_color = ColorConfig::Always, |
| 1737 | "artifacts" => json_artifact_notifications = true, |
Jeremy Fitzhardinge | c6bafa7 | 2022-04-17 00:11:33 | [diff] [blame] | 1738 | "unused-externs" => json_unused_externs = JsonUnusedExterns::Loud, |
| 1739 | "unused-externs-silent" => json_unused_externs = JsonUnusedExterns::Silent, |
Aaron Hill | 63523e4 | 2021-12-04 19:34:20 | [diff] [blame] | 1740 | "future-incompat" => json_future_incompat = true, |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1741 | s => early_dcx.early_fatal(format!("unknown `--json` option `{s}`")), |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1742 | } |
| 1743 | } |
Kurtis Nusbaum | 320fdaa | 2018-04-20 04:03:21 | [diff] [blame] | 1744 | } |
est31 | 2d52006 | 2020-07-05 17:35:46 | [diff] [blame] | 1745 | |
| 1746 | JsonConfig { |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1747 | json_rendered, |
| 1748 | json_color, |
est31 | 2d52006 | 2020-07-05 17:35:46 | [diff] [blame] | 1749 | json_artifact_notifications, |
| 1750 | json_unused_externs, |
Aaron Hill | 63523e4 | 2021-12-04 19:34:20 | [diff] [blame] | 1751 | json_future_incompat, |
est31 | 2d52006 | 2020-07-05 17:35:46 | [diff] [blame] | 1752 | } |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1753 | } |
Kurtis Nusbaum | 320fdaa | 2018-04-20 04:03:21 | [diff] [blame] | 1754 | |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 1755 | /// Parses the `--error-format` flag. |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1756 | pub fn parse_error_format( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1757 | early_dcx: &mut EarlyDiagCtxt, |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1758 | matches: &getopts::Matches, |
| 1759 | color: ColorConfig, |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1760 | json_color: ColorConfig, |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1761 | json_rendered: HumanReadableErrorType, |
| 1762 | ) -> ErrorOutputType { |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 1763 | // We need the `opts_present` check because the driver will send us Matches |
Nick Cameron | 82f8e5c | 2016-01-06 20:23:01 | [diff] [blame] | 1764 | // with only stable options if no unstable options are used. Since error-format |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 1765 | // is unstable, it will not be present. We have to use `opts_present` not |
| 1766 | // `opt_present` because the latter will panic. |
Nick Cameron | 82f8e5c | 2016-01-06 20:23:01 | [diff] [blame] | 1767 | let error_format = if matches.opts_present(&["error-format".to_owned()]) { |
Maybe Waffle | 94470f4 | 2022-11-16 21:58:58 | [diff] [blame] | 1768 | match matches.opt_str("error-format").as_deref() { |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1769 | None | Some("human") => { |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1770 | ErrorOutputType::HumanReadable(HumanReadableErrorType::Default, color) |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1771 | } |
Philipp Hansch | c04a2cc | 2019-05-31 19:15:59 | [diff] [blame] | 1772 | Some("human-annotate-rs") => { |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1773 | ErrorOutputType::HumanReadable(HumanReadableErrorType::AnnotateSnippet, color) |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1774 | } |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1775 | Some("json") => { |
| 1776 | ErrorOutputType::Json { pretty: false, json_rendered, color_config: json_color } |
| 1777 | } |
| 1778 | Some("pretty-json") => { |
| 1779 | ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color } |
| 1780 | } |
| 1781 | Some("short") => ErrorOutputType::HumanReadable(HumanReadableErrorType::Short, color), |
Esteban Küber | 1d78004 | 2024-06-17 15:14:07 | [diff] [blame] | 1782 | Some("human-unicode") => { |
| 1783 | ErrorOutputType::HumanReadable(HumanReadableErrorType::Unicode, color) |
| 1784 | } |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1785 | Some(arg) => { |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1786 | early_dcx.abort_if_error_and_set_error_format(ErrorOutputType::HumanReadable( |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1787 | HumanReadableErrorType::Default, |
| 1788 | color, |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1789 | )); |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1790 | early_dcx.early_fatal(format!( |
León Orell Valerian Liehr | acf6344 | 2024-11-10 16:51:37 | [diff] [blame] | 1791 | "argument for `--error-format` must be `human`, `human-annotate-rs`, \ |
| 1792 | `human-unicode`, `json`, `pretty-json` or `short` (instead was `{arg}`)" |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1793 | )) |
| 1794 | } |
Nick Cameron | fd46c78 | 2015-12-31 03:50:06 | [diff] [blame] | 1795 | } |
| 1796 | } else { |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 1797 | ErrorOutputType::HumanReadable(HumanReadableErrorType::Default, color) |
Nick Cameron | fd46c78 | 2015-12-31 03:50:06 | [diff] [blame] | 1798 | }; |
| 1799 | |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1800 | match error_format { |
| 1801 | ErrorOutputType::Json { .. } => {} |
| 1802 | |
| 1803 | // Conservatively require that the `--json` argument is coupled with |
| 1804 | // `--error-format=json`. This means that `--json` is specified we |
| 1805 | // should actually be emitting JSON blobs. |
Matthias Krüger | 9523c89 | 2020-02-28 13:20:33 | [diff] [blame] | 1806 | _ if !matches.opt_strs("json").is_empty() => { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1807 | early_dcx.early_fatal("using `--json` requires also using `--error-format=json`"); |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | _ => {} |
| 1811 | } |
| 1812 | |
Matthias Krüger | ad00e91 | 2020-03-20 14:03:11 | [diff] [blame] | 1813 | error_format |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1814 | } |
| 1815 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1816 | pub fn parse_crate_edition(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Edition { |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1817 | let edition = match matches.opt_str("edition") { |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1818 | Some(arg) => Edition::from_str(&arg).unwrap_or_else(|_| { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1819 | early_dcx.early_fatal(format!( |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1820 | "argument for `--edition` must be one of: \ |
Jubilee Young | de66e08 | 2022-03-25 05:11:05 | [diff] [blame] | 1821 | {EDITION_NAME_LIST}. (instead was `{arg}`)" |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1822 | )) |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1823 | }), |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1824 | None => DEFAULT_EDITION, |
| 1825 | }; |
| 1826 | |
Mara Bos | 3d9d0e9 | 2020-12-24 15:48:41 | [diff] [blame] | 1827 | if !edition.is_stable() && !nightly_options::is_unstable_enabled(matches) { |
Kornel | 40af086 | 2021-04-10 10:46:36 | [diff] [blame] | 1828 | let is_nightly = nightly_options::match_is_nightly_build(matches); |
| 1829 | let msg = if !is_nightly { |
| 1830 | format!( |
Matthias Krüger | 2381546 | 2023-07-25 20:00:13 | [diff] [blame] | 1831 | "the crate requires edition {edition}, but the latest edition supported by this Rust version is {LATEST_STABLE_EDITION}" |
Kornel | 40af086 | 2021-04-10 10:46:36 | [diff] [blame] | 1832 | ) |
| 1833 | } else { |
Jubilee Young | de66e08 | 2022-03-25 05:11:05 | [diff] [blame] | 1834 | format!("edition {edition} is unstable and only available with -Z unstable-options") |
Kornel | 40af086 | 2021-04-10 10:46:36 | [diff] [blame] | 1835 | }; |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1836 | early_dcx.early_fatal(msg) |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1837 | } |
| 1838 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1839 | edition |
| 1840 | } |
Alex Crichton | 1731233 | 2019-07-17 19:52:56 | [diff] [blame] | 1841 | |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 1842 | fn check_error_format_stability( |
klensy | aa35530 | 2024-03-28 08:30:49 | [diff] [blame] | 1843 | early_dcx: &EarlyDiagCtxt, |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 1844 | unstable_opts: &UnstableOptions, |
León Orell Valerian Liehr | acf6344 | 2024-11-10 16:51:37 | [diff] [blame] | 1845 | format: ErrorOutputType, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1846 | ) { |
León Orell Valerian Liehr | acf6344 | 2024-11-10 16:51:37 | [diff] [blame] | 1847 | if unstable_opts.unstable_options { |
| 1848 | return; |
Oliver Schneider | c7cb2cf | 2017-11-03 12:38:26 | [diff] [blame] | 1849 | } |
León Orell Valerian Liehr | acf6344 | 2024-11-10 16:51:37 | [diff] [blame] | 1850 | let format = match format { |
| 1851 | ErrorOutputType::Json { pretty: true, .. } => "pretty-json", |
| 1852 | ErrorOutputType::HumanReadable(format, _) => match format { |
| 1853 | HumanReadableErrorType::AnnotateSnippet => "human-annotate-rs", |
| 1854 | HumanReadableErrorType::Unicode => "human-unicode", |
| 1855 | _ => return, |
| 1856 | }, |
| 1857 | _ => return, |
| 1858 | }; |
| 1859 | early_dcx.early_fatal(format!("`--error-format={format}` is unstable")) |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1860 | } |
Oliver Schneider | c7cb2cf | 2017-11-03 12:38:26 | [diff] [blame] | 1861 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1862 | fn parse_output_types( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1863 | early_dcx: &EarlyDiagCtxt, |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 1864 | unstable_opts: &UnstableOptions, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1865 | matches: &getopts::Matches, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1866 | ) -> OutputTypes { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 1867 | let mut output_types = BTreeMap::new(); |
Nicholas Nethercote | 76adf05 | 2024-11-28 19:05:56 | [diff] [blame] | 1868 | if !unstable_opts.parse_crate_root_only { |
Alex Crichton | 8c963c0 | 2015-09-30 17:08:37 | [diff] [blame] | 1869 | for list in matches.opt_strs("emit") { |
| 1870 | for output_type in list.split(',') { |
David Tolnay | f72bdb1 | 2023-07-17 00:58:46 | [diff] [blame] | 1871 | let (shorthand, path) = split_out_file_name(output_type); |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1872 | let output_type = OutputType::from_shorthand(shorthand).unwrap_or_else(|| { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1873 | early_dcx.early_fatal(format!( |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1874 | "unknown emission type: `{shorthand}` - expected one of: {display}", |
| 1875 | display = OutputType::shorthands_display(), |
| 1876 | )) |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1877 | }); |
Augie Fackler | aa91871 | 2024-01-19 19:42:43 | [diff] [blame] | 1878 | if output_type == OutputType::ThinLinkBitcode && !unstable_opts.unstable_options { |
| 1879 | early_dcx.early_fatal(format!( |
| 1880 | "{} requested but -Zunstable-options not specified", |
| 1881 | OutputType::ThinLinkBitcode.shorthand() |
| 1882 | )); |
| 1883 | } |
Alex Crichton | 8c963c0 | 2015-09-30 17:08:37 | [diff] [blame] | 1884 | output_types.insert(output_type, path); |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | }; |
Michael Kohl | 9873acc | 2017-05-28 06:49:14 | [diff] [blame] | 1888 | if output_types.is_empty() { |
Alex Crichton | 8c963c0 | 2015-09-30 17:08:37 | [diff] [blame] | 1889 | output_types.insert(OutputType::Exe, None); |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1890 | } |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1891 | OutputTypes(output_types) |
| 1892 | } |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 1893 | |
David Tolnay | f72bdb1 | 2023-07-17 00:58:46 | [diff] [blame] | 1894 | fn split_out_file_name(arg: &str) -> (&str, Option<OutFileName>) { |
| 1895 | match arg.split_once('=') { |
| 1896 | None => (arg, None), |
| 1897 | Some((kind, "-")) => (kind, Some(OutFileName::Stdout)), |
| 1898 | Some((kind, path)) => (kind, Some(OutFileName::Real(PathBuf::from(path)))), |
| 1899 | } |
| 1900 | } |
| 1901 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1902 | fn should_override_cgus_and_disable_thinlto( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1903 | early_dcx: &EarlyDiagCtxt, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1904 | output_types: &OutputTypes, |
| 1905 | matches: &getopts::Matches, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1906 | mut codegen_units: Option<usize>, |
| 1907 | ) -> (bool, Option<usize>) { |
Wesley Wiser | 7c6345d | 2022-10-27 00:28:25 | [diff] [blame] | 1908 | let mut disable_local_thinlto = false; |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 1909 | // Issue #30063: if user requests LLVM-related output to one |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 1910 | // particular path, disable codegen-units. |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 1911 | let incompatible: Vec<_> = output_types |
| 1912 | .0 |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1913 | .iter() |
Alex Crichton | 9e35b79 | 2017-09-25 19:26:25 | [diff] [blame] | 1914 | .map(|ot_path| ot_path.0) |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 1915 | .filter(|ot| !ot.is_compatible_with_codegen_units_and_single_output_file()) |
Alex Crichton | 9e35b79 | 2017-09-25 19:26:25 | [diff] [blame] | 1916 | .map(|ot| ot.shorthand()) |
| 1917 | .collect(); |
| 1918 | if !incompatible.is_empty() { |
| 1919 | match codegen_units { |
| 1920 | Some(n) if n > 1 => { |
| 1921 | if matches.opt_present("o") { |
| 1922 | for ot in &incompatible { |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1923 | early_dcx.early_warn(format!( |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1924 | "`--emit={ot}` with `-o` incompatible with \ |
Alexander Regueiro | 022d9c8 | 2019-09-01 17:09:59 | [diff] [blame] | 1925 | `-C codegen-units=N` for N > 1", |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 1926 | )); |
Alex Crichton | 9e35b79 | 2017-09-25 19:26:25 | [diff] [blame] | 1927 | } |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1928 | early_dcx.early_warn("resetting to default -C codegen-units=1"); |
Alex Crichton | 9e35b79 | 2017-09-25 19:26:25 | [diff] [blame] | 1929 | codegen_units = Some(1); |
Wesley Wiser | 7c6345d | 2022-10-27 00:28:25 | [diff] [blame] | 1930 | disable_local_thinlto = true; |
Alex Crichton | 9e35b79 | 2017-09-25 19:26:25 | [diff] [blame] | 1931 | } |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 1932 | } |
Alex Crichton | 855f6d1 | 2017-11-25 19:13:58 | [diff] [blame] | 1933 | _ => { |
| 1934 | codegen_units = Some(1); |
Wesley Wiser | 7c6345d | 2022-10-27 00:28:25 | [diff] [blame] | 1935 | disable_local_thinlto = true; |
Alex Crichton | 855f6d1 | 2017-11-25 19:13:58 | [diff] [blame] | 1936 | } |
Felix S. Klock II | f90c21a | 2015-12-04 18:35:16 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1940 | if codegen_units == Some(0) { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 1941 | early_dcx.early_fatal("value for codegen units must be a positive non-zero integer"); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1942 | } |
| 1943 | |
Wesley Wiser | 7c6345d | 2022-10-27 00:28:25 | [diff] [blame] | 1944 | (disable_local_thinlto, codegen_units) |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1945 | } |
| 1946 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1947 | fn collect_print_requests( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 1948 | early_dcx: &EarlyDiagCtxt, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1949 | cg: &mut CodegenOptions, |
klensy | b6cfe71 | 2024-03-28 08:03:48 | [diff] [blame] | 1950 | unstable_opts: &UnstableOptions, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1951 | matches: &getopts::Matches, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 1952 | ) -> Vec<PrintRequest> { |
Cameron Hart | e1efa32 | 2016-07-10 14:22:13 | [diff] [blame] | 1953 | let mut prints = Vec::<PrintRequest>::new(); |
Maybe Waffle | fb0f74a | 2023-05-24 14:19:22 | [diff] [blame] | 1954 | if cg.target_cpu.as_ref().is_some_and(|s| s == "help") { |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 1955 | prints.push(PrintRequest { kind: PrintKind::TargetCPUs, out: OutFileName::Stdout }); |
Cameron Hart | e1efa32 | 2016-07-10 14:22:13 | [diff] [blame] | 1956 | cg.target_cpu = None; |
| 1957 | }; |
| 1958 | if cg.target_feature == "help" { |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 1959 | prints.push(PrintRequest { kind: PrintKind::TargetFeatures, out: OutFileName::Stdout }); |
Matthias Krüger | ede1f7d | 2018-08-23 08:14:52 | [diff] [blame] | 1960 | cg.target_feature = String::new(); |
Cameron Hart | e1efa32 | 2016-07-10 14:22:13 | [diff] [blame] | 1961 | } |
Cameron Hart | e1efa32 | 2016-07-10 14:22:13 | [diff] [blame] | 1962 | |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 1963 | const PRINT_KINDS: &[(&str, PrintKind)] = &[ |
Nicholas Nethercote | 20046ce | 2023-11-29 05:33:08 | [diff] [blame] | 1964 | // tidy-alphabetical-start |
| 1965 | ("all-target-specs-json", PrintKind::AllTargetSpecs), |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 1966 | ("calling-conventions", PrintKind::CallingConventions), |
Nicholas Nethercote | 20046ce | 2023-11-29 05:33:08 | [diff] [blame] | 1967 | ("cfg", PrintKind::Cfg), |
Urgau | ac59bdc | 2024-04-22 21:47:48 | [diff] [blame] | 1968 | ("check-cfg", PrintKind::CheckCfg), |
Nicholas Nethercote | 20046ce | 2023-11-29 05:33:08 | [diff] [blame] | 1969 | ("code-models", PrintKind::CodeModels), |
| 1970 | ("crate-name", PrintKind::CrateName), |
| 1971 | ("deployment-target", PrintKind::DeploymentTarget), |
| 1972 | ("file-names", PrintKind::FileNames), |
Noratrieb | ba48151 | 2024-10-17 17:03:06 | [diff] [blame] | 1973 | ("host-tuple", PrintKind::HostTuple), |
Nicholas Nethercote | 20046ce | 2023-11-29 05:33:08 | [diff] [blame] | 1974 | ("link-args", PrintKind::LinkArgs), |
| 1975 | ("native-static-libs", PrintKind::NativeStaticLibs), |
| 1976 | ("relocation-models", PrintKind::RelocationModels), |
| 1977 | ("split-debuginfo", PrintKind::SplitDebuginfo), |
| 1978 | ("stack-protector-strategies", PrintKind::StackProtectorStrategies), |
| 1979 | ("sysroot", PrintKind::Sysroot), |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 1980 | ("target-cpus", PrintKind::TargetCPUs), |
| 1981 | ("target-features", PrintKind::TargetFeatures), |
Nicholas Nethercote | 20046ce | 2023-11-29 05:33:08 | [diff] [blame] | 1982 | ("target-libdir", PrintKind::TargetLibdir), |
| 1983 | ("target-list", PrintKind::TargetList), |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 1984 | ("target-spec-json", PrintKind::TargetSpec), |
Nicholas Nethercote | 20046ce | 2023-11-29 05:33:08 | [diff] [blame] | 1985 | ("tls-models", PrintKind::TlsModels), |
| 1986 | // tidy-alphabetical-end |
nils | b20d969 | 2022-11-01 15:24:01 | [diff] [blame] | 1987 | ]; |
| 1988 | |
David Tolnay | 32cac2e | 2023-07-17 02:06:04 | [diff] [blame] | 1989 | // We disallow reusing the same path in multiple prints, such as `--print |
| 1990 | // cfg=output.txt --print link-args=output.txt`, because outputs are printed |
| 1991 | // by disparate pieces of the compiler, and keeping track of which files |
| 1992 | // need to be overwritten vs appended to is annoying. |
| 1993 | let mut printed_paths = FxHashSet::default(); |
| 1994 | |
nils | b20d969 | 2022-11-01 15:24:01 | [diff] [blame] | 1995 | prints.extend(matches.opt_strs("print").into_iter().map(|req| { |
David Tolnay | f72bdb1 | 2023-07-17 00:58:46 | [diff] [blame] | 1996 | let (req, out) = split_out_file_name(&req); |
| 1997 | |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 1998 | let kind = match PRINT_KINDS.iter().find(|&&(name, _)| name == req) { |
| 1999 | Some((_, PrintKind::TargetSpec)) => { |
nils | b20d969 | 2022-11-01 15:24:01 | [diff] [blame] | 2000 | if unstable_opts.unstable_options { |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 2001 | PrintKind::TargetSpec |
nils | b20d969 | 2022-11-01 15:24:01 | [diff] [blame] | 2002 | } else { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2003 | early_dcx.early_fatal( |
nils | b20d969 | 2022-11-01 15:24:01 | [diff] [blame] | 2004 | "the `-Z unstable-options` flag must also be passed to \ |
Pietro Albini | ef2bf6d | 2023-03-09 13:52:45 | [diff] [blame] | 2005 | enable the target-spec-json print option", |
| 2006 | ); |
| 2007 | } |
| 2008 | } |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 2009 | Some((_, PrintKind::AllTargetSpecs)) => { |
Pietro Albini | ef2bf6d | 2023-03-09 13:52:45 | [diff] [blame] | 2010 | if unstable_opts.unstable_options { |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 2011 | PrintKind::AllTargetSpecs |
Pietro Albini | ef2bf6d | 2023-03-09 13:52:45 | [diff] [blame] | 2012 | } else { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2013 | early_dcx.early_fatal( |
Pietro Albini | ef2bf6d | 2023-03-09 13:52:45 | [diff] [blame] | 2014 | "the `-Z unstable-options` flag must also be passed to \ |
| 2015 | enable the all-target-specs-json print option", |
nils | b20d969 | 2022-11-01 15:24:01 | [diff] [blame] | 2016 | ); |
| 2017 | } |
| 2018 | } |
Urgau | ac59bdc | 2024-04-22 21:47:48 | [diff] [blame] | 2019 | Some((_, PrintKind::CheckCfg)) => { |
| 2020 | if unstable_opts.unstable_options { |
| 2021 | PrintKind::CheckCfg |
| 2022 | } else { |
| 2023 | early_dcx.early_fatal( |
| 2024 | "the `-Z unstable-options` flag must also be passed to \ |
| 2025 | enable the check-cfg print option", |
| 2026 | ); |
| 2027 | } |
| 2028 | } |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 2029 | Some(&(_, print_kind)) => print_kind, |
nils | b20d969 | 2022-11-01 15:24:01 | [diff] [blame] | 2030 | None => { |
| 2031 | let prints = |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 2032 | PRINT_KINDS.iter().map(|(name, _)| format!("`{name}`")).collect::<Vec<_>>(); |
nils | b20d969 | 2022-11-01 15:24:01 | [diff] [blame] | 2033 | let prints = prints.join(", "); |
Urgau | 153b1f0 | 2024-04-24 13:34:57 | [diff] [blame] | 2034 | |
| 2035 | let mut diag = |
| 2036 | early_dcx.early_struct_fatal(format!("unknown print request: `{req}`")); |
| 2037 | #[allow(rustc::diagnostic_outside_of_impl)] |
| 2038 | diag.help(format!("valid print requests are: {prints}")); |
| 2039 | diag.emit() |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2040 | } |
David Tolnay | c0dc0c6 | 2023-07-17 00:20:28 | [diff] [blame] | 2041 | }; |
David Tolnay | f72bdb1 | 2023-07-17 00:58:46 | [diff] [blame] | 2042 | |
| 2043 | let out = out.unwrap_or(OutFileName::Stdout); |
David Tolnay | 32cac2e | 2023-07-17 02:06:04 | [diff] [blame] | 2044 | if let OutFileName::Real(path) = &out { |
| 2045 | if !printed_paths.insert(path.clone()) { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2046 | early_dcx.early_fatal(format!( |
David Tolnay | 32cac2e | 2023-07-17 02:06:04 | [diff] [blame] | 2047 | "cannot print multiple outputs to the same path: {}", |
| 2048 | path.display(), |
| 2049 | )); |
| 2050 | } |
| 2051 | } |
| 2052 | |
David Tolnay | f72bdb1 | 2023-07-17 00:58:46 | [diff] [blame] | 2053 | PrintRequest { kind, out } |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2054 | })); |
Alex Crichton | 117984b | 2014-12-16 00:03:39 | [diff] [blame] | 2055 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2056 | prints |
| 2057 | } |
| 2058 | |
Noratrieb | a26450c | 2024-10-17 17:02:32 | [diff] [blame] | 2059 | pub fn parse_target_triple(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> TargetTuple { |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2060 | match matches.opt_str("target") { |
| 2061 | Some(target) if target.ends_with(".json") => { |
Philipp Oppermann | 7b49190 | 2018-03-24 19:14:59 | [diff] [blame] | 2062 | let path = Path::new(&target); |
Noratrieb | a26450c | 2024-10-17 17:02:32 | [diff] [blame] | 2063 | TargetTuple::from_path(path).unwrap_or_else(|_| { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2064 | early_dcx.early_fatal(format!("target file {path:?} does not exist")) |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 2065 | }) |
Philipp Oppermann | 3908b2e | 2018-03-14 14:27:06 | [diff] [blame] | 2066 | } |
Noratrieb | a26450c | 2024-10-17 17:02:32 | [diff] [blame] | 2067 | Some(target) => TargetTuple::TargetTuple(target), |
| 2068 | _ => TargetTuple::from_tuple(host_tuple()), |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | fn parse_opt_level( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2073 | early_dcx: &EarlyDiagCtxt, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2074 | matches: &getopts::Matches, |
| 2075 | cg: &CodegenOptions, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2076 | ) -> OptLevel { |
| 2077 | // The `-O` and `-C opt-level` flags specify the same setting, so we want to be able |
| 2078 | // to use them interchangeably. However, because they're technically different flags, |
| 2079 | // we need to work out manually which should take precedence if both are supplied (i.e. |
| 2080 | // the rightmost flag). We do this by finding the (rightmost) position of both flags and |
| 2081 | // comparing them. Note that if a flag is not found, its position will be `None`, which |
| 2082 | // always compared less than `Some(_)`. |
| 2083 | let max_o = matches.opt_positions("O").into_iter().max(); |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 2084 | let max_c = matches |
| 2085 | .opt_strs_pos("C") |
| 2086 | .into_iter() |
Eric Arellano | 12db222 | 2020-12-07 18:59:24 | [diff] [blame] | 2087 | .flat_map(|(i, s)| { |
| 2088 | // NB: This can match a string without `=`. |
Matthias Krüger | de59844 | 2022-12-14 23:06:34 | [diff] [blame] | 2089 | if let Some("opt-level") = s.split('=').next() { Some(i) } else { None } |
Eric Arellano | 12db222 | 2020-12-07 18:59:24 | [diff] [blame] | 2090 | }) |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 2091 | .max(); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2092 | if max_o > max_c { |
| 2093 | OptLevel::Default |
Philipp Oppermann | 3908b2e | 2018-03-14 14:27:06 | [diff] [blame] | 2094 | } else { |
Nicholas Nethercote | 58217bc | 2020-04-02 05:44:47 | [diff] [blame] | 2095 | match cg.opt_level.as_ref() { |
| 2096 | "0" => OptLevel::No, |
| 2097 | "1" => OptLevel::Less, |
| 2098 | "2" => OptLevel::Default, |
| 2099 | "3" => OptLevel::Aggressive, |
| 2100 | "s" => OptLevel::Size, |
| 2101 | "z" => OptLevel::SizeMin, |
| 2102 | arg => { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2103 | early_dcx.early_fatal(format!( |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2104 | "optimization level needs to be \ |
Jubilee Young | de66e08 | 2022-03-25 05:11:05 | [diff] [blame] | 2105 | between 0-3, s or z (instead was `{arg}`)" |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2106 | )); |
Alex Crichton | 117984b | 2014-12-16 00:03:39 | [diff] [blame] | 2107 | } |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 2108 | } |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | |
Julia Tatz | 0504a33 | 2021-04-06 20:00:35 | [diff] [blame] | 2112 | fn select_debuginfo(matches: &getopts::Matches, cg: &CodegenOptions) -> DebugInfo { |
varkor | e8e43c9 | 2019-04-30 20:52:05 | [diff] [blame] | 2113 | let max_g = matches.opt_positions("g").into_iter().max(); |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 2114 | let max_c = matches |
| 2115 | .opt_strs_pos("C") |
| 2116 | .into_iter() |
Eric Arellano | 12db222 | 2020-12-07 18:59:24 | [diff] [blame] | 2117 | .flat_map(|(i, s)| { |
| 2118 | // NB: This can match a string without `=`. |
Matthias Krüger | de59844 | 2022-12-14 23:06:34 | [diff] [blame] | 2119 | if let Some("debuginfo") = s.split('=').next() { Some(i) } else { None } |
Eric Arellano | 12db222 | 2020-12-07 18:59:24 | [diff] [blame] | 2120 | }) |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 2121 | .max(); |
Julia Tatz | 0504a33 | 2021-04-06 20:00:35 | [diff] [blame] | 2122 | if max_g > max_c { DebugInfo::Full } else { cg.debuginfo } |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2123 | } |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 2124 | |
Nicholas Nethercote | 5f11d19 | 2023-11-28 05:30:57 | [diff] [blame] | 2125 | fn parse_assert_incr_state( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2126 | early_dcx: &EarlyDiagCtxt, |
pierwill | 1642fdf | 2021-10-31 22:05:48 | [diff] [blame] | 2127 | opt_assertion: &Option<String>, |
pierwill | 1642fdf | 2021-10-31 22:05:48 | [diff] [blame] | 2128 | ) -> Option<IncrementalStateAssertion> { |
| 2129 | match opt_assertion { |
| 2130 | Some(s) if s.as_str() == "loaded" => Some(IncrementalStateAssertion::Loaded), |
| 2131 | Some(s) if s.as_str() == "not-loaded" => Some(IncrementalStateAssertion::NotLoaded), |
Jubilee Young | de66e08 | 2022-03-25 05:11:05 | [diff] [blame] | 2132 | Some(s) => { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2133 | early_dcx.early_fatal(format!("unexpected incremental state assertion value: {s}")) |
Jubilee Young | de66e08 | 2022-03-25 05:11:05 | [diff] [blame] | 2134 | } |
pierwill | 1642fdf | 2021-10-31 22:05:48 | [diff] [blame] | 2135 | None => None, |
| 2136 | } |
| 2137 | } |
| 2138 | |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2139 | pub fn parse_externs( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2140 | early_dcx: &EarlyDiagCtxt, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2141 | matches: &getopts::Matches, |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 2142 | unstable_opts: &UnstableOptions, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2143 | ) -> Externs { |
Nicholas Nethercote | dba9416 | 2023-11-28 14:16:25 | [diff] [blame] | 2144 | fn is_ascii_ident(string: &str) -> bool { |
| 2145 | let mut chars = string.chars(); |
| 2146 | if let Some(start) = chars.next() |
| 2147 | && (start.is_ascii_alphabetic() || start == '_') |
| 2148 | { |
| 2149 | chars.all(|char| char.is_ascii_alphanumeric() || char == '_') |
| 2150 | } else { |
| 2151 | false |
| 2152 | } |
| 2153 | } |
| 2154 | |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 2155 | let is_unstable_enabled = unstable_opts.unstable_options; |
Aaron Hill | 482b77a | 2019-04-07 22:48:40 | [diff] [blame] | 2156 | let mut externs: BTreeMap<String, ExternEntry> = BTreeMap::new(); |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2157 | for arg in matches.opt_strs("extern") { |
Eric Arellano | 12db222 | 2020-12-07 18:59:24 | [diff] [blame] | 2158 | let (name, path) = match arg.split_once('=') { |
| 2159 | None => (arg, None), |
Ryan Levick | 6c7ecd0 | 2021-01-26 21:27:42 | [diff] [blame] | 2160 | Some((name, path)) => (name.to_string(), Some(Path::new(path))), |
Eric Arellano | 12db222 | 2020-12-07 18:59:24 | [diff] [blame] | 2161 | }; |
| 2162 | let (options, name) = match name.split_once(':') { |
| 2163 | None => (None, name), |
| 2164 | Some((opts, name)) => (Some(opts), name.to_string()), |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2165 | }; |
Alex Crichton | cc3c8bb | 2014-07-01 15:37:54 | [diff] [blame] | 2166 | |
Nicholas Nethercote | dba9416 | 2023-11-28 14:16:25 | [diff] [blame] | 2167 | if !is_ascii_ident(&name) { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2168 | let mut error = early_dcx.early_struct_fatal(format!( |
León Orell Valerian Liehr | 8d81d5a | 2023-08-18 11:23:53 | [diff] [blame] | 2169 | "crate name `{name}` passed to `--extern` is not a valid ASCII identifier" |
| 2170 | )); |
Nicholas Nethercote | 1b3733e | 2023-11-06 02:06:40 | [diff] [blame] | 2171 | let adjusted_name = name.replace('-', "_"); |
Nicholas Nethercote | dba9416 | 2023-11-28 14:16:25 | [diff] [blame] | 2172 | if is_ascii_ident(&adjusted_name) { |
Nicholas Nethercote | b7d58ee | 2024-02-20 03:12:50 | [diff] [blame] | 2173 | #[allow(rustc::diagnostic_outside_of_impl)] // FIXME |
León Orell Valerian Liehr | 8d81d5a | 2023-08-18 11:23:53 | [diff] [blame] | 2174 | error.help(format!( |
| 2175 | "consider replacing the dashes with underscores: `{adjusted_name}`" |
| 2176 | )); |
| 2177 | } |
| 2178 | error.emit(); |
| 2179 | } |
| 2180 | |
Ryan Levick | 6c7ecd0 | 2021-01-26 21:27:42 | [diff] [blame] | 2181 | let path = path.map(|p| CanonicalizedPath::new(p)); |
| 2182 | |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2183 | let entry = externs.entry(name.to_owned()); |
Aaron Hill | 482b77a | 2019-04-07 22:48:40 | [diff] [blame] | 2184 | |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2185 | use std::collections::btree_map::Entry; |
Aaron Hill | 4dfce34 | 2019-04-09 21:24:24 | [diff] [blame] | 2186 | |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2187 | let entry = if let Some(path) = path { |
| 2188 | // --extern prelude_name=some_file.rlib |
| 2189 | match entry { |
| 2190 | Entry::Vacant(vacant) => { |
| 2191 | let files = BTreeSet::from_iter(iter::once(path)); |
| 2192 | vacant.insert(ExternEntry::new(ExternLocation::ExactPaths(files))) |
| 2193 | } |
| 2194 | Entry::Occupied(occupied) => { |
| 2195 | let ext_ent = occupied.into_mut(); |
| 2196 | match ext_ent { |
| 2197 | ExternEntry { location: ExternLocation::ExactPaths(files), .. } => { |
| 2198 | files.insert(path); |
| 2199 | } |
| 2200 | ExternEntry { |
| 2201 | location: location @ ExternLocation::FoundInLibrarySearchDirectories, |
| 2202 | .. |
| 2203 | } => { |
| 2204 | // Exact paths take precedence over search directories. |
| 2205 | let files = BTreeSet::from_iter(iter::once(path)); |
| 2206 | *location = ExternLocation::ExactPaths(files); |
| 2207 | } |
| 2208 | } |
| 2209 | ext_ent |
| 2210 | } |
| 2211 | } |
| 2212 | } else { |
| 2213 | // --extern prelude_name |
| 2214 | match entry { |
| 2215 | Entry::Vacant(vacant) => { |
| 2216 | vacant.insert(ExternEntry::new(ExternLocation::FoundInLibrarySearchDirectories)) |
| 2217 | } |
| 2218 | Entry::Occupied(occupied) => { |
| 2219 | // Ignore if already specified. |
| 2220 | occupied.into_mut() |
| 2221 | } |
| 2222 | } |
| 2223 | }; |
| 2224 | |
| 2225 | let mut is_private_dep = false; |
| 2226 | let mut add_prelude = true; |
Jeremy Fitzhardinge | 9102edf | 2022-04-14 22:09:00 | [diff] [blame] | 2227 | let mut nounused_dep = false; |
Matt Hammerly | 812f2d7 | 2023-03-14 03:55:43 | [diff] [blame] | 2228 | let mut force = false; |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2229 | if let Some(opts) = options { |
| 2230 | if !is_unstable_enabled { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2231 | early_dcx.early_fatal( |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2232 | "the `-Z unstable-options` flag must also be passed to \ |
est31 | ef65890 | 2023-03-03 07:38:07 | [diff] [blame] | 2233 | enable `--extern` options", |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2234 | ); |
| 2235 | } |
| 2236 | for opt in opts.split(',') { |
| 2237 | match opt { |
| 2238 | "priv" => is_private_dep = true, |
| 2239 | "noprelude" => { |
| 2240 | if let ExternLocation::ExactPaths(_) = &entry.location { |
| 2241 | add_prelude = false; |
| 2242 | } else { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2243 | early_dcx.early_fatal( |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2244 | "the `noprelude` --extern option requires a file path", |
| 2245 | ); |
| 2246 | } |
| 2247 | } |
Jeremy Fitzhardinge | 9102edf | 2022-04-14 22:09:00 | [diff] [blame] | 2248 | "nounused" => nounused_dep = true, |
Matt Hammerly | 812f2d7 | 2023-03-14 03:55:43 | [diff] [blame] | 2249 | "force" => force = true, |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2250 | _ => early_dcx.early_fatal(format!("unknown --extern option `{opt}`")), |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | // Crates start out being not private, and go to being private `priv` |
| 2256 | // is specified. |
| 2257 | entry.is_private_dep |= is_private_dep; |
Jeremy Fitzhardinge | 9102edf | 2022-04-14 22:09:00 | [diff] [blame] | 2258 | // likewise `nounused` |
| 2259 | entry.nounused_dep |= nounused_dep; |
Matt Hammerly | 812f2d7 | 2023-03-14 03:55:43 | [diff] [blame] | 2260 | // and `force` |
| 2261 | entry.force |= force; |
Eric Huss | 590dd7d | 2019-12-05 22:43:53 | [diff] [blame] | 2262 | // If any flag is missing `noprelude`, then add to the prelude. |
| 2263 | entry.add_prelude |= add_prelude; |
Aaron Hill | 482b77a | 2019-04-07 22:48:40 | [diff] [blame] | 2264 | } |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2265 | Externs(externs) |
| 2266 | } |
Aaron Hill | 7cc3ce3 | 2019-03-25 03:06:32 | [diff] [blame] | 2267 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2268 | fn parse_remap_path_prefix( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2269 | early_dcx: &EarlyDiagCtxt, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2270 | matches: &getopts::Matches, |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 2271 | unstable_opts: &UnstableOptions, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2272 | ) -> Vec<(PathBuf, PathBuf)> { |
danakj | ce35f8e | 2021-07-22 18:52:45 | [diff] [blame] | 2273 | let mut mapping: Vec<(PathBuf, PathBuf)> = matches |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2274 | .opt_strs("remap-path-prefix") |
Jeremy Fitzhardinge | 56a6828 | 2018-02-18 23:05:24 | [diff] [blame] | 2275 | .into_iter() |
Eric Arellano | 12db222 | 2020-12-07 18:59:24 | [diff] [blame] | 2276 | .map(|remap| match remap.rsplit_once('=') { |
Nicholas Nethercote | f6aa418 | 2023-12-18 00:15:13 | [diff] [blame] | 2277 | None => { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2278 | early_dcx.early_fatal("--remap-path-prefix must contain '=' between FROM and TO") |
Nicholas Nethercote | f6aa418 | 2023-12-18 00:15:13 | [diff] [blame] | 2279 | } |
Eric Arellano | 12db222 | 2020-12-07 18:59:24 | [diff] [blame] | 2280 | Some((from, to)) => (PathBuf::from(from), PathBuf::from(to)), |
Jeremy Fitzhardinge | 56a6828 | 2018-02-18 23:05:24 | [diff] [blame] | 2281 | }) |
danakj | ce35f8e | 2021-07-22 18:52:45 | [diff] [blame] | 2282 | .collect(); |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 2283 | match &unstable_opts.remap_cwd_prefix { |
danakj | ce35f8e | 2021-07-22 18:52:45 | [diff] [blame] | 2284 | Some(to) => match std::env::current_dir() { |
| 2285 | Ok(cwd) => mapping.push((cwd, to.clone())), |
| 2286 | Err(_) => (), |
| 2287 | }, |
| 2288 | None => (), |
| 2289 | }; |
| 2290 | mapping |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2291 | } |
Jeremy Fitzhardinge | 56a6828 | 2018-02-18 23:05:24 | [diff] [blame] | 2292 | |
Guillaume Gomez | 486e55e | 2023-11-27 12:47:26 | [diff] [blame] | 2293 | fn parse_logical_env( |
klensy | aa35530 | 2024-03-28 08:30:49 | [diff] [blame] | 2294 | early_dcx: &EarlyDiagCtxt, |
Guillaume Gomez | 486e55e | 2023-11-27 12:47:26 | [diff] [blame] | 2295 | matches: &getopts::Matches, |
| 2296 | ) -> FxIndexMap<String, String> { |
| 2297 | let mut vars = FxIndexMap::default(); |
| 2298 | |
Guillaume Gomez | 462bcac | 2024-01-12 10:02:57 | [diff] [blame] | 2299 | for arg in matches.opt_strs("env-set") { |
Guillaume Gomez | 486e55e | 2023-11-27 12:47:26 | [diff] [blame] | 2300 | if let Some((name, val)) = arg.split_once('=') { |
| 2301 | vars.insert(name.to_string(), val.to_string()); |
| 2302 | } else { |
Guillaume Gomez | 462bcac | 2024-01-12 10:02:57 | [diff] [blame] | 2303 | early_dcx.early_fatal(format!("`--env-set`: specify value for variable `{arg}`")); |
Guillaume Gomez | 486e55e | 2023-11-27 12:47:26 | [diff] [blame] | 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | vars |
| 2308 | } |
| 2309 | |
David Wood | 7bab769 | 2022-07-25 12:02:39 | [diff] [blame] | 2310 | // JUSTIFICATION: before wrapper fn is available |
Mark Rousskov | 154a09d | 2022-08-09 13:56:13 | [diff] [blame] | 2311 | #[allow(rustc::bad_opt_access)] |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2312 | pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::Matches) -> Options { |
| 2313 | let color = parse_color(early_dcx, matches); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2314 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2315 | let edition = parse_crate_edition(early_dcx, matches); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2316 | |
Aaron Hill | 63523e4 | 2021-12-04 19:34:20 | [diff] [blame] | 2317 | let JsonConfig { |
| 2318 | json_rendered, |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 2319 | json_color, |
Aaron Hill | 63523e4 | 2021-12-04 19:34:20 | [diff] [blame] | 2320 | json_artifact_notifications, |
| 2321 | json_unused_externs, |
| 2322 | json_future_incompat, |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2323 | } = parse_json(early_dcx, matches); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2324 | |
Esteban Küber | ae696f8 | 2024-08-08 01:46:16 | [diff] [blame] | 2325 | let error_format = parse_error_format(early_dcx, matches, color, json_color, json_rendered); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2326 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2327 | early_dcx.abort_if_error_and_set_error_format(error_format); |
许杰友 Jieyou Xu (Joe) | 53245a1 | 2023-06-28 15:51:12 | [diff] [blame] | 2328 | |
David Wood | 44c1fcc | 2022-07-06 10:57:41 | [diff] [blame] | 2329 | let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_else(|_| { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2330 | early_dcx.early_fatal("`--diagnostic-width` must be an positive integer"); |
David Wood | e528884 | 2022-02-14 06:01:38 | [diff] [blame] | 2331 | }); |
| 2332 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2333 | let unparsed_crate_types = matches.opt_strs("crate-type"); |
| 2334 | let crate_types = parse_crate_types_from_list(unparsed_crate_types) |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2335 | .unwrap_or_else(|e| early_dcx.early_fatal(e)); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2336 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2337 | let mut unstable_opts = UnstableOptions::build(early_dcx, matches); |
| 2338 | let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(early_dcx, matches); |
Ryan Levick | 3b206b7 | 2021-06-02 15:09:07 | [diff] [blame] | 2339 | |
Nicholas Nethercote | 8843223 | 2024-01-05 03:11:12 | [diff] [blame] | 2340 | check_error_format_stability(early_dcx, &unstable_opts, error_format); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2341 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2342 | let output_types = parse_output_types(early_dcx, &unstable_opts, matches); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2343 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2344 | let mut cg = CodegenOptions::build(early_dcx, matches); |
Zalathar | ce3e14a | 2024-10-17 09:11:20 | [diff] [blame] | 2345 | let (disable_local_thinlto, codegen_units) = should_override_cgus_and_disable_thinlto( |
Nicholas Nethercote | f6aa418 | 2023-12-18 00:15:13 | [diff] [blame] | 2346 | early_dcx, |
| 2347 | &output_types, |
| 2348 | matches, |
| 2349 | cg.codegen_units, |
| 2350 | ); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2351 | |
Tomasz Miąsko | a73d1bf | 2023-11-29 00:00:00 | [diff] [blame] | 2352 | if unstable_opts.threads == 0 { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2353 | early_dcx.early_fatal("value for threads must be a positive non-zero integer"); |
Tomasz Miąsko | a73d1bf | 2023-11-29 00:00:00 | [diff] [blame] | 2354 | } |
| 2355 | |
Michal Piotrowski | 7591eb6 | 2024-10-30 11:31:26 | [diff] [blame] | 2356 | if unstable_opts.threads == parse::MAX_THREADS_CAP { |
| 2357 | early_dcx.early_warn(format!("number of threads was capped at {}", parse::MAX_THREADS_CAP)); |
| 2358 | } |
| 2359 | |
Matthias Krüger | 08f2904 | 2020-03-29 18:19:14 | [diff] [blame] | 2360 | let incremental = cg.incremental.as_ref().map(PathBuf::from); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2361 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2362 | let assert_incr_state = parse_assert_incr_state(early_dcx, &unstable_opts.assert_incr_state); |
pierwill | 1642fdf | 2021-10-31 22:05:48 | [diff] [blame] | 2363 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2364 | if cg.profile_generate.enabled() && cg.profile_use.is_some() { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2365 | early_dcx.early_fatal("options `-C profile-generate` and `-C profile-use` are exclusive"); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2366 | } |
| 2367 | |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 2368 | if unstable_opts.profile_sample_use.is_some() |
Michael Benfield | a17193d | 2021-05-07 07:41:37 | [diff] [blame] | 2369 | && (cg.profile_generate.enabled() || cg.profile_use.is_some()) |
| 2370 | { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2371 | early_dcx.early_fatal( |
Michael Benfield | a17193d | 2021-05-07 07:41:37 | [diff] [blame] | 2372 | "option `-Z profile-sample-use` cannot be used with `-C profile-generate` or `-C profile-use`", |
| 2373 | ); |
| 2374 | } |
| 2375 | |
Zalathar | 76103a8 | 2023-11-02 06:34:05 | [diff] [blame] | 2376 | // Check for unstable values of `-C symbol-mangling-version`. |
| 2377 | // This is what prevents them from being used on stable compilers. |
| 2378 | match cg.symbol_mangling_version { |
| 2379 | // Stable values: |
| 2380 | None | Some(SymbolManglingVersion::V0) => {} |
h1467792822 | 6e53e66 | 2023-12-05 04:42:57 | [diff] [blame] | 2381 | |
Zalathar | 76103a8 | 2023-11-02 06:34:05 | [diff] [blame] | 2382 | // Unstable values: |
| 2383 | Some(SymbolManglingVersion::Legacy) => { |
| 2384 | if !unstable_opts.unstable_options { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2385 | early_dcx.early_fatal( |
Zalathar | 76103a8 | 2023-11-02 06:34:05 | [diff] [blame] | 2386 | "`-C symbol-mangling-version=legacy` requires `-Z unstable-options`", |
| 2387 | ); |
| 2388 | } |
Josh Triplett | bbf4b66 | 2021-10-21 12:02:59 | [diff] [blame] | 2389 | } |
h1467792822 | 6e53e66 | 2023-12-05 04:42:57 | [diff] [blame] | 2390 | Some(SymbolManglingVersion::Hashed) => { |
| 2391 | if !unstable_opts.unstable_options { |
| 2392 | early_dcx.early_fatal( |
| 2393 | "`-C symbol-mangling-version=hashed` requires `-Z unstable-options`", |
| 2394 | ); |
| 2395 | } |
| 2396 | } |
Josh Triplett | bbf4b66 | 2021-10-21 12:02:59 | [diff] [blame] | 2397 | } |
| 2398 | |
Zalathar | 9f287dd | 2023-10-25 03:57:25 | [diff] [blame] | 2399 | if cg.instrument_coverage != InstrumentCoverage::No { |
Rich Kadel | a6f8b8a | 2020-07-02 18:27:15 | [diff] [blame] | 2400 | if cg.profile_generate.enabled() || cg.profile_use.is_some() { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2401 | early_dcx.early_fatal( |
Josh Triplett | 34106f8 | 2021-10-21 14:04:22 | [diff] [blame] | 2402 | "option `-C instrument-coverage` is not compatible with either `-C profile-use` \ |
Rich Kadel | a6f8b8a | 2020-07-02 18:27:15 | [diff] [blame] | 2403 | or `-C profile-generate`", |
| 2404 | ); |
| 2405 | } |
| 2406 | |
Josh Triplett | 34106f8 | 2021-10-21 14:04:22 | [diff] [blame] | 2407 | // `-C instrument-coverage` implies `-C symbol-mangling-version=v0` - to ensure consistent |
Rich Kadel | ddb054a | 2020-08-27 19:53:43 | [diff] [blame] | 2408 | // and reversible name mangling. Note, LLVM coverage tools can analyze coverage over |
| 2409 | // multiple runs, including some changes to source code; so mangled names must be consistent |
| 2410 | // across compilations. |
Josh Triplett | bbf4b66 | 2021-10-21 12:02:59 | [diff] [blame] | 2411 | match cg.symbol_mangling_version { |
| 2412 | None => cg.symbol_mangling_version = Some(SymbolManglingVersion::V0), |
Rich Kadel | 4f550f1 | 2020-12-14 08:25:29 | [diff] [blame] | 2413 | Some(SymbolManglingVersion::Legacy) => { |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2414 | early_dcx.early_warn( |
Josh Triplett | 34106f8 | 2021-10-21 14:04:22 | [diff] [blame] | 2415 | "-C instrument-coverage requires symbol mangling version `v0`, \ |
Josh Triplett | bbf4b66 | 2021-10-21 12:02:59 | [diff] [blame] | 2416 | but `-C symbol-mangling-version=legacy` was specified", |
Rich Kadel | 4f550f1 | 2020-12-14 08:25:29 | [diff] [blame] | 2417 | ); |
| 2418 | } |
| 2419 | Some(SymbolManglingVersion::V0) => {} |
h1467792822 | 6e53e66 | 2023-12-05 04:42:57 | [diff] [blame] | 2420 | Some(SymbolManglingVersion::Hashed) => { |
| 2421 | early_dcx.early_warn( |
| 2422 | "-C instrument-coverage requires symbol mangling version `v0`, \ |
| 2423 | but `-C symbol-mangling-version=hashed` was specified", |
| 2424 | ); |
| 2425 | } |
Rich Kadel | 4f550f1 | 2020-12-14 08:25:29 | [diff] [blame] | 2426 | } |
Rich Kadel | a6f8b8a | 2020-07-02 18:27:15 | [diff] [blame] | 2427 | } |
| 2428 | |
Rich Kadel | 3875abe | 2020-09-16 17:47:56 | [diff] [blame] | 2429 | if let Ok(graphviz_font) = std::env::var("RUSTC_GRAPHVIZ_FONT") { |
klensy | b6cfe71 | 2024-03-28 08:03:48 | [diff] [blame] | 2430 | // FIXME: this is only mutation of UnstableOptions here, move into |
| 2431 | // UnstableOptions::build? |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 2432 | unstable_opts.graphviz_font = graphviz_font; |
Rich Kadel | 3875abe | 2020-09-16 17:47:56 | [diff] [blame] | 2433 | } |
| 2434 | |
Alex Crichton | e1832fa | 2020-04-30 17:53:16 | [diff] [blame] | 2435 | if !cg.embed_bitcode { |
Nicholas Nethercote | ae322ff | 2020-04-19 10:48:43 | [diff] [blame] | 2436 | match cg.lto { |
| 2437 | LtoCli::No | LtoCli::Unspecified => {} |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2438 | LtoCli::Yes | LtoCli::NoParam | LtoCli::Thin | LtoCli::Fat => { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2439 | early_dcx.early_fatal("options `-C embed-bitcode=no` and `-C lto` are incompatible") |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2440 | } |
Nicholas Nethercote | ae322ff | 2020-04-19 10:48:43 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | |
Jubilee Young | 7d160ae | 2024-05-05 00:52:47 | [diff] [blame] | 2444 | if !nightly_options::is_unstable_enabled(matches) |
| 2445 | && cg.force_frame_pointers == FramePointer::NonLeaf |
| 2446 | { |
| 2447 | early_dcx.early_fatal( |
Jubilee Young | 598e265 | 2024-05-14 19:26:01 | [diff] [blame] | 2448 | "`-Cforce-frame-pointers=non-leaf` or `always` also requires `-Zunstable-options` \ |
Jubilee Young | 7d160ae | 2024-05-05 00:52:47 | [diff] [blame] | 2449 | and a nightly compiler", |
| 2450 | ) |
| 2451 | } |
| 2452 | |
Rémy Rakic | 38dca73 | 2023-06-21 21:49:41 | [diff] [blame] | 2453 | // For testing purposes, until we have more feedback about these options: ensure `-Z |
Rémy Rakic | 71285c1 | 2023-09-20 17:00:57 | [diff] [blame] | 2454 | // unstable-options` is required when using the unstable `-C link-self-contained` and `-C |
| 2455 | // linker-flavor` options. |
Rémy Rakic | 38dca73 | 2023-06-21 21:49:41 | [diff] [blame] | 2456 | if !nightly_options::is_unstable_enabled(matches) { |
| 2457 | let uses_unstable_self_contained_option = |
| 2458 | cg.link_self_contained.are_unstable_variants_set(); |
| 2459 | if uses_unstable_self_contained_option { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2460 | early_dcx.early_fatal( |
Rémy Rakic | 38dca73 | 2023-06-21 21:49:41 | [diff] [blame] | 2461 | "only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off` are stable, \ |
| 2462 | the `-Z unstable-options` flag must also be passed to use the unstable values", |
Vadim Petrochenkov | 23f177d | 2023-05-22 21:00:35 | [diff] [blame] | 2463 | ); |
Rémy Rakic | 38dca73 | 2023-06-21 21:49:41 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | if let Some(flavor) = cg.linker_flavor { |
| 2467 | if flavor.is_unstable() { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2468 | early_dcx.early_fatal(format!( |
Rémy Rakic | 38dca73 | 2023-06-21 21:49:41 | [diff] [blame] | 2469 | "the linker flavor `{}` is unstable, the `-Z unstable-options` \ |
| 2470 | flag must also be passed to use the unstable values", |
| 2471 | flavor.desc() |
| 2472 | )); |
| 2473 | } |
Vadim Petrochenkov | 23f177d | 2023-05-22 21:00:35 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | |
Rémy Rakic | 1394874 | 2023-09-21 13:27:45 | [diff] [blame] | 2477 | // Check `-C link-self-contained` for consistency: individual components cannot be both enabled |
| 2478 | // and disabled at the same time. |
| 2479 | if let Some(erroneous_components) = cg.link_self_contained.check_consistency() { |
| 2480 | let names: String = erroneous_components |
| 2481 | .into_iter() |
| 2482 | .map(|c| c.as_str().unwrap()) |
| 2483 | .intersperse(", ") |
| 2484 | .collect(); |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2485 | early_dcx.early_fatal(format!( |
Rémy Rakic | 1394874 | 2023-09-21 13:27:45 | [diff] [blame] | 2486 | "some `-C link-self-contained` components were both enabled and disabled: {names}" |
| 2487 | )); |
| 2488 | } |
| 2489 | |
klensy | b6cfe71 | 2024-03-28 08:03:48 | [diff] [blame] | 2490 | let prints = collect_print_requests(early_dcx, &mut cg, &unstable_opts, matches); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2491 | |
| 2492 | let cg = cg; |
| 2493 | |
| 2494 | let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m)); |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2495 | let target_triple = parse_target_triple(early_dcx, matches); |
| 2496 | let opt_level = parse_opt_level(early_dcx, matches, &cg); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2497 | // The `-g` and `-C debuginfo` flags specify the same setting, so we want to be able |
| 2498 | // to use them interchangeably. See the note above (regarding `-O` and `-C opt-level`) |
| 2499 | // for more details. |
| 2500 | let debug_assertions = cg.debug_assertions.unwrap_or(opt_level == OptLevel::No); |
Julia Tatz | 0504a33 | 2021-04-06 20:00:35 | [diff] [blame] | 2501 | let debuginfo = select_debuginfo(matches, &cg); |
Nicholas Nethercote | b6d0493 | 2023-11-29 05:56:15 | [diff] [blame] | 2502 | let debuginfo_compression = unstable_opts.debuginfo_compression; |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2503 | |
Zalathar | 78edefe | 2024-11-11 10:42:42 | [diff] [blame] | 2504 | let crate_name = matches.opt_str("crate-name"); |
| 2505 | let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref()); |
| 2506 | // Parse any `-l` flags, which link to native libraries. |
| 2507 | let libs = parse_native_libs(early_dcx, &unstable_opts, unstable_features, matches); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2508 | |
| 2509 | let test = matches.opt_present("test"); |
| 2510 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2511 | if !cg.remark.is_empty() && debuginfo == DebugInfo::None { |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2512 | early_dcx.early_warn("-C remark requires \"-C debuginfo=n\" to show source locations"); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2513 | } |
| 2514 | |
Jakub Beránek | 62728c7 | 2023-06-25 21:39:02 | [diff] [blame] | 2515 | if cg.remark.is_empty() && unstable_opts.remark_dir.is_some() { |
Nicholas Nethercote | f6aa418 | 2023-12-18 00:15:13 | [diff] [blame] | 2516 | early_dcx |
| 2517 | .early_warn("using -Z remark-dir without enabling remarks using e.g. -C remark=all"); |
Jakub Beránek | 62728c7 | 2023-06-25 21:39:02 | [diff] [blame] | 2518 | } |
| 2519 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2520 | let externs = parse_externs(early_dcx, matches, &unstable_opts); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2521 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2522 | let remap_path_prefix = parse_remap_path_prefix(early_dcx, matches, &unstable_opts); |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2523 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2524 | let pretty = parse_pretty(early_dcx, &unstable_opts); |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2525 | |
gftea | 2c5583e | 2023-01-15 17:42:04 | [diff] [blame] | 2526 | // query-dep-graph is required if dump-dep-graph is given #106736 |
| 2527 | if unstable_opts.dump_dep_graph && !unstable_opts.query_dep_graph { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2528 | early_dcx.early_fatal("can't dump dependency graph without `-Z query-dep-graph`"); |
gftea | 2c5583e | 2023-01-15 17:42:04 | [diff] [blame] | 2529 | } |
| 2530 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2531 | let logical_env = parse_logical_env(early_dcx, matches); |
Guillaume Gomez | 486e55e | 2023-11-27 12:47:26 | [diff] [blame] | 2532 | |
Maybe Waffle | 5441523 | 2024-02-15 00:03:34 | [diff] [blame] | 2533 | let sysroot = filesearch::materialize_sysroot(sysroot_opt); |
Lukas Wirth | 3d65c92 | 2024-03-06 12:28:12 | [diff] [blame] | 2534 | |
Joshua Nelson | 39648ea | 2021-04-27 16:25:12 | [diff] [blame] | 2535 | let real_rust_source_base_dir = { |
| 2536 | // This is the location used by the `rust-src` `rustup` component. |
| 2537 | let mut candidate = sysroot.join("lib/rustlib/src/rust"); |
| 2538 | if let Ok(metadata) = candidate.symlink_metadata() { |
onur-ozkan | 4819270 | 2024-07-06 21:07:08 | [diff] [blame] | 2539 | // Replace the symlink bootstrap creates, with its destination. |
Joshua Nelson | 39648ea | 2021-04-27 16:25:12 | [diff] [blame] | 2540 | // We could try to use `fs::canonicalize` instead, but that might |
| 2541 | // produce unnecessarily verbose path. |
| 2542 | if metadata.file_type().is_symlink() { |
| 2543 | if let Ok(symlink_dest) = std::fs::read_link(&candidate) { |
| 2544 | candidate = symlink_dest; |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | // Only use this directory if it has a file we can expect to always find. |
Maybe Waffle | 5bf6a46 | 2023-02-15 17:39:43 | [diff] [blame] | 2550 | candidate.join("library/std/src/lib.rs").is_file().then_some(candidate) |
Joshua Nelson | 39648ea | 2021-04-27 16:25:12 | [diff] [blame] | 2551 | }; |
| 2552 | |
Lukas Wirth | 9154757 | 2024-02-29 13:16:32 | [diff] [blame] | 2553 | let mut search_paths = vec![]; |
| 2554 | for s in &matches.opt_strs("L") { |
Lukas Wirth | 4815155 | 2024-04-23 08:44:14 | [diff] [blame] | 2555 | search_paths.push(SearchPath::from_cli_opt( |
| 2556 | &sysroot, |
| 2557 | &target_triple, |
| 2558 | early_dcx, |
| 2559 | s, |
| 2560 | unstable_opts.unstable_options, |
| 2561 | )); |
Lukas Wirth | 9154757 | 2024-02-29 13:16:32 | [diff] [blame] | 2562 | } |
| 2563 | |
Aaron Hill | a895069 | 2021-08-12 20:30:40 | [diff] [blame] | 2564 | let working_dir = std::env::current_dir().unwrap_or_else(|e| { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2565 | early_dcx.early_fatal(format!("Current directory is invalid: {e}")); |
Aaron Hill | a895069 | 2021-08-12 20:30:40 | [diff] [blame] | 2566 | }); |
| 2567 | |
Urgau | 4f4fa42 | 2024-03-21 20:13:06 | [diff] [blame] | 2568 | let file_mapping = file_path_mapping(remap_path_prefix.clone(), &unstable_opts); |
| 2569 | let working_dir = file_mapping.to_real_filename(&working_dir); |
Aaron Hill | a895069 | 2021-08-12 20:30:40 | [diff] [blame] | 2570 | |
jyn | cb6d033 | 2023-12-19 18:24:05 | [diff] [blame] | 2571 | let verbose = matches.opt_present("verbose") || unstable_opts.verbose_internals; |
| 2572 | |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2573 | Options { |
pierwill | 1642fdf | 2021-10-31 22:05:48 | [diff] [blame] | 2574 | assert_incr_state, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2575 | crate_types, |
| 2576 | optimize: opt_level, |
| 2577 | debuginfo, |
Augie Fackler | af9e550 | 2023-07-12 21:07:34 | [diff] [blame] | 2578 | debuginfo_compression, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2579 | lint_opts, |
| 2580 | lint_cap, |
| 2581 | describe_lints, |
| 2582 | output_types, |
| 2583 | search_paths, |
Maybe Waffle | 5441523 | 2024-02-15 00:03:34 | [diff] [blame] | 2584 | maybe_sysroot: Some(sysroot), |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2585 | target_triple, |
| 2586 | test, |
| 2587 | incremental, |
Alex Macleod | 59f6f04 | 2023-10-13 17:28:34 | [diff] [blame] | 2588 | untracked_state_hash: Default::default(), |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 2589 | unstable_opts, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2590 | prints, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2591 | cg, |
| 2592 | error_format, |
David Wood | 44c1fcc | 2022-07-06 10:57:41 | [diff] [blame] | 2593 | diagnostic_width, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2594 | externs, |
Zalathar | 78edefe | 2024-11-11 10:42:42 | [diff] [blame] | 2595 | unstable_features, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2596 | crate_name, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2597 | libs, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2598 | debug_assertions, |
| 2599 | actually_rustdoc: false, |
Vadim Petrochenkov | da4ce6b | 2023-02-06 17:57:45 | [diff] [blame] | 2600 | resolve_doc_links: ResolveDocLinks::ExportedMetadata, |
Nicholas Nethercote | 32de78c | 2024-01-10 01:47:22 | [diff] [blame] | 2601 | trimmed_def_paths: false, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2602 | cli_forced_codegen_units: codegen_units, |
Wesley Wiser | 7c6345d | 2022-10-27 00:28:25 | [diff] [blame] | 2603 | cli_forced_local_thinlto_off: disable_local_thinlto, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2604 | remap_path_prefix, |
Joshua Nelson | 39648ea | 2021-04-27 16:25:12 | [diff] [blame] | 2605 | real_rust_source_base_dir, |
Mazdak Farrokhzad | 9d766ed | 2019-10-11 21:30:58 | [diff] [blame] | 2606 | edition, |
| 2607 | json_artifact_notifications, |
est31 | 3f2ca47 | 2020-06-30 18:17:07 | [diff] [blame] | 2608 | json_unused_externs, |
Aaron Hill | 63523e4 | 2021-12-04 19:34:20 | [diff] [blame] | 2609 | json_future_incompat, |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2610 | pretty, |
Aaron Hill | a895069 | 2021-08-12 20:30:40 | [diff] [blame] | 2611 | working_dir, |
Trevor Gross | 6a1c10b | 2022-12-19 18:09:40 | [diff] [blame] | 2612 | color, |
Guillaume Gomez | 486e55e | 2023-11-27 12:47:26 | [diff] [blame] | 2613 | logical_env, |
jyn | cb6d033 | 2023-12-19 18:24:05 | [diff] [blame] | 2614 | verbose, |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2615 | } |
| 2616 | } |
| 2617 | |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2618 | fn parse_pretty(early_dcx: &EarlyDiagCtxt, unstable_opts: &UnstableOptions) -> Option<PpMode> { |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2619 | use PpMode::*; |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2620 | |
Joshua Nelson | 3c9765c | 2022-07-06 12:44:47 | [diff] [blame] | 2621 | let first = match unstable_opts.unpretty.as_deref()? { |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2622 | "normal" => Source(PpSourceMode::Normal), |
| 2623 | "identified" => Source(PpSourceMode::Identified), |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2624 | "expanded" => Source(PpSourceMode::Expanded), |
| 2625 | "expanded,identified" => Source(PpSourceMode::ExpandedIdentified), |
| 2626 | "expanded,hygiene" => Source(PpSourceMode::ExpandedHygiene), |
Nicholas Nethercote | 1467ba0 | 2023-10-10 01:17:06 | [diff] [blame] | 2627 | "ast-tree" => AstTree, |
| 2628 | "ast-tree,expanded" => AstTreeExpanded, |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2629 | "hir" => Hir(PpHirMode::Normal), |
| 2630 | "hir,identified" => Hir(PpHirMode::Identified), |
| 2631 | "hir,typed" => Hir(PpHirMode::Typed), |
| 2632 | "hir-tree" => HirTree, |
| 2633 | "thir-tree" => ThirTree, |
b-naber | 9438126 | 2023-01-26 22:35:24 | [diff] [blame] | 2634 | "thir-flat" => ThirFlat, |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2635 | "mir" => Mir, |
Oğuz Ağcayazı | 3883645 | 2023-11-14 13:21:55 | [diff] [blame] | 2636 | "stable-mir" => StableMir, |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2637 | "mir-cfg" => MirCFG, |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2638 | name => early_dcx.early_fatal(format!( |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2639 | "argument to `unpretty` must be one of `normal`, `identified`, \ |
Alex Macleod | 1d64b85 | 2022-03-06 12:43:30 | [diff] [blame] | 2640 | `expanded`, `expanded,identified`, `expanded,hygiene`, \ |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2641 | `ast-tree`, `ast-tree,expanded`, `hir`, `hir,identified`, \ |
Oğuz Ağcayazı | 3883645 | 2023-11-14 13:21:55 | [diff] [blame] | 2642 | `hir,typed`, `hir-tree`, `thir-tree`, `thir-flat`, `mir`, `stable-mir`, or \ |
b-naber | 9438126 | 2023-01-26 22:35:24 | [diff] [blame] | 2643 | `mir-cfg`; got {name}" |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2644 | )), |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2645 | }; |
Oli Scherer | ee3c835 | 2022-08-31 13:09:26 | [diff] [blame] | 2646 | debug!("got unpretty option: {first:?}"); |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2647 | Some(first) |
Nick Cameron | cacd6b6 | 2015-01-30 08:44:27 | [diff] [blame] | 2648 | } |
| 2649 | |
Aaron Hill | 1498608 | 2019-07-20 20:34:41 | [diff] [blame] | 2650 | pub fn make_crate_type_option() -> RustcOptGroup { |
Zalathar | 001013c | 2024-11-07 11:47:15 | [diff] [blame] | 2651 | make_opt( |
| 2652 | OptionStability::Stable, |
| 2653 | OptionKind::Multi, |
Aaron Hill | 1498608 | 2019-07-20 20:34:41 | [diff] [blame] | 2654 | "", |
| 2655 | "crate-type", |
| 2656 | "Comma separated list of types of crates |
| 2657 | for the compiler to emit", |
| 2658 | "[bin|lib|rlib|dylib|cdylib|staticlib|proc-macro]", |
| 2659 | ) |
| 2660 | } |
| 2661 | |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2662 | pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateType>, String> { |
Brian Anderson | 1c3655b | 2014-07-20 04:11:26 | [diff] [blame] | 2663 | let mut crate_types: Vec<CrateType> = Vec::new(); |
Jorge Aparicio | d5d7e65 | 2015-01-31 17:20:46 | [diff] [blame] | 2664 | for unparsed_crate_type in &list_list { |
Jorge Aparicio | 00f3c3f | 2014-11-27 18:53:34 | [diff] [blame] | 2665 | for part in unparsed_crate_type.split(',') { |
Brian Anderson | 1c3655b | 2014-07-20 04:11:26 | [diff] [blame] | 2666 | let new_part = match part { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2667 | "lib" => default_lib_output(), |
Mark Rousskov | 2a93442 | 2018-07-26 17:13:11 | [diff] [blame] | 2668 | "rlib" => CrateType::Rlib, |
| 2669 | "staticlib" => CrateType::Staticlib, |
| 2670 | "dylib" => CrateType::Dylib, |
| 2671 | "cdylib" => CrateType::Cdylib, |
| 2672 | "bin" => CrateType::Executable, |
| 2673 | "proc-macro" => CrateType::ProcMacro, |
Jubilee Young | de66e08 | 2022-03-25 05:11:05 | [diff] [blame] | 2674 | _ => return Err(format!("unknown crate type: `{part}`")), |
Brian Anderson | 1c3655b | 2014-07-20 04:11:26 | [diff] [blame] | 2675 | }; |
Simonas Kazlauskas | a6e8496 | 2015-02-09 17:30:22 | [diff] [blame] | 2676 | if !crate_types.contains(&new_part) { |
| 2677 | crate_types.push(new_part) |
| 2678 | } |
Brian Anderson | 1c3655b | 2014-07-20 04:11:26 | [diff] [blame] | 2679 | } |
| 2680 | } |
| 2681 | |
Michael Kohl | 9873acc | 2017-05-28 06:49:14 | [diff] [blame] | 2682 | Ok(crate_types) |
Brian Anderson | 1c3655b | 2014-07-20 04:11:26 | [diff] [blame] | 2683 | } |
| 2684 | |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2685 | pub mod nightly_options { |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 2686 | use rustc_feature::UnstableFeatures; |
| 2687 | |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2688 | use super::{OptionStability, RustcOptGroup}; |
Nicholas Nethercote | cce1701 | 2023-12-17 11:01:06 | [diff] [blame] | 2689 | use crate::EarlyDiagCtxt; |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2690 | |
| 2691 | pub fn is_unstable_enabled(matches: &getopts::Matches) -> bool { |
Joshua Nelson | 622c48e | 2020-10-10 18:27:52 | [diff] [blame] | 2692 | match_is_nightly_build(matches) |
| 2693 | && matches.opt_strs("Z").iter().any(|x| *x == "unstable-options") |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2694 | } |
| 2695 | |
Joshua Nelson | 622c48e | 2020-10-10 18:27:52 | [diff] [blame] | 2696 | pub fn match_is_nightly_build(matches: &getopts::Matches) -> bool { |
| 2697 | is_nightly_build(matches.opt_str("crate-name").as_deref()) |
| 2698 | } |
| 2699 | |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 2700 | fn is_nightly_build(krate: Option<&str>) -> bool { |
Joshua Nelson | 622c48e | 2020-10-10 18:27:52 | [diff] [blame] | 2701 | UnstableFeatures::from_environment(krate).is_nightly_build() |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2702 | } |
| 2703 | |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2704 | pub fn check_nightly_options( |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2705 | early_dcx: &EarlyDiagCtxt, |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2706 | matches: &getopts::Matches, |
| 2707 | flags: &[RustcOptGroup], |
| 2708 | ) { |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 2709 | let has_z_unstable_option = matches.opt_strs("Z").iter().any(|x| *x == "unstable-options"); |
Joshua Nelson | 622c48e | 2020-10-10 18:27:52 | [diff] [blame] | 2710 | let really_allows_unstable_options = match_is_nightly_build(matches); |
yukang | 12888d2 | 2023-09-09 16:01:32 | [diff] [blame] | 2711 | let mut nightly_options_on_stable = 0; |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2712 | |
| 2713 | for opt in flags.iter() { |
| 2714 | if opt.stability == OptionStability::Stable { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2715 | continue; |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2716 | } |
Alex Crichton | 5c3d0e6 | 2017-06-08 21:20:55 | [diff] [blame] | 2717 | if !matches.opt_present(opt.name) { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2718 | continue; |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2719 | } |
Alex Crichton | 5c3d0e6 | 2017-06-08 21:20:55 | [diff] [blame] | 2720 | if opt.name != "Z" && !has_z_unstable_option { |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2721 | early_dcx.early_fatal(format!( |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2722 | "the `-Z unstable-options` flag must also be passed to enable \ |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2723 | the flag `{}`", |
许杰友 Jieyou Xu (Joe) | cef812b | 2023-06-22 21:56:09 | [diff] [blame] | 2724 | opt.name |
| 2725 | )); |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2726 | } |
| 2727 | if really_allows_unstable_options { |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2728 | continue; |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2729 | } |
| 2730 | match opt.stability { |
| 2731 | OptionStability::Unstable => { |
yukang | 12888d2 | 2023-09-09 16:01:32 | [diff] [blame] | 2732 | nightly_options_on_stable += 1; |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2733 | let msg = format!( |
Nicholas Nethercote | 01e33a3 | 2023-05-16 06:04:03 | [diff] [blame] | 2734 | "the option `{}` is only accepted on the nightly compiler", |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 2735 | opt.name |
| 2736 | ); |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2737 | let _ = early_dcx.early_err(msg); |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2738 | } |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2739 | OptionStability::Stable => {} |
| 2740 | } |
| 2741 | } |
yukang | 12888d2 | 2023-09-09 16:01:32 | [diff] [blame] | 2742 | if nightly_options_on_stable > 0 { |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2743 | early_dcx |
yukang | 12888d2 | 2023-09-09 16:01:32 | [diff] [blame] | 2744 | .early_help("consider switching to a nightly toolchain: `rustup default nightly`"); |
Nicholas Nethercote | d58e372 | 2023-12-17 23:57:26 | [diff] [blame] | 2745 | early_dcx.early_note("selecting a toolchain with `+toolchain` arguments require a rustup proxy; see <https://rust-lang.github.io/rustup/concepts/index.html>"); |
| 2746 | early_dcx.early_note("for more information about Rust's stability policy, see <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html#unstable-features>"); |
Nicholas Nethercote | 3a1b8e6 | 2023-12-20 03:53:50 | [diff] [blame] | 2747 | early_dcx.early_fatal(format!( |
yukang | 12888d2 | 2023-09-09 16:01:32 | [diff] [blame] | 2748 | "{} nightly option{} were parsed", |
| 2749 | nightly_options_on_stable, |
| 2750 | if nightly_options_on_stable > 1 { "s" } else { "" } |
| 2751 | )); |
| 2752 | } |
Guillaume Gomez | ded701b | 2016-03-15 08:09:29 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | |
Alex Crichton | 3cb9fa2 | 2015-01-20 23:45:07 | [diff] [blame] | 2756 | impl fmt::Display for CrateType { |
Zack M. Davis | 5b22d9b | 2018-08-30 05:02:42 | [diff] [blame] | 2757 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
Valerii Hiora | 70a79a9 | 2014-06-11 07:48:17 | [diff] [blame] | 2758 | match *self { |
Mark Rousskov | 2a93442 | 2018-07-26 17:13:11 | [diff] [blame] | 2759 | CrateType::Executable => "bin".fmt(f), |
| 2760 | CrateType::Dylib => "dylib".fmt(f), |
| 2761 | CrateType::Rlib => "rlib".fmt(f), |
| 2762 | CrateType::Staticlib => "staticlib".fmt(f), |
| 2763 | CrateType::Cdylib => "cdylib".fmt(f), |
| 2764 | CrateType::ProcMacro => "proc-macro".fmt(f), |
Valerii Hiora | 70a79a9 | 2014-06-11 07:48:17 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | } |
Nick Cameron | 37ca367 | 2014-05-06 11:38:01 | [diff] [blame] | 2768 | |
Nicholas Nethercote | a09b1d3 | 2024-03-05 05:53:24 | [diff] [blame] | 2769 | impl IntoDiagArg for CrateType { |
| 2770 | fn into_diag_arg(self) -> DiagArgValue { |
| 2771 | self.to_string().into_diag_arg() |
David Wood | d450048 | 2022-08-30 16:01:54 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2775 | #[derive(Copy, Clone, PartialEq, Debug)] |
| 2776 | pub enum PpSourceMode { |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2777 | /// `-Zunpretty=normal` |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2778 | Normal, |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2779 | /// `-Zunpretty=expanded` |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2780 | Expanded, |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2781 | /// `-Zunpretty=identified` |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2782 | Identified, |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2783 | /// `-Zunpretty=expanded,identified` |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2784 | ExpandedIdentified, |
Joshua Nelson | 23bbd65 | 2021-03-25 19:48:21 | [diff] [blame] | 2785 | /// `-Zunpretty=expanded,hygiene` |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2786 | ExpandedHygiene, |
| 2787 | } |
| 2788 | |
| 2789 | #[derive(Copy, Clone, PartialEq, Debug)] |
| 2790 | pub enum PpHirMode { |
| 2791 | /// `-Zunpretty=hir` |
| 2792 | Normal, |
| 2793 | /// `-Zunpretty=hir,identified` |
| 2794 | Identified, |
| 2795 | /// `-Zunpretty=hir,typed` |
| 2796 | Typed, |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | #[derive(Copy, Clone, PartialEq, Debug)] |
Orion Gonzalez | a007d34 | 2024-08-27 18:04:47 | [diff] [blame] | 2800 | /// Pretty print mode |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2801 | pub enum PpMode { |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2802 | /// Options that print the source code, i.e. |
bjorn3 | 2f84484 | 2021-06-25 09:56:14 | [diff] [blame] | 2803 | /// `-Zunpretty=normal` and `-Zunpretty=expanded` |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2804 | Source(PpSourceMode), |
Nicholas Nethercote | 1467ba0 | 2023-10-10 01:17:06 | [diff] [blame] | 2805 | /// `-Zunpretty=ast-tree` |
| 2806 | AstTree, |
| 2807 | /// `-Zunpretty=ast-tree,expanded` |
| 2808 | AstTreeExpanded, |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2809 | /// Options that print the HIR, i.e. `-Zunpretty=hir` |
| 2810 | Hir(PpHirMode), |
| 2811 | /// `-Zunpretty=hir-tree` |
| 2812 | HirTree, |
LeSeulArtichaut | 6bf4147 | 2021-03-07 14:09:39 | [diff] [blame] | 2813 | /// `-Zunpretty=thir-tree` |
| 2814 | ThirTree, |
est31 | ff2c609 | 2023-03-03 03:10:46 | [diff] [blame] | 2815 | /// `-Zunpretty=thir-flat` |
b-naber | 9438126 | 2023-01-26 22:35:24 | [diff] [blame] | 2816 | ThirFlat, |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2817 | /// `-Zunpretty=mir` |
| 2818 | Mir, |
| 2819 | /// `-Zunpretty=mir-cfg` |
| 2820 | MirCFG, |
Oğuz Ağcayazı | 3883645 | 2023-11-14 13:21:55 | [diff] [blame] | 2821 | /// `-Zunpretty=stable-mir` |
| 2822 | StableMir, |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | impl PpMode { |
Mark Rousskov | 7ec20dd3 | 2019-11-20 13:27:42 | [diff] [blame] | 2826 | pub fn needs_ast_map(&self) -> bool { |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2827 | use PpMode::*; |
| 2828 | use PpSourceMode::*; |
| 2829 | match *self { |
Nicholas Nethercote | 1467ba0 | 2023-10-10 01:17:06 | [diff] [blame] | 2830 | Source(Normal | Identified) | AstTree => false, |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2831 | |
bjorn3 | 2f84484 | 2021-06-25 09:56:14 | [diff] [blame] | 2832 | Source(Expanded | ExpandedIdentified | ExpandedHygiene) |
Nicholas Nethercote | 1467ba0 | 2023-10-10 01:17:06 | [diff] [blame] | 2833 | | AstTreeExpanded |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2834 | | Hir(_) |
| 2835 | | HirTree |
LeSeulArtichaut | 6bf4147 | 2021-03-07 14:09:39 | [diff] [blame] | 2836 | | ThirTree |
b-naber | 9438126 | 2023-01-26 22:35:24 | [diff] [blame] | 2837 | | ThirFlat |
LeSeulArtichaut | 3ed189e | 2021-02-18 19:21:18 | [diff] [blame] | 2838 | | Mir |
Oğuz Ağcayazı | ae179a0 | 2023-11-08 09:37:26 | [diff] [blame] | 2839 | | MirCFG |
Oğuz Ağcayazı | 3883645 | 2023-11-14 13:21:55 | [diff] [blame] | 2840 | | StableMir => true, |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2841 | } |
| 2842 | } |
Michael Goulet | 26ecd44 | 2022-07-17 03:35:54 | [diff] [blame] | 2843 | pub fn needs_hir(&self) -> bool { |
| 2844 | use PpMode::*; |
| 2845 | match *self { |
Nicholas Nethercote | 1467ba0 | 2023-10-10 01:17:06 | [diff] [blame] | 2846 | Source(_) | AstTree | AstTreeExpanded => false, |
Michael Goulet | 26ecd44 | 2022-07-17 03:35:54 | [diff] [blame] | 2847 | |
Oğuz Ağcayazı | 3883645 | 2023-11-14 13:21:55 | [diff] [blame] | 2848 | Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG | StableMir => true, |
Michael Goulet | 26ecd44 | 2022-07-17 03:35:54 | [diff] [blame] | 2849 | } |
| 2850 | } |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2851 | |
| 2852 | pub fn needs_analysis(&self) -> bool { |
| 2853 | use PpMode::*; |
Oğuz Ağcayazı | 3883645 | 2023-11-14 13:21:55 | [diff] [blame] | 2854 | matches!(*self, Hir(PpHirMode::Typed) | Mir | StableMir | MirCFG | ThirTree | ThirFlat) |
Mark Rousskov | dd6df0d | 2019-11-04 02:42:03 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | |
Nicholas Nethercote | 4b90b26 | 2023-11-29 22:45:03 | [diff] [blame] | 2858 | #[derive(Clone, Hash, PartialEq, Eq, Debug)] |
| 2859 | pub enum WasiExecModel { |
| 2860 | Command, |
| 2861 | Reactor, |
| 2862 | } |
| 2863 | |
Andy Russell | 4e35cbb2 | 2018-11-12 18:05:20 | [diff] [blame] | 2864 | /// Command-line arguments passed to the compiler have to be incorporated with |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2865 | /// the dependency tracking system for incremental compilation. This module |
| 2866 | /// provides some utilities to make this more convenient. |
| 2867 | /// |
Andy Russell | 4e35cbb2 | 2018-11-12 18:05:20 | [diff] [blame] | 2868 | /// The values of all command-line arguments that are relevant for dependency |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2869 | /// tracking are hashed into a single value that determines whether the |
| 2870 | /// incremental compilation cache can be re-used or not. This hashing is done |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 2871 | /// via the `DepTrackingHash` trait defined below, since the standard `Hash` |
| 2872 | /// implementation might not be suitable (e.g., arguments are stored in a `Vec`, |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2873 | /// the hash of which is order dependent, but we might not want the order of |
| 2874 | /// arguments to make a difference for the hash). |
| 2875 | /// |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 2876 | /// However, since the value provided by `Hash::hash` often *is* suitable, |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2877 | /// especially for primitive types, there is the |
Alexander Regueiro | c1d29ee | 2019-09-06 02:57:44 | [diff] [blame] | 2878 | /// `impl_dep_tracking_hash_via_hash!()` macro that allows to simply reuse the |
| 2879 | /// `Hash` implementation for `DepTrackingHash`. It's important though that |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2880 | /// we have an opt-in scheme here, so one is hopefully forced to think about |
Andy Russell | 4e35cbb2 | 2018-11-12 18:05:20 | [diff] [blame] | 2881 | /// how the hash should be calculated when adding a new command-line argument. |
Jacob Pratt | 49c82f3 | 2022-05-20 23:51:09 | [diff] [blame] | 2882 | pub(crate) mod dep_tracking { |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 2883 | use std::collections::BTreeMap; |
| 2884 | use std::hash::{DefaultHasher, Hash}; |
| 2885 | use std::num::NonZero; |
| 2886 | use std::path::PathBuf; |
| 2887 | |
Folkert de Vries | 47573bf | 2024-12-18 21:03:07 | [diff] [blame] | 2888 | use rustc_abi::Align; |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 2889 | use rustc_data_structures::fx::FxIndexMap; |
| 2890 | use rustc_data_structures::stable_hasher::Hash64; |
| 2891 | use rustc_errors::LanguageIdentifier; |
| 2892 | use rustc_feature::UnstableFeatures; |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 2893 | use rustc_span::RealFileName; |
Michael Goulet | c682aa1 | 2024-09-22 23:05:04 | [diff] [blame] | 2894 | use rustc_span::edition::Edition; |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 2895 | use rustc_target::spec::{ |
| 2896 | CodeModel, FramePointer, MergeFunctions, OnBrokenPipe, PanicStrategy, RelocModel, |
Noratrieb | a26450c | 2024-10-17 17:02:32 | [diff] [blame] | 2897 | RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, SymbolVisibility, TargetTuple, |
David Lattimore | f48194e | 2024-09-30 22:55:10 | [diff] [blame] | 2898 | TlsModel, WasmCAbi, |
Nicholas Nethercote | 84ac80f | 2024-07-28 22:13:50 | [diff] [blame] | 2899 | }; |
| 2900 | |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 2901 | use super::{ |
Zalathar | 3407fcc | 2024-03-08 07:07:04 | [diff] [blame] | 2902 | BranchProtection, CFGuard, CFProtection, CollapseMacroDebuginfo, CoverageOptions, |
Kornel | 88b9edc | 2024-04-14 12:52:58 | [diff] [blame] | 2903 | CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn, |
Zalathar | 3407fcc | 2024-03-08 07:07:04 | [diff] [blame] | 2904 | InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail, |
Scott McMurray | a7fc76a | 2024-12-05 08:47:36 | [diff] [blame] | 2905 | LtoCli, MirStripDebugInfo, NextSolverConfig, OomStrategy, OptLevel, OutFileName, |
| 2906 | OutputType, OutputTypes, PatchableFunctionEntry, Polonius, RemapPathScopeComponents, |
| 2907 | ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath, |
| 2908 | SymbolManglingVersion, WasiExecModel, |
Mark Rousskov | a06baa5 | 2019-12-22 22:42:04 | [diff] [blame] | 2909 | }; |
Mark Rousskov | cc2c33a | 2019-11-29 21:05:28 | [diff] [blame] | 2910 | use crate::lint; |
Alex Macleod | 59f6f04 | 2023-10-13 17:28:34 | [diff] [blame] | 2911 | use crate::utils::NativeLib; |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2912 | |
Nicholas Nethercote | de38888 | 2024-03-19 02:31:28 | [diff] [blame] | 2913 | pub(crate) trait DepTrackingHash { |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 2914 | fn hash( |
| 2915 | &self, |
| 2916 | hasher: &mut DefaultHasher, |
| 2917 | error_format: ErrorOutputType, |
| 2918 | for_crate_hash: bool, |
| 2919 | ); |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | macro_rules! impl_dep_tracking_hash_via_hash { |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2923 | ($($t:ty),+ $(,)?) => {$( |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2924 | impl DepTrackingHash for $t { |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 2925 | fn hash(&self, hasher: &mut DefaultHasher, _: ErrorOutputType, _for_crate_hash: bool) { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2926 | Hash::hash(self, hasher); |
| 2927 | } |
| 2928 | } |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2929 | )+}; |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 2930 | } |
| 2931 | |
Joshua Nelson | 76502de | 2021-04-15 23:36:25 | [diff] [blame] | 2932 | impl<T: DepTrackingHash> DepTrackingHash for Option<T> { |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 2933 | fn hash( |
| 2934 | &self, |
| 2935 | hasher: &mut DefaultHasher, |
| 2936 | error_format: ErrorOutputType, |
| 2937 | for_crate_hash: bool, |
| 2938 | ) { |
Joshua Nelson | 76502de | 2021-04-15 23:36:25 | [diff] [blame] | 2939 | match self { |
| 2940 | Some(x) => { |
| 2941 | Hash::hash(&1, hasher); |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 2942 | DepTrackingHash::hash(x, hasher, error_format, for_crate_hash); |
Joshua Nelson | 76502de | 2021-04-15 23:36:25 | [diff] [blame] | 2943 | } |
| 2944 | None => Hash::hash(&0, hasher), |
| 2945 | } |
| 2946 | } |
| 2947 | } |
| 2948 | |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2949 | impl_dep_tracking_hash_via_hash!( |
| 2950 | bool, |
| 2951 | usize, |
Markus Reiter | 746a58d | 2024-01-29 22:59:09 | [diff] [blame] | 2952 | NonZero<usize>, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2953 | u64, |
Alex Macleod | 59f6f04 | 2023-10-13 17:28:34 | [diff] [blame] | 2954 | Hash64, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2955 | String, |
| 2956 | PathBuf, |
| 2957 | lint::Level, |
Joshua Nelson | 76502de | 2021-04-15 23:36:25 | [diff] [blame] | 2958 | WasiExecModel, |
| 2959 | u32, |
Jubilee Young | b3a1975 | 2024-05-04 23:19:42 | [diff] [blame] | 2960 | FramePointer, |
Joshua Nelson | 76502de | 2021-04-15 23:36:25 | [diff] [blame] | 2961 | RelocModel, |
| 2962 | CodeModel, |
| 2963 | TlsModel, |
| 2964 | InstrumentCoverage, |
Zalathar | 3407fcc | 2024-03-08 07:07:04 | [diff] [blame] | 2965 | CoverageOptions, |
Oleksii Lozovskyi | 0e60df9 | 2022-09-24 11:02:44 | [diff] [blame] | 2966 | InstrumentXRay, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2967 | CrateType, |
| 2968 | MergeFunctions, |
Martin Nordholts | cde0cde | 2024-04-28 16:02:21 | [diff] [blame] | 2969 | OnBrokenPipe, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2970 | PanicStrategy, |
| 2971 | RelroLevel, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2972 | OptLevel, |
| 2973 | LtoCli, |
| 2974 | DebugInfo, |
Augie Fackler | af9e550 | 2023-07-12 21:07:34 | [diff] [blame] | 2975 | DebugInfoCompression, |
Scott McMurray | a7fc76a | 2024-12-05 08:47:36 | [diff] [blame] | 2976 | MirStripDebugInfo, |
Andrew Zhogin | 8507f51 | 2024-01-10 18:36:05 | [diff] [blame] | 2977 | CollapseMacroDebuginfo, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2978 | UnstableFeatures, |
Luqman Aden | db555e1 | 2021-03-25 04:45:09 | [diff] [blame] | 2979 | NativeLib, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2980 | SanitizerSet, |
| 2981 | CFGuard, |
Andrew Brown | 8d6c973 | 2022-01-28 17:48:59 | [diff] [blame] | 2982 | CFProtection, |
Noratrieb | a26450c | 2024-10-17 17:02:32 | [diff] [blame] | 2983 | TargetTuple, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2984 | Edition, |
| 2985 | LinkerPluginLto, |
Vadim Petrochenkov | da4ce6b | 2023-02-06 17:57:45 | [diff] [blame] | 2986 | ResolveDocLinks, |
Joshua Nelson | 76502de | 2021-04-15 23:36:25 | [diff] [blame] | 2987 | SplitDebuginfo, |
Michael Woerister | 822957f | 2022-07-04 12:04:35 | [diff] [blame] | 2988 | SplitDwarfKind, |
Benjamin A. Bjørnseth | bb9dee9 | 2021-04-06 19:37:49 | [diff] [blame] | 2989 | StackProtector, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 2990 | SwitchWithOptPath, |
Joshua Nelson | 76502de | 2021-04-15 23:36:25 | [diff] [blame] | 2991 | SymbolManglingVersion, |
David Lattimore | f48194e | 2024-09-30 22:55:10 | [diff] [blame] | 2992 | SymbolVisibility, |
Urgau | 30f9471 | 2023-08-23 09:18:20 | [diff] [blame] | 2993 | RemapPathScopeComponents, |
Joshua Nelson | 76502de | 2021-04-15 23:36:25 | [diff] [blame] | 2994 | SourceFileHashAlgorithm, |
Jing Peng | 9b1a1e1 | 2023-02-26 20:27:27 | [diff] [blame] | 2995 | OutFileName, |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 2996 | OutputType, |
Aaron Hill | a895069 | 2021-08-12 20:30:40 | [diff] [blame] | 2997 | RealFileName, |
Hudson Ayers | a9a1393 | 2021-10-14 00:01:31 | [diff] [blame] | 2998 | LocationDetail, |
Kornel | 88b9edc | 2024-04-14 12:52:58 | [diff] [blame] | 2999 | FmtDebug, |
James McGregor | 837cc16 | 2021-07-13 11:14:26 | [diff] [blame] | 3000 | BranchProtection, |
Amanieu d'Antras | aa36237 | 2021-10-06 14:52:54 | [diff] [blame] | 3001 | OomStrategy, |
David Wood | d5119c5 | 2022-03-28 08:36:20 | [diff] [blame] | 3002 | LanguageIdentifier, |
lcnr | 5d97ada | 2023-12-14 12:00:23 | [diff] [blame] | 3003 | NextSolverConfig, |
Matthew Maurer | ac7595f | 2023-12-12 21:32:43 | [diff] [blame] | 3004 | PatchableFunctionEntry, |
Rémy Rakic | 4f7a27b | 2023-06-30 11:55:38 | [diff] [blame] | 3005 | Polonius, |
Ben Kimock | fcdd99e | 2023-11-07 00:55:05 | [diff] [blame] | 3006 | InliningThreshold, |
Miguel Ojeda | 2d47622 | 2023-10-18 14:58:17 | [diff] [blame] | 3007 | FunctionReturn, |
daxpedda | f09c19a | 2024-02-27 22:06:44 | [diff] [blame] | 3008 | WasmCAbi, |
Folkert de Vries | 47573bf | 2024-12-18 21:03:07 | [diff] [blame] | 3009 | Align, |
Joshua Nelson | dd43d13 | 2021-04-22 15:45:08 | [diff] [blame] | 3010 | ); |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3011 | |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3012 | impl<T1, T2> DepTrackingHash for (T1, T2) |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 3013 | where |
| 3014 | T1: DepTrackingHash, |
| 3015 | T2: DepTrackingHash, |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3016 | { |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3017 | fn hash( |
| 3018 | &self, |
| 3019 | hasher: &mut DefaultHasher, |
| 3020 | error_format: ErrorOutputType, |
| 3021 | for_crate_hash: bool, |
| 3022 | ) { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3023 | Hash::hash(&0, hasher); |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3024 | DepTrackingHash::hash(&self.0, hasher, error_format, for_crate_hash); |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3025 | Hash::hash(&1, hasher); |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3026 | DepTrackingHash::hash(&self.1, hasher, error_format, for_crate_hash); |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3027 | } |
| 3028 | } |
| 3029 | |
Vadim Chugunov | 13477c7 | 2016-11-24 00:09:51 | [diff] [blame] | 3030 | impl<T1, T2, T3> DepTrackingHash for (T1, T2, T3) |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 3031 | where |
| 3032 | T1: DepTrackingHash, |
| 3033 | T2: DepTrackingHash, |
| 3034 | T3: DepTrackingHash, |
Vadim Chugunov | 13477c7 | 2016-11-24 00:09:51 | [diff] [blame] | 3035 | { |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3036 | fn hash( |
| 3037 | &self, |
| 3038 | hasher: &mut DefaultHasher, |
| 3039 | error_format: ErrorOutputType, |
| 3040 | for_crate_hash: bool, |
| 3041 | ) { |
Vadim Chugunov | 13477c7 | 2016-11-24 00:09:51 | [diff] [blame] | 3042 | Hash::hash(&0, hasher); |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3043 | DepTrackingHash::hash(&self.0, hasher, error_format, for_crate_hash); |
Vadim Chugunov | 13477c7 | 2016-11-24 00:09:51 | [diff] [blame] | 3044 | Hash::hash(&1, hasher); |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3045 | DepTrackingHash::hash(&self.1, hasher, error_format, for_crate_hash); |
Vadim Chugunov | 13477c7 | 2016-11-24 00:09:51 | [diff] [blame] | 3046 | Hash::hash(&2, hasher); |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3047 | DepTrackingHash::hash(&self.2, hasher, error_format, for_crate_hash); |
Vadim Chugunov | 13477c7 | 2016-11-24 00:09:51 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | |
Aaron Hill | 605513a | 2021-05-26 00:43:02 | [diff] [blame] | 3051 | impl<T: DepTrackingHash> DepTrackingHash for Vec<T> { |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3052 | fn hash( |
| 3053 | &self, |
| 3054 | hasher: &mut DefaultHasher, |
| 3055 | error_format: ErrorOutputType, |
| 3056 | for_crate_hash: bool, |
| 3057 | ) { |
Aaron Hill | 605513a | 2021-05-26 00:43:02 | [diff] [blame] | 3058 | Hash::hash(&self.len(), hasher); |
| 3059 | for (index, elem) in self.iter().enumerate() { |
| 3060 | Hash::hash(&index, hasher); |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3061 | DepTrackingHash::hash(elem, hasher, error_format, for_crate_hash); |
| 3062 | } |
| 3063 | } |
| 3064 | } |
| 3065 | |
Guillaume Gomez | 486e55e | 2023-11-27 12:47:26 | [diff] [blame] | 3066 | impl<T: DepTrackingHash, V: DepTrackingHash> DepTrackingHash for FxIndexMap<T, V> { |
| 3067 | fn hash( |
| 3068 | &self, |
| 3069 | hasher: &mut DefaultHasher, |
| 3070 | error_format: ErrorOutputType, |
| 3071 | for_crate_hash: bool, |
| 3072 | ) { |
| 3073 | Hash::hash(&self.len(), hasher); |
| 3074 | for (key, value) in self.iter() { |
| 3075 | DepTrackingHash::hash(key, hasher, error_format, for_crate_hash); |
| 3076 | DepTrackingHash::hash(value, hasher, error_format, for_crate_hash); |
| 3077 | } |
| 3078 | } |
| 3079 | } |
| 3080 | |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3081 | impl DepTrackingHash for OutputTypes { |
| 3082 | fn hash( |
| 3083 | &self, |
| 3084 | hasher: &mut DefaultHasher, |
| 3085 | error_format: ErrorOutputType, |
| 3086 | for_crate_hash: bool, |
| 3087 | ) { |
| 3088 | Hash::hash(&self.0.len(), hasher); |
| 3089 | for (key, val) in &self.0 { |
| 3090 | DepTrackingHash::hash(key, hasher, error_format, for_crate_hash); |
| 3091 | if !for_crate_hash { |
| 3092 | DepTrackingHash::hash(val, hasher, error_format, for_crate_hash); |
| 3093 | } |
Aaron Hill | 605513a | 2021-05-26 00:43:02 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | } |
| 3097 | |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3098 | // This is a stable hash because BTreeMap is a sorted container |
Jacob Pratt | 49c82f3 | 2022-05-20 23:51:09 | [diff] [blame] | 3099 | pub(crate) fn stable_hash( |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 3100 | sub_hashes: BTreeMap<&'static str, &dyn DepTrackingHash>, |
| 3101 | hasher: &mut DefaultHasher, |
| 3102 | error_format: ErrorOutputType, |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3103 | for_crate_hash: bool, |
Santiago Pastorino | 52a47d4 | 2018-03-06 05:29:03 | [diff] [blame] | 3104 | ) { |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3105 | for (key, sub_hash) in sub_hashes { |
| 3106 | // Using Hash::hash() instead of DepTrackingHash::hash() is fine for |
| 3107 | // the keys, as they are just plain strings |
| 3108 | Hash::hash(&key.len(), hasher); |
| 3109 | Hash::hash(key, hasher); |
Aaron Hill | 99f652f | 2021-06-20 00:22:14 | [diff] [blame] | 3110 | sub_hash.hash(hasher, error_format, for_crate_hash); |
Michael Woerister | 3241431 | 2016-08-02 20:53:58 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | } |
Amanieu d'Antras | aa36237 | 2021-10-06 14:52:54 | [diff] [blame] | 3114 | |
| 3115 | /// Default behavior to use in out-of-memory situations. |
| 3116 | #[derive(Clone, Copy, PartialEq, Hash, Debug, Encodable, Decodable, HashStable_Generic)] |
| 3117 | pub enum OomStrategy { |
| 3118 | /// Generate a panic that can be caught by `catch_unwind`. |
| 3119 | Panic, |
| 3120 | |
| 3121 | /// Abort the process immediately. |
| 3122 | Abort, |
| 3123 | } |
| 3124 | |
| 3125 | impl OomStrategy { |
| 3126 | pub const SYMBOL: &'static str = "__rust_alloc_error_handler_should_panic"; |
| 3127 | |
| 3128 | pub fn should_panic(self) -> u8 { |
| 3129 | match self { |
| 3130 | OomStrategy::Panic => 1, |
| 3131 | OomStrategy::Abort => 0, |
| 3132 | } |
| 3133 | } |
| 3134 | } |
Nika Layzell | 6d1650f | 2022-06-18 18:15:03 | [diff] [blame] | 3135 | |
| 3136 | /// How to run proc-macro code when building this crate |
| 3137 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 3138 | pub enum ProcMacroExecutionStrategy { |
| 3139 | /// Run the proc-macro code on the same thread as the server. |
| 3140 | SameThread, |
| 3141 | |
| 3142 | /// Run the proc-macro code on a different thread. |
| 3143 | CrossThread, |
| 3144 | } |
Joshua Nelson | eb53eea | 2022-12-29 21:08:09 | [diff] [blame] | 3145 | |
Andrew Zhogin | 8507f51 | 2024-01-10 18:36:05 | [diff] [blame] | 3146 | /// How to perform collapse macros debug info |
| 3147 | /// if-ext - if macro from different crate (related to callsite code) |
| 3148 | /// | cmd \ attr | no | (unspecified) | external | yes | |
| 3149 | /// | no | no | no | no | no | |
| 3150 | /// | (unspecified) | no | no | if-ext | yes | |
| 3151 | /// | external | no | if-ext | if-ext | yes | |
| 3152 | /// | yes | yes | yes | yes | yes | |
| 3153 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 3154 | pub enum CollapseMacroDebuginfo { |
| 3155 | /// Don't collapse debuginfo for the macro |
| 3156 | No = 0, |
| 3157 | /// Unspecified value |
| 3158 | Unspecified = 1, |
| 3159 | /// Collapse debuginfo if the macro comes from a different crate |
| 3160 | External = 2, |
| 3161 | /// Collapse debuginfo for the macro |
| 3162 | Yes = 3, |
| 3163 | } |
| 3164 | |
Joshua Nelson | eb53eea | 2022-12-29 21:08:09 | [diff] [blame] | 3165 | /// Which format to use for `-Z dump-mono-stats` |
| 3166 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 3167 | pub enum DumpMonoStatsFormat { |
| 3168 | /// Pretty-print a markdown table |
| 3169 | Markdown, |
| 3170 | /// Emit structured JSON |
| 3171 | Json, |
| 3172 | } |
| 3173 | |
| 3174 | impl DumpMonoStatsFormat { |
| 3175 | pub fn extension(self) -> &'static str { |
| 3176 | match self { |
| 3177 | Self::Markdown => "md", |
| 3178 | Self::Json => "json", |
| 3179 | } |
| 3180 | } |
| 3181 | } |
Rémy Rakic | 4f7a27b | 2023-06-30 11:55:38 | [diff] [blame] | 3182 | |
Matthew Maurer | ac7595f | 2023-12-12 21:32:43 | [diff] [blame] | 3183 | /// `-Z patchable-function-entry` representation - how many nops to put before and after function |
| 3184 | /// entry. |
| 3185 | #[derive(Clone, Copy, PartialEq, Hash, Debug, Default)] |
| 3186 | pub struct PatchableFunctionEntry { |
| 3187 | /// Nops before the entry |
| 3188 | prefix: u8, |
| 3189 | /// Nops after the entry |
| 3190 | entry: u8, |
| 3191 | } |
| 3192 | |
| 3193 | impl PatchableFunctionEntry { |
Florian Schmiderer | 7c56398 | 2024-05-02 21:19:02 | [diff] [blame] | 3194 | pub fn from_total_and_prefix_nops( |
| 3195 | total_nops: u8, |
| 3196 | prefix_nops: u8, |
| 3197 | ) -> Option<PatchableFunctionEntry> { |
| 3198 | if total_nops < prefix_nops { |
Matthew Maurer | ac7595f | 2023-12-12 21:32:43 | [diff] [blame] | 3199 | None |
| 3200 | } else { |
Florian Schmiderer | 7c56398 | 2024-05-02 21:19:02 | [diff] [blame] | 3201 | Some(Self { prefix: prefix_nops, entry: total_nops - prefix_nops }) |
Matthew Maurer | ac7595f | 2023-12-12 21:32:43 | [diff] [blame] | 3202 | } |
| 3203 | } |
| 3204 | pub fn prefix(&self) -> u8 { |
| 3205 | self.prefix |
| 3206 | } |
| 3207 | pub fn entry(&self) -> u8 { |
| 3208 | self.entry |
| 3209 | } |
| 3210 | } |
| 3211 | |
Rémy Rakic | 4f7a27b | 2023-06-30 11:55:38 | [diff] [blame] | 3212 | /// `-Zpolonius` values, enabling the borrow checker polonius analysis, and which version: legacy, |
| 3213 | /// or future prototype. |
Rémy Rakic | d9c213c | 2023-10-20 15:32:22 | [diff] [blame] | 3214 | #[derive(Clone, Copy, PartialEq, Hash, Debug, Default)] |
Rémy Rakic | 4f7a27b | 2023-06-30 11:55:38 | [diff] [blame] | 3215 | pub enum Polonius { |
| 3216 | /// The default value: disabled. |
Rémy Rakic | d9c213c | 2023-10-20 15:32:22 | [diff] [blame] | 3217 | #[default] |
Rémy Rakic | 4f7a27b | 2023-06-30 11:55:38 | [diff] [blame] | 3218 | Off, |
| 3219 | |
| 3220 | /// Legacy version, using datalog and the `polonius-engine` crate. Historical value for `-Zpolonius`. |
| 3221 | Legacy, |
| 3222 | |
Rémy Rakic | b012615 | 2023-06-30 13:18:11 | [diff] [blame] | 3223 | /// In-tree prototype, extending the NLL infrastructure. |
Rémy Rakic | 4f7a27b | 2023-06-30 11:55:38 | [diff] [blame] | 3224 | Next, |
| 3225 | } |
| 3226 | |
Rémy Rakic | 4f7a27b | 2023-06-30 11:55:38 | [diff] [blame] | 3227 | impl Polonius { |
| 3228 | /// Returns whether the legacy version of polonius is enabled |
| 3229 | pub fn is_legacy_enabled(&self) -> bool { |
| 3230 | matches!(self, Polonius::Legacy) |
| 3231 | } |
Rémy Rakic | b012615 | 2023-06-30 13:18:11 | [diff] [blame] | 3232 | |
| 3233 | /// Returns whether the "next" version of polonius is enabled |
| 3234 | pub fn is_next_enabled(&self) -> bool { |
| 3235 | matches!(self, Polonius::Next) |
| 3236 | } |
Rémy Rakic | 4f7a27b | 2023-06-30 11:55:38 | [diff] [blame] | 3237 | } |
Ben Kimock | fcdd99e | 2023-11-07 00:55:05 | [diff] [blame] | 3238 | |
| 3239 | #[derive(Clone, Copy, PartialEq, Hash, Debug)] |
| 3240 | pub enum InliningThreshold { |
| 3241 | Always, |
| 3242 | Sometimes(usize), |
| 3243 | Never, |
| 3244 | } |
| 3245 | |
| 3246 | impl Default for InliningThreshold { |
| 3247 | fn default() -> Self { |
| 3248 | Self::Sometimes(100) |
| 3249 | } |
| 3250 | } |
Miguel Ojeda | 2d47622 | 2023-10-18 14:58:17 | [diff] [blame] | 3251 | |
| 3252 | /// The different settings that the `-Zfunction-return` flag can have. |
| 3253 | #[derive(Clone, Copy, PartialEq, Hash, Debug, Default)] |
| 3254 | pub enum FunctionReturn { |
| 3255 | /// Keep the function return unmodified. |
| 3256 | #[default] |
| 3257 | Keep, |
| 3258 | |
| 3259 | /// Replace returns with jumps to thunk, without emitting the thunk. |
| 3260 | ThunkExtern, |
| 3261 | } |
Rémy Rakic | e0bb1c7 | 2024-08-28 21:13:56 | [diff] [blame] | 3262 | |
| 3263 | /// Whether extra span comments are included when dumping MIR, via the `-Z mir-include-spans` flag. |
| 3264 | /// By default, only enabled in the NLL MIR dumps, and disabled in all other passes. |
| 3265 | #[derive(Clone, Copy, Default, PartialEq, Debug)] |
| 3266 | pub enum MirIncludeSpans { |
| 3267 | Off, |
| 3268 | On, |
| 3269 | /// Default: include extra comments in NLL MIR dumps only. Can be ignored and considered as |
| 3270 | /// `Off` in all other cases. |
| 3271 | #[default] |
| 3272 | Nll, |
| 3273 | } |
| 3274 | |
| 3275 | impl MirIncludeSpans { |
| 3276 | /// Unless opting into extra comments for all passes, they can be considered disabled. |
| 3277 | /// The cases where a distinction between on/off and a per-pass value can exist will be handled |
| 3278 | /// in the passes themselves: i.e. the `Nll` value is considered off for all intents and |
| 3279 | /// purposes, except for the NLL MIR dump pass. |
| 3280 | pub fn is_enabled(self) -> bool { |
| 3281 | self == MirIncludeSpans::On |
| 3282 | } |
| 3283 | } |