From 247cab691dab2983c8d25df49debfcbdf801b81a Mon Sep 17 00:00:00 2001 From: Rajveer Date: Sat, 6 Apr 2024 17:29:50 +0530 Subject: [PATCH] Bootstrap: Check validity of `--target` and `--host` triples before starting a build Resolves #122128 --- src/bootstrap/src/core/sanity.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/bootstrap/src/core/sanity.rs b/src/bootstrap/src/core/sanity.rs index e03b1e179084e..5d023ce4f6934 100644 --- a/src/bootstrap/src/core/sanity.rs +++ b/src/bootstrap/src/core/sanity.rs @@ -177,6 +177,30 @@ than building it. continue; } + // Check if there exists a built-in target in the list of supported targets. + let mut has_target = false; + let target_str = target.to_string(); + + let supported_target_list = Command::new("rustc") + .args(["--print", "target-list"]) + .output() + .expect("Should have been able to fetch the target list."); + + if let Some(stdout) = String::from_utf8(supported_target_list.stdout).ok() { + has_target |= stdout.contains(&target_str); + } + + // If not, check for a valid file location that may have been specified + // by the user for the custom target. + has_target |= env::var_os("RUST_TARGET_PATH").is_some(); + + if !has_target && !(target_str == "A" || target_str == "B" || target_str == "C") { + panic!( + "No such target exists in the target list, + specify a correct location of the JSON specification file for custom targets!" + ); + } + if !build.config.dry_run() { cmd_finder.must_have(build.cc(*target)); if let Some(ar) = build.ar(*target) {