Skip to content

Commit 09c0768

Browse files
committed
Bootstrap: Check validity of --target and --host triples before starting a build
Resolves #122128
1 parent ca7d34e commit 09c0768

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/bootstrap/src/core/sanity.rs

+29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::ffi::{OsStr, OsString};
1414
use std::fs;
1515
use std::path::PathBuf;
1616
use std::process::Command;
17+
use walkdir::WalkDir;
1718

1819
use crate::builder::Kind;
1920
use crate::core::config::Target;
@@ -177,6 +178,34 @@ than building it.
177178
continue;
178179
}
179180

181+
// Check if there exists a built-in target in the list of supported targets.
182+
let mut has_target = false;
183+
let target_str = target.to_string();
184+
185+
let supported_target_list =
186+
output(Command::new(&build.config.initial_rustc).args(["--print", "target-list"]));
187+
188+
has_target |= supported_target_list.contains(&target_str);
189+
190+
// If not, check for a valid file location that may have been specified
191+
// by the user for the custom target.
192+
if let Some(custom_target_path) = env::var_os("RUST_TARGET_PATH") {
193+
let mut target_os_str = OsString::from(&target_str);
194+
target_os_str.push(".json");
195+
// Recursively traverse through nested directories.
196+
let walker = WalkDir::new(custom_target_path).into_iter();
197+
for entry in walker.filter_map(|e| e.ok()) {
198+
has_target |= entry.file_name() == target_os_str;
199+
}
200+
}
201+
202+
if !has_target && !["A", "B", "C"].contains(&target_str.as_str()) {
203+
panic!(
204+
"No such target exists in the target list,
205+
specify a correct location of the JSON specification file for custom targets!"
206+
);
207+
}
208+
180209
if !build.config.dry_run() {
181210
cmd_finder.must_have(build.cc(*target));
182211
if let Some(ar) = build.ar(*target) {

0 commit comments

Comments
 (0)