Files
nixpkgs/pkgs/development/compilers/swift/swift-driver/patches/linux-fix-linking.patch
Emily 809f7d037c swiftPackages.swift{,-unwrapped,-driver}: ignore $NIX_CC
Swift needs Clang to link with. The linker patch breaks linking
when using Swift with a GCC‐based standard environment.

Similarly, the internal Clang headers in the resource directory are
coupled to the corresponding version of Clang. The resource root patch
caused Swift’s Clang to use the resource directory from the version
of Clang used in the build environment, which is only compatible if
the versions match.

Instead, hard‐code the Clang path in the patches and wrappers.
2025-09-02 04:09:46 +01:00

41 lines
1.9 KiB
Diff

diff --git a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
index a4a735f498..381522cc1f 100644
--- a/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
+++ b/Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
@@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//
+import Foundation
import SwiftOptions
import func TSCBasic.lookupExecutablePath
@@ -120,7 +121,18 @@
// just using `clang` and avoid a dependency on the C++ runtime.
let clangTool: Tool =
parsedOptions.hasArgument(.enableExperimentalCxxInterop) ? .clangxx : .clang
- var clangPath = try getToolPath(clangTool)
+
+ // For Nix, prefer linking using the wrapped Nixpkgs clang, instead of using
+ // the unwrapped clang packaged with swift. The latter is unable to link, but
+ // we still want to use it for other purposes (clang importer).
+ var clangPath: AbsolutePath
+ if let binPath = try? AbsolutePath(validating: "@clang@/bin"),
+ let tool = lookupExecutablePath(filename: parsedOptions.hasArgument(.enableExperimentalCxxInterop)
+ ? "clang++" : "clang",
+ searchPaths: [binPath]) {
+ clangPath = tool
+ } else {
+ clangPath = try getToolPath(clangTool)
if let toolsDirPath = parsedOptions.getLastArgument(.toolsDirectory) {
// FIXME: What if this isn't an absolute path?
let toolsDir = try AbsolutePath(validating: toolsDirPath.asSingle)
@@ -136,6 +148,7 @@
commandLine.appendFlag("-B")
commandLine.appendPath(toolsDir)
}
+ } // Nix
// Executables on Linux get -pie
if targetTriple.os == .linux && linkerOutputType == .executable {