Skip to content

Commit 9ec748c

Browse files
Meinersburtru
authored andcommitted
[Flang] Search flang_rt in clang_rt path (llvm#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 llvm#151031 (cherry picked from commit 8de4819)
1 parent 79f3653 commit 9ec748c

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
@@ -837,17 +837,30 @@ void ToolChain::addFortranRuntimeLibs(const ArgList &Args,
837837

838838
void ToolChain::addFortranRuntimeLibraryPath(const llvm::opt::ArgList &Args,
839839
ArgStringList &CmdArgs) const {
840-
// Default to the <driver-path>/../lib directory. This works fine on the
841-
// platforms that we have tested so far. We will probably have to re-fine
842-
// this in the future. In particular, on some platforms, we may need to use
843-
// lib64 instead of lib.
840+
auto AddLibSearchPathIfExists = [&](const Twine &Path) {
841+
// Linker may emit warnings about non-existing directories
842+
if (!llvm::sys::fs::is_directory(Path))
843+
return;
844+
845+
if (getTriple().isKnownWindowsMSVCEnvironment())
846+
CmdArgs.push_back(Args.MakeArgString("-libpath:" + Path));
847+
else
848+
CmdArgs.push_back(Args.MakeArgString("-L" + Path));
849+
};
850+
851+
// Search for flang_rt.* at the same location as clang_rt.* with
852+
// LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=0. On most platforms, flang_rt is
853+
// located at the path returned by getRuntimePath() which is already added to
854+
// the library search path. This exception is for Apple-Darwin.
855+
AddLibSearchPathIfExists(getCompilerRTPath());
856+
857+
// Fall back to the non-resource directory <driver-path>/../lib. We will
858+
// probably have to refine this in the future. In particular, on some
859+
// platforms, we may need to use lib64 instead of lib.
844860
SmallString<256> DefaultLibPath =
845861
llvm::sys::path::parent_path(getDriver().Dir);
846862
llvm::sys::path::append(DefaultLibPath, "lib");
847-
if (getTriple().isKnownWindowsMSVCEnvironment())
848-
CmdArgs.push_back(Args.MakeArgString("-libpath:" + DefaultLibPath));
849-
else
850-
CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath));
863+
AddLibSearchPathIfExists(DefaultLibPath);
851864
}
852865

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

0 commit comments

Comments
 (0)