-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Expand file tree
/
Copy pathprint_request.rs
More file actions
272 lines (224 loc) · 8.37 KB
/
print_request.rs
File metadata and controls
272 lines (224 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Code for dealing with `--print` requests.
use std::fmt;
use std::sync::LazyLock;
use rustc_data_structures::fx::FxHashSet;
use crate::EarlyDiagCtxt;
use crate::config::{
CodegenOptions, OutFileName, UnstableOptions, nightly_options, split_out_file_name,
};
use crate::macros::AllVariants;
#[derive(Clone, PartialEq, Debug)]
pub struct PrintRequest {
pub kind: PrintKind,
pub out: OutFileName,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(AllVariants)]
pub enum PrintKind {
// tidy-alphabetical-start
/// All target specs.
AllTargetSpecsJson,
/// Does the backend supports Zstd compression? (perma-unstable)
BackendHasZstd,
/// List of supported calling conventions.
CallingConventions,
/// List of cfg values (1st flavour).
Cfg,
/// List of cfg values (2nd flavour).
CheckCfg,
/// List of available code models for the current backend.
CodeModels,
/// Name of the crate being compiled.
CrateName,
CrateRootLintLevels,
/// The current selected deployment target (Apple platforms).
DeploymentTarget,
/// THe names of the files created by the `link` `--emit` kind.
FileNames,
/// Target-tuple of the host compiler.
HostTuple,
/// Linker invocations.
LinkArgs,
/// When compiling a `staticlib` crate, print the linker flags used.
NativeStaticLibs,
/// List of available relocation models for the current backend.
RelocationModels,
/// List of available split debuginfos for the current target.
SplitDebuginfo,
/// List of available stack protector strategies for the current backend.
StackProtectorStrategies,
/// List of available crate types for the current target.
SupportedCrateTypes,
/// Path to the sysroot.
Sysroot,
/// List of available CPU values for the current target.
TargetCPUs,
/// List of available target features for the current target.
TargetFeatures,
/// Path to the target libdir.
TargetLibdir,
/// List of supported targets.
TargetList,
/// Current target spec.
TargetSpecJson,
/// Target spec schema.
TargetSpecJsonSchema,
/// List of available TLS models for the current backend.
TlsModels,
// tidy-alphabetical-end
}
impl PrintKind {
/// FIXME: rust-analyzer doesn't support `#![feature(macro_derive)]` yet
/// (<https://github.com/rust-lang/rust-analyzer/issues/21043>), which breaks autocomplete.
/// Work around that by aliasing the trait constant to a regular constant.
const ALL_VARIANTS: &[Self] = <Self as AllVariants>::ALL_VARIANTS;
fn name(self) -> &'static str {
use PrintKind::*;
match self {
// tidy-alphabetical-start
AllTargetSpecsJson => "all-target-specs-json",
BackendHasZstd => "backend-has-zstd",
CallingConventions => "calling-conventions",
Cfg => "cfg",
CheckCfg => "check-cfg",
CodeModels => "code-models",
CrateName => "crate-name",
CrateRootLintLevels => "crate-root-lint-levels",
DeploymentTarget => "deployment-target",
FileNames => "file-names",
HostTuple => "host-tuple",
LinkArgs => "link-args",
NativeStaticLibs => "native-static-libs",
RelocationModels => "relocation-models",
SplitDebuginfo => "split-debuginfo",
StackProtectorStrategies => "stack-protector-strategies",
SupportedCrateTypes => "supported-crate-types",
Sysroot => "sysroot",
TargetCPUs => "target-cpus",
TargetFeatures => "target-features",
TargetLibdir => "target-libdir",
TargetList => "target-list",
TargetSpecJson => "target-spec-json",
TargetSpecJsonSchema => "target-spec-json-schema",
TlsModels => "tls-models",
// tidy-alphabetical-end
}
}
fn is_stable(self) -> bool {
use PrintKind::*;
match self {
// Stable values:
CallingConventions
| Cfg
| CodeModels
| CrateName
| DeploymentTarget
| FileNames
| HostTuple
| LinkArgs
| NativeStaticLibs
| RelocationModels
| SplitDebuginfo
| StackProtectorStrategies
| Sysroot
| TargetCPUs
| TargetFeatures
| TargetLibdir
| TargetList
| TlsModels => true,
// Unstable values:
AllTargetSpecsJson => false,
BackendHasZstd => false, // (perma-unstable, for use by compiletest)
CheckCfg => false,
CrateRootLintLevels => false,
SupportedCrateTypes => false,
TargetSpecJson => false,
TargetSpecJsonSchema => false,
}
}
fn from_str(s: &str) -> Option<Self> {
Self::ALL_VARIANTS.iter().find(|kind| kind.name() == s).copied()
}
}
impl fmt::Display for PrintKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.name().fmt(f)
}
}
pub(crate) static PRINT_HELP: LazyLock<String> = LazyLock::new(|| {
let print_kinds =
PrintKind::ALL_VARIANTS.iter().map(|kind| kind.name()).collect::<Vec<_>>().join("|");
format!(
"Compiler information to print on stdout (or to a file)\n\
INFO may be one of <{print_kinds}>.",
)
});
pub fn collect_print_requests(
early_dcx: &EarlyDiagCtxt,
cg: &mut CodegenOptions,
unstable_opts: &UnstableOptions,
matches: &getopts::Matches,
) -> Vec<PrintRequest> {
let mut prints = Vec::<PrintRequest>::new();
if cg.target_cpu.as_deref() == Some("help") {
prints.push(PrintRequest { kind: PrintKind::TargetCPUs, out: OutFileName::Stdout });
cg.target_cpu = None;
};
if cg.target_feature == "help" {
prints.push(PrintRequest { kind: PrintKind::TargetFeatures, out: OutFileName::Stdout });
cg.target_feature = String::new();
}
// We disallow reusing the same path in multiple prints, such as `--print
// cfg=output.txt --print link-args=output.txt`, because outputs are printed
// by disparate pieces of the compiler, and keeping track of which files
// need to be overwritten vs appended to is annoying.
let mut printed_paths = FxHashSet::default();
prints.extend(matches.opt_strs("print").into_iter().map(|req| {
let (req, out) = split_out_file_name(&req);
let kind = if let Some(print_kind) = PrintKind::from_str(req) {
check_print_request_stability(early_dcx, unstable_opts, print_kind);
print_kind
} else {
let is_nightly = nightly_options::match_is_nightly_build(matches);
emit_unknown_print_request_help(early_dcx, req, is_nightly)
};
let out = out.unwrap_or(OutFileName::Stdout);
if let OutFileName::Real(path) = &out {
if !printed_paths.insert(path.clone()) {
early_dcx.early_fatal(format!(
"cannot print multiple outputs to the same path: {}",
path.display(),
));
}
}
PrintRequest { kind, out }
}));
prints
}
fn check_print_request_stability(
early_dcx: &EarlyDiagCtxt,
unstable_opts: &UnstableOptions,
print_kind: PrintKind,
) {
if !print_kind.is_stable() && !unstable_opts.unstable_options {
early_dcx.early_fatal(format!(
"the `-Z unstable-options` flag must also be passed to enable the `{print_kind}` print option"
));
}
}
fn emit_unknown_print_request_help(early_dcx: &EarlyDiagCtxt, req: &str, is_nightly: bool) -> ! {
let prints = PrintKind::ALL_VARIANTS
.iter()
// If we're not on nightly, we don't want to print unstable options
.filter(|kind| is_nightly || kind.is_stable())
.map(|kind| format!("`{kind}`"))
.collect::<Vec<_>>()
.join(", ");
let mut diag = early_dcx.early_struct_fatal(format!("unknown print request: `{req}`"));
diag.help(format!("valid print requests are: {prints}"));
if req == "lints" {
diag.help(format!("use `-Whelp` to print a list of lints"));
}
diag.help(format!("for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information"));
diag.emit()
}