Skip to content

Swift SDKs: fix toolset.linker.path not passed to -ld-path #7021

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 3 commits into from
Nov 7, 2023
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
14 changes: 14 additions & 0 deletions Sources/Build/BuildManifest/LLBuildManifestBuilder+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ extension LLBuildManifestBuilder {
fileSystem: self.fileSystem,
executor: executor
)
try driver.checkLDPathOption(commandLine: commandLine)

let jobs = try driver.planBuild()
try self.addSwiftDriverJobs(
for: target,
Expand Down Expand Up @@ -291,6 +293,8 @@ extension LLBuildManifestBuilder {
externalTargetModuleDetailsMap: dependencyModuleDetailsMap,
interModuleDependencyOracle: dependencyOracle
)
try driver.checkLDPathOption(commandLine: commandLine)

let jobs = try driver.planBuild()
try self.addSwiftDriverJobs(
for: targetDescription,
Expand Down Expand Up @@ -577,3 +581,13 @@ extension TypedVirtualPath {
}
}
}

extension Driver {
func checkLDPathOption(commandLine: [String]) throws {
// `-ld-path` option is only available in recent versions of the compiler: rdar://117049947
if let option = commandLine.first(where: { $0.hasPrefix("-ld-path") }),
!self.supportedFrontendFeatures.contains("ld-path-driver-option") {
throw LLBuildManifestBuilder.Error.ldPathDriverOptionUnavailable(option: option)
}
}
}
11 changes: 11 additions & 0 deletions Sources/Build/BuildManifest/LLBuildManifestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ import enum TSCBasic.ProcessEnv
import func TSCBasic.topologicalSort

public class LLBuildManifestBuilder {
enum Error: Swift.Error {
case ldPathDriverOptionUnavailable(option: String)

var description: String {
switch self {
case .ldPathDriverOptionUnavailable(let option):
return "Unable to pass \(option), currently used version of `swiftc` doesn't support it."
}
}
}

public enum TargetKind {
case main
case test
Expand Down
6 changes: 5 additions & 1 deletion Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ public final class UserToolchain: Toolchain {
swiftSDK: SwiftSDK,
environment: EnvironmentVariables
) throws -> [String] {
let swiftCompilerFlags = swiftSDK.toolset.knownTools[.swiftCompiler]?.extraCLIOptions ?? []
var swiftCompilerFlags = swiftSDK.toolset.knownTools[.swiftCompiler]?.extraCLIOptions ?? []

if let linker = swiftSDK.toolset.knownTools[.linker]?.path {
swiftCompilerFlags += ["-ld-path=\(linker)"]
}

guard let sdkDir = swiftSDK.pathsConfiguration.sdkRootPath else {
if triple.isWindows() {
Expand Down
10 changes: 7 additions & 3 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3716,7 +3716,7 @@ final class BuildPlanTests: XCTestCase {
.cxxCompiler: .init(extraCLIOptions: [jsonFlag(tool: .cxxCompiler)]),
.swiftCompiler: .init(extraCLIOptions: [jsonFlag(tool: .swiftCompiler)]),
.librarian: .init(path: "/fake/toolchain/usr/bin/librarian"),
.linker: .init(extraCLIOptions: [jsonFlag(tool: .linker)]),
.linker: .init(path: "/fake/toolchain/usr/bin/linker", extraCLIOptions: [jsonFlag(tool: .linker)]),
],
rootPaths: try UserToolchain.default.swiftSDK.toolset.rootPaths)
let targetTriple = try Triple("armv7em-unknown-none-macho")
Expand Down Expand Up @@ -3797,7 +3797,9 @@ final class BuildPlanTests: XCTestCase {
// Compile Swift Target
let exeCompileArguments = try result.target(for: "exe").swiftTarget().compileArguments()
let exeCompileArgumentsPattern: [StringPattern] = [
jsonFlag(tool: .swiftCompiler), "-g", cliFlag(tool: .swiftCompiler),
jsonFlag(tool: .swiftCompiler),
"-ld-path=/fake/toolchain/usr/bin/linker",
"-g", cliFlag(tool: .swiftCompiler),
.anySequence,
"-Xcc", jsonFlag(tool: .cCompiler), "-Xcc", "-g", "-Xcc", cliFlag(tool: .cCompiler),
// TODO: Pass -Xcxx flags to swiftc (#6491)
Expand All @@ -3820,7 +3822,9 @@ final class BuildPlanTests: XCTestCase {
// Link Product
let exeLinkArguments = try result.buildProduct(for: "exe").linkArguments()
let exeLinkArgumentsPattern: [StringPattern] = [
jsonFlag(tool: .swiftCompiler), "-g", cliFlag(tool: .swiftCompiler),
jsonFlag(tool: .swiftCompiler),
"-ld-path=/fake/toolchain/usr/bin/linker",
"-g", cliFlag(tool: .swiftCompiler),
.anySequence,
"-Xlinker", jsonFlag(tool: .linker), "-Xlinker", cliFlag(tool: .linker),
]
Expand Down