From 55378996ba095b9cee99a5ee4b44b9f477316aa7 Mon Sep 17 00:00:00 2001 From: Finagolfin Date: Thu, 27 Feb 2025 10:09:12 +0530 Subject: [PATCH 1/2] [Unix] Go back to only checking the runtime resource path for swiftrt.o --- .../GenericUnixToolchain+LinkerSupport.swift | 23 ++++++------------- Tests/SwiftDriverTests/SwiftDriverTests.swift | 10 +++----- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift index fa2bd8132..3e9382e2a 100644 --- a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift @@ -181,22 +181,13 @@ extension GenericUnixToolchain { } if !isEmbeddedEnabled && !parsedOptions.hasArgument(.nostartfiles) { - let rsrc: VirtualPath - // Prefer the swiftrt.o runtime file from the SDK if it's specified. - if let sdk = targetInfo.sdkPath { - let swiftDir: String - if staticStdlib || staticExecutable { - swiftDir = "swift_static" - } else { - swiftDir = "swift" - } - rsrc = VirtualPath.lookup(sdk.path).appending(components: "usr", "lib", swiftDir) - } else { - rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path) - } - let platform: String = targetTriple.platformName() ?? "" - let architecture: String = majorArchitectureName(for: targetTriple) - commandLine.appendPath(rsrc.appending(components: platform, architecture, "swiftrt.o")) + let swiftrtPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path) + .appending( + components: targetTriple.platformName() ?? "", + String(majorArchitectureName(for: targetTriple)), + "swiftrt.o" + ) + commandLine.appendPath(swiftrtPath) } // If we are linking statically, we need to add all diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index 57559f00d..eebc439e1 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -7621,14 +7621,10 @@ final class SwiftDriverTests: XCTestCase { func testRelativeResourceDir() throws { do { - // Reset the environment to avoid 'SDKROOT' influencing the - // linux driver paths and taking the priority over the resource directory. - var env = ProcessEnv.block - env["SDKROOT"] = nil var driver = try Driver(args: ["swiftc", "-target", "x86_64-unknown-linux", "-lto=llvm-thin", "foo.swift", - "-resource-dir", "resource/dir"], env: env) + "-resource-dir", "resource/dir"]) let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs() let compileJob = plannedJobs[0] @@ -7643,7 +7639,7 @@ final class SwiftDriverTests: XCTestCase { } } - func testSDKDirLinuxPrioritizedOverRelativeResourceDirForLinkingSwiftRT() throws { + func testRelativeResourceDirLinuxPrioritizedOverSDKDirForLinkingSwiftRT() throws { do { let sdkRoot = try testInputsPath.appending(component: "mock-sdk.sdk") var env = ProcessEnv.block @@ -7657,7 +7653,7 @@ final class SwiftDriverTests: XCTestCase { XCTAssertEqual(compileJob.kind, .compile) let linkJob = plannedJobs[1] XCTAssertEqual(linkJob.kind, .link) - try XCTAssertJobInvocationMatches(linkJob, toPathOption(sdkRoot.pathString + "/usr/lib/swift/linux/x86_64/swiftrt.o", isRelative: false)) + try XCTAssertJobInvocationMatches(linkJob, toPathOption("resource/dir/linux/x86_64/swiftrt.o")) } } From c65363be5411c1412bd83877c3c7a8c5f92b8eb1 Mon Sep 17 00:00:00 2001 From: Finagolfin Date: Mon, 10 Nov 2025 23:03:15 +0530 Subject: [PATCH 2/2] Only change if `-sysroot` is specified --- .../GenericUnixToolchain+LinkerSupport.swift | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift index 3e9382e2a..adff764f3 100644 --- a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift @@ -181,13 +181,23 @@ extension GenericUnixToolchain { } if !isEmbeddedEnabled && !parsedOptions.hasArgument(.nostartfiles) { - let swiftrtPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path) - .appending( - components: targetTriple.platformName() ?? "", - String(majorArchitectureName(for: targetTriple)), - "swiftrt.o" - ) - commandLine.appendPath(swiftrtPath) + let rsrc: VirtualPath + // Prefer the swiftrt.o runtime file from the SDK, if it's specified + // with a separate C/C++ sysroot. + if let sdk = targetInfo.sdkPath, parsedOptions.hasArgument(.sysroot) { + let swiftDir: String + if staticStdlib || staticExecutable { + swiftDir = "swift_static" + } else { + swiftDir = "swift" + } + rsrc = VirtualPath.lookup(sdk.path).appending(components: "usr", "lib", swiftDir) + } else { + rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path) + } + let platform: String = targetTriple.platformName() ?? "" + let architecture: String = majorArchitectureName(for: targetTriple) + commandLine.appendPath(rsrc.appending(components: platform, architecture, "swiftrt.o")) } // If we are linking statically, we need to add all