Skip to content

Commit af744f0

Browse files
committed
[LLD][COFF] Add LLVM toolchain library paths by default.
We want lld-link to automatically find compiler-rt's and libc++ when it's in the same directory as the rest of the toolchain. This is because on Windows linking isn't done via the clang driver - but instead invoked directly. This prepends: <llvm>/lib <llvm>/lib/clang/XX/lib and <llvm>/lib/clang/XX/lib/windows automatically to the library search paths. Related to #63827 Differential Revision: https://reviews.llvm.org/D151188
1 parent dc2b2ae commit af744f0

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ std::string Driver::GetResourcesPath(StringRef BinaryPath,
184184
// path of the embedding binary, which for LLVM binaries will be in bin/.
185185
// ../lib gets us to lib/ in both cases.
186186
P = llvm::sys::path::parent_path(Dir);
187+
// This search path is also created in the COFF driver of lld, so any
188+
// changes here also needs to happen in lld/COFF/Driver.cpp
187189
llvm::sys::path::append(P, CLANG_INSTALL_LIBDIR_BASENAME, "clang",
188190
CLANG_VERSION_MAJOR_STRING);
189191
}

lld/COFF/Driver.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,31 @@ void LinkerDriver::detectWinSysRoot(const opt::InputArgList &Args) {
637637
}
638638
}
639639

640+
void LinkerDriver::addClangLibSearchPaths(const std::string &argv0) {
641+
std::string lldBinary = sys::fs::getMainExecutable(argv0.c_str(), nullptr);
642+
SmallString<128> binDir(lldBinary);
643+
sys::path::remove_filename(binDir); // remove lld-link.exe
644+
StringRef rootDir = sys::path::parent_path(binDir); // remove 'bin'
645+
646+
SmallString<128> libDir(rootDir);
647+
sys::path::append(libDir, "lib");
648+
// We need to prepend the paths here in order to make sure that we always
649+
// try to link the clang versions of the builtins over the ones supplied by MSVC.
650+
searchPaths.insert(searchPaths.begin(), saver().save(libDir.str()));
651+
652+
// Add the resource dir library path
653+
SmallString<128> runtimeLibDir(rootDir);
654+
sys::path::append(runtimeLibDir, "lib", "clang", std::to_string(LLVM_VERSION_MAJOR), "lib");
655+
searchPaths.insert(searchPaths.begin(), saver().save(runtimeLibDir.str()));
656+
657+
// Resource dir + osname, which is hardcoded to windows since we are in the
658+
// COFF driver.
659+
SmallString<128> runtimeLibDirWithOS(runtimeLibDir);
660+
sys::path::append(runtimeLibDirWithOS, "windows");
661+
searchPaths.insert(searchPaths.begin(), saver().save(runtimeLibDirWithOS.str()));
662+
663+
}
664+
640665
void LinkerDriver::addWinSysRootLibSearchPaths() {
641666
if (!diaPath.empty()) {
642667
// The DIA SDK always uses the legacy vc arch, even in new MSVC versions.
@@ -1543,6 +1568,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
15431568
detectWinSysRoot(args);
15441569
if (!args.hasArg(OPT_lldignoreenv) && !args.hasArg(OPT_winsysroot))
15451570
addLibSearchPaths();
1571+
addClangLibSearchPaths(argsArr[0]);
15461572

15471573
// Handle /ignore
15481574
for (auto *arg : args.filtered(OPT_ignore)) {

lld/COFF/Driver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class LinkerDriver {
8484
// config->machine has been set.
8585
void addWinSysRootLibSearchPaths();
8686

87+
void addClangLibSearchPaths(const std::string &argv0);
88+
8789
// Used by the resolver to parse .drectve section contents.
8890
void parseDirectives(InputFile *file);
8991

lld/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ COFF Improvements
4242
current directory.
4343
I.e. ``lld-link /libpath:c:\relative\root relative\path\my.lib`` where before
4444
we would have to do ``lld-link /libpath:c:\relative\root\relative\path my.lib``
45+
* lld-link learned -print-search-paths that will print all the paths where it will
46+
search for libraries.
47+
* By default lld-link will now search for libraries in the toolchain directories.
48+
Specifically it will search:
49+
``<toolchain>/lib``, ``<toolchain>/lib/clang/<version>/lib`` and
50+
``<toolchain>/lib/clang/<version>/lib/windows``.
4551

4652
MinGW Improvements
4753
------------------

lld/test/COFF/print-search-paths.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
# RUN: llvm-mc -triple i686-windows-msvc %s -filetype=obj -o %t_32.obj
55
# RUN: lld-link -safeseh:no /dll /noentry /winsysroot:%t.dir/sysroot /vctoolsversion:1.1.1.1 /winsdkversion:10.0.1 %t_32.obj -print-search-paths | FileCheck -DSYSROOT=%t.dir -check-prefix=X86 %s
66
# CHECK: Library search paths:
7+
# CHECK: [[CPATH:.*]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib{{[/\\]}}windows
8+
# CHECK: [[CPATH]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib
9+
# CHECK: [[CPATH]]lib
710
# CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x64
811
# CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}atlmfc{{[/\\]}}lib{{[/\\]}}x64
912
# CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}ucrt{{[/\\]}}x64
1013
# CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}um{{[/\\]}}x64
1114
# X86: Library search paths:
15+
# X86: [[CPATH:.*]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib{{[/\\]}}windows
16+
# X86: [[CPATH]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib
17+
# X86: [[CPATH]]lib
1218
# X86: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x86
1319
# X86: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}atlmfc{{[/\\]}}lib{{[/\\]}}x86
1420
# X86: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}ucrt{{[/\\]}}x86

0 commit comments

Comments
 (0)