[clang] [LinkerWrapper] Skip GNU -l/-L for Windows MSVC links (PR #243300)
Vicky Nguyen via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 08:55:48 PST 2026
https://github.com/dotrucvi updated https://github.com/llvm/llvm-project/pull/243300
>From ebede944aa94ee3682c29478555ffee3f2bcf1b6 Mon Sep 17 00:00:00 2001
From: Vicky Nguyen <vicky.nguyen at intel.com>
Date: Mon, 16 Nov 2026 11:24:44 -0800
Subject: [PATCH 1/4] [LinkerWrapper] Skip GNU -l/-L for Windows MSVC links
link.exe does not accept -l or -L. Parsing them turned -link into the
library name "ink" and split -LTCG into "-L" and "TCG". The Clang driver
already rewrites GNU -l and -L for MSVC, so the wrapper skips those
options and forwards the original text.
---
.../clang-linker-wrapper/msvc-link-options.c | 21 +++++++++++++++++
.../ClangLinkerWrapper.cpp | 19 +++++++++++++++
.../clang-linker-wrapper/LinkerWrapperOpts.td | 23 ++++++++++++-------
3 files changed, 55 insertions(+), 8 deletions(-)
create mode 100644 clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c b/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
new file mode 100644
index 00000000000000..e615ce9f8d3e4e
--- /dev/null
+++ b/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
@@ -0,0 +1,21 @@
+// A link.exe-style host linker has no GNU '-l' or '-L' options. Dash-prefixed
+// MSVC linker options that start with 'l' or 'L' must be forwarded verbatim
+// to the host linker rather than being misparsed as '-l <lib>' or '-L <dir>'.
+
+// The wrapper only scans the input for offload sections, which '--dry-run'
+// skips, so an empty file stands in for a real object.
+// RUN: touch %t.o
+
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --linker-path=/usr/bin/lld-link %t.o -link -LTCG -LARGEADDRESSAWARE \
+// RUN: /link -libpath:%t.dir -out:%t.exe 2>&1 | FileCheck %s --check-prefix=MSVC
+
+// MSVC-NOT: error
+// MSVC: "/usr/bin/lld-link" {{.*}}.o -link -LTCG -LARGEADDRESSAWARE /link -libpath:{{.*}} -out:{{.*}}.exe
+
+// GNU-style host linkers still treat '-l' and '-L' as library options.
+// RUN: not clang-linker-wrapper --host-triple=x86_64-w64-windows-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld.lld %t.o -L%t.dir -link -o %t.exe 2>&1 \
+// RUN: | FileCheck %s --check-prefix=GNU
+
+// GNU: error: unable to find library -link
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index f305a0a4bdac1a..ef07fef053a018 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -157,6 +157,8 @@ using std::error_code;
enum WrapperFlags {
WrapperOnlyOption = (1 << 4), // Options only used by the linker wrapper.
DeviceOnlyOption = (1 << 5), // Options only used for device linking.
+ // Options only valid for GNU-style host linkers.
+ GNULinkerOnlyOption = (1 << 6),
};
enum ID {
@@ -1538,6 +1540,23 @@ int main(int Argc, char **Argv) {
reportError(createStringError(Err));
});
+ // link.exe does not accept GNU -l or -L. The Clang driver already
+ // rewrites -lfoo as foo.lib and -L<dir> as -libpath:<dir>. Parsing those
+ // options here reads -link as library "ink" and splits -LTCG into "-L"
+ // and "TCG". For a Windows MSVC host, parse again without them and
+ // forward -link and -LTCG unchanged.
+ if (llvm::Triple(Args.getLastArgValue(OPT_host_triple_EQ,
+ sys::getDefaultTargetTriple()))
+ .isWindowsMSVCEnvironment()) {
+ SmallVector<const char *, 0> ArgStrings;
+ for (unsigned I = 0, E = Args.getNumInputArgStrings(); I != E; ++I)
+ ArgStrings.push_back(Args.getArgString(I));
+ unsigned MissingArgIndex, MissingArgCount;
+ Args = Tbl.ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount,
+ /*FlagsToInclude=*/0,
+ /*FlagsToExclude=*/GNULinkerOnlyOption);
+ }
+
if (Args.hasArg(OPT_help) || Args.hasArg(OPT_help_hidden)) {
Tbl.printHelp(
outs(),
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index fc48eac2637eeb..467cc9ec74e49a 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -2,6 +2,8 @@ include "llvm/Option/OptParser.td"
def WrapperOnlyOption : OptionFlag;
def DeviceOnlyOption : OptionFlag;
+// Options that only exist for GNU-style host linkers (ld, ld.lld).
+def GNULinkerOnlyOption : OptionFlag;
def help : Flag<["--"], "help">,
HelpText<"Display available options (--help-hidden for more)">;
@@ -110,19 +112,24 @@ def output_EQ : Joined<["--"], "output=">, Alias<o>, Flags<[HelpHidden]>,
def output : Separate<["--"], "output">, Alias<o>, Flags<[HelpHidden]>,
HelpText<"Alias for -o">;
+// GNU-style library and library path options. link.exe-style linkers have no
+// such options (the driver renders them as 'foo.lib' and '-libpath:'), so they
+// are not parsed for a Windows MSVC host; see GNULinkerOnlyOption.
def library_path : JoinedOrSeparate<["-"], "L">, MetaVarName<"<dir>">,
+ Flags<[GNULinkerOnlyOption]>,
HelpText<"Add <dir> to the library search path">;
-def library_path_S : Separate<["--", "-"], "library-path">, Flags<[HelpHidden]>,
- Alias<library_path>;
-def library_path_EQ : Joined<["--", "-"], "library-path=">, Flags<[HelpHidden]>,
- Alias<library_path>;
+def library_path_S : Separate<["--", "-"], "library-path">,
+ Flags<[GNULinkerOnlyOption, HelpHidden]>, Alias<library_path>;
+def library_path_EQ : Joined<["--", "-"], "library-path=">,
+ Flags<[GNULinkerOnlyOption, HelpHidden]>, Alias<library_path>;
def library : JoinedOrSeparate<["-"], "l">, MetaVarName<"<libname>">,
+ Flags<[GNULinkerOnlyOption]>,
HelpText<"Search for library <libname>">;
-def library_S : Separate<["--", "-"], "library">, Flags<[HelpHidden]>,
- Alias<library>;
-def library_EQ : Joined<["--", "-"], "library=">, Flags<[HelpHidden]>,
- Alias<library>;
+def library_S : Separate<["--", "-"], "library">,
+ Flags<[GNULinkerOnlyOption, HelpHidden]>, Alias<library>;
+def library_EQ : Joined<["--", "-"], "library=">,
+ Flags<[GNULinkerOnlyOption, HelpHidden]>, Alias<library>;
def rpath : Separate<["--", "-"], "rpath">;
def rpath_EQ : Joined<["--", "-"], "rpath=">, Flags<[HelpHidden]>, Alias<rpath>;
>From 81e4171287805783990edf694a165acb5b03afe2 Mon Sep 17 00:00:00 2001
From: Vicky Nguyen <vicky.nguyen at intel.com>
Date: Tue, 17 Nov 2026 12:13:25 -0800
Subject: [PATCH 2/4] [LinkerWrapper] Select GNU -l/-L by option visibility in
one parse
Read the host triple from the expanded arguments before parsing, and
parse once with a visibility mask that hides GNU -l/-L for a Windows
MSVC host.
---
.../ClangLinkerWrapper.cpp | 42 +++++++++++--------
.../clang-linker-wrapper/LinkerWrapperOpts.td | 16 +++----
2 files changed, 32 insertions(+), 26 deletions(-)
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index ef07fef053a018..17827769e6ba29 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -157,8 +157,11 @@ using std::error_code;
enum WrapperFlags {
WrapperOnlyOption = (1 << 4), // Options only used by the linker wrapper.
DeviceOnlyOption = (1 << 5), // Options only used for device linking.
- // Options only valid for GNU-style host linkers.
- GNULinkerOnlyOption = (1 << 6),
+};
+
+/// Must not overlap with llvm::opt::DriverVisibility.
+enum WrapperVisibility {
+ GNULinkerVis = (1 << 1), // Options only valid for GNU-style host linkers.
};
enum ID {
@@ -1536,26 +1539,29 @@ int main(int Argc, char **Argv) {
const OptTable &Tbl = getOptTable();
BumpPtrAllocator Alloc;
StringSaver Saver(Alloc);
- auto Args = Tbl.parseArgs(Argc, Argv, OPT_INVALID, Saver, [&](StringRef Err) {
- reportError(createStringError(Err));
- });
+ SmallVector<const char *, 0> ArgStrings;
+ cl::expandResponseFiles(Argc, Argv, /*EnvVar=*/nullptr, Saver, ArgStrings);
// link.exe does not accept GNU -l or -L. The Clang driver already
// rewrites -lfoo as foo.lib and -L<dir> as -libpath:<dir>. Parsing those
- // options here reads -link as library "ink" and splits -LTCG into "-L"
- // and "TCG". For a Windows MSVC host, parse again without them and
- // forward -link and -LTCG unchanged.
- if (llvm::Triple(Args.getLastArgValue(OPT_host_triple_EQ,
- sys::getDefaultTargetTriple()))
- .isWindowsMSVCEnvironment()) {
- SmallVector<const char *, 0> ArgStrings;
- for (unsigned I = 0, E = Args.getNumInputArgStrings(); I != E; ++I)
- ArgStrings.push_back(Args.getArgString(I));
- unsigned MissingArgIndex, MissingArgCount;
- Args = Tbl.ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount,
- /*FlagsToInclude=*/0,
- /*FlagsToExclude=*/GNULinkerOnlyOption);
+ // options here would read -link as library "ink" and split -LTCG into "-L"
+ // and "TCG". The host triple selects the visible options, so it is read
+ // before parsing.
+ std::string HostTriple = sys::getDefaultTargetTriple();
+ for (StringRef Arg : ArgStrings) {
+ if (Arg.consume_front("--host-triple="))
+ HostTriple = Arg.str();
}
+ unsigned VisibilityMask = DefaultVis;
+ if (!llvm::Triple(HostTriple).isWindowsMSVCEnvironment())
+ VisibilityMask |= GNULinkerVis;
+
+ unsigned MissingArgIndex, MissingArgCount;
+ auto Args = Tbl.ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount,
+ Visibility(VisibilityMask));
+ if (MissingArgCount)
+ reportError(createStringError(Twine(Args.getArgString(MissingArgIndex)) +
+ ": missing argument"));
if (Args.hasArg(OPT_help) || Args.hasArg(OPT_help_hidden)) {
Tbl.printHelp(
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index 467cc9ec74e49a..e3067decb97902 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -3,7 +3,7 @@ include "llvm/Option/OptParser.td"
def WrapperOnlyOption : OptionFlag;
def DeviceOnlyOption : OptionFlag;
// Options that only exist for GNU-style host linkers (ld, ld.lld).
-def GNULinkerOnlyOption : OptionFlag;
+def GNULinkerVis : OptionVisibility;
def help : Flag<["--"], "help">,
HelpText<"Display available options (--help-hidden for more)">;
@@ -114,22 +114,22 @@ def output : Separate<["--"], "output">, Alias<o>, Flags<[HelpHidden]>,
// GNU-style library and library path options. link.exe-style linkers have no
// such options (the driver renders them as 'foo.lib' and '-libpath:'), so they
-// are not parsed for a Windows MSVC host; see GNULinkerOnlyOption.
+// are not visible for a Windows MSVC host; see GNULinkerVis.
def library_path : JoinedOrSeparate<["-"], "L">, MetaVarName<"<dir>">,
- Flags<[GNULinkerOnlyOption]>,
+ Visibility<[GNULinkerVis]>,
HelpText<"Add <dir> to the library search path">;
def library_path_S : Separate<["--", "-"], "library-path">,
- Flags<[GNULinkerOnlyOption, HelpHidden]>, Alias<library_path>;
+ Flags<[HelpHidden]>, Visibility<[GNULinkerVis]>, Alias<library_path>;
def library_path_EQ : Joined<["--", "-"], "library-path=">,
- Flags<[GNULinkerOnlyOption, HelpHidden]>, Alias<library_path>;
+ Flags<[HelpHidden]>, Visibility<[GNULinkerVis]>, Alias<library_path>;
def library : JoinedOrSeparate<["-"], "l">, MetaVarName<"<libname>">,
- Flags<[GNULinkerOnlyOption]>,
+ Visibility<[GNULinkerVis]>,
HelpText<"Search for library <libname>">;
def library_S : Separate<["--", "-"], "library">,
- Flags<[GNULinkerOnlyOption, HelpHidden]>, Alias<library>;
+ Flags<[HelpHidden]>, Visibility<[GNULinkerVis]>, Alias<library>;
def library_EQ : Joined<["--", "-"], "library=">,
- Flags<[GNULinkerOnlyOption, HelpHidden]>, Alias<library>;
+ Flags<[HelpHidden]>, Visibility<[GNULinkerVis]>, Alias<library>;
def rpath : Separate<["--", "-"], "rpath">;
def rpath_EQ : Joined<["--", "-"], "rpath=">, Flags<[HelpHidden]>, Alias<rpath>;
>From 48dc2897827c7d54f2188d29b17c869e8dd94a48 Mon Sep 17 00:00:00 2001
From: Vicky Nguyen <vicky.nguyen at intel.com>
Date: Wed, 18 Nov 2026 15:01:29 -0800
Subject: [PATCH 3/4] [LinkerWrapper] Apply MSVC -l/-L handling to the
per-device parse
---
.../clang-linker-wrapper/msvc-link-options.c | 17 +++++-
.../ClangLinkerWrapper.cpp | 59 ++++++++++---------
2 files changed, 46 insertions(+), 30 deletions(-)
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c b/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
index e615ce9f8d3e4e..7453e1de058f63 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
@@ -2,8 +2,7 @@
// MSVC linker options that start with 'l' or 'L' must be forwarded verbatim
// to the host linker rather than being misparsed as '-l <lib>' or '-L <dir>'.
-// The wrapper only scans the input for offload sections, which '--dry-run'
-// skips, so an empty file stands in for a real object.
+// An empty object has no embedded device code, so only the host link is run.
// RUN: touch %t.o
// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
@@ -13,6 +12,20 @@
// MSVC-NOT: error
// MSVC: "/usr/bin/lld-link" {{.*}}.o -link -LTCG -LARGEADDRESSAWARE /link -libpath:{{.*}} -out:{{.*}}.exe
+// The same options must not be misparsed when the wrapper re-parses them to
+// link a CPU offload image, which copies library options into the device link.
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
+// RUN: llvm-offload-binary -o %t.out \
+// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
+// RUN: %clang -cc1 %s -triple x86_64-pc-windows-msvc -emit-obj -o %t.host.o \
+// RUN: -fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --linker-path=/usr/bin/lld-link %t.host.o -link -LTCG -out:%t.exe 2>&1 \
+// RUN: | FileCheck %s --check-prefix=MSVC-CPU
+
+// MSVC-CPU: clang{{.*}} --target=x86_64-unknown-linux-gnu {{.*}} -Wl,-Bsymbolic -shared{{$}}
+// MSVC-CPU: "/usr/bin/lld-link" {{.*}}.o -link -LTCG -out:{{.*}}.exe
+
// GNU-style host linkers still treat '-l' and '-L' as library options.
// RUN: not clang-linker-wrapper --host-triple=x86_64-w64-windows-gnu --dry-run \
// RUN: --linker-path=/usr/bin/ld.lld %t.o -L%t.dir -link -o %t.exe 2>&1 \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 17827769e6ba29..502eacbc15ef85 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -201,6 +201,35 @@ void printCommands(ArrayRef<StringRef> CmdArgs) {
exit(EXIT_FAILURE);
}
+/// Parse the wrapper's command line. Expanded response file strings are owned
+/// by \p Saver, which must outlive the returned list.
+InputArgList parseWrapperArgs(int Argc, char **Argv, StringSaver &Saver) {
+ SmallVector<const char *, 0> ArgStrings;
+ cl::expandResponseFiles(Argc, Argv, /*EnvVar=*/nullptr, Saver, ArgStrings);
+
+ // link.exe does not accept GNU -l or -L. The Clang driver already
+ // rewrites -lfoo as foo.lib and -L<dir> as -libpath:<dir>. Parsing those
+ // options here would read -link as library "ink" and split -LTCG into "-L"
+ // and "TCG". The host triple selects the visible options, so it is read
+ // before parsing.
+ std::string HostTriple = sys::getDefaultTargetTriple();
+ for (StringRef Arg : ArgStrings) {
+ if (Arg.consume_front("--host-triple="))
+ HostTriple = Arg.str();
+ }
+ unsigned VisibilityMask = DefaultVis;
+ if (!llvm::Triple(HostTriple).isWindowsMSVCEnvironment())
+ VisibilityMask |= GNULinkerVis;
+
+ unsigned MissingArgIndex, MissingArgCount;
+ InputArgList Args = getOptTable().ParseArgs(
+ ArgStrings, MissingArgIndex, MissingArgCount, Visibility(VisibilityMask));
+ if (MissingArgCount)
+ reportError(createStringError(Twine(Args.getArgString(MissingArgIndex)) +
+ ": missing argument"));
+ return Args;
+}
+
std::string getExecutableDir(const char *Name) {
if (!CanonicalPrefixes)
return sys::path::parent_path(LinkerExecutable).str();
@@ -1124,13 +1153,9 @@ linkAndWrapDeviceFiles(ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
// Each thread needs its own copy of the base arguments to maintain
// per-device argument storage of synthetic strings.
- const OptTable &Tbl = getOptTable();
BumpPtrAllocator Alloc;
StringSaver Saver(Alloc);
- auto BaseArgs =
- Tbl.parseArgs(Argc, Argv, OPT_INVALID, Saver, [](StringRef Err) {
- reportError(createStringError(Err));
- });
+ auto BaseArgs = parseWrapperArgs(Argc, Argv, Saver);
auto LinkerArgs = getLinkerArgs(Input, BaseArgs);
uint16_t ActiveOffloadKindMask = 0u;
@@ -1539,29 +1564,7 @@ int main(int Argc, char **Argv) {
const OptTable &Tbl = getOptTable();
BumpPtrAllocator Alloc;
StringSaver Saver(Alloc);
- SmallVector<const char *, 0> ArgStrings;
- cl::expandResponseFiles(Argc, Argv, /*EnvVar=*/nullptr, Saver, ArgStrings);
-
- // link.exe does not accept GNU -l or -L. The Clang driver already
- // rewrites -lfoo as foo.lib and -L<dir> as -libpath:<dir>. Parsing those
- // options here would read -link as library "ink" and split -LTCG into "-L"
- // and "TCG". The host triple selects the visible options, so it is read
- // before parsing.
- std::string HostTriple = sys::getDefaultTargetTriple();
- for (StringRef Arg : ArgStrings) {
- if (Arg.consume_front("--host-triple="))
- HostTriple = Arg.str();
- }
- unsigned VisibilityMask = DefaultVis;
- if (!llvm::Triple(HostTriple).isWindowsMSVCEnvironment())
- VisibilityMask |= GNULinkerVis;
-
- unsigned MissingArgIndex, MissingArgCount;
- auto Args = Tbl.ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount,
- Visibility(VisibilityMask));
- if (MissingArgCount)
- reportError(createStringError(Twine(Args.getArgString(MissingArgIndex)) +
- ": missing argument"));
+ auto Args = parseWrapperArgs(Argc, Argv, Saver);
if (Args.hasArg(OPT_help) || Args.hasArg(OPT_help_hidden)) {
Tbl.printHelp(
>From 231056d65eef0bf33f344bfa0cbdad514d728fc5 Mon Sep 17 00:00:00 2001
From: Vicky Nguyen <vicky.nguyen at intel.com>
Date: Mon, 23 Nov 2026 08:31:27 -0800
Subject: [PATCH 4/4] [LinkerWrapper] Add isMSVCHost helper and test long
library options
---
.../clang-linker-wrapper/msvc-link-options.c | 21 +++++++++++++++
.../ClangLinkerWrapper.cpp | 26 +++++++++++--------
2 files changed, 36 insertions(+), 11 deletions(-)
diff --git a/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c b/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
index 7453e1de058f63..f89d2f3a628132 100644
--- a/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
+++ b/clang/test/OffloadTools/clang-linker-wrapper/msvc-link-options.c
@@ -32,3 +32,24 @@
// RUN: | FileCheck %s --check-prefix=GNU
// GNU: error: unable to find library -link
+
+// The long forms of the GNU library options are also hidden for a Windows MSVC
+// host and forwarded verbatim.
+// RUN: clang-linker-wrapper --host-triple=x86_64-pc-windows-msvc --dry-run \
+// RUN: --linker-path=/usr/bin/lld-link %t.o --library=foo --library bar \
+// RUN: --library-path=%t.dir --library-path %t.dir -out:%t.exe 2>&1 \
+// RUN: | FileCheck %s --check-prefix=MSVC-LONG
+
+// MSVC-LONG-NOT: error
+// MSVC-LONG: "/usr/bin/lld-link" {{.*}}.o --library=foo --library bar --library-path={{.*}} --library-path {{.*}} -out:{{.*}}.exe
+
+// GNU-style host linkers still treat the long forms as library options.
+// RUN: not clang-linker-wrapper --host-triple=x86_64-w64-windows-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld.lld %t.o --library-path=%t.dir --library=foo \
+// RUN: -o %t.exe 2>&1 | FileCheck %s --check-prefix=GNU-LONG-EQ
+// RUN: not clang-linker-wrapper --host-triple=x86_64-w64-windows-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld.lld %t.o --library-path %t.dir --library bar \
+// RUN: -o %t.exe 2>&1 | FileCheck %s --check-prefix=GNU-LONG-S
+
+// GNU-LONG-EQ: error: unable to find library -lfoo
+// GNU-LONG-S: error: unable to find library -lbar
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 502eacbc15ef85..5ef51d680aa23a 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -201,24 +201,28 @@ void printCommands(ArrayRef<StringRef> CmdArgs) {
exit(EXIT_FAILURE);
}
+/// Return true if the host uses an MSVC-style linker such as link.exe. The
+/// host triple decides which options we parse, so read it before parsing.
+bool isMSVCHost(ArrayRef<const char *> ArgStrings) {
+ StringRef HostTriple;
+ for (StringRef Arg : ArgStrings)
+ if (Arg.consume_front("--host-triple="))
+ HostTriple = Arg;
+ return llvm::Triple(HostTriple.empty() ? sys::getDefaultTargetTriple()
+ : HostTriple.str())
+ .isWindowsMSVCEnvironment();
+}
+
/// Parse the wrapper's command line. Expanded response file strings are owned
/// by \p Saver, which must outlive the returned list.
InputArgList parseWrapperArgs(int Argc, char **Argv, StringSaver &Saver) {
SmallVector<const char *, 0> ArgStrings;
cl::expandResponseFiles(Argc, Argv, /*EnvVar=*/nullptr, Saver, ArgStrings);
- // link.exe does not accept GNU -l or -L. The Clang driver already
- // rewrites -lfoo as foo.lib and -L<dir> as -libpath:<dir>. Parsing those
- // options here would read -link as library "ink" and split -LTCG into "-L"
- // and "TCG". The host triple selects the visible options, so it is read
- // before parsing.
- std::string HostTriple = sys::getDefaultTargetTriple();
- for (StringRef Arg : ArgStrings) {
- if (Arg.consume_front("--host-triple="))
- HostTriple = Arg.str();
- }
+ // MSVC linkers have no -l or -L. Hide those options so that MSVC options
+ // such as -link and -LTCG are not misread as "-l ink" and "-L TCG".
unsigned VisibilityMask = DefaultVis;
- if (!llvm::Triple(HostTriple).isWindowsMSVCEnvironment())
+ if (!isMSVCHost(ArgStrings))
VisibilityMask |= GNULinkerVis;
unsigned MissingArgIndex, MissingArgCount;
More information about the cfe-commits
mailing list