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