Skip to content

Commit e21e723

Browse files
authored
Merge pull request #1026 from compnerd/resources
SwiftDriver: add support for resolving the registrar via SDKROOT
2 parents 0f9f247 + 6c6c4a9 commit e21e723

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

Sources/SwiftDriver/Jobs/WindowsToolchain+LinkerSupport.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313
import TSCBasic
1414
import SwiftOptions
1515

16+
private func architecture(for triple: Triple) -> String {
17+
// The concept of a "major" arch name only applies to Linux triples
18+
guard triple.os == .linux else { return triple.archName }
19+
20+
// HACK: We don't wrap LLVM's ARM target architecture parsing, and we should
21+
// definitely not try to port it. This check was only normalizing
22+
// "armv7a/armv7r" and similar variants for armv6 to 'armv7' and
23+
// 'armv6', so just take a brute-force approach
24+
if triple.archName.contains("armv7") { return "armv7" }
25+
if triple.archName.contains("armv6") { return "armv6" }
26+
return triple.archName
27+
}
28+
1629
extension WindowsToolchain {
1730
public func addPlatformSpecificLinkerArgs(to commandLine: inout [Job.ArgTemplate],
1831
parsedOptions: inout ParsedOptions,
@@ -111,10 +124,22 @@ extension WindowsToolchain {
111124
commandLine.appendFlag(.L)
112125
commandLine.appendPath(VirtualPath.lookup(targetInfo.runtimeLibraryImportPaths.last!.path))
113126

114-
// FIXME(compnerd) figure out how to ensure that the SDK relative path is
115-
// the last one
116-
commandLine.appendPath(VirtualPath.lookup(targetInfo.runtimeLibraryImportPaths.last!.path)
117-
.appending(component: "swiftrt.obj"))
127+
// Locate the Swift registration helper by honouring any explicit
128+
// `-resource-dir`, `-sdk`, or the `SDKROOT` environment variable, and
129+
// finally falling back to the target information.
130+
let rsrc: VirtualPath
131+
if let resourceDir = parsedOptions.getLastArgument(.resourceDir) {
132+
rsrc = try VirtualPath(path: resourceDir.asSingle)
133+
} else if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
134+
rsrc = try VirtualPath(path: AbsolutePath(validating: sdk)
135+
.appending(components: "usr", "lib", "swift",
136+
targetTriple.platformName() ?? "",
137+
architecture(for: targetTriple))
138+
.pathString)
139+
} else {
140+
rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
141+
}
142+
commandLine.appendPath(rsrc.appending(component: "swiftrt.obj"))
118143

119144
commandLine.append(contentsOf: inputs.compactMap { (input: TypedVirtualPath) -> Job.ArgTemplate? in
120145
switch input.type {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6074,6 +6074,84 @@ final class SwiftDriverTests: XCTestCase {
60746074
let job = jobs.first!
60756075
XCTAssertEqual(job.kind, .link)
60766076
XCTAssertTrue(job.tool.name.hasSuffix(executableName("clang++")))
6077+
#endif
6078+
}
6079+
6080+
func testRegistrarLookup() throws {
6081+
#if os(Windows)
6082+
let SDKROOT: AbsolutePath =
6083+
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/SDKROOT", relativeTo: $0) }
6084+
?? AbsolutePath(validating: "/SDKROOT")
6085+
6086+
let resourceDir: AbsolutePath =
6087+
try localFileSystem.currentWorkingDirectory.map { AbsolutePath("/swift/resources", relativeTo: $0) }
6088+
?? AbsolutePath(validating: "/swift/resources")
6089+
6090+
let platform: String = "windows"
6091+
#if arch(x86_64)
6092+
let arch: String = "x86_64"
6093+
#elseif arch(arm64)
6094+
let arch: String = "aarch64"
6095+
#else
6096+
#error("unsupported build architecture")
6097+
#endif
6098+
6099+
do {
6100+
var driver = try Driver(args: [
6101+
"swiftc", "-emit-library", "-o", "library.dll", "library.obj", "-resource-dir", resourceDir.nativePathString(escaped: false),
6102+
])
6103+
let jobs = try driver.planBuild().removingAutolinkExtractJobs()
6104+
XCTAssertEqual(jobs.count, 1)
6105+
let job = jobs.first!
6106+
XCTAssertEqual(job.kind, .link)
6107+
XCTAssertTrue(job.commandLine.contains(.path(.absolute(resourceDir.appending(component: "swiftrt.obj")))))
6108+
}
6109+
6110+
do {
6111+
var driver = try Driver(args: [
6112+
"swiftc", "-emit-library", "-o", "library.dll", "library.obj", "-sdk", SDKROOT.nativePathString(escaped: false),
6113+
])
6114+
let jobs = try driver.planBuild().removingAutolinkExtractJobs()
6115+
XCTAssertEqual(jobs.count, 1)
6116+
let job = jobs.first!
6117+
XCTAssertEqual(job.kind, .link)
6118+
XCTAssertTrue(job.commandLine.contains(.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrt.obj")))))
6119+
}
6120+
6121+
do {
6122+
var env = ProcessEnv.vars
6123+
env["SDKROOT"] = SDKROOT.nativePathString(escaped: false)
6124+
6125+
var driver = try Driver(args: [
6126+
"swiftc", "-emit-library", "-o", "library.dll", "library.obj"
6127+
], env: env)
6128+
let jobs = try driver.planBuild().removingAutolinkExtractJobs()
6129+
XCTAssertEqual(jobs.count, 1)
6130+
let job = jobs.first!
6131+
XCTAssertEqual(job.kind, .link)
6132+
XCTAssertTrue(job.commandLine.contains(.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrt.obj")))))
6133+
}
6134+
6135+
// Cannot test this due to `SDKROOT` escaping from the execution environment
6136+
// into the `-print-target-info` step, which then resets the
6137+
// `runtimeResourcePath` to be the SDK relative path rahter than the
6138+
// toolchain relative path.
6139+
#if false
6140+
do {
6141+
var env = ProcessEnv.vars
6142+
env["SDKROOT"] = nil
6143+
6144+
var driver = try Driver(args: [
6145+
"swiftc", "-emit-library", "-o", "library.dll", "library.obj"
6146+
], env: env)
6147+
driver.frontendTargetInfo.runtimeResourcePath = SDKROOT
6148+
let jobs = try driver.planBuild().removingAutolinkExtractJobs()
6149+
XCTAssertEqual(jobs.count, 1)
6150+
let job = jobs.first!
6151+
XCTAssertEqual(job.kind, .link)
6152+
XCTAssertTrue(job.commandLine.contains(.path(.absolute(SDKROOT.appending(components: "usr", "lib", "swift", platform, arch, "swiftrt.obj")))))
6153+
}
6154+
#endif
60776155
#endif
60786156
}
60796157
}

0 commit comments

Comments
 (0)