From 2dcdf602729e7923ec59b2e1ce67a0cb3f0d5c7a Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sat, 2 Mar 2024 18:47:01 +0100 Subject: [PATCH] swift: fix build w/ glibc-2.39 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Failing Hydra build: https://hydra.nixos.org/build/249763077/nixlog/12 The problem is that glibc commit 64b1a44183a3094672ed304532bedb9acc707554 marked the `FILE*` argument of a few functions including `fread` & `ferror` as non-null. The applied patch ("Android: add better nullability checks for nullability annotations added in NDK 26") is targeted for the Android platform, but fixes said issue as well: the handle returned from `fopen` is of type `Optional` and the `guard` expression unwraps that now (and throws an exception if `nil` is returned). The previous `nil`-check didn't modify the type of `fp`, but only raised the exception and moved on with `Optional`. It's a little sad that the patch needs to be applied at so many places, but I guess that's what you get with language-level package managers 🤷 Also, seems good-enough to me given that it's actually temporary, the patch is already upstream and will probably be obsolete at one of the next Swift updates. --- .../compilers/swift/sourcekit-lsp/default.nix | 7 +++- .../compilers/swift/swift-driver/default.nix | 6 +++- .../patches/force-unwrap-file-handles.patch | 33 ------------------- .../compilers/swift/swift-format/default.nix | 7 +++- .../patches/force-unwrap-file-handles.patch | 33 ------------------- .../compilers/swift/swiftpm/default.nix | 15 +++++++-- .../patches/force-unwrap-file-handles.patch | 33 ------------------- 7 files changed, 30 insertions(+), 104 deletions(-) delete mode 100644 pkgs/development/compilers/swift/swift-driver/patches/force-unwrap-file-handles.patch delete mode 100644 pkgs/development/compilers/swift/swift-format/patches/force-unwrap-file-handles.patch delete mode 100644 pkgs/development/compilers/swift/swiftpm/patches/force-unwrap-file-handles.patch diff --git a/pkgs/development/compilers/swift/sourcekit-lsp/default.nix b/pkgs/development/compilers/swift/sourcekit-lsp/default.nix index caba3e3441f3..74e687594c16 100644 --- a/pkgs/development/compilers/swift/sourcekit-lsp/default.nix +++ b/pkgs/development/compilers/swift/sourcekit-lsp/default.nix @@ -1,6 +1,7 @@ { lib , stdenv , callPackage +, fetchpatch , pkg-config , swift , swiftpm @@ -41,7 +42,11 @@ stdenv.mkDerivation { patch -p1 -d .build/checkouts/indexstore-db -i ${./patches/indexstore-db-macos-target.patch} swiftpmMakeMutable swift-tools-support-core - patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch} + patch -p1 -d .build/checkouts/swift-tools-support-core -i ${fetchpatch { + url = "https://github.com/apple/swift-tools-support-core/commit/990afca47e75cce136d2f59e464577e68a164035.patch"; + hash = "sha256-PLzWsp+syiUBHhEFS8+WyUcSae5p0Lhk7SSRdNvfouE="; + includes = [ "Sources/TSCBasic/FileSystem.swift" ]; + }} # This toggles a section specific to Xcode XCTest, which doesn't work on # Darwin, where we also use swift-corelibs-xctest. diff --git a/pkgs/development/compilers/swift/swift-driver/default.nix b/pkgs/development/compilers/swift/swift-driver/default.nix index d69a4da0eb3e..3245fa1d8787 100644 --- a/pkgs/development/compilers/swift/swift-driver/default.nix +++ b/pkgs/development/compilers/swift/swift-driver/default.nix @@ -54,7 +54,11 @@ stdenv.mkDerivation { configurePhase = generated.configure + '' swiftpmMakeMutable swift-tools-support-core - patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch} + patch -p1 -d .build/checkouts/swift-tools-support-core -i ${fetchpatch { + url = "https://github.com/apple/swift-tools-support-core/commit/990afca47e75cce136d2f59e464577e68a164035.patch"; + hash = "sha256-PLzWsp+syiUBHhEFS8+WyUcSae5p0Lhk7SSRdNvfouE="; + includes = [ "Sources/TSCBasic/FileSystem.swift" ]; + }} ''; # TODO: Tests depend on indexstore-db being provided by an existing Swift diff --git a/pkgs/development/compilers/swift/swift-driver/patches/force-unwrap-file-handles.patch b/pkgs/development/compilers/swift/swift-driver/patches/force-unwrap-file-handles.patch deleted file mode 100644 index a2f2d38c37c8..000000000000 --- a/pkgs/development/compilers/swift/swift-driver/patches/force-unwrap-file-handles.patch +++ /dev/null @@ -1,33 +0,0 @@ -From 8d9ab4b6ed24a97e8af0cc338a52aacdcf438b8c Mon Sep 17 00:00:00 2001 -From: Pavel Sobolev -Date: Tue, 21 Nov 2023 20:53:33 +0300 -Subject: [PATCH] Force-unwrap file handles. - ---- - Sources/TSCBasic/FileSystem.swift | 4 ++-- - 1 file changed, 2 insertions(+), 2 deletions(-) - -diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift -index 3a63bdf..a1f3d9d 100644 ---- a/Sources/TSCBasic/FileSystem.swift -+++ b/Sources/TSCBasic/FileSystem.swift -@@ -425,7 +425,7 @@ private class LocalFileSystem: FileSystem { - if fp == nil { - throw FileSystemError(errno: errno, path) - } -- defer { fclose(fp) } -+ defer { fclose(fp!) } - - // Read the data one block at a time. - let data = BufferedOutputByteStream() -@@ -455,7 +455,7 @@ private class LocalFileSystem: FileSystem { - if fp == nil { - throw FileSystemError(errno: errno, path) - } -- defer { fclose(fp) } -+ defer { fclose(fp!) } - - // Write the data in one chunk. - var contents = bytes.contents --- -2.42.0 diff --git a/pkgs/development/compilers/swift/swift-format/default.nix b/pkgs/development/compilers/swift/swift-format/default.nix index 2f7e630e6804..a3d939b85cbd 100644 --- a/pkgs/development/compilers/swift/swift-format/default.nix +++ b/pkgs/development/compilers/swift/swift-format/default.nix @@ -1,5 +1,6 @@ { lib , stdenv +, fetchpatch , callPackage , swift , swiftpm @@ -21,7 +22,11 @@ stdenv.mkDerivation { configurePhase = generated.configure + '' swiftpmMakeMutable swift-tools-support-core - patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch} + patch -p1 -d .build/checkouts/swift-tools-support-core -i ${fetchpatch { + url = "https://github.com/apple/swift-tools-support-core/commit/990afca47e75cce136d2f59e464577e68a164035.patch"; + hash = "sha256-PLzWsp+syiUBHhEFS8+WyUcSae5p0Lhk7SSRdNvfouE="; + includes = [ "Sources/TSCBasic/FileSystem.swift" ]; + }} ''; # We only install the swift-format binary, so don't need the other products. diff --git a/pkgs/development/compilers/swift/swift-format/patches/force-unwrap-file-handles.patch b/pkgs/development/compilers/swift/swift-format/patches/force-unwrap-file-handles.patch deleted file mode 100644 index a2f2d38c37c8..000000000000 --- a/pkgs/development/compilers/swift/swift-format/patches/force-unwrap-file-handles.patch +++ /dev/null @@ -1,33 +0,0 @@ -From 8d9ab4b6ed24a97e8af0cc338a52aacdcf438b8c Mon Sep 17 00:00:00 2001 -From: Pavel Sobolev -Date: Tue, 21 Nov 2023 20:53:33 +0300 -Subject: [PATCH] Force-unwrap file handles. - ---- - Sources/TSCBasic/FileSystem.swift | 4 ++-- - 1 file changed, 2 insertions(+), 2 deletions(-) - -diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift -index 3a63bdf..a1f3d9d 100644 ---- a/Sources/TSCBasic/FileSystem.swift -+++ b/Sources/TSCBasic/FileSystem.swift -@@ -425,7 +425,7 @@ private class LocalFileSystem: FileSystem { - if fp == nil { - throw FileSystemError(errno: errno, path) - } -- defer { fclose(fp) } -+ defer { fclose(fp!) } - - // Read the data one block at a time. - let data = BufferedOutputByteStream() -@@ -455,7 +455,7 @@ private class LocalFileSystem: FileSystem { - if fp == nil { - throw FileSystemError(errno: errno, path) - } -- defer { fclose(fp) } -+ defer { fclose(fp!) } - - // Write the data in one chunk. - var contents = bytes.contents --- -2.42.0 diff --git a/pkgs/development/compilers/swift/swiftpm/default.nix b/pkgs/development/compilers/swift/swiftpm/default.nix index 4a7a4ab63cce..2f3cb9530cfe 100644 --- a/pkgs/development/compilers/swift/swiftpm/default.nix +++ b/pkgs/development/compilers/swift/swiftpm/default.nix @@ -1,6 +1,7 @@ { lib , stdenv , callPackage +, fetchpatch , cmake , ninja , git @@ -195,12 +196,22 @@ let ''; }; + # Part of this patch fixes for glibc 2.39: glibc patch 64b1a44183a3094672ed304532bedb9acc707554 + # marks the `FILE*` argument to a few functions including `ferror` & `fread` as non-null. However + # the code passes an `Optional` to these functions. + # This patch uses a `guard` which effectively unwraps the type (or throws an exception). + swift-tools-support-core-glibc-fix = fetchpatch { + url = "https://github.com/apple/swift-tools-support-core/commit/990afca47e75cce136d2f59e464577e68a164035.patch"; + hash = "sha256-PLzWsp+syiUBHhEFS8+WyUcSae5p0Lhk7SSRdNvfouE="; + includes = [ "Sources/TSCBasic/FileSystem.swift" ]; + }; + swift-tools-support-core = mkBootstrapDerivation { name = "swift-tools-support-core"; src = generated.sources.swift-tools-support-core; patches = [ - ./patches/force-unwrap-file-handles.patch + swift-tools-support-core-glibc-fix ]; buildInputs = [ @@ -389,7 +400,7 @@ in stdenv.mkDerivation (commonAttrs // { swiftpmMakeMutable swift-tools-support-core substituteInPlace .build/checkouts/swift-tools-support-core/Sources/TSCTestSupport/XCTestCasePerf.swift \ --replace 'canImport(Darwin)' 'false' - patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch} + patch -p1 -d .build/checkouts/swift-tools-support-core -i ${swift-tools-support-core-glibc-fix} # Prevent a warning about SDK directories we don't have. swiftpmMakeMutable swift-driver diff --git a/pkgs/development/compilers/swift/swiftpm/patches/force-unwrap-file-handles.patch b/pkgs/development/compilers/swift/swiftpm/patches/force-unwrap-file-handles.patch deleted file mode 100644 index a2f2d38c37c8..000000000000 --- a/pkgs/development/compilers/swift/swiftpm/patches/force-unwrap-file-handles.patch +++ /dev/null @@ -1,33 +0,0 @@ -From 8d9ab4b6ed24a97e8af0cc338a52aacdcf438b8c Mon Sep 17 00:00:00 2001 -From: Pavel Sobolev -Date: Tue, 21 Nov 2023 20:53:33 +0300 -Subject: [PATCH] Force-unwrap file handles. - ---- - Sources/TSCBasic/FileSystem.swift | 4 ++-- - 1 file changed, 2 insertions(+), 2 deletions(-) - -diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift -index 3a63bdf..a1f3d9d 100644 ---- a/Sources/TSCBasic/FileSystem.swift -+++ b/Sources/TSCBasic/FileSystem.swift -@@ -425,7 +425,7 @@ private class LocalFileSystem: FileSystem { - if fp == nil { - throw FileSystemError(errno: errno, path) - } -- defer { fclose(fp) } -+ defer { fclose(fp!) } - - // Read the data one block at a time. - let data = BufferedOutputByteStream() -@@ -455,7 +455,7 @@ private class LocalFileSystem: FileSystem { - if fp == nil { - throw FileSystemError(errno: errno, path) - } -- defer { fclose(fp) } -+ defer { fclose(fp!) } - - // Write the data in one chunk. - var contents = bytes.contents --- -2.42.0