1Detect and fetch custom target configurations when rustc is
2bootstrapped in rust oe-selftest.
3
4Upstream-Status: Backport [https://github.com/rust-lang/rust/pull/119619/commits/26c71cbcf1a9bce6ceb962d753c467d098f63cf6]
5
6Signed-off-by: onur-ozkan <work@onurozkan.dev>
7Signed-off-by: Yash Shinde <Yash.Shinde@windriver.com>
8---
9diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs
10index e85f6319936..c45c0b3c652 100644
11--- a/src/tools/compiletest/src/common.rs
12+++ b/src/tools/compiletest/src/common.rs
13@@ -479,6 +479,7 @@ fn new(config: &Config) -> TargetCfgs {
14         let mut targets: HashMap<String, TargetCfg> = serde_json::from_str(&rustc_output(
15             config,
16             &["--print=all-target-specs-json", "-Zunstable-options"],
17+            Default::default(),
18         ))
19         .unwrap();
20
21@@ -491,16 +492,33 @@ fn new(config: &Config) -> TargetCfgs {
22         let mut all_families = HashSet::new();
23         let mut all_pointer_widths = HashSet::new();
24
25-        // Handle custom target specs, which are not included in `--print=all-target-specs-json`.
26-        if config.target.ends_with(".json") {
27-            targets.insert(
28-                config.target.clone(),
29-                serde_json::from_str(&rustc_output(
30-                    config,
31-                    &["--print=target-spec-json", "-Zunstable-options", "--target", &config.target],
32-                ))
33-                .unwrap(),
34-            );
35+        // If current target is not included in the `--print=all-target-specs-json` output,
36+        // we check whether it is a custom target from the user or a synthetic target from bootstrap.
37+        if !targets.contains_key(&config.target) {
38+            let mut envs: HashMap<String, String> = HashMap::new();
39+
40+            if let Ok(t) = std::env::var("RUST_TARGET_PATH") {
41+                envs.insert("RUST_TARGET_PATH".into(), t);
42+            }
43+
44+            // This returns false only when the target is neither a synthetic target
45+            // nor a custom target from the user, indicating it is most likely invalid.
46+            if config.target.ends_with(".json") || !envs.is_empty() {
47+                targets.insert(
48+                    config.target.clone(),
49+                    serde_json::from_str(&rustc_output(
50+                        config,
51+                        &[
52+                            "--print=target-spec-json",
53+                            "-Zunstable-options",
54+                            "--target",
55+                            &config.target,
56+                        ],
57+                        envs,
58+                    ))
59+                    .unwrap(),
60+                );
61+            }
62         }
63
64         for (target, cfg) in targets.iter() {
65@@ -545,7 +563,9 @@ fn get_current_target_config(
66         // code below extracts them from `--print=cfg`: make sure to only override fields that can
67         // actually be changed with `-C` flags.
68         for config in
69-            rustc_output(config, &["--print=cfg", "--target", &config.target]).trim().lines()
70+            rustc_output(config, &["--print=cfg", "--target", &config.target], Default::default())
71+                .trim()
72+                .lines()
73         {
74             let (name, value) = config
75                 .split_once("=\"")
76@@ -624,11 +644,12 @@ pub enum Endian {
77     Big,
78 }
79
80-fn rustc_output(config: &Config, args: &[&str]) -> String {
81+fn rustc_output(config: &Config, args: &[&str], envs: HashMap<String, String>) -> String {
82     let mut command = Command::new(&config.rustc_path);
83     add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
84     command.args(&config.target_rustcflags).args(args);
85     command.env("RUSTC_BOOTSTRAP", "1");
86+    command.envs(envs);
87
88     let output = match command.output() {
89         Ok(output) => output,
90
91