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

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