Skip to content

Commit 528b91f

Browse files
committed
Add support for -lto_library linker flag
Allow users to specify a particular LTO library on Darwin.
1 parent c5c10de commit 528b91f

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,12 @@ extension DarwinToolchain {
338338
)
339339

340340
if lto != nil {
341-
try addLTOLibArgs(to: &commandLine)
341+
if let arg = parsedOptions.getLastArgument(.ltoLibrary)?.asSingle {
342+
commandLine.appendFlag("-lto_library")
343+
commandLine.appendPath(try VirtualPath(path: arg))
344+
} else {
345+
try addLTOLibArgs(to: &commandLine)
346+
}
342347
}
343348

344349
let fSystemArgs = parsedOptions.arguments(for: .F, .Fsystem)

Sources/SwiftOptions/Options.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ extension Option {
387387
public static let location: Option = Option("-location", .separate, attributes: [.noDriver], metaVar: "<location>", helpText: "Filter nodes with the given location.")
388388
public static let location_: Option = Option("--location", .separate, alias: Option.location, attributes: [.noDriver], metaVar: "<location>", helpText: "Filter nodes with the given location.")
389389
public static let lto: Option = Option("-lto=", .joined, attributes: [.frontend, .noInteractive], helpText: "Specify the LTO type to either 'llvm-thin' or 'llvm-full'")
390+
public static let ltoLibrary: Option = Option("-lto-library", .separate, attributes: [.frontend, .argumentIsPath, .noInteractive], metaVar: "<lto-library>", helpText: "Perform LTO with <lto-library>")
390391
public static let L: Option = Option("-L", .joinedOrSeparate, attributes: [.frontend, .doesNotAffectIncrementalBuild, .argumentIsPath], helpText: "Add directory to library link search path", group: .linkerOption)
391392
public static let l: Option = Option("-l", .joined, attributes: [.frontend, .doesNotAffectIncrementalBuild], helpText: "Specifies a library which should be linked against", group: .linkerOption)
392393
public static let mergeModules: Option = Option("-merge-modules", .flag, attributes: [.frontend, .noDriver], helpText: "Merge the input modules without otherwise processing them", group: .modes)

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3786,12 +3786,32 @@ final class SwiftDriverTests: XCTestCase {
37863786
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .link])
37873787
XCTAssertTrue(plannedJobs[1].commandLine.contains("-lto_library"))
37883788
}
3789+
3790+
do {
3791+
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-thin", "-lto-library", "/foo/libLTO.dylib", "-target", "x86_64-apple-macos11.0"])
3792+
let plannedJobs = try driver.planBuild()
3793+
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .link])
3794+
XCTAssertFalse(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/foo/libLTO.dylib"))))
3795+
XCTAssertTrue(plannedJobs[1].commandLine.contains("-lto_library"))
3796+
XCTAssertTrue(plannedJobs[1].commandLine.contains(.path(try VirtualPath(path: "/foo/libLTO.dylib"))))
3797+
}
3798+
37893799
do {
37903800
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full", "-target", "x86_64-apple-macos11.0"])
37913801
let plannedJobs = try driver.planBuild()
37923802
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .link])
37933803
XCTAssertTrue(plannedJobs[1].commandLine.contains("-lto_library"))
37943804
}
3805+
3806+
do {
3807+
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full", "-lto-library", "/foo/libLTO.dylib", "-target", "x86_64-apple-macos11.0"])
3808+
let plannedJobs = try driver.planBuild()
3809+
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .link])
3810+
XCTAssertFalse(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/foo/libLTO.dylib"))))
3811+
XCTAssertTrue(plannedJobs[1].commandLine.contains("-lto_library"))
3812+
XCTAssertTrue(plannedJobs[1].commandLine.contains(.path(try VirtualPath(path: "/foo/libLTO.dylib"))))
3813+
}
3814+
37953815
do {
37963816
var driver = try Driver(args: ["swiftc", "foo.swift", "-target", "x86_64-apple-macos11.0"])
37973817
let plannedJobs = try driver.planBuild()

0 commit comments

Comments
 (0)