Skip to content

Commit 6c90ac8

Browse files
committed
Auto merge of #123546 - Rajveer100:branch-for-issue-122128, r=onur-ozkan
Bootstrap: Check validity of `--target` and `--host` triples before starting a build Resolves #122128 As described in the issue, validating the `target` and `host` triples would save a lot of time before actually starting a build. This would also check for custom targets by looking for a valid JSON spec if the specified target does not exist in the [supported](https://github.com/rust-lang/rust/blob/42825768b103c28b10ce0407749acb21d32abeec/compiler/rustc_target/src/spec/mod.rs#L1401-L1689) list of targets.
2 parents 2207179 + 09c0768 commit 6c90ac8

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)