From 4eba26cb252829d19476d7fea04cb178e4963da6 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Thu, 6 Aug 2026 19:56:05 -0400 Subject: [PATCH] swiftPackages_ng.swift: include the stdlib in the toolchain --- .../swift_ng/by-name/sw/swift/package.nix | 21 +++++++++++++ .../swift_ng/by-name/sw/swift/setup-hook.sh | 30 +++++++++++++++++++ pkgs/top-level/swift-packages.nix | 1 + 3 files changed, 52 insertions(+) create mode 100644 pkgs/development/compilers/swift_ng/by-name/sw/swift/setup-hook.sh diff --git a/pkgs/development/compilers/swift_ng/by-name/sw/swift/package.nix b/pkgs/development/compilers/swift_ng/by-name/sw/swift/package.nix index b090cfe4203f..5216918eec0f 100644 --- a/pkgs/development/compilers/swift_ng/by-name/sw/swift/package.nix +++ b/pkgs/development/compilers/swift_ng/by-name/sw/swift/package.nix @@ -3,7 +3,9 @@ apple-sdk_14, apple-sdk_26, llvmPackages_upstream, + patchelf, stdenv, + stdlib, swift-corelibs-foundation, swift-corelibs-libdispatch, swift-driver, @@ -29,6 +31,11 @@ let swiftc.out swiftc.dev ] + ++ lib.optionals (stdlib != null) [ + stdlib.dev + stdlib.out + swiftc.dev + ] ++ lib.optionals (swift-driver != null) [ swift-driver.out swift-driver.dev @@ -87,6 +94,10 @@ stdenv.mkDerivation (finalAttrs: { # Will effectively be `buildInputs` when swift is put in `nativeBuildInputs`. depsTargetTargetPropagated = lib.optionals stdenv.targetPlatform.isDarwin [ propagated-sdk ] + ++ lib.optionals (stdlib != null) [ + # Propagate the stdlib to make sure the linker wrapper will pick up the dynamic and static libraries. + stdlib + ] ++ lib.optionals (!stdenv.hostPlatform.isDarwin) ( lib.optionals (swift-corelibs-libdispatch != null) [ swift-corelibs-libdispatch-no-overlay.out @@ -152,6 +163,16 @@ stdenv.mkDerivation (finalAttrs: { recordPropagatedDependencies + ${lib.optionalString (stdlib != null) '' + # Can’t use `replaceVars` because it needs to substitute $out. + substitute ${./setup-hook.sh} "$out/nix-support/setup-hook" \ + --replace-fail @patchelf@ ${lib.escapeShellArg (lib.getExe patchelf)} \ + --replace-fail @objdump@ ${lib.escapeShellArg (lib.getExe' llvmPackages_upstream.llvm "llvm-objdump")} \ + --replace-fail @install_name_tool@ ${lib.escapeShellArg (lib.getExe' llvmPackages_upstream.llvm "llvm-install-name-tool")} \ + --replace-fail @stdlibPath@ ${lib.escapeShellArg stdlib.out} \ + --replace-fail @swiftPath@ "$out" \ + --replace-fail @swiftPlatform@ ${stdenv.hostPlatform.swift.platform} + ''} chmod -R u-w "$out/bin" "$out/lib" "$out/nix-support" # Have to invoke manually because of `buildCommand`. diff --git a/pkgs/development/compilers/swift_ng/by-name/sw/swift/setup-hook.sh b/pkgs/development/compilers/swift_ng/by-name/sw/swift/setup-hook.sh new file mode 100644 index 000000000000..1d82805dab7b --- /dev/null +++ b/pkgs/development/compilers/swift_ng/by-name/sw/swift/setup-hook.sh @@ -0,0 +1,30 @@ +fixSwiftRpathsIn() { + local dir=$1 + + local swiftPath=@swiftPath@/lib + local swiftPathForPlatform=@swiftPath@/lib/swift/@swiftPlatform@ + local stdlibPath=@stdlibPath@/lib + + local f + while IFS= read -d "" f; do + if LC_ALL=C isMachO "$f"; then + IFS= readarray -d $'\n' -t rpaths < <(@objdump@ --macho --rpaths "$f" | tail -n +2) + for oldPath in "${rpaths[@]}"; do + local newPath=${oldPath/$swiftPathForPlatform/$stdlibPath} + newPath=${newPath/$swiftPath/$stdlibPath} + if [ "$newPath" != "$oldPath" ]; then + @install_name_tool@ "$f" -rpath "$oldPath" "$newPath" + fi + done + elif isELF "$f"; then + local oldRpaths=$(@patchelf@ --print-rpath "$f") + local newRpaths=${oldRpaths/$swiftPathForPlatform/$stdlibPath} + newRpaths=${newRpaths/$swiftPath/$stdlibPath} + if [ "$newRpaths" != "$oldRpaths" ]; then + @patchelf@ --set-rpath "$newRpaths" "$f" + fi + fi + done < <(find "$dir" -type f -print0) +} + +fixupOutputHooks+=('fixSwiftRpathsIn $prefix') diff --git a/pkgs/top-level/swift-packages.nix b/pkgs/top-level/swift-packages.nix index 5f053304fae6..6a52a8b5fa7c 100644 --- a/pkgs/top-level/swift-packages.nix +++ b/pkgs/top-level/swift-packages.nix @@ -31,6 +31,7 @@ let ); } // lib.optionalAttrs (bootstrapStage != 2) { + stdlib = null; # Have the bootstrap compiler use its own build of the stdlib. swift-driver = prev.swift-driver.overrideAttrs (old: { pname = "early-${old.pname}"; });