Skip to content

Commit 0e13fcd

Browse files
Merge pull request #38750 from RAJAGOPALAN-GANGADHARAN/sr_14122
Improve usability of -l flag
2 parents e235256 + 855eeaf commit 0e13fcd

File tree

10 files changed

+54
-8
lines changed

10 files changed

+54
-8
lines changed

include/swift/Driver/ToolChain.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ class ToolChain {
249249
const Driver &getDriver() const { return D; }
250250
const llvm::Triple &getTriple() const { return Triple; }
251251

252+
/// Special handling for passing down '-l' arguments.
253+
///
254+
/// Not all downstream tools (lldb, ld etc.) consistently accept
255+
/// a space between the '-l' flag and its argument, so we remove
256+
/// the extra space if it was present in \c Args.
257+
static void addLinkedLibArgs(const llvm::opt::ArgList &Args,
258+
llvm::opt::ArgStringList &FrontendArgs);
259+
252260
/// Construct a Job for the action \p JA, taking the given information into
253261
/// account.
254262
///

include/swift/Option/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def libc : Separate<["-"], "libc">, HelpText<"libc runtime library to use">;
691691

692692
def linker_option_Group : OptionGroup<"<linker-specific options>">;
693693

694-
def l : Joined<["-"], "l">, Group<linker_option_Group>,
694+
def l : JoinedOrSeparate<["-"], "l">, Group<linker_option_Group>,
695695
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
696696
HelpText<"Specifies a library which should be linked against">;
697697
def framework : Separate<["-"], "framework">, Group<linker_option_Group>,

lib/Driver/DarwinToolChains.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,9 @@ toolchains::Darwin::constructInvocation(const DynamicLinkJobAction &job,
796796
Arguments.push_back("-no_objc_category_merging");
797797

798798
// These custom arguments should be right before the object file at the end.
799-
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
799+
context.Args.AddAllArgsExcept(Arguments, {options::OPT_linker_option_Group},
800+
{options::OPT_l});
801+
ToolChain::addLinkedLibArgs(context.Args, Arguments);
800802
context.Args.AddAllArgValues(Arguments, options::OPT_Xlinker);
801803

802804
// This should be the last option, for convenience in checking output.

lib/Driver/ToolChain.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,15 @@ mergeBatchInputs(ArrayRef<const Job *> jobs,
287287
return false;
288288
}
289289

290+
void ToolChain::addLinkedLibArgs(const llvm::opt::ArgList &Args,
291+
llvm::opt::ArgStringList &FrontendArgs) {
292+
Args.getLastArg(options::OPT_l);
293+
for (auto Arg : Args.getAllArgValues(options::OPT_l)) {
294+
const std::string lArg("-l" + Arg);
295+
FrontendArgs.push_back(Args.MakeArgString(Twine(lArg)));
296+
}
297+
}
298+
290299
/// Construct a \c BatchJob by merging the constituent \p jobs' CommandOutput,
291300
/// input \c Job and \c Action members. Call through to \c constructInvocation
292301
/// on \p BatchJob, to build the \c InvocationInfo.

lib/Driver/ToolChains.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ static void addLTOArgs(const OutputInfo &OI, ArgStringList &arguments) {
142142
}
143143
}
144144

145+
145146
void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
146147
const CommandOutput &output,
147148
const ArgList &inputArgs,
@@ -838,7 +839,8 @@ ToolChain::constructInvocation(const InterpretJobAction &job,
838839
Arguments.push_back("-module-name");
839840
Arguments.push_back(context.Args.MakeArgString(context.OI.ModuleName));
840841

841-
context.Args.AddAllArgs(Arguments, options::OPT_l, options::OPT_framework);
842+
context.Args.AddAllArgs(Arguments, options::OPT_framework);
843+
ToolChain::addLinkedLibArgs(context.Args, Arguments);
842844

843845
// The immediate arguments must be last.
844846
context.Args.AddLastArg(Arguments, options::OPT__DASH_DASH);
@@ -1186,8 +1188,8 @@ ToolChain::constructInvocation(const REPLJobAction &job,
11861188
addRuntimeLibraryFlags(context.OI, FrontendArgs);
11871189

11881190
context.Args.AddLastArg(FrontendArgs, options::OPT_import_objc_header);
1189-
context.Args.AddAllArgs(FrontendArgs, options::OPT_l, options::OPT_framework,
1190-
options::OPT_L);
1191+
context.Args.AddAllArgs(FrontendArgs, options::OPT_framework, options::OPT_L);
1192+
ToolChain::addLinkedLibArgs(context.Args, FrontendArgs);
11911193

11921194
if (!useLLDB) {
11931195
FrontendArgs.insert(FrontendArgs.begin(), {"-frontend", "-repl"});

lib/Driver/UnixToolChains.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,9 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
358358
}
359359

360360
// These custom arguments should be right before the object file at the end.
361-
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
361+
context.Args.AddAllArgsExcept(Arguments, {options::OPT_linker_option_Group},
362+
{options::OPT_l});
363+
ToolChain::addLinkedLibArgs(context.Args, Arguments);
362364
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
363365
context.Args.AddAllArgValues(Arguments, options::OPT_Xclang_linker);
364366

lib/Driver/WindowsToolChains.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
179179
}
180180

181181
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
182-
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
182+
context.Args.AddAllArgsExcept(Arguments, {options::OPT_linker_option_Group},
183+
{options::OPT_l});
184+
ToolChain::addLinkedLibArgs(context.Args, Arguments);
183185
context.Args.AddAllArgValues(Arguments, options::OPT_Xclang_linker);
184186

185187
// Run clang in verbose mode if "-v" is set
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %swiftc_driver -sdk "" -driver-print-jobs -target x86_64-unknown-linux-gnu -Ffoo -Fsystem car -F cdr -framework bar -Lbaz -lboo -Xlinker -undefined %s 2>&1 > %t.linux.txt
2+
// RUN: %FileCheck -check-prefix LINUX-lib-flag-space %s < %t.linux.txt
3+
4+
// LINUX-lib-flag-space: swift
5+
// LINUX-lib-flag-space: -o [[OBJECTFILE:.*]]
6+
7+
// LINUX-lib-flag-space: clang{{(\.exe)?"? }}
8+
// LINUX-lib-flag-space-DAG: -pie
9+
// LINUX-lib-flag-space-DAG: [[OBJECTFILE]]
10+
// LINUX-lib-flag-space-DAG: -lswiftCore
11+
// LINUX-lib-flag-space-DAG: -L [[STDLIB_PATH:[^ ]+(/|\\\\)lib(/|\\\\)swift(/|\\\\)]]
12+
// LINUX-lib-flag-space-DAG: -Xlinker -rpath -Xlinker [[STDLIB_PATH]]
13+
// LINUX-lib-flag-space-DAG: -F foo -iframework car -F cdr
14+
// LINUX-lib-flag-space-DAG: -framework bar
15+
// LINUX-lib-flag-space-DAG: -L baz
16+
// LINUX-lib-flag-space-DAG: -lboo
17+
// LINUX-lib-flag-space-DAG: -Xlinker -undefined
18+
// LINUX-lib-flag-space: -o main

test/Driver/linker.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Must be able to run xcrun-return-self.sh
22
// REQUIRES: shell
33
// REQUIRES: rdar65281056
4+
// FIXME: When this is turned on, please move the test from linker-library-with-space.swift
5+
// to this file and remove that file.
46
// RUN: %swiftc_driver -sdk "" -driver-print-jobs -target x86_64-apple-macosx10.9 %s 2>&1 > %t.simple.txt
57
// RUN: %FileCheck %s < %t.simple.txt
68
// RUN: %FileCheck -check-prefix SIMPLE %s < %t.simple.txt

test/Driver/options-repl.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
// RUN: %swift_driver -sdk "" -lldb-repl -### | %FileCheck -check-prefix=LLDB %s
19-
// RUN: %swift_driver -sdk "" -lldb-repl -D A -DB -D C -DD -L /path/to/libraries -L /path/to/more/libraries -F /path/to/frameworks -lsomelib -framework SomeFramework -sdk / -I "this folder" -module-name Test -target %target-triple -### | %FileCheck -check-prefix=LLDB-OPTS %s
19+
// RUN: %swift_driver -sdk "" -lldb-repl -D A -DB -D C -DD -L /path/to/libraries -L /path/to/more/libraries -F /path/to/frameworks -lsomelib -l otherlib -framework SomeFramework -sdk / -I "this folder" -module-name Test -target %target-triple -### | %FileCheck -check-prefix=LLDB-OPTS %s
2020

2121
// LLDB: lldb{{(\.exe)?"?}} {{"?}}--repl=
2222
// LLDB-NOT: -module-name
@@ -30,6 +30,7 @@
3030
// LLDB-OPTS-DAG: -L /path/to/more/libraries
3131
// LLDB-OPTS-DAG: -F /path/to/frameworks
3232
// LLDB-OPTS-DAG: -lsomelib
33+
// LLDB-OPTS-DAG: -lotherlib
3334
// LLDB-OPTS-DAG: -framework SomeFramework
3435
// LLDB-OPTS-DAG: -I \"this folder\"
3536
// LLDB-OPTS: "

0 commit comments

Comments
 (0)