Skip to content

Commit 4d5c8c1

Browse files
committed
Add -gcc-toolchain flag to swift-driver
GCC toolchains are useful when cross-compiling to Linux with a given gcc toolchain. The gcc toolchain contains tools for linking, libraries for linking against, and the headers that map to those libraries. This patch adds a gcc-toolchain flag to the driver so that Swift can pass that down to both the clang importer and clang linker appropriately.
1 parent 679b324 commit 4d5c8c1

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import class TSCBasic.LocalFileOutputByteStream
1414
import class TSCBasic.TerminalController
1515
import struct TSCBasic.RelativePath
16+
import struct TSCBasic.AbsolutePath
1617
import var TSCBasic.stderrStream
1718

1819
/// Whether we should produce color diagnostics by default.
@@ -141,6 +142,11 @@ extension Driver {
141142
try commandLine.appendAll(.F, .Fsystem, from: &parsedOptions)
142143
try commandLine.appendAll(.vfsoverlay, from: &parsedOptions)
143144

145+
if let gccToolchain = parsedOptions.getLastArgument(.gccToolchain) {
146+
commandLine.appendFlag(.Xcc)
147+
commandLine.appendFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
148+
}
149+
144150
try commandLine.appendLast(.AssertConfig, from: &parsedOptions)
145151
try commandLine.appendLast(.autolinkForceLoad, from: &parsedOptions)
146152

Sources/SwiftDriver/Jobs/LinkJob.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ extension Driver {
5555
outputFile = try outputFileForImage
5656
}
5757

58+
if let gccToolchain = parsedOptions.getLastArgument(.gccToolchain) {
59+
commandLine.appendFlag(.XclangLinker)
60+
commandLine.appendFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
61+
}
62+
5863
// Defer to the toolchain for platform-specific linking
5964
let linkTool = try toolchain.addPlatformSpecificLinkerArgs(
6065
to: &commandLine,

Sources/SwiftOptions/Options.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ extension Option {
758758
public static let Xfrontend: Option = Option("-Xfrontend", .separate, attributes: [.helpHidden], metaVar: "<arg>", helpText: "Pass <arg> to the Swift frontend")
759759
public static let Xlinker: Option = Option("-Xlinker", .separate, attributes: [.doesNotAffectIncrementalBuild], helpText: "Specifies an option which should be passed to the linker")
760760
public static let Xllvm: Option = Option("-Xllvm", .separate, attributes: [.helpHidden, .frontend], metaVar: "<arg>", helpText: "Pass <arg> to LLVM.")
761+
public static let gccToolchain: Option = Option("-gcc-toolchain", .separate, attributes: [.helpHidden, .argumentIsPath], metaVar: "<path>", helpText: "Specify a directory where the clang importer and clang linker can find headers and libraries" )
761762
public static let DASHDASH: Option = Option("--", .remaining, attributes: [.frontend, .doesNotAffectIncrementalBuild])
762763
}
763764

@@ -1511,6 +1512,7 @@ extension Option {
15111512
Option.Xfrontend,
15121513
Option.Xlinker,
15131514
Option.Xllvm,
1515+
Option.gccToolchain,
15141516
Option.DASHDASH,
15151517
]
15161518
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6650,6 +6650,18 @@ final class SwiftDriverTests: XCTestCase {
66506650
#endif
66516651
}
66526652

6653+
func testGccToolchainFlags() throws {
6654+
VirtualPath.resetTemporaryFileStore()
6655+
var driver = try Driver(args: [
6656+
"swiftc", "-gcc-toolchain", "foo/as/blarpy", "test.swift"
6657+
])
6658+
let jobs = try driver.planBuild().removingAutolinkExtractJobs()
6659+
XCTAssertEqual(jobs.count, 2)
6660+
let (compileJob, linkJob) = (jobs[0], jobs[1])
6661+
compileJob.commandLine.contains(.flag("--gcc-toolchain=foo/as/blarpy"))
6662+
linkJob.commandLine.contains(.flag("--gcc-toolchain=foo/as/blarpy"))
6663+
}
6664+
66536665
func testPluginPaths() throws {
66546666
var driver = try Driver(args: ["swiftc", "-typecheck", "foo.swift"])
66556667
guard driver.isFrontendArgSupported(.pluginPath) else {

0 commit comments

Comments
 (0)