[COFF] Look for libfoo.a if foo.lib is specified, for MinGW

This allows using #pragma comment(lib, "foo") in MinGW built code,
if built with -fms-extensions. (This works for system libraries and
static libraries only, as it doesn't try to look for .dll.a. As
ld.bfd doesn't support embedded defaultlib directives, this isn't
in widespread use among mingw users.)

Differential Revision: https://reviews.llvm.org/D53017

llvm-svn: 344124
This commit is contained in:
Martin Storsjo 2018-10-10 09:00:10 +00:00
parent 803b37ad5d
commit 33d43ff851
3 changed files with 27 additions and 1 deletions

View File

@ -371,13 +371,30 @@ Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
return Path;
}
// MinGW specific. If an embedded directive specified to link to
// foo.lib, but it isn't found, try libfoo.a instead.
StringRef LinkerDriver::doFindLibMinGW(StringRef Filename) {
if (Filename.contains('/') || Filename.contains('\\'))
return Filename;
SmallString<128> S = Filename;
sys::path::replace_extension(S, ".a");
StringRef LibName = Saver.save("lib" + S.str());
return doFindFile(LibName);
}
// Find library file from search path.
StringRef LinkerDriver::doFindLib(StringRef Filename) {
// Add ".lib" to Filename if that has no file extension.
bool HasExt = Filename.contains('.');
if (!HasExt)
Filename = Saver.save(Filename + ".lib");
return doFindFile(Filename);
StringRef Ret = doFindFile(Filename);
// For MinGW, if the find above didn't turn up anything, try
// looking for a MinGW formatted library name.
if (Config->MinGW && Ret == Filename)
return doFindLibMinGW(Filename);
return Ret;
}
// Resolves a library path. /nodefaultlib options are taken into

View File

@ -89,6 +89,7 @@ private:
Optional<StringRef> findLib(StringRef Filename);
StringRef doFindFile(StringRef Filename);
StringRef doFindLib(StringRef Filename);
StringRef doFindLibMinGW(StringRef Filename);
// Parses LIB environment which contains a list of search paths.
void addLibSearchPaths();

View File

@ -0,0 +1,8 @@
# RUN: mkdir -p %t/a
# RUN: cp %p/Inputs/std64.lib %t/a/libstd64.a
# RUN: lld-link /lldmingw /out:%t.exe /entry:main /verbose \
# RUN: /defaultlib:std64.lib /subsystem:console %p/Inputs/hello64.obj \
# RUN: /libpath:%t/a 2>&1 | FileCheck %s
CHECK: a{{[/\\]}}libstd64.a