diff --git a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift index fa2bd8132..adff764f3 100644 --- a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift @@ -182,8 +182,9 @@ 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 { + // 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" 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")) } }