Skip to content

Commit 9e23e85

Browse files
[LLD][Cygwin] Implement --dll-search-prefix (#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 65d88d3 commit 9e23e85

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
@@ -138,8 +138,9 @@ static std::optional<std::string> findFile(StringRef path1,
138138
}
139139

140140
// This is for -lfoo. We'll look for libfoo.dll.a or libfoo.a from search paths.
141-
static std::string
142-
searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
141+
static std::string searchLibrary(StringRef name,
142+
ArrayRef<StringRef> searchPaths, bool bStatic,
143+
StringRef prefix) {
143144
if (name.starts_with(":")) {
144145
for (StringRef dir : searchPaths)
145146
if (std::optional<std::string> s = findFile(dir, name.substr(1)))
@@ -160,7 +161,7 @@ searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
160161
if (std::optional<std::string> s = findFile(dir, name + ".lib"))
161162
return *s;
162163
if (!bStatic) {
163-
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".dll"))
164+
if (std::optional<std::string> s = findFile(dir, prefix + name + ".dll"))
164165
return *s;
165166
if (std::optional<std::string> s = findFile(dir, name + ".dll"))
166167
return *s;
@@ -554,6 +555,10 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
554555
add("-libpath:" + StringRef(a->getValue()));
555556
}
556557

558+
StringRef dllPrefix = "lib";
559+
if (auto *arg = args.getLastArg(OPT_dll_search_prefix))
560+
dllPrefix = arg->getValue();
561+
557562
StringRef prefix = "";
558563
bool isStatic = false;
559564
for (auto *a : args) {
@@ -565,7 +570,8 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
565570
add(prefix + StringRef(a->getValue()));
566571
break;
567572
case OPT_l:
568-
add(prefix + searchLibrary(a->getValue(), searchPaths, isStatic));
573+
add(prefix +
574+
searchLibrary(a->getValue(), searchPaths, isStatic, dllPrefix));
569575
break;
570576
case OPT_whole_archive:
571577
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)