Skip to content

Add support for -lto_library linker flag #745

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 2 commits into from
Jul 15, 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
7 changes: 6 additions & 1 deletion Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@ extension DarwinToolchain {
)

if lto != nil {
try addLTOLibArgs(to: &commandLine)
if let arg = parsedOptions.getLastArgument(.ltoLibrary)?.asSingle {
commandLine.appendFlag("-lto_library")
commandLine.appendPath(try VirtualPath(path: arg))
} else {
try addLTOLibArgs(to: &commandLine)
}
}

let fSystemArgs = parsedOptions.arguments(for: .F, .Fsystem)
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftOptions/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ extension Option {
public static let localizationPath: Option = Option("-localization-path", .separate, attributes: [.frontend, .doesNotAffectIncrementalBuild, .argumentIsPath], metaVar: "<path>", helpText: "Path to localized diagnostic messages directory")
public static let location: Option = Option("-location", .separate, attributes: [.noDriver], metaVar: "<location>", helpText: "Filter nodes with the given location.")
public static let location_: Option = Option("--location", .separate, alias: Option.location, attributes: [.noDriver], metaVar: "<location>", helpText: "Filter nodes with the given location.")
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)
Expand Down Expand Up @@ -986,6 +987,7 @@ extension Option {
Option.localizationPath,
Option.location,
Option.location_,
Option.ltoLibrary,
Option.lto,
Option.L,
Option.l,
Expand Down
20 changes: 20 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3786,12 +3786,32 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .link])
XCTAssertTrue(plannedJobs[1].commandLine.contains("-lto_library"))
}

do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-thin", "-lto-library", "/foo/libLTO.dylib", "-target", "x86_64-apple-macos11.0"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .link])
XCTAssertFalse(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/foo/libLTO.dylib"))))
XCTAssertTrue(plannedJobs[1].commandLine.contains("-lto_library"))
XCTAssertTrue(plannedJobs[1].commandLine.contains(.path(try VirtualPath(path: "/foo/libLTO.dylib"))))
}

do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full", "-target", "x86_64-apple-macos11.0"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .link])
XCTAssertTrue(plannedJobs[1].commandLine.contains("-lto_library"))
}

do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full", "-lto-library", "/foo/libLTO.dylib", "-target", "x86_64-apple-macos11.0"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .link])
XCTAssertFalse(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/foo/libLTO.dylib"))))
XCTAssertTrue(plannedJobs[1].commandLine.contains("-lto_library"))
XCTAssertTrue(plannedJobs[1].commandLine.contains(.path(try VirtualPath(path: "/foo/libLTO.dylib"))))
}

do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-target", "x86_64-apple-macos11.0"])
let plannedJobs = try driver.planBuild()
Expand Down