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