Skip to content

Commit e738423

Browse files
[LLD][Cygwin] Implement --dll-search-prefix (llvm#143263)
GCC on Cygwin environment invokes linker with passing `--dll-search-prefix=cyg`. Implementing this option makes lld-mingw invokable by `gcc -fuse-ld=lld`. --------- Co-authored-by: jeremyd2019 <[email protected]>
1 parent f41e097 commit e738423

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

lld/MinGW/Driver.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ static std::optional<std::string> findFile(StringRef path1,
139139
}
140140

141141
// This is for -lfoo. We'll look for libfoo.dll.a or libfoo.a from search paths.
142-
static std::string
143-
searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
142+
static std::string searchLibrary(StringRef name,
143+
ArrayRef<StringRef> searchPaths, bool bStatic,
144+
StringRef prefix) {
144145
if (name.starts_with(":")) {
145146
for (StringRef dir : searchPaths)
146147
if (std::optional<std::string> s = findFile(dir, name.substr(1)))
@@ -161,7 +162,7 @@ searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
161162
if (std::optional<std::string> s = findFile(dir, name + ".lib"))
162163
return *s;
163164
if (!bStatic) {
164-
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".dll"))
165+
if (std::optional<std::string> s = findFile(dir, prefix + name + ".dll"))
165166
return *s;
166167
if (std::optional<std::string> s = findFile(dir, name + ".dll"))
167168
return *s;
@@ -545,6 +546,10 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
545546
add("-libpath:" + StringRef(a->getValue()));
546547
}
547548

549+
StringRef dllPrefix = "lib";
550+
if (auto *arg = args.getLastArg(OPT_dll_search_prefix))
551+
dllPrefix = arg->getValue();
552+
548553
StringRef prefix = "";
549554
bool isStatic = false;
550555
for (auto *a : args) {
@@ -556,7 +561,8 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
556561
add(prefix + StringRef(a->getValue()));
557562
break;
558563
case OPT_l:
559-
add(prefix + searchLibrary(a->getValue(), searchPaths, isStatic));
564+
add(prefix +
565+
searchLibrary(a->getValue(), searchPaths, isStatic, dllPrefix));
560566
break;
561567
case OPT_whole_archive:
562568
prefix = "-wholearchive:";

lld/MinGW/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ defm exclude_symbols: Eq<"exclude-symbols",
7979
"Exclude symbols from automatic export">, MetaVarName<"<symbol[,symbol,...]>">;
8080
def export_all_symbols: F<"export-all-symbols">,
8181
HelpText<"Export all symbols even if a def file or dllexport attributes are used">;
82+
defm dll_search_prefix:Eq<"dll-search-prefix", "Specify DLL prefix instead of 'lib'">,
83+
MetaVarName<"<dll_search_prefix>">;
8284
defm fatal_warnings: B<"fatal-warnings",
8385
"Treat warnings as errors",
8486
"Do not treat warnings as errors (default)">;

lld/test/MinGW/lib.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LIB1: unable to find library -lfoo
55

66
RUN: echo > %t/lib/libfoo.dll.a
77
RUN: ld.lld -### -m i386pep -lfoo -L%t/lib 2>&1 | FileCheck -check-prefix=LIB2 %s
8+
RUN: ld.lld -### -m i386pep -lfoo --dll-search-prefix=cyg -L%t/lib 2>&1 | FileCheck -check-prefix=LIB2 %s
89
LIB2: libfoo.dll.a
910

1011
RUN: not ld.lld -### -m i386pep -l:barefilename -L%t/lib 2>&1 | FileCheck -check-prefix=LIB-LITERAL-FAIL %s
@@ -22,6 +23,7 @@ LIB3: unable to find library -lfoo
2223

2324
RUN: echo > %t/lib/libfoo.a
2425
RUN: ld.lld -### -m i386pep -Bstatic -lfoo -L%t/lib 2>&1 | FileCheck -check-prefix=LIB4 %s
26+
RUN: ld.lld -### -m i386pep -Bstatic -lfoo --dll-search-prefix=cyg -L%t/lib 2>&1 | FileCheck -check-prefix=LIB4 %s
2527
LIB4: libfoo.a
2628

2729
RUN: echo > %t/lib/libbar.dll.a
@@ -46,12 +48,17 @@ MSVCSTYLE: msvcstyle.lib
4648

4749
RUN: echo > %t/lib/libnoimplib.dll
4850
RUN: echo > %t/lib/noprefix_noimplib.dll
51+
RUN: echo > %t/lib/cygnoimplib2.dll
4952
RUN: ld.lld -### -m i386pep -L%t/lib -lnoimplib 2>&1 | FileCheck -check-prefix=DLL1 %s
5053
RUN: ld.lld -### -m i386pep -L%t/lib -lnoprefix_noimplib 2>&1 | FileCheck -check-prefix=DLL2 %s
54+
RUN: ld.lld -### -m i386pep -L%t/lib -lnoimplib2 --dll-search-prefix=cyg 2>&1 | FileCheck -check-prefix=DLL3 %s
5155
DLL1: libnoimplib.dll
5256
DLL2: noprefix_noimplib.dll
57+
DLL3: cygnoimplib2.dll
5358

5459
RUN: not ld.lld -### -m i386pep -L%t/lib -static -lnoimplib 2>&1 | FileCheck -check-prefix=ERROR-NOIMPLIB %s
5560
RUN: not ld.lld -### -m i386pep -L%t/lib -static -lnoprefix_noimplib 2>&1 | FileCheck -check-prefix=ERROR-NOPREFIX-NOIMPLIB %s
61+
RUN: not ld.lld -### -m i386pep -L%t/lib -static -lnoimplib2 --dll-search-prefix=cyg 2>&1 | FileCheck -check-prefix=ERROR-CYG-NOIMPLIB %s
5662
ERROR-NOIMPLIB: unable to find library -lnoimplib
5763
ERROR-NOPREFIX-NOIMPLIB: unable to find library -lnoprefix_noimplib
64+
ERROR-CYG-NOIMPLIB: unable to find library -lnoimplib2

0 commit comments

Comments
 (0)