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