From cdc476f3f45bc63711c4c69afbe7e53c4276f41f 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/ToolchainTests.swift | 11 +++------ 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift index 7724d4a8d..272861c9e 100644 --- a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift @@ -187,22 +187,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/ToolchainTests.swift b/Tests/SwiftDriverTests/ToolchainTests.swift index 770509efa..d98b5066d 100644 --- a/Tests/SwiftDriverTests/ToolchainTests.swift +++ b/Tests/SwiftDriverTests/ToolchainTests.swift @@ -485,18 +485,13 @@ import CRT @Test func relativeResourceDir() async 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 TestDriver( args: [ "swiftc", "-target", "x86_64-unknown-linux", "-lto=llvm-thin", "foo.swift", "-resource-dir", "resource/dir", - ], - env: env + ] ) let plannedJobs = try await driver.planBuild().removingAutolinkExtractJobs() @@ -518,7 +513,7 @@ import CRT } } - @Test func sdkDirLinuxPrioritizedOverRelativeResourceDirForLinkingSwiftRT() async throws { + @Test func RelativeResourceDirLinuxPrioritizedOverSDKDirForLinkingSwiftRT() async throws { do { let sdkRoot = try testInputsPath.appending(component: "mock-sdk.sdk") var env = ProcessEnv.block @@ -539,7 +534,7 @@ import CRT #expect(linkJob.kind == .link) try expectJobInvocationMatches( linkJob, - toPathOption(sdkRoot.pathString + "/usr/lib/swift/linux/x86_64/swiftrt.o", isRelative: false) + toPathOption("resource/dir/linux/x86_64/swiftrt.o") ) } } From 14c512a454773ec35de1fc23bc1be95351b4a343 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 272861c9e..31c49312e 100644 --- a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift @@ -187,13 +187,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