Blame SOURCES/0001-compiletest-set-the-dylib-path-when-gathering-target.patch

0cabd1
From 92b0b20e4119241aaeabb4b91189a9fca8ff8b5d Mon Sep 17 00:00:00 2001
0cabd1
From: Josh Stone <jistone@redhat.com>
0cabd1
Date: Fri, 14 Oct 2022 16:11:28 -0700
0cabd1
Subject: [PATCH] compiletest: set the dylib path when gathering target cfg
0cabd1
0cabd1
If the compiler is built with `rpath = false`, then it won't find its
0cabd1
own libraries unless the library search path is set. We already do that
0cabd1
while running the actual compiletests, but #100260 added another rustc
0cabd1
command for getting the target cfg.
0cabd1
0cabd1
    Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
0cabd1
    thread 'main' panicked at 'error: failed to get cfg info from "[...]/build/x86_64-unknown-linux-gnu/stage1/bin/rustc"
0cabd1
    --- stdout
0cabd1
0cabd1
    --- stderr
0cabd1
    [...]/build/x86_64-unknown-linux-gnu/stage1/bin/rustc: error while loading shared libraries: librustc_driver-a2a76dc626cd02d2.so: cannot open shared object file: No such file or directory
0cabd1
    ', src/tools/compiletest/src/common.rs:476:13
0cabd1
0cabd1
Now the library path is set here as well, so it works without rpath.
0cabd1
0cabd1
(cherry picked from commit 97c3608326d123f5462e3504409a3a069611c0fb)
0cabd1
---
0cabd1
 src/tools/compiletest/src/common.rs  | 17 ++++++++++-------
0cabd1
 src/tools/compiletest/src/runtest.rs | 27 +++------------------------
0cabd1
 src/tools/compiletest/src/util.rs    | 23 +++++++++++++++++++++++
0cabd1
 3 files changed, 36 insertions(+), 31 deletions(-)
0cabd1
0cabd1
diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs
0cabd1
index 64df76e27720..53b64e7d1fc3 100644
0cabd1
--- a/src/tools/compiletest/src/common.rs
0cabd1
+++ b/src/tools/compiletest/src/common.rs
0cabd1
@@ -2,11 +2,12 @@
0cabd1
 
0cabd1
 use std::ffi::OsString;
0cabd1
 use std::fmt;
0cabd1
+use std::iter;
0cabd1
 use std::path::{Path, PathBuf};
0cabd1
 use std::process::Command;
0cabd1
 use std::str::FromStr;
0cabd1
 
0cabd1
-use crate::util::PathBufExt;
0cabd1
+use crate::util::{add_dylib_path, PathBufExt};
0cabd1
 use lazycell::LazyCell;
0cabd1
 use test::ColorConfig;
0cabd1
 
0cabd1
@@ -389,7 +390,7 @@ pub fn run_enabled(&self) -> bool {
0cabd1
     }
0cabd1
 
0cabd1
     fn target_cfg(&self) -> &TargetCfg {
0cabd1
-        self.target_cfg.borrow_with(|| TargetCfg::new(&self.rustc_path, &self.target))
0cabd1
+        self.target_cfg.borrow_with(|| TargetCfg::new(self))
0cabd1
     }
0cabd1
 
0cabd1
     pub fn matches_arch(&self, arch: &str) -> bool {
0cabd1
@@ -455,20 +456,22 @@ pub enum Endian {
0cabd1
 }
0cabd1
 
0cabd1
 impl TargetCfg {
0cabd1
-    fn new(rustc_path: &Path, target: &str) -> TargetCfg {
0cabd1
-        let output = match Command::new(rustc_path)
0cabd1
+    fn new(config: &Config) -> TargetCfg {
0cabd1
+        let mut command = Command::new(&config.rustc_path);
0cabd1
+        add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
0cabd1
+        let output = match command
0cabd1
             .arg("--print=cfg")
0cabd1
             .arg("--target")
0cabd1
-            .arg(target)
0cabd1
+            .arg(&config.target)
0cabd1
             .output()
0cabd1
         {
0cabd1
             Ok(output) => output,
0cabd1
-            Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", rustc_path),
0cabd1
+            Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", config.rustc_path),
0cabd1
         };
0cabd1
         if !output.status.success() {
0cabd1
             panic!(
0cabd1
                 "error: failed to get cfg info from {:?}\n--- stdout\n{}\n--- stderr\n{}",
0cabd1
-                rustc_path,
0cabd1
+                config.rustc_path,
0cabd1
                 String::from_utf8(output.stdout).unwrap(),
0cabd1
                 String::from_utf8(output.stderr).unwrap(),
0cabd1
             );
0cabd1
diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs
0cabd1
index 8f289876f730..eb467170249d 100644
0cabd1
--- a/src/tools/compiletest/src/runtest.rs
0cabd1
+++ b/src/tools/compiletest/src/runtest.rs
0cabd1
@@ -13,7 +13,7 @@
0cabd1
 use crate::header::TestProps;
0cabd1
 use crate::json;
0cabd1
 use crate::read2::read2_abbreviated;
0cabd1
-use crate::util::{logv, PathBufExt};
0cabd1
+use crate::util::{add_dylib_path, dylib_env_var, logv, PathBufExt};
0cabd1
 use crate::ColorConfig;
0cabd1
 use regex::{Captures, Regex};
0cabd1
 use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};
0cabd1
@@ -26,6 +26,7 @@
0cabd1
 use std::hash::{Hash, Hasher};
0cabd1
 use std::io::prelude::*;
0cabd1
 use std::io::{self, BufReader};
0cabd1
+use std::iter;
0cabd1
 use std::path::{Path, PathBuf};
0cabd1
 use std::process::{Child, Command, ExitStatus, Output, Stdio};
0cabd1
 use std::str;
0cabd1
@@ -72,19 +73,6 @@ fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
0cabd1
     f()
0cabd1
 }
0cabd1
 
0cabd1
-/// The name of the environment variable that holds dynamic library locations.
0cabd1
-pub fn dylib_env_var() -> &'static str {
0cabd1
-    if cfg!(windows) {
0cabd1
-        "PATH"
0cabd1
-    } else if cfg!(target_os = "macos") {
0cabd1
-        "DYLD_LIBRARY_PATH"
0cabd1
-    } else if cfg!(target_os = "haiku") {
0cabd1
-        "LIBRARY_PATH"
0cabd1
-    } else {
0cabd1
-        "LD_LIBRARY_PATH"
0cabd1
-    }
0cabd1
-}
0cabd1
-
0cabd1
 /// The platform-specific library name
0cabd1
 pub fn get_lib_name(lib: &str, dylib: bool) -> String {
0cabd1
     // In some casess (e.g. MUSL), we build a static
0cabd1
@@ -1826,16 +1814,7 @@ fn compose_and_run(
0cabd1
 
0cabd1
         // Need to be sure to put both the lib_path and the aux path in the dylib
0cabd1
         // search path for the child.
0cabd1
-        let mut path =
0cabd1
-            env::split_paths(&env::var_os(dylib_env_var()).unwrap_or_default()).collect::<Vec<_>>();
0cabd1
-        if let Some(p) = aux_path {
0cabd1
-            path.insert(0, PathBuf::from(p))
0cabd1
-        }
0cabd1
-        path.insert(0, PathBuf::from(lib_path));
0cabd1
-
0cabd1
-        // Add the new dylib search path var
0cabd1
-        let newpath = env::join_paths(&path).unwrap();
0cabd1
-        command.env(dylib_env_var(), newpath);
0cabd1
+        add_dylib_path(&mut command, iter::once(lib_path).chain(aux_path));
0cabd1
 
0cabd1
         let mut child = disable_error_reporting(|| command.spawn())
0cabd1
             .unwrap_or_else(|_| panic!("failed to exec `{:?}`", &command));
0cabd1
diff --git a/src/tools/compiletest/src/util.rs b/src/tools/compiletest/src/util.rs
0cabd1
index 9d047b63c859..4b73be0fbb90 100644
0cabd1
--- a/src/tools/compiletest/src/util.rs
0cabd1
+++ b/src/tools/compiletest/src/util.rs
0cabd1
@@ -2,6 +2,7 @@
0cabd1
 use std::env;
0cabd1
 use std::ffi::OsStr;
0cabd1
 use std::path::PathBuf;
0cabd1
+use std::process::Command;
0cabd1
 
0cabd1
 use tracing::*;
0cabd1
 
0cabd1
@@ -105,3 +106,25 @@ fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
0cabd1
         }
0cabd1
     }
0cabd1
 }
0cabd1
+
0cabd1
+/// The name of the environment variable that holds dynamic library locations.
0cabd1
+pub fn dylib_env_var() -> &'static str {
0cabd1
+    if cfg!(windows) {
0cabd1
+        "PATH"
0cabd1
+    } else if cfg!(target_os = "macos") {
0cabd1
+        "DYLD_LIBRARY_PATH"
0cabd1
+    } else if cfg!(target_os = "haiku") {
0cabd1
+        "LIBRARY_PATH"
0cabd1
+    } else {
0cabd1
+        "LD_LIBRARY_PATH"
0cabd1
+    }
0cabd1
+}
0cabd1
+
0cabd1
+/// Adds a list of lookup paths to `cmd`'s dynamic library lookup path.
0cabd1
+/// If the dylib_path_var is already set for this cmd, the old value will be overwritten!
0cabd1
+pub fn add_dylib_path(cmd: &mut Command, paths: impl Iterator<Item = impl Into<PathBuf>>) {
0cabd1
+    let path_env = env::var_os(dylib_env_var());
0cabd1
+    let old_paths = path_env.as_ref().map(env::split_paths);
0cabd1
+    let new_paths = paths.map(Into::into).chain(old_paths.into_iter().flatten());
0cabd1
+    cmd.env(dylib_env_var(), env::join_paths(new_paths).unwrap());
0cabd1
+}
0cabd1
-- 
0cabd1
2.37.3
0cabd1