From 236c0272667d69380d03984cfa8bad44b033fd69 Mon Sep 17 00:00:00 2001
From: Daniel Kolesa <daniel@octaforge.org>
Date: Tue, 21 Dec 2021 02:46:30 +0100
Subject: [PATCH 13/15] allow specifying native sysroot to use for linkage

This allows us to get around the linker attempting to use
incompatible libs.
---
 src/bootstrap/builder.rs | 4 ++++
 src/bootstrap/config.rs  | 3 +++
 src/bootstrap/lib.rs     | 4 ++++
 3 files changed, 11 insertions(+)

diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs
index 6a02a225c..483aa8818 100644
--- a/src/bootstrap/builder.rs
+++ b/src/bootstrap/builder.rs
@@ -1708,6 +1708,10 @@ impl<'a> Builder<'a> {
             }
         }
 
+        if let Some(sysroot) = self.native_sysroot(target) {
+            rustflags.arg(&format!("-Clink-args=--sysroot={}", sysroot.display()));
+        }
+
         if let Some(host_linker) = self.linker(compiler.host) {
             cargo.env("RUSTC_HOST_LINKER", host_linker);
         }
diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs
index 18a764689..f8b9bbd96 100644
--- a/src/bootstrap/config.rs
+++ b/src/bootstrap/config.rs
@@ -395,6 +395,7 @@ pub struct Target {
     pub ranlib: Option<PathBuf>,
     pub default_linker: Option<PathBuf>,
     pub linker: Option<PathBuf>,
+    pub sysroot: Option<PathBuf>,
     pub ndk: Option<PathBuf>,
     pub sanitizers: Option<bool>,
     pub profiler: Option<bool>,
@@ -723,6 +724,7 @@ define_config! {
         ranlib: Option<String> = "ranlib",
         default_linker: Option<PathBuf> = "default-linker",
         linker: Option<String> = "linker",
+        sysroot: Option<String> = "sysroot",
         llvm_config: Option<String> = "llvm-config",
         llvm_filecheck: Option<String> = "llvm-filecheck",
         llvm_libunwind: Option<String> = "llvm-libunwind",
@@ -1156,6 +1158,7 @@ impl Config {
                 target.ar = cfg.ar.map(PathBuf::from);
                 target.ranlib = cfg.ranlib.map(PathBuf::from);
                 target.linker = cfg.linker.map(PathBuf::from);
+                target.sysroot = cfg.sysroot.map(PathBuf::from);
                 target.crt_static = cfg.crt_static;
                 target.wasi_root = cfg.wasi_root.map(PathBuf::from);
                 target.qemu_rootfs = cfg.qemu_rootfs.map(PathBuf::from);
diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs
index 0a7012dbd..cded50d20 100644
--- a/src/bootstrap/lib.rs
+++ b/src/bootstrap/lib.rs
@@ -1152,6 +1152,10 @@ impl Build {
             self.config.target_config.get(&target).and_then(|t| t.crt_static)
         }
     }
+    
+    fn native_sysroot(&self, target: TargetSelection) -> Option<&Path> {
+        self.config.target_config.get(&target).and_then(|c| c.sysroot.as_ref()).map(|p| &**p)
+    }
 
     /// Returns the sysroot for the wasi target, if defined
     fn wasi_root(&self, target: TargetSelection) -> Option<&Path> {
-- 
2.37.2

