Skip to content

Commit 8de4819

Browse files
authored
[Flang] Search flang_rt in clang_rt path (#151954)
The clang/flang driver has two separate systems for find the location of clang_rt (simplified): * `getCompilerRTPath()`, e.g. `../lib/clang/22/lib/windows`, used when `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=0` * `getRuntimePath()`, e.g. `../lib/clang/22/lib/x86_64-pc-windows-msvc`, used when `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=1` To simplify the search path, Flang-RT normally assumes only `getRuntimePath()`, i.e. ignoring `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR` and always using the `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=1` mechanism. There is an exception for Apple Darwin triples where `getRuntimePath()` returns nothing. The flang-rt/compiler-rt CMake code for library location also ignores `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR` but uses the `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=0` path instead. Since only `getRuntimePath()` is automatically added to the linker command line, this patch explicitly adds `getCompilerRTPath()` to the path when linking flang_rt. Fixes #151031
1 parent df0325a commit 8de4819

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

clang/lib/Driver/ToolChain.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -855,17 +855,30 @@ void ToolChain::addFortranRuntimeLibs(const ArgList &Args,
855855

856856
void ToolChain::addFortranRuntimeLibraryPath(const llvm::opt::ArgList &Args,
857857
ArgStringList &CmdArgs) const {
858-
// Default to the <driver-path>/../lib directory. This works fine on the
859-
// platforms that we have tested so far. We will probably have to re-fine
860-
// this in the future. In particular, on some platforms, we may need to use
861-
// lib64 instead of lib.
858+
auto AddLibSearchPathIfExists = [&](const Twine &Path) {
859+
// Linker may emit warnings about non-existing directories
860+
if (!llvm::sys::fs::is_directory(Path))
861+
return;
862+
863+
if (getTriple().isKnownWindowsMSVCEnvironment())
864+
CmdArgs.push_back(Args.MakeArgString("-libpath:" + Path));
865+
else
866+
CmdArgs.push_back(Args.MakeArgString("-L" + Path));
867+
};
868+
869+
// Search for flang_rt.* at the same location as clang_rt.* with
870+
// LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=0. On most platforms, flang_rt is
871+
// located at the path returned by getRuntimePath() which is already added to
872+
// the library search path. This exception is for Apple-Darwin.
873+
AddLibSearchPathIfExists(getCompilerRTPath());
874+
875+
// Fall back to the non-resource directory <driver-path>/../lib. We will
876+
// probably have to refine this in the future. In particular, on some
877+
// platforms, we may need to use lib64 instead of lib.
862878
SmallString<256> DefaultLibPath =
863879
llvm::sys::path::parent_path(getDriver().Dir);
864880
llvm::sys::path::append(DefaultLibPath, "lib");
865-
if (getTriple().isKnownWindowsMSVCEnvironment())
866-
CmdArgs.push_back(Args.MakeArgString("-libpath:" + DefaultLibPath));
867-
else
868-
CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath));
881+
AddLibSearchPathIfExists(DefaultLibPath);
869882
}
870883

871884
void ToolChain::addFlangRTLibPath(const ArgList &Args,

0 commit comments

Comments
 (0)