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