Skip to content

Commit e276cf4

Browse files
committed
[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
1 parent 9f6c005 commit e276cf4

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

clang/lib/Driver/ToolChain.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,24 @@ std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
655655
// Check for runtime files in the new layout without the architecture first.
656656
std::string CRTBasename =
657657
buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false);
658+
SmallString<128> Path;
658659
for (const auto &LibPath : getLibraryPaths()) {
659660
SmallString<128> P(LibPath);
660661
llvm::sys::path::append(P, CRTBasename);
661662
if (getVFS().exists(P))
662663
return std::string(P);
664+
if (Path.empty())
665+
Path = P;
663666
}
664667

665-
// Fall back to the old expected compiler-rt name if the new one does not
666-
// exist.
668+
#if !LLVM_ENABLE_PER_TARGET_RUNTIME_DIR
669+
// When LLVM_ENABLE_PER_TARGET_RUNTIME_DIR is off, fall back to the old
670+
// expected compiler-rt name if the new one does not exist.
667671
CRTBasename =
668672
buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/true);
669-
SmallString<128> Path(getCompilerRTPath());
673+
Path = getCompilerRTPath();
670674
llvm::sys::path::append(Path, CRTBasename);
675+
#endif
671676
return std::string(Path);
672677
}
673678

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/clang_rt.crtbegin.o

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/clang_rt.crtend.o

Whitespace-only changes.

clang/test/Driver/linux-ld.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@
6161
// CHECK-LD-RT: "--eh-frame-hdr"
6262
// CHECK-LD-RT: "-m" "elf_x86_64"
6363
// CHECK-LD-RT: "-dynamic-linker"
64-
// CHECK-LD-RT: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}clang_rt.crtbegin-x86_64.o"
64+
// CHECK-LD-RT: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-linux{{/|\\\\}}clang_rt.crtbegin.o"
6565
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/10.2.0"
6666
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/10.2.0/../../../../x86_64-unknown-linux/lib"
6767
// CHECK-LD-RT: "-L[[SYSROOT]]/lib"
6868
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib"
69-
// CHECK-LD-RT: libclang_rt.builtins-x86_64.a"
69+
// CHECK-LD-RT: libclang_rt.builtins.a"
7070
// CHECK-LD-RT: "-lc"
71-
// CHECK-LD-RT: libclang_rt.builtins-x86_64.a"
72-
// CHECK-LD-RT: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}clang_rt.crtend-x86_64.o"
71+
// CHECK-LD-RT: libclang_rt.builtins.a"
72+
// CHECK-LD-RT: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-linux{{/|\\\\}}clang_rt.crtend.o"
7373
//
7474
// RUN: %clang -### %s -no-pie 2>&1 \
7575
// RUN: --target=i686-unknown-linux \
@@ -84,15 +84,15 @@
8484
// CHECK-LD-RT-I686: "--eh-frame-hdr"
8585
// CHECK-LD-RT-I686: "-m" "elf_i386"
8686
// CHECK-LD-RT-I686: "-dynamic-linker"
87-
// CHECK-LD-RT-I686: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}clang_rt.crtbegin-i386.o"
87+
// CHECK-LD-RT-I686: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}i686-unknown-linux{{/|\\\\}}clang_rt.crtbegin.o"
8888
// CHECK-LD-RT-I686: "-L[[SYSROOT]]/usr/lib/gcc/i686-unknown-linux/10.2.0"
8989
// CHECK-LD-RT-I686: "-L[[SYSROOT]]/usr/lib/gcc/i686-unknown-linux/10.2.0/../../../../i686-unknown-linux/lib"
9090
// CHECK-LD-RT-I686: "-L[[SYSROOT]]/lib"
9191
// CHECK-LD-RT-I686: "-L[[SYSROOT]]/usr/lib"
92-
// CHECK-LD-RT-I686: libclang_rt.builtins-i386.a"
92+
// CHECK-LD-RT-I686: libclang_rt.builtins.a"
9393
// CHECK-LD-RT-I686: "-lc"
94-
// CHECK-LD-RT-I686: libclang_rt.builtins-i386.a"
95-
// CHECK-LD-RT-I686: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}clang_rt.crtend-i386.o"
94+
// CHECK-LD-RT-I686: libclang_rt.builtins.a"
95+
// CHECK-LD-RT-I686: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}i686-unknown-linux{{/|\\\\}}clang_rt.crtend.o"
9696
//
9797
// RUN: %clang -### %s -no-pie 2>&1 \
9898
// RUN: --target=arm-linux-androideabi \

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
/* Doesn't use `cmakedefine` because it is allowed to be empty. */
2222
#define LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}"
2323

24+
#cmakedefine01 LLVM_ENABLE_PER_TARGET_RUNTIME_DIR
25+
2426
/* Define if threads enabled */
2527
#cmakedefine01 LLVM_ENABLE_THREADS
2628

0 commit comments

Comments
 (0)