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
#![feature(rustc_private)]
extern crate rustc_driver;
extern crate rustc_interface;
extern crate rustc_lint;
extern crate rustc_session;
use std::{env, io, mem, process::ExitCode};
use rustc_lint::LintStore;
use rustc_session::Session;
type BoxCallback = Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>;
#[derive(Default)]
pub struct Driver {
cfg: Vec<String>,
callbacks: Vec<BoxCallback>,
}
impl Driver {
pub fn new() -> Self {
Self::default()
}
pub fn with_cfg(mut self, cfg: impl Into<String>) -> Self {
self.cfg.push(cfg.into());
self
}
pub fn with_callback<F>(mut self, callback: F) -> Self
where
F: Fn(&Session, &mut LintStore) + Send + Sync + 'static,
{
self.callbacks.push(Box::new(callback));
self
}
pub fn with_callbacks<F, I>(mut self, callbacks: I) -> Self
where
F: Fn(&Session, &mut LintStore) + Send + Sync + 'static,
I: IntoIterator<Item = F>,
{
self.callbacks
.extend(callbacks.into_iter().map(|cb| Box::new(cb) as _));
self
}
#[must_use]
pub fn run(self) -> ExitCode {
self.run_with_args(env::args().skip(1))
}
#[must_use]
pub fn run_with_args(mut self, args: impl IntoIterator<Item = String>) -> ExitCode {
if env::var("CARGO_NAG_DUMP_EXECUTION_PARAMS").is_ok() {
dump_execution_params();
return ExitCode::SUCCESS;
}
let mut args: Vec<_> = args.into_iter().collect();
for cfg in &self.cfg {
args.push(format!("--cfg={cfg}"));
}
if let Ok(sysroot) = env::var("CARGO_NAG_SYSROOT") {
args.push(format!("--sysroot={sysroot}"));
}
if rustc_driver::RunCompiler::new(&args, &mut self)
.run()
.is_err()
{
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}
}
#[must_use]
pub fn run<F, I, C>(cfg: C, callbacks: I) -> ExitCode
where
F: Fn(&Session, &mut LintStore) + Send + Sync + 'static,
I: IntoIterator<Item = F>,
C: Into<String>,
{
Driver::new().with_cfg(cfg).with_callbacks(callbacks).run()
}
impl rustc_driver::Callbacks for Driver {
fn config(&mut self, config: &mut rustc_interface::Config) {
let previous = config.register_lints.take();
let callbacks = mem::take(&mut self.callbacks);
config.register_lints = Some(Box::new(move |sess, lint_store| {
if let Some(previous) = &previous {
previous(sess, lint_store);
}
for callback in &callbacks {
callback(sess, lint_store);
}
}));
}
}
fn dump_execution_params() {
#[derive(serde::Serialize)]
struct ExecutionParams {
binary: String,
environment: std::collections::HashMap<String, String>,
}
let environment = env::vars().collect();
let binary = env::args().next().expect("no binary name in argv");
serde_json::to_writer(
io::stdout().lock(),
&ExecutionParams {
binary,
environment,
},
)
.expect("failed to write execution params to stdout");
}