Blob Blame History Raw
From 27593a7ad3796cf3afaf4145275ff20d5aff333f Mon Sep 17 00:00:00 2001
From: Josh Stone <jistone@redhat.com>
Date: Wed, 10 Apr 2024 17:25:10 -0700
Subject: [PATCH 1/2] Set the host library path in run-make v2

When the build is configured with `[rust] rpath = false`, we need to set
`LD_LIBRARY_PATH` (or equivalent) to what would have been the `RPATH`,
so the compiler can find its own libraries. The old `tools.mk` code has
this environment prefixed in the `$(BARE_RUSTC)` variable, so we just
need to wire up something similar for run-make v2.

This is now set while building each `rmake.rs` itself, as well as in the
`rust-make-support` helpers for `rustc` and `rustdoc` commands. This is
also available in a `set_host_rpath` function for manual commands, like
in the `compiler-builtins` test.

(cherry picked from commit 8a5409bbdbadb522f25e7e5e3d54b967cb5eee56)
---
 src/tools/compiletest/src/runtest.rs  |  6 ++++++
 src/tools/run-make-support/src/lib.rs | 15 +++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs
index 7be0571b1111..3c775ea0651c 100644
--- a/src/tools/compiletest/src/runtest.rs
+++ b/src/tools/compiletest/src/runtest.rs
@@ -3767,6 +3767,11 @@ fn run_rmake_v2_test(&self) {
         debug!(?support_lib_deps);
         debug!(?support_lib_deps_deps);
 
+        let mut host_dylib_env_paths = String::new();
+        host_dylib_env_paths.push_str(&cwd.join(&self.config.compile_lib_path).to_string_lossy());
+        host_dylib_env_paths.push(':');
+        host_dylib_env_paths.push_str(&env::var(dylib_env_var()).unwrap());
+
         let res = self.cmd2procres(
             Command::new(&self.config.rustc_path)
                 .arg("-o")
@@ -3787,6 +3792,7 @@ fn run_rmake_v2_test(&self) {
                 .env("RUSTC", cwd.join(&self.config.rustc_path))
                 .env("TMPDIR", &tmpdir)
                 .env("LD_LIB_PATH_ENVVAR", dylib_env_var())
+                .env(dylib_env_var(), &host_dylib_env_paths)
                 .env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
                 .env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
                 .env("LLVM_COMPONENTS", &self.config.llvm_components)
diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs
index 674860f8413b..da3efd292b3f 100644
--- a/src/tools/run-make-support/src/lib.rs
+++ b/src/tools/run-make-support/src/lib.rs
@@ -11,6 +11,7 @@ pub fn out_dir() -> PathBuf {
 fn setup_common_build_cmd() -> Command {
     let rustc = env::var("RUSTC").unwrap();
     let mut cmd = Command::new(rustc);
+    set_host_rpath(&mut cmd);
     cmd.arg("--out-dir").arg(out_dir()).arg("-L").arg(out_dir());
     cmd
 }
@@ -157,3 +158,17 @@ pub fn run_fail(bin_name: &str) -> Output {
     }
     output
 }
+
+/// Set the runtime library path as needed for running the host rustc/rustdoc/etc.
+pub fn set_host_rpath(cmd: &mut Command) {
+    let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap();
+    cmd.env(&ld_lib_path_envvar, {
+        let mut paths = vec![];
+        paths.push(PathBuf::from(env::var("TMPDIR").unwrap()));
+        paths.push(PathBuf::from(env::var("HOST_RPATH_DIR").unwrap()));
+        for p in env::split_paths(&env::var(&ld_lib_path_envvar).unwrap()) {
+            paths.push(p.to_path_buf());
+        }
+        env::join_paths(paths.iter()).unwrap()
+    });
+}
-- 
2.44.0