[clang] [Clang] Make '-frtlib-add-rpath' include the standard library directory (PR #86217)

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 21 16:30:53 PDT 2024


https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/86217

Summary:
The original intention of the `openmp-add-rpath` option was to add the
rpath to the language runtime directory. However, the current
implementation only adds it to the compiler's resource directory. This
patch adds support for appending the `-rpath` to the compiler's standard
library directory as well. Currently this is `<exe>/../lib/<triple>`.


>From 6bd078cb080903127a8dfa27fb978350e94b8375 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Thu, 21 Mar 2024 18:25:35 -0500
Subject: [PATCH] [Clang] Make '-frtlib-add-rpath' include the standard library
 directory

Summary:
The original intention of the `openmp-add-rpath` option was to add the
rpath to the language runtime directory. However, the current
implementation only adds it to the compiler's resource directory. This
patch adds support for appending the `-rpath` to the compiler's standard
library directory as well. Currently this is `<exe>/../lib/<triple>`.
---
 clang/lib/Driver/ToolChains/CommonArgs.cpp     |  6 +++++-
 clang/test/Driver/arch-specific-libdir-rpath.c | 13 +++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 4478865313636d..6b1fbba7abd031 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1142,7 +1142,11 @@ void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,
                     options::OPT_fno_rtlib_add_rpath, false))
     return;
 
-  for (const auto &CandidateRPath : TC.getArchSpecificLibPaths()) {
+  SmallVector<std::string> CandidateRPaths(TC.getArchSpecificLibPaths());
+  if (const auto CandidateRPath = TC.getStdlibPath())
+    CandidateRPaths.emplace_back(*CandidateRPath);
+
+  for (const auto &CandidateRPath : CandidateRPaths) {
     if (TC.getVFS().exists(CandidateRPath)) {
       CmdArgs.push_back("-rpath");
       CmdArgs.push_back(Args.MakeArgString(CandidateRPath));
diff --git a/clang/test/Driver/arch-specific-libdir-rpath.c b/clang/test/Driver/arch-specific-libdir-rpath.c
index 1e6bbbc5929ac2..e95fb21c0a5fb1 100644
--- a/clang/test/Driver/arch-specific-libdir-rpath.c
+++ b/clang/test/Driver/arch-specific-libdir-rpath.c
@@ -84,6 +84,15 @@
 // RUN:     -frtlib-add-rpath \
 // RUN:   | FileCheck --check-prefixes=PERTARGET %s
 
+// Test that the driver adds an per-target arch-specific subdirectory to the
+// stdlib path.
+//
+// RUN: %clang %s -### 2>&1 --target=x86_64-linux-gnu \
+// RUN:     -fsanitize=address -shared-libasan \
+// RUN:     -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
+// RUN:     -frtlib-add-rpath \
+// RUN:   | FileCheck --check-prefixes=STDLIB %s
+
 // RESDIR: "-resource-dir" "[[RESDIR:[^"]*]]"
 //
 // LIBPATH-X86_64: -L[[RESDIR]]{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}
@@ -101,3 +110,7 @@
 // PERTARGET: "-resource-dir" "[[PTRESDIR:[^"]*]]"
 // PERTARGET: -L[[PTRESDIR]]{{(/|\\\\)lib(/|\\\\)x86_64-unknown-linux-gnu}}
 // PERTARGET:   "-rpath" "[[PTRESDIR]]{{(/|\\\\)lib(/|\\\\)x86_64-unknown-linux-gnu}}"
+
+// STDLIB: InstalledDir: [[LIBDIR:.+$]]
+// STDLIB: -L[[LIBDIR]]/..{{(/|\\\\)lib(/|\\\\)x86_64-unknown-linux-gnu}}
+// STDLIB:   "-rpath" "[[LIBDIR]]/..{{(/|\\\\)lib(/|\\\\)x86_64-unknown-linux-gnu}}"



More information about the cfe-commits mailing list