Blame SOURCES/0001-Try-to-get-the-target-triple-from-rustc-itself.patch

c3f84f
From 72cd8aedc2901d6a6b598eadc001cc39040ae487 Mon Sep 17 00:00:00 2001
c3f84f
From: Josh Stone <jistone@redhat.com>
c3f84f
Date: Wed, 12 Dec 2018 16:51:31 -0800
c3f84f
Subject: [PATCH] Try to get the target triple from rustc itself
c3f84f
c3f84f
The prior method was trying to pick the triple out of the sysroot path.
c3f84f
A FIXME comment already notes that this doesn't work with custom
c3f84f
toolchains in rustup. It also fails with distro-installed toolchains,
c3f84f
where the sysroot may simply be `/usr`.
c3f84f
c3f84f
The output of `rustc -Vv` is a more reliable source, as it contains a
c3f84f
line like `host: x86_64-unknown-linux-gnu`.  This should be enough to
c3f84f
identify the triple for any `rustc`, but just in case, the path-based
c3f84f
code is kept as a fallback.
c3f84f
---
c3f84f
 src/loader.rs | 41 ++++++++++++++++++++++++++++++++++++++---
c3f84f
 1 file changed, 38 insertions(+), 3 deletions(-)
c3f84f
c3f84f
diff --git a/src/loader.rs b/src/loader.rs
c3f84f
index 645c95139164..fe92bef1c596 100644
c3f84f
--- a/src/loader.rs
c3f84f
+++ b/src/loader.rs
c3f84f
@@ -108,9 +108,33 @@ impl AnalysisLoader for CargoAnalysisLoader {
c3f84f
     }
c3f84f
 }
c3f84f
 
c3f84f
+fn extract_target_triple(sys_root_path: &Path) -> String {
c3f84f
+    // First try to get the triple from the rustc version output,
c3f84f
+    // otherwise fall back on the rustup-style toolchain path.
c3f84f
+    extract_rustc_host_triple()
c3f84f
+        .unwrap_or_else(|| extract_rustup_target_triple(sys_root_path))
c3f84f
+}
c3f84f
+
c3f84f
+fn extract_rustc_host_triple() -> Option<String> {
c3f84f
+    let rustc = env::var("RUSTC").unwrap_or(String::from("rustc"));
c3f84f
+    let verbose_version = Command::new(rustc)
c3f84f
+        .arg("--verbose")
c3f84f
+        .arg("--version")
c3f84f
+        .output()
c3f84f
+        .ok()
c3f84f
+        .and_then(|out| String::from_utf8(out.stdout).ok())?;
c3f84f
+
c3f84f
+    // Extracts the triple from a line like `host: x86_64-unknown-linux-gnu`
c3f84f
+    verbose_version
c3f84f
+        .lines()
c3f84f
+        .find(|line| line.starts_with("host: "))
c3f84f
+        .and_then(|host| host.split_whitespace().nth(1))
c3f84f
+        .map(String::from)
c3f84f
+}
c3f84f
+
c3f84f
 // FIXME: This can fail when using a custom toolchain in rustup (often linked to
c3f84f
 // `/$rust_repo/build/$target/stage2`)
c3f84f
-fn extract_target_triple(sys_root_path: &Path) -> String {
c3f84f
+fn extract_rustup_target_triple(sys_root_path: &Path) -> String {
c3f84f
     // Extracts nightly-x86_64-pc-windows-msvc from
c3f84f
     // $HOME/.rustup/toolchains/nightly-x86_64-pc-windows-msvc
c3f84f
     let toolchain = sys_root_path
c3f84f
@@ -169,7 +193,7 @@ mod tests {
c3f84f
             r#"C:\Users\user\.rustup\toolchains\nightly-x86_64-pc-windows-msvc"#,
c3f84f
         );
c3f84f
         assert_eq!(
c3f84f
-            extract_target_triple(path),
c3f84f
+            extract_rustup_target_triple(path),
c3f84f
             String::from("x86_64-pc-windows-msvc")
c3f84f
         );
c3f84f
     }
c3f84f
@@ -180,8 +204,19 @@ mod tests {
c3f84f
             "/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu",
c3f84f
         );
c3f84f
         assert_eq!(
c3f84f
-            extract_target_triple(path),
c3f84f
+            extract_rustup_target_triple(path),
c3f84f
             String::from("x86_64-unknown-linux-gnu")
c3f84f
         );
c3f84f
     }
c3f84f
+
c3f84f
+    #[test]
c3f84f
+    fn target_triple() {
c3f84f
+        let sys_root_path = sys_root_path();
c3f84f
+        let target_triple = extract_target_triple(&sys_root_path);
c3f84f
+        let target_path = sys_root_path
c3f84f
+            .join("lib")
c3f84f
+            .join("rustlib")
c3f84f
+            .join(&target_triple);
c3f84f
+        assert!(target_path.is_dir(), "{:?} is not a directory!", target_path);
c3f84f
+    }
c3f84f
 }
c3f84f
-- 
c3f84f
2.19.2
c3f84f