Skip to content

Commit 41aa521

Browse files
committed
bootstrap: validate rust.codegen-backends & targer.<triple>.codegen-backends
1 parent fb644e6 commit 41aa521

File tree

3 files changed

+27
-39
lines changed

3 files changed

+27
-39
lines changed

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,14 +1534,13 @@ fn needs_codegen_config(run: &RunConfig<'_>) -> bool {
15341534
needs_codegen_cfg
15351535
}
15361536

1537-
pub(crate) const CODEGEN_BACKEND_PREFIX: &str = "rustc_codegen_";
1538-
15391537
fn is_codegen_cfg_needed(path: &TaskPath, run: &RunConfig<'_>) -> bool {
15401538
let path = path.path.to_str().unwrap();
15411539

15421540
let is_explicitly_called = |p| -> bool { run.builder.paths.contains(p) };
15431541
let should_enforce = run.builder.kind == Kind::Dist || run.builder.kind == Kind::Install;
15441542

1543+
const CODEGEN_BACKEND_PREFIX: &str = "rustc_codegen_";
15451544
if path.contains(CODEGEN_BACKEND_PREFIX) {
15461545
let mut needs_codegen_backend_config = true;
15471546
for backend in run.builder.config.codegen_backends(run.target) {

src/bootstrap/src/core/config/toml/rust.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::str::FromStr;
55

66
use serde::{Deserialize, Deserializer};
77

8-
use crate::core::build_steps::compile::CODEGEN_BACKEND_PREFIX;
98
use crate::core::config::toml::TomlConfig;
109
use crate::core::config::{
1110
DebuginfoLevel, Merge, ReplaceOpt, RustcLto, StringOrBool, set, threads_from_config,
@@ -394,6 +393,23 @@ pub fn check_incompatible_options_for_ci_rustc(
394393
Ok(())
395394
}
396395

396+
pub(crate) const VALID_CODEGEN_BACKENDS: &[&str] = &["llvm", "cranelift", "gcc"];
397+
398+
pub(crate) fn validate_codegen_backends(
399+
backends: Vec<String>,
400+
section: impl FnOnce() -> String,
401+
) -> Vec<String> {
402+
for backend in &backends {
403+
if !VALID_CODEGEN_BACKENDS.contains(&&**backend) {
404+
let section = section();
405+
panic!(
406+
"Invalid value '{backend}' for '{section}.codegen-backends'. Permitted values: {VALID_CODEGEN_BACKENDS:?}."
407+
);
408+
}
409+
}
410+
backends
411+
}
412+
397413
impl Config {
398414
pub fn apply_rust_config(
399415
&mut self,
@@ -572,24 +588,11 @@ impl Config {
572588
set(&mut self.ehcont_guard, ehcont_guard);
573589
self.llvm_libunwind_default =
574590
llvm_libunwind.map(|v| v.parse().expect("failed to parse rust.llvm-libunwind"));
575-
576-
if let Some(ref backends) = codegen_backends {
577-
let available_backends = ["llvm", "cranelift", "gcc"];
578-
579-
self.rust_codegen_backends = backends.iter().map(|s| {
580-
if let Some(backend) = s.strip_prefix(CODEGEN_BACKEND_PREFIX) {
581-
if available_backends.contains(&backend) {
582-
panic!("Invalid value '{s}' for 'rust.codegen-backends'. Instead, please use '{backend}'.");
583-
} else {
584-
println!("HELP: '{s}' for 'rust.codegen-backends' might fail. \
585-
Codegen backends are mostly defined without the '{CODEGEN_BACKEND_PREFIX}' prefix. \
586-
In this case, it would be referred to as '{backend}'.");
587-
}
588-
}
589-
590-
s.clone()
591-
}).collect();
592-
}
591+
set(
592+
&mut self.rust_codegen_backends,
593+
codegen_backends
594+
.map(|backends| validate_codegen_backends(backends, || "rust".into())),
595+
);
593596

594597
self.rust_codegen_units = codegen_units.map(threads_from_config);
595598
self.rust_codegen_units_std = codegen_units_std.map(threads_from_config);

src/bootstrap/src/core/config/toml/target.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::collections::HashMap;
1616

1717
use serde::{Deserialize, Deserializer};
1818

19-
use crate::core::build_steps::compile::CODEGEN_BACKEND_PREFIX;
19+
use crate::core::config::toml::rust::validate_codegen_backends;
2020
use crate::core::config::{LlvmLibunwind, Merge, ReplaceOpt, SplitDebuginfo, StringOrBool};
2121
use crate::{Config, HashSet, PathBuf, TargetSelection, define_config, exit};
2222

@@ -142,23 +142,9 @@ impl Config {
142142
target.rpath = cfg.rpath;
143143
target.optimized_compiler_builtins = cfg.optimized_compiler_builtins;
144144
target.jemalloc = cfg.jemalloc;
145-
146-
if let Some(ref backends) = cfg.codegen_backends {
147-
let available_backends = ["llvm", "cranelift", "gcc"];
148-
149-
target.codegen_backends = Some(backends.iter().map(|s| {
150-
if let Some(backend) = s.strip_prefix(CODEGEN_BACKEND_PREFIX) {
151-
if available_backends.contains(&backend) {
152-
panic!("Invalid value '{s}' for 'target.{triple}.codegen-backends'. Instead, please use '{backend}'.");
153-
} else {
154-
println!("HELP: '{s}' for 'target.{triple}.codegen-backends' might fail. \
155-
Codegen backends are mostly defined without the '{CODEGEN_BACKEND_PREFIX}' prefix. \
156-
In this case, it would be referred to as '{backend}'.");
157-
}
158-
}
159-
160-
s.clone()
161-
}).collect());
145+
if let Some(backends) = cfg.codegen_backends {
146+
target.codegen_backends =
147+
Some(validate_codegen_backends(backends, || format!("target.{triple}")))
162148
}
163149

164150
target.split_debuginfo = cfg.split_debuginfo.as_ref().map(|v| {

0 commit comments

Comments
 (0)