Skip to content

Commit 71bc62b

Browse files
committed
Add option to pass a custom codegen backend from a driver
1 parent 956e06c commit 71bc62b

File tree

8 files changed

+37
-5
lines changed

8 files changed

+37
-5
lines changed

Diff for: compiler/rustc_driver/src/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ pub fn run_compiler(
141141
callbacks: &mut (dyn Callbacks + Send),
142142
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
143143
emitter: Option<Box<dyn Write + Send>>,
144+
make_codegen_backend: Option<
145+
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
146+
>,
144147
) -> interface::Result<()> {
145148
let mut args = Vec::new();
146149
for arg in at_args {
@@ -162,6 +165,11 @@ pub fn run_compiler(
162165
let sopts = config::build_session_options(&matches);
163166
let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
164167

168+
// We wrap `make_codegen_backend` in another `Option` such that `dummy_config` can take
169+
// ownership of it when necessary, while also allowing the non-dummy config to take ownership
170+
// when `dummy_config` is not used.
171+
let mut make_codegen_backend = Some(make_codegen_backend);
172+
165173
let mut dummy_config = |sopts, cfg, diagnostic_output| {
166174
let mut config = interface::Config {
167175
opts: sopts,
@@ -177,6 +185,7 @@ pub fn run_compiler(
177185
lint_caps: Default::default(),
178186
register_lints: None,
179187
override_queries: None,
188+
make_codegen_backend: make_codegen_backend.take().unwrap(),
180189
registry: diagnostics_registry(),
181190
};
182191
callbacks.config(&mut config);
@@ -253,6 +262,7 @@ pub fn run_compiler(
253262
lint_caps: Default::default(),
254263
register_lints: None,
255264
override_queries: None,
265+
make_codegen_backend: make_codegen_backend.unwrap(),
256266
registry: diagnostics_registry(),
257267
};
258268

@@ -1265,7 +1275,7 @@ pub fn main() -> ! {
12651275
})
12661276
})
12671277
.collect::<Vec<_>>();
1268-
run_compiler(&args, &mut callbacks, None, None)
1278+
run_compiler(&args, &mut callbacks, None, None, None)
12691279
});
12701280
// The extra `\t` is necessary to align this label with the others.
12711281
print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());

Diff for: compiler/rustc_interface/src/interface.rs

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ pub struct Config {
154154
pub override_queries:
155155
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,
156156

157+
/// This is a callback from the driver that is called to create a codegen backend.
158+
pub make_codegen_backend:
159+
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
160+
157161
/// Registry of diagnostics codes.
158162
pub registry: Registry,
159163
}
@@ -167,6 +171,7 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
167171
config.file_loader,
168172
config.input_path.clone(),
169173
config.lint_caps,
174+
config.make_codegen_backend,
170175
registry.clone(),
171176
);
172177

Diff for: compiler/rustc_interface/src/util.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,17 @@ pub fn create_session(
6363
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
6464
input_path: Option<PathBuf>,
6565
lint_caps: FxHashMap<lint::LintId, lint::Level>,
66+
make_codegen_backend: Option<
67+
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
68+
>,
6669
descriptions: Registry,
6770
) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>) {
68-
let codegen_backend = get_codegen_backend(&sopts);
71+
let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend {
72+
make_codegen_backend(&sopts)
73+
} else {
74+
get_codegen_backend(&sopts)
75+
};
76+
6977
// target_override is documented to be called before init(), so this is okay
7078
let target_override = codegen_backend.target_override(&sopts);
7179

Diff for: src/librustdoc/core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ pub fn run_core(
419419
(rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id)
420420
};
421421
}),
422+
make_codegen_backend: None,
422423
registry: rustc_driver::diagnostics_registry(),
423424
};
424425

Diff for: src/librustdoc/doctest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub fn run(options: Options) -> Result<(), ErrorReported> {
9595
lint_caps,
9696
register_lints: None,
9797
override_queries: None,
98+
make_codegen_backend: None,
9899
registry: rustc_driver::diagnostics_registry(),
99100
};
100101

Diff for: src/test/run-make-fulldeps/issue-19371/foo.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
6060
lint_caps: Default::default(),
6161
register_lints: None,
6262
override_queries: None,
63+
make_codegen_backend: None,
6364
registry: rustc_driver::diagnostics_registry(),
6465
};
6566

Diff for: src/test/ui-fulldeps/compiler-calls.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ fn main() {
2626
let mut count = 1;
2727
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
2828
rustc_driver::catch_fatal_errors(|| {
29-
rustc_driver::run_compiler(&args, &mut TestCalls { count: &mut count }, None, None).ok();
29+
rustc_driver::run_compiler(
30+
&args,
31+
&mut TestCalls { count: &mut count },
32+
None,
33+
None,
34+
None,
35+
).ok();
3036
}).ok();
3137
assert_eq!(count, 2);
3238
}

Diff for: src/tools/clippy/src/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ pub fn main() {
357357
args.extend(vec!["--sysroot".into(), sys_root]);
358358
};
359359

360-
return rustc_driver::run_compiler(&args, &mut DefaultCallbacks, None, None);
360+
return rustc_driver::run_compiler(&args, &mut DefaultCallbacks, None, None, None);
361361
}
362362

363363
if orig_args.iter().any(|a| a == "--version" || a == "-V") {
@@ -420,6 +420,6 @@ pub fn main() {
420420
let mut default = DefaultCallbacks;
421421
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
422422
if clippy_enabled { &mut clippy } else { &mut default };
423-
rustc_driver::run_compiler(&args, callbacks, None, None)
423+
rustc_driver::run_compiler(&args, callbacks, None, None, None)
424424
}))
425425
}

0 commit comments

Comments
 (0)