Skip to content

Commit 401f6f7

Browse files
authored
swiftPackages.*: build with the default LLVM version (#439408)
2 parents c6a788f + 997d97b commit 401f6f7

File tree

21 files changed

+312
-238
lines changed

21 files changed

+312
-238
lines changed

pkgs/by-name/ag/age-plugin-se/package.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
lib,
33
fetchFromGitHub,
4+
llvmPackages,
45
swiftPackages,
56
swift,
67
swiftpm,
78
nix-update-script,
89
}:
910
let
10-
inherit (swiftPackages) stdenv;
11+
inherit (llvmPackages) stdenv;
1112
in
1213
stdenv.mkDerivation (finalAttrs: {
1314
pname = "age-plugin-se";

pkgs/development/compilers/swift/compiler/default.nix

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ let
111111
"swift-remote-mirror-headers"
112112
];
113113

114+
clangForWrappers = clang.override (prev: {
115+
extraBuildCommands =
116+
prev.extraBuildCommands
117+
# We need to use the resource directory corresponding to Swift’s
118+
# version of Clang instead of passing along the one from the
119+
# `cc-wrapper` flags.
120+
+ ''
121+
substituteInPlace $out/nix-support/cc-cflags \
122+
--replace-fail " -resource-dir=$out/resource-root" ""
123+
'';
124+
});
125+
114126
# Build a tool used during the build to create a custom clang wrapper, with
115127
# which we wrap the clang produced by the swift build.
116128
#
@@ -130,11 +142,14 @@ let
130142
unwrappedClang="$targetFile-unwrapped"
131143
132144
mv "$targetFile" "$unwrappedClang"
133-
sed < '${clang}/bin/clang' > "$targetFile" \
145+
sed < '${clangForWrappers}/bin/clang' > "$targetFile" \
134146
-e 's|^\s*exec|exec -a "$0"|g' \
135147
-e 's|^\[\[ "${clang.cc}/bin/clang" = \*++ ]]|[[ "$0" = *++ ]]|' \
136148
-e "s|${clang.cc}/bin/clang|$unwrappedClang|g" \
137-
-e "s|^\(\s*\)\($unwrappedClang\) \"@\\\$responseFile\"|\1argv0=\$0\n\1${bash}/bin/bash -c \"exec -a '\$argv0' \2 '@\$responseFile'\"|"
149+
-e "s|^\(\s*\)\($unwrappedClang\) \"@\\\$responseFile\"|\1argv0=\$0\n\1${bash}/bin/bash -c \"exec -a '\$argv0' \2 '@\$responseFile'\"|" \
150+
${lib.optionalString (clang.libcxx != null) ''
151+
-e 's|$NIX_CXXSTDLIB_COMPILE_${clang.suffixSalt}|-isystem '$SWIFT_BUILD_ROOT'/libcxx/include/c++/v1|g'
152+
''}
138153
chmod a+x "$targetFile"
139154
'';
140155

@@ -146,13 +161,12 @@ let
146161
# executable uses $0 to detect what tool is called.
147162
wrapperParams = {
148163
inherit bintools;
149-
default_cc_wrapper = clang; # Instead of `@out@` in the original.
150164
coreutils_bin = lib.getBin coreutils;
151165
gnugrep_bin = gnugrep;
152166
suffixSalt = lib.replaceStrings [ "-" "." ] [ "_" "_" ] targetPlatform.config;
153167
use_response_file_by_default = 1;
154168
swiftDriver = "";
155-
# NOTE: @prog@ needs to be filled elsewhere.
169+
# NOTE: @cc_wrapper@ and @prog@ need to be filled elsewhere.
156170
};
157171
swiftWrapper = runCommand "swift-wrapper.sh" wrapperParams ''
158172
# Make empty to avoid adding the SDK’s modules in the bootstrap wrapper. Otherwise, the SDK conflicts with the
@@ -168,7 +182,11 @@ let
168182
mv "$targetFile" "$unwrappedSwift"
169183
sed < '${swiftWrapper}' > "$targetFile" \
170184
-e "s|@prog@|'$unwrappedSwift'|g" \
171-
-e 's|exec "$prog"|exec -a "$0" "$prog"|g'
185+
-e 's|@cc_wrapper@|${clangForWrappers}|g' \
186+
-e 's|exec "$prog"|exec -a "$0" "$prog"|g' \
187+
${lib.optionalString (clang.libcxx != null) ''
188+
-e 's|$NIX_CXXSTDLIB_COMPILE_${clang.suffixSalt}|-isystem '$SWIFT_BUILD_ROOT'/libcxx/include/c++/v1|g'
189+
''}
172190
chmod a+x "$targetFile"
173191
'';
174192

@@ -308,10 +326,12 @@ stdenv.mkDerivation {
308326
309327
patch -p1 -d swift -i ${./patches/swift-cmake-3.25-compat.patch}
310328
patch -p1 -d swift -i ${./patches/swift-wrap.patch}
311-
patch -p1 -d swift -i ${./patches/swift-nix-resource-root.patch}
312329
patch -p1 -d swift -i ${./patches/swift-linux-fix-libc-paths.patch}
313-
patch -p1 -d swift -i ${./patches/swift-linux-fix-linking.patch}
314-
patch -p1 -d swift -i ${./patches/swift-darwin-libcxx-flags.patch}
330+
patch -p1 -d swift -i ${
331+
replaceVars ./patches/swift-linux-fix-linking.patch {
332+
inherit clang;
333+
}
334+
}
315335
patch -p1 -d swift -i ${
316336
replaceVars ./patches/swift-darwin-plistbuddy-workaround.patch {
317337
inherit swiftArch;
@@ -354,14 +374,27 @@ stdenv.mkDerivation {
354374
stripLen = 1;
355375
hash = "sha256-u0zSejEjfrH3ZoMFm1j+NVv2t5AP9cE5yhsrdTS1dG4=";
356376
})
377+
378+
# Fix the build with modern libc++.
379+
(fetchpatch {
380+
name = "add-cstdio.patch";
381+
url = "https://github.com/llvm/llvm-project/commit/73e15b5edb4fa4a77e68c299a6e3b21e610d351f.patch";
382+
stripLen = 1;
383+
hash = "sha256-eFcvxZaAuBsY/bda1h9212QevrXyvCHw8Cr9ngetDr0=";
384+
})
385+
(fetchpatch {
386+
url = "https://github.com/llvm/llvm-project/commit/68744ffbdd7daac41da274eef9ac0d191e11c16d.patch";
387+
stripLen = 1;
388+
hash = "sha256-QCGhsL/mi7610ZNb5SqxjRGjwJeK2rwtsFVGeG3PUGc=";
389+
})
357390
]
358391
}; do
359392
patch -p1 -d llvm-project/lldb -i $lldbPatch
360393
done
361394
362395
patch -p1 -d llvm-project/clang -i ${./patches/clang-toolchain-dir.patch}
363396
patch -p1 -d llvm-project/clang -i ${./patches/clang-wrap.patch}
364-
patch -p1 -d llvm-project/clang -i ${../../llvm/12/clang/purity.patch}
397+
patch -p1 -d llvm-project/clang -i ${./patches/clang-purity.patch}
365398
patch -p2 -d llvm-project/clang -i ${
366399
fetchpatch {
367400
name = "clang-cmake-fix-interpreter.patch";
@@ -419,14 +452,26 @@ stdenv.mkDerivation {
419452
patchShebangs .
420453
421454
${lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
455+
patch -p1 -d swift-corelibs-libdispatch -i ${
456+
# Fix the build with modern Clang.
457+
fetchpatch {
458+
url = "https://github.com/swiftlang/swift-corelibs-libdispatch/commit/30bb8019ba79cdae0eb1dc0c967c17996dd5cc0a.patch";
459+
hash = "sha256-wPZQ4wtEWk8HaKMfzjamlU6p/IW5EFiTssY63rGM+ZA=";
460+
}
461+
}
462+
422463
# NOTE: This interferes with ABI stability on Darwin, which uses the system
423464
# libraries in the hardcoded path /usr/lib/swift.
424465
fixCmakeFiles .
425466
''}
426467
'';
427468

428469
# > clang-15-unwrapped: error: unsupported option '-fzero-call-used-regs=used-gpr' for target 'arm64-apple-macosx10.9.0'
429-
hardeningDisable = lib.optional stdenv.hostPlatform.isAarch64 "zerocallusedregs";
470+
# > clang-15-unwrapped: error: argument unused during compilation: '-fstack-clash-protection' [-Werror,-Wunused-command-line-argument]
471+
hardeningDisable = lib.optionals stdenv.hostPlatform.isAarch64 [
472+
"zerocallusedregs"
473+
"stackclashprotection"
474+
];
430475

431476
configurePhase = ''
432477
export SWIFT_SOURCE_ROOT="$PWD"
@@ -469,6 +514,19 @@ stdenv.mkDerivation {
469514
cmakeFlags="-GNinja"
470515
buildProject swift-cmark
471516
517+
${lib.optionalString (clang.libcxx != null) ''
518+
# Install the libc++ headers corresponding to the LLVM version of
519+
# Swift’s Clang.
520+
cmakeFlags="
521+
-GNinja
522+
-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi
523+
-DLIBCXXABI_INSTALL_INCLUDE_DIR=$dev/include/c++/v1
524+
"
525+
ninjaFlags="install-cxx-headers install-cxxabi-headers"
526+
buildProject libcxx llvm-project/runtimes
527+
unset ninjaFlags
528+
''}
529+
472530
# Some notes:
473531
# - The Swift build just needs Clang.
474532
# - We can further reduce targets to just our targetPlatform.
@@ -486,6 +544,10 @@ stdenv.mkDerivation {
486544
"
487545
buildProject llvm llvm-project/llvm
488546
547+
# Ensure that the built Clang can find the runtime libraries by
548+
# copying the symlinks from the main wrapper.
549+
cp -P ${clang}/resource-root/{lib,share} $SWIFT_BUILD_ROOT/llvm/lib/clang/15.0.0/
550+
489551
''
490552
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
491553
# Add appleSwiftCore to the search paths. Adding the whole SDK results in build failures.
@@ -658,7 +720,7 @@ stdenv.mkDerivation {
658720
mv llvm/bin/clang-15{-unwrapped,}
659721
mv swift/bin/swift-frontend{-unwrapped,}
660722
661-
mkdir $out $lib
723+
mkdir $lib
662724
663725
# Install clang binaries only. We hide these with the wrapper, so they are
664726
# for private use by Swift only.
@@ -693,14 +755,8 @@ stdenv.mkDerivation {
693755
ln -s $lib/lib/swift $out/lib/swift
694756
695757
# Swift has a separate resource root from Clang, but locates the Clang
696-
# resource root via subdir or symlink. Provide a default here, but we also
697-
# patch Swift to prefer NIX_CC if set.
698-
#
699-
# NOTE: We don't symlink directly here, because that'd add a run-time dep
700-
# on the full Clang compiler to every Swift executable. The copy here is
701-
# just copying the 3 symlinks inside to smaller closures.
702-
mkdir $lib/lib/swift/clang
703-
cp -P ${clang}/resource-root/* $lib/lib/swift/clang/
758+
# resource root via subdir or symlink.
759+
mv $SWIFT_BUILD_ROOT/llvm/lib/clang/15.0.0 $lib/lib/swift/clang
704760
'';
705761

706762
preFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
@@ -756,6 +812,10 @@ stdenv.mkDerivation {
756812
swiftStaticLibSubdir
757813
;
758814

815+
tests = {
816+
cxx-interop-test = callPackage ../cxx-interop-test { };
817+
};
818+
759819
# Internal attr for the wrapper.
760820
_wrapperParams = wrapperParams;
761821
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
From 4add81bba40dcec62c4ea4481be8e35ac53e89d8 Mon Sep 17 00:00:00 2001
2+
From: Will Dietz <[email protected]>
3+
Date: Thu, 18 May 2017 11:56:12 -0500
4+
Subject: [PATCH] "purity" patch for 5.0
5+
6+
---
7+
lib/Driver/ToolChains/Gnu.cpp | 7 -------
8+
1 file changed, 7 deletions(-)
9+
10+
diff --git a/lib/Driver/ToolChains/Gnu.cpp b/lib/Driver/ToolChains/Gnu.cpp
11+
index fe3c0191bb..c6a482bece 100644
12+
--- a/lib/Driver/ToolChains/Gnu.cpp
13+
+++ b/lib/Driver/ToolChains/Gnu.cpp
14+
@@ -487,12 +487,6 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
15+
if (!IsStatic) {
16+
if (Args.hasArg(options::OPT_rdynamic))
17+
CmdArgs.push_back("-export-dynamic");
18+
-
19+
- if (!Args.hasArg(options::OPT_shared) && !IsStaticPIE) {
20+
- CmdArgs.push_back("-dynamic-linker");
21+
- CmdArgs.push_back(Args.MakeArgString(Twine(D.DyldPrefix) +
22+
- ToolChain.getDynamicLinker(Args)));
23+
- }
24+
}
25+
26+
CmdArgs.push_back("-o");
27+
--
28+
2.11.0

pkgs/development/compilers/swift/compiler/patches/swift-darwin-libcxx-flags.patch

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1+
diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp
2+
index c0ee9217e8..bf7737d6fa 100644
13
--- a/lib/Driver/ToolChains.cpp
24
+++ b/lib/Driver/ToolChains.cpp
3-
@@ -1475,7 +1475,17 @@ const char *ToolChain::getClangLinkerDriver(
5+
@@ -1489,6 +1489,12 @@
6+
LinkerDriver = Args.MakeArgString(tool.get());
7+
}
48

5-
// If there is a linker driver in the toolchain folder, use that instead.
6-
if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {toolchainPath}))
7-
- LinkerDriver = Args.MakeArgString(tool.get());
8-
+ return Args.MakeArgString(tool.get());
9-
+ }
10-
+
11-
+ // For Nix, prefer linking using the wrapped system clang, instead of using
9+
+ // For Nix, prefer linking using the wrapped Nixpkgs clang, instead of using
1210
+ // the unwrapped clang packaged with swift. The latter is unable to link, but
1311
+ // we still want to use it for other purposes (clang importer).
14-
+ if (auto nixCC = llvm::sys::Process::GetEnv("NIX_CC")) {
15-
+ llvm::SmallString<128> binDir(nixCC.getValue());
16-
+ llvm::sys::path::append(binDir, "bin");
17-
+ if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {binDir.str()}))
18-
+ return Args.MakeArgString(tool.get());
19-
}
20-
12+
+ if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {"@clang@/bin"}))
13+
+ return Args.MakeArgString(tool.get());
14+
+
2115
return LinkerDriver;
16+
}
17+

0 commit comments

Comments
 (0)