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

2024-02-07 Thread Martin Storsjö via cfe-commits

mstorsjo wrote:

I would, generally, prefer to not hardcode `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR` 
(which only affects how runtimes are installed) into Clang. Runtimes may or may 
not be built at the same time as Clang, and one build of Clang can be used for 
a multitude of targets with different setups. I wrote a more lengthy response 
on discourse, see 
https://discourse.llvm.org/t/runtime-directory-fallback/76860/7.

https://github.com/llvm/llvm-project/pull/81037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-02-07 Thread Fangrui Song via cfe-commits

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

>From e276cf4e52fd6328860bb31d73d2bb1b6a11c45d Mon Sep 17 00:00:00 2001
From: Fangrui Song 
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   | 11 ---
 .../lib/x86_64-unknown-linux/clang_rt.crtbegin.o |  0
 .../lib/x86_64-unknown-linux/clang_rt.crtend.o   |  0
 clang/test/Driver/linux-ld.c | 16 
 llvm/include/llvm/Config/llvm-config.h.cmake |  2 ++
 5 files changed, 18 insertions(+), 11 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/clang_rt.crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/clang_rt.crtend.o

diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 388030592b483..25031fa6f0c80 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -655,19 +655,24 @@ std::string ToolChain::getCompilerRT(const ArgList , 
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  : getLibraryPaths()) {
 SmallString<128> P(LibPath);
 llvm::sys::path::append(P, CRTBasename);
 if (getVFS().exists(P))
   return std::string(P);
+if (Path.empty())
+  Path = P;
   }
 
-  // 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/clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/clang_rt.crtbegin.o
 
b/clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/clang_rt.crtbegin.o
new file mode 100644
index 0..e69de29bb2d1d
diff --git 
a/clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/clang_rt.crtend.o
 
b/clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/clang_rt.crtend.o
new file mode 100644
index 0..e69de29bb2d1d
diff --git a/clang/test/Driver/linux-ld.c b/clang/test/Driver/linux-ld.c
index b8efd64cd91f0..9da3952539a68 100644
--- a/clang/test/Driver/linux-ld.c
+++ b/clang/test/Driver/linux-ld.c
@@ -61,15 +61,15 @@
 // CHECK-LD-RT: "--eh-frame-hdr"
 // CHECK-LD-RT: "-m" "elf_x86_64"
 // CHECK-LD-RT: "-dynamic-linker"
-// CHECK-LD-RT: 
"[[RESDIR]]{{/|}}lib{{/|}}linux{{/|}}clang_rt.crtbegin-x86_64.o"
+// CHECK-LD-RT: 
"[[RESDIR]]{{/|}}lib{{/|}}x86_64-unknown-linux{{/|}}clang_rt.crtbegin.o"
 // CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/10.2.0"
 // CHECK-LD-RT: 
"-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/10.2.0/../../../../x86_64-unknown-linux/lib"
 // CHECK-LD-RT: "-L[[SYSROOT]]/lib"
 // CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib"
-// CHECK-LD-RT: libclang_rt.builtins-x86_64.a"
+// CHECK-LD-RT: libclang_rt.builtins.a"
 // CHECK-LD-RT: "-lc"
-// CHECK-LD-RT: libclang_rt.builtins-x86_64.a"
-// CHECK-LD-RT: 
"[[RESDIR]]{{/|}}lib{{/|}}linux{{/|}}clang_rt.crtend-x86_64.o"
+// CHECK-LD-RT: libclang_rt.builtins.a"
+// CHECK-LD-RT: 
"[[RESDIR]]{{/|}}lib{{/|}}x86_64-unknown-linux{{/|}}clang_rt.crtend.o"
 //
 // RUN: %clang -### %s -no-pie 2>&1 \
 // RUN: --target=i686-unknown-linux \
@@ -84,15 +84,15 @@
 // CHECK-LD-RT-I686: "--eh-frame-hdr"
 // CHECK-LD-RT-I686: "-m" "elf_i386"
 // CHECK-LD-RT-I686: "-dynamic-linker"
-// CHECK-LD-RT-I686: 
"[[RESDIR]]{{/|}}lib{{/|}}linux{{/|}}clang_rt.crtbegin-i386.o"
+// CHECK-LD-RT-I686: 

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

2024-02-07 Thread Fangrui Song via cfe-commits

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 
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 , 
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  : 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
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits