Skip to content

Improve usability of -l flag #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/SwiftDriver/Jobs/CommandLineArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ extension Array where Element == Job.ArgTemplate {
}
}

/// Append all parsed options from the given groups except from excludeList to this command line.
mutating func appendAllExcept(includeList: [Option.Group], excludeList: [Option], from parsedOptions: inout ParsedOptions) throws {
for group in includeList{
for optGroup in parsedOptions.arguments(in: group){
if !excludeList.contains(where: {$0 == optGroup.option}) {
try append(optGroup)
}
}
}
}

/// Append the last of the given flags that appears in the parsed options,
/// or the flag that corresponds to the default value if neither
/// appears.
Expand Down
7 changes: 5 additions & 2 deletions Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,12 @@ extension DarwinToolchain {

// These custom arguments should be right before the object file at the
// end.
try commandLine.append(
contentsOf: parsedOptions.arguments(in: .linkerOption)
try commandLine.appendAllExcept(
includeList: [.linkerOption],
excludeList: [.l],
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,12 @@ extension GenericUnixToolchain {

// These custom arguments should be right before the object file at the
// end.
try commandLine.append(
contentsOf: parsedOptions.arguments(in: .linkerOption)
try commandLine.appendAllExcept(
includeList: [.linkerOption],
excludeList: [.l],
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
// Because we invoke `clang` as the linker executable, we must still
// use `-Xlinker` for linker-specific arguments.
for linkerOpt in parsedOptions.arguments(for: .Xlinker) {
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftDriver/Jobs/InterpretJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ extension Driver {
// FIXME: MSVC runtime flags

try commandLine.appendLast(.parseSil, from: &parsedOptions)
try commandLine.appendAll(.l, .framework, from: &parsedOptions)
toolchain.addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
try commandLine.appendAll(.framework, from: &parsedOptions)

// The immediate arguments must be last.
try commandLine.appendLast(.DASHDASH, from: &parsedOptions)
Expand Down
6 changes: 5 additions & 1 deletion Sources/SwiftDriver/Jobs/ReplJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ extension Driver {
// FIXME: MSVC runtime flags

try commandLine.appendLast(.importObjcHeader, from: &parsedOptions)
try commandLine.appendAll(.l, .framework, .L, from: &parsedOptions)
toolchain.addLinkedLibArgs(
to: &commandLine,
parsedOptions: &parsedOptions
)
try commandLine.appendAll(.framework, .L, from: &parsedOptions)

// Squash important frontend options into a single argument for LLDB.
let lldbCommandLine: [Job.ArgTemplate] = [.squashedArgumentList(option: "--repl=", args: commandLine)]
Expand Down
9 changes: 9 additions & 0 deletions Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ extension Toolchain {
).appending(component: runtimeName)
return try fileSystem.exists(path)
}

func addLinkedLibArgs(
to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions
) {
for match in parsedOptions.arguments(for: .l) {
commandLine.appendFlag(match.option.spelling + match.argument.asSingle)
}
}
}

// MARK: - Common argument routines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ extension WebAssemblyToolchain {

// These custom arguments should be right before the object file at the
// end.
try commandLine.append(
contentsOf: parsedOptions.arguments(in: .linkerOption)
try commandLine.appendAllExcept(
includeList: [.linkerOption],
excludeList: [.l],
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions)
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftOptions/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ extension Option {
public static let ltoLibrary: Option = Option("-lto-library", .separate, attributes: [.frontend, .argumentIsPath], metaVar: "<lto-library>", helpText: "Perform LTO with <lto-library>")
public static let lto: Option = Option("-lto=", .joined, attributes: [.frontend, .noInteractive], helpText: "Specify the LTO type to either 'llvm-thin' or 'llvm-full'")
public static let L: Option = Option("-L", .joinedOrSeparate, attributes: [.frontend, .doesNotAffectIncrementalBuild, .argumentIsPath], helpText: "Add directory to library link search path", group: .linkerOption)
public static let l: Option = Option("-l", .joined, attributes: [.frontend, .doesNotAffectIncrementalBuild], helpText: "Specifies a library which should be linked against", group: .linkerOption)
public static let l: Option = Option("-l", .joinedOrSeparate, attributes: [.frontend, .doesNotAffectIncrementalBuild], helpText: "Specifies a library which should be linked against", group: .linkerOption)
public static let mergeModules: Option = Option("-merge-modules", .flag, attributes: [.frontend, .noDriver], helpText: "Merge the input modules without otherwise processing them", group: .modes)
public static let migrateKeepObjcVisibility: Option = Option("-migrate-keep-objc-visibility", .flag, attributes: [.frontend, .noInteractive], helpText: "When migrating, add '@objc' to declarations that would've been implicitly visible in Swift 3")
public static let migratorUpdateSdk: Option = Option("-migrator-update-sdk", .flag, attributes: [.frontend, .noInteractive], helpText: "Does nothing. Temporary compatibility flag for Xcode.")
Expand Down
30 changes: 29 additions & 1 deletion Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ final class SwiftDriverTests: XCTestCase {
env["SWIFT_DRIVER_DSYMUTIL_EXEC"] = "/garbage/dsymutil"

let commonArgs = ["swiftc", "foo.swift", "bar.swift", "-module-name", "Test"]

do {
// macOS target
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-apple-macosx10.15"], env: env)
Expand Down Expand Up @@ -1456,6 +1457,16 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertFalse(cmd.contains(.flag("-shared")))
}
}

do {
// Linker flags with and without space
var driver = try Driver(args: commonArgs + ["-lsomelib","-l","otherlib"], env: env)
let plannedJobs = try driver.planBuild()
let cmd = plannedJobs.last!.commandLine
XCTAssertTrue(cmd.contains(.flag("-lsomelib")))
XCTAssertTrue(cmd.contains(.flag("-lotherlib")))
}

}

func testWebAssemblyUnsupportedFeatures() throws {
Expand Down Expand Up @@ -2346,6 +2357,20 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertEqual(error as? PlanningError, .replReceivedInput)
}
}

do {
// Linked library arguments with space
var driver = try Driver(args: ["swift", "-repl", "-l", "somelib", "-lotherlib"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
let cmd = plannedJobs.first!.commandLine
guard case let .squashedArgumentList(option: _, args: args) = cmd[0] else {
XCTFail()
return
}
XCTAssertTrue(args.contains(.flag("-lsomelib")))
XCTAssertTrue(args.contains(.flag("-lotherlib")))
}
}

func testInstallAPI() throws {
Expand Down Expand Up @@ -2411,7 +2436,7 @@ final class SwiftDriverTests: XCTestCase {
}

do {
var driver = try Driver(args: ["swift", "-L/path/to/lib", "-F/path/to/framework", "foo.swift"])
var driver = try Driver(args: ["swift", "-L/path/to/lib", "-F/path/to/framework", "-lsomelib", "-l", "otherlib", "foo.swift"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)
let job = plannedJobs[0]
Expand All @@ -2425,6 +2450,9 @@ final class SwiftDriverTests: XCTestCase {
if driver.targetTriple.isDarwin {
XCTAssertTrue(job.extraEnvironment.contains { $0 == "DYLD_FRAMEWORK_PATH" && $1.contains("/path/to/framework") })
}

XCTAssertTrue(job.commandLine.contains(.flag("-lsomelib")))
XCTAssertTrue(job.commandLine.contains(.flag("-lotherlib")))
}
}

Expand Down