about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-28 04:16:05 +0000
committerbors <bors@rust-lang.org>2024-04-28 04:16:05 +0000
commit6c90ac8d8f4489472720fce03c338cd5d0977f33 (patch)
treed8fe94de80bc2fde7359e11cf8106b902bf8b86f
parent2207179a591f5f252885a94ab014dafeb6e8e9e8 (diff)
parent09c076810cb7649e5817f316215010d49e78e8d7 (diff)
downloadrust-6c90ac8d8f4489472720fce03c338cd5d0977f33.tar.gz
rust-6c90ac8d8f4489472720fce03c338cd5d0977f33.zip
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.
-rw-r--r--src/bootstrap/src/core/sanity.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/bootstrap/src/core/sanity.rs b/src/bootstrap/src/core/sanity.rs
index e03b1e17908..6ccf29fb6cb 100644
--- a/src/bootstrap/src/core/sanity.rs
+++ b/src/bootstrap/src/core/sanity.rs
@@ -14,6 +14,7 @@ use std::ffi::{OsStr, OsString};
 use std::fs;
 use std::path::PathBuf;
 use std::process::Command;
+use walkdir::WalkDir;
 
 use crate::builder::Kind;
 use crate::core::config::Target;
@@ -177,6 +178,34 @@ 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 =
+            output(Command::new(&build.config.initial_rustc).args(["--print", "target-list"]));
+
+        has_target |= supported_target_list.contains(&target_str);
+
+        // If not, check for a valid file location that may have been specified
+        // by the user for the custom target.
+        if let Some(custom_target_path) = env::var_os("RUST_TARGET_PATH") {
+            let mut target_os_str = OsString::from(&target_str);
+            target_os_str.push(".json");
+            // Recursively traverse through nested directories.
+            let walker = WalkDir::new(custom_target_path).into_iter();
+            for entry in walker.filter_map(|e| e.ok()) {
+                has_target |= entry.file_name() == target_os_str;
+            }
+        }
+
+        if !has_target && !["A", "B", "C"].contains(&target_str.as_str()) {
+            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) {