[clang] [llvm] [Driver] Improve error when a compiler-rt library is not found (PR #81037)

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 7 13:01:42 PST 2024


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/81037

BSD/Linux/OS390x enable `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR` by default.
When a compiler-rt library is not found, we currently report an
incorrect filename `libclang_rt.XXX-$arch.a`
```
% /tmp/Debug/bin/clang++ a.cc -fsanitize=address -o a
ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/linux/libclang_rt.asan-x86_64.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

With this change, we will correctly report:
```
% /tmp/Debug/bin/clang++ a.cc -fsanitize=address -o a
ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/x86_64-unknown-linux-gnu/libclang_rt.asan.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

Link: https://discourse.llvm.org/t/runtime-directory-fallback/76860


>From c3167fe2b6cdebb7bede5aa4e09874c34727987a Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Wed, 7 Feb 2024 12:56:05 -0800
Subject: [PATCH] [Driver] Improve error when a compiler-rt library is not
 found

BSD/Linux/OS390x enable `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR` by default.
When a compiler-rt library is not found, we currently report an
incorrect filename `libclang_rt.XXX-$arch.a`
```
% /tmp/Debug/bin/clang++ a.cc -fsanitize=address -o a
ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/linux/libclang_rt.asan-x86_64.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

With this change, we will correctly report:
```
% /tmp/Debug/bin/clang++ a.cc -fsanitize=address -o a
ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/x86_64-unknown-linux-gnu/libclang_rt.asan.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

Link: https://discourse.llvm.org/t/runtime-directory-fallback/76860
---
 clang/lib/Driver/ToolChain.cpp               | 17 ++++++++++-------
 llvm/include/llvm/Config/llvm-config.h.cmake |  2 ++
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 388030592b483..0572e4073a446 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -655,19 +655,22 @@ std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
   // Check for runtime files in the new layout without the architecture first.
   std::string CRTBasename =
       buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false);
+  SmallString<128> Path;
   for (const auto &LibPath : getLibraryPaths()) {
-    SmallString<128> P(LibPath);
-    llvm::sys::path::append(P, CRTBasename);
-    if (getVFS().exists(P))
-      return std::string(P);
+    Path = LibPath;
+    llvm::sys::path::append(Path, CRTBasename);
+    if (getVFS().exists(Path))
+      return std::string(Path);
   }
 
-  // Fall back to the old expected compiler-rt name if the new one does not
-  // exist.
+#if !LLVM_ENABLE_PER_TARGET_RUNTIME_DIR
+  // When LLVM_ENABLE_PER_TARGET_RUNTIME_DIR is off, fall back to the old
+  // expected compiler-rt name if the new one does not exist.
   CRTBasename =
       buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/true);
-  SmallString<128> Path(getCompilerRTPath());
+  Path = getCompilerRTPath();
   llvm::sys::path::append(Path, CRTBasename);
+#endif
   return std::string(Path);
 }
 
diff --git a/llvm/include/llvm/Config/llvm-config.h.cmake b/llvm/include/llvm/Config/llvm-config.h.cmake
index 6605ea60df99e..ec2b86e859cbf 100644
--- a/llvm/include/llvm/Config/llvm-config.h.cmake
+++ b/llvm/include/llvm/Config/llvm-config.h.cmake
@@ -21,6 +21,8 @@
 /* Doesn't use `cmakedefine` because it is allowed to be empty. */
 #define LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}"
 
+#cmakedefine01 LLVM_ENABLE_PER_TARGET_RUNTIME_DIR
+
 /* Define if threads enabled */
 #cmakedefine01 LLVM_ENABLE_THREADS
 



More information about the cfe-commits mailing list