-
-
Notifications
You must be signed in to change notification settings - Fork 19.7k
swiftPackages.*: build with the default LLVM version #439408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0ec026a
efde96d
810bed0
e0d9c93
3b773c7
828d2d7
1474a62
1884f3d
809f7d0
37cad5d
2153141
a1a2f97
997d97b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,6 +111,18 @@ let | |
| "swift-remote-mirror-headers" | ||
| ]; | ||
|
|
||
| clangForWrappers = clang.override (prev: { | ||
| extraBuildCommands = | ||
| prev.extraBuildCommands | ||
| # We need to use the resource directory corresponding to Swift’s | ||
| # version of Clang instead of passing along the one from the | ||
| # `cc-wrapper` flags. | ||
| + '' | ||
| substituteInPlace $out/nix-support/cc-cflags \ | ||
| --replace-fail " -resource-dir=$out/resource-root" "" | ||
| ''; | ||
| }); | ||
|
|
||
| # Build a tool used during the build to create a custom clang wrapper, with | ||
| # which we wrap the clang produced by the swift build. | ||
| # | ||
|
|
@@ -130,11 +142,14 @@ let | |
| unwrappedClang="$targetFile-unwrapped" | ||
|
|
||
| mv "$targetFile" "$unwrappedClang" | ||
| sed < '${clang}/bin/clang' > "$targetFile" \ | ||
| sed < '${clangForWrappers}/bin/clang' > "$targetFile" \ | ||
| -e 's|^\s*exec|exec -a "$0"|g' \ | ||
| -e 's|^\[\[ "${clang.cc}/bin/clang" = \*++ ]]|[[ "$0" = *++ ]]|' \ | ||
| -e "s|${clang.cc}/bin/clang|$unwrappedClang|g" \ | ||
| -e "s|^\(\s*\)\($unwrappedClang\) \"@\\\$responseFile\"|\1argv0=\$0\n\1${bash}/bin/bash -c \"exec -a '\$argv0' \2 '@\$responseFile'\"|" | ||
| -e "s|^\(\s*\)\($unwrappedClang\) \"@\\\$responseFile\"|\1argv0=\$0\n\1${bash}/bin/bash -c \"exec -a '\$argv0' \2 '@\$responseFile'\"|" \ | ||
| ${lib.optionalString (clang.libcxx != null) '' | ||
| -e 's|$NIX_CXXSTDLIB_COMPILE_${clang.suffixSalt}|-isystem '$SWIFT_BUILD_ROOT'/libcxx/include/c++/v1|g' | ||
| ''} | ||
| chmod a+x "$targetFile" | ||
| ''; | ||
|
|
||
|
|
@@ -146,13 +161,12 @@ let | |
| # executable uses $0 to detect what tool is called. | ||
| wrapperParams = { | ||
| inherit bintools; | ||
| default_cc_wrapper = clang; # Instead of `@out@` in the original. | ||
| coreutils_bin = lib.getBin coreutils; | ||
| gnugrep_bin = gnugrep; | ||
| suffixSalt = lib.replaceStrings [ "-" "." ] [ "_" "_" ] targetPlatform.config; | ||
| use_response_file_by_default = 1; | ||
| swiftDriver = ""; | ||
| # NOTE: @prog@ needs to be filled elsewhere. | ||
| # NOTE: @cc_wrapper@ and @prog@ need to be filled elsewhere. | ||
| }; | ||
| swiftWrapper = runCommand "swift-wrapper.sh" wrapperParams '' | ||
| # Make empty to avoid adding the SDK’s modules in the bootstrap wrapper. Otherwise, the SDK conflicts with the | ||
|
|
@@ -168,7 +182,11 @@ let | |
| mv "$targetFile" "$unwrappedSwift" | ||
| sed < '${swiftWrapper}' > "$targetFile" \ | ||
| -e "s|@prog@|'$unwrappedSwift'|g" \ | ||
| -e 's|exec "$prog"|exec -a "$0" "$prog"|g' | ||
| -e 's|@cc_wrapper@|${clangForWrappers}|g' \ | ||
| -e 's|exec "$prog"|exec -a "$0" "$prog"|g' \ | ||
| ${lib.optionalString (clang.libcxx != null) '' | ||
| -e 's|$NIX_CXXSTDLIB_COMPILE_${clang.suffixSalt}|-isystem '$SWIFT_BUILD_ROOT'/libcxx/include/c++/v1|g' | ||
| ''} | ||
| chmod a+x "$targetFile" | ||
| ''; | ||
|
|
||
|
|
@@ -308,10 +326,12 @@ stdenv.mkDerivation { | |
|
|
||
| patch -p1 -d swift -i ${./patches/swift-cmake-3.25-compat.patch} | ||
| patch -p1 -d swift -i ${./patches/swift-wrap.patch} | ||
| patch -p1 -d swift -i ${./patches/swift-nix-resource-root.patch} | ||
| patch -p1 -d swift -i ${./patches/swift-linux-fix-libc-paths.patch} | ||
| patch -p1 -d swift -i ${./patches/swift-linux-fix-linking.patch} | ||
| patch -p1 -d swift -i ${./patches/swift-darwin-libcxx-flags.patch} | ||
| patch -p1 -d swift -i ${ | ||
| replaceVars ./patches/swift-linux-fix-linking.patch { | ||
| inherit clang; | ||
| } | ||
| } | ||
| patch -p1 -d swift -i ${ | ||
| replaceVars ./patches/swift-darwin-plistbuddy-workaround.patch { | ||
| inherit swiftArch; | ||
|
|
@@ -354,14 +374,27 @@ stdenv.mkDerivation { | |
| stripLen = 1; | ||
| hash = "sha256-u0zSejEjfrH3ZoMFm1j+NVv2t5AP9cE5yhsrdTS1dG4="; | ||
| }) | ||
|
|
||
| # Fix the build with modern libc++. | ||
| (fetchpatch { | ||
| name = "add-cstdio.patch"; | ||
| url = "https://github.com/llvm/llvm-project/commit/73e15b5edb4fa4a77e68c299a6e3b21e610d351f.patch"; | ||
| stripLen = 1; | ||
| hash = "sha256-eFcvxZaAuBsY/bda1h9212QevrXyvCHw8Cr9ngetDr0="; | ||
| }) | ||
| (fetchpatch { | ||
| url = "https://github.com/llvm/llvm-project/commit/68744ffbdd7daac41da274eef9ac0d191e11c16d.patch"; | ||
| stripLen = 1; | ||
| hash = "sha256-QCGhsL/mi7610ZNb5SqxjRGjwJeK2rwtsFVGeG3PUGc="; | ||
| }) | ||
| ] | ||
| }; do | ||
| patch -p1 -d llvm-project/lldb -i $lldbPatch | ||
| done | ||
|
|
||
| patch -p1 -d llvm-project/clang -i ${./patches/clang-toolchain-dir.patch} | ||
| patch -p1 -d llvm-project/clang -i ${./patches/clang-wrap.patch} | ||
| patch -p1 -d llvm-project/clang -i ${../../llvm/12/clang/purity.patch} | ||
| patch -p1 -d llvm-project/clang -i ${./patches/clang-purity.patch} | ||
| patch -p2 -d llvm-project/clang -i ${ | ||
| fetchpatch { | ||
| name = "clang-cmake-fix-interpreter.patch"; | ||
|
|
@@ -419,14 +452,26 @@ stdenv.mkDerivation { | |
| patchShebangs . | ||
|
|
||
| ${lib.optionalString (!stdenv.hostPlatform.isDarwin) '' | ||
| patch -p1 -d swift-corelibs-libdispatch -i ${ | ||
| # Fix the build with modern Clang. | ||
| fetchpatch { | ||
| url = "https://github.com/swiftlang/swift-corelibs-libdispatch/commit/30bb8019ba79cdae0eb1dc0c967c17996dd5cc0a.patch"; | ||
| hash = "sha256-wPZQ4wtEWk8HaKMfzjamlU6p/IW5EFiTssY63rGM+ZA="; | ||
| } | ||
| } | ||
|
|
||
| # NOTE: This interferes with ABI stability on Darwin, which uses the system | ||
| # libraries in the hardcoded path /usr/lib/swift. | ||
| fixCmakeFiles . | ||
| ''} | ||
| ''; | ||
|
|
||
| # > clang-15-unwrapped: error: unsupported option '-fzero-call-used-regs=used-gpr' for target 'arm64-apple-macosx10.9.0' | ||
| hardeningDisable = lib.optional stdenv.hostPlatform.isAarch64 "zerocallusedregs"; | ||
| # > clang-15-unwrapped: error: argument unused during compilation: '-fstack-clash-protection' [-Werror,-Wunused-command-line-argument] | ||
| hardeningDisable = lib.optionals stdenv.hostPlatform.isAarch64 [ | ||
| "zerocallusedregs" | ||
| "stackclashprotection" | ||
| ]; | ||
|
|
||
| configurePhase = '' | ||
| export SWIFT_SOURCE_ROOT="$PWD" | ||
|
|
@@ -469,6 +514,19 @@ stdenv.mkDerivation { | |
| cmakeFlags="-GNinja" | ||
| buildProject swift-cmark | ||
|
|
||
| ${lib.optionalString (clang.libcxx != null) '' | ||
| # Install the libc++ headers corresponding to the LLVM version of | ||
| # Swift’s Clang. | ||
| cmakeFlags=" | ||
| -GNinja | ||
| -DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi | ||
| -DLIBCXXABI_INSTALL_INCLUDE_DIR=$dev/include/c++/v1 | ||
| " | ||
| ninjaFlags="install-cxx-headers install-cxxabi-headers" | ||
| buildProject libcxx llvm-project/runtimes | ||
| unset ninjaFlags | ||
| ''} | ||
|
|
||
| # Some notes: | ||
| # - The Swift build just needs Clang. | ||
| # - We can further reduce targets to just our targetPlatform. | ||
|
|
@@ -486,6 +544,10 @@ stdenv.mkDerivation { | |
| " | ||
| buildProject llvm llvm-project/llvm | ||
|
|
||
| # Ensure that the built Clang can find the runtime libraries by | ||
| # copying the symlinks from the main wrapper. | ||
| cp -P ${clang}/resource-root/{lib,share} $SWIFT_BUILD_ROOT/llvm/lib/clang/15.0.0/ | ||
|
|
||
| '' | ||
| + lib.optionalString stdenv.hostPlatform.isDarwin '' | ||
| # Add appleSwiftCore to the search paths. Adding the whole SDK results in build failures. | ||
|
|
@@ -658,7 +720,7 @@ stdenv.mkDerivation { | |
| mv llvm/bin/clang-15{-unwrapped,} | ||
| mv swift/bin/swift-frontend{-unwrapped,} | ||
|
|
||
| mkdir $out $lib | ||
| mkdir $lib | ||
|
|
||
| # Install clang binaries only. We hide these with the wrapper, so they are | ||
| # for private use by Swift only. | ||
|
|
@@ -693,14 +755,8 @@ stdenv.mkDerivation { | |
| ln -s $lib/lib/swift $out/lib/swift | ||
|
|
||
| # Swift has a separate resource root from Clang, but locates the Clang | ||
| # resource root via subdir or symlink. Provide a default here, but we also | ||
| # patch Swift to prefer NIX_CC if set. | ||
| # | ||
| # NOTE: We don't symlink directly here, because that'd add a run-time dep | ||
| # on the full Clang compiler to every Swift executable. The copy here is | ||
|
Comment on lines
-699
to
-700
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be worth keeping this comment around?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’d need some tweaking to the wording now that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good enough for me! |
||
| # just copying the 3 symlinks inside to smaller closures. | ||
| mkdir $lib/lib/swift/clang | ||
| cp -P ${clang}/resource-root/* $lib/lib/swift/clang/ | ||
| # resource root via subdir or symlink. | ||
| mv $SWIFT_BUILD_ROOT/llvm/lib/clang/15.0.0 $lib/lib/swift/clang | ||
| ''; | ||
|
|
||
| preFixup = lib.optionalString stdenv.hostPlatform.isLinux '' | ||
|
|
@@ -756,6 +812,10 @@ stdenv.mkDerivation { | |
| swiftStaticLibSubdir | ||
| ; | ||
|
|
||
| tests = { | ||
| cxx-interop-test = callPackage ../cxx-interop-test { }; | ||
| }; | ||
|
|
||
| # Internal attr for the wrapper. | ||
| _wrapperParams = wrapperParams; | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| From 4add81bba40dcec62c4ea4481be8e35ac53e89d8 Mon Sep 17 00:00:00 2001 | ||
| From: Will Dietz <w@wdtz.org> | ||
| Date: Thu, 18 May 2017 11:56:12 -0500 | ||
| Subject: [PATCH] "purity" patch for 5.0 | ||
|
|
||
| --- | ||
| lib/Driver/ToolChains/Gnu.cpp | 7 ------- | ||
| 1 file changed, 7 deletions(-) | ||
|
|
||
| diff --git a/lib/Driver/ToolChains/Gnu.cpp b/lib/Driver/ToolChains/Gnu.cpp | ||
| index fe3c0191bb..c6a482bece 100644 | ||
| --- a/lib/Driver/ToolChains/Gnu.cpp | ||
| +++ b/lib/Driver/ToolChains/Gnu.cpp | ||
| @@ -487,12 +487,6 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, | ||
| if (!IsStatic) { | ||
| if (Args.hasArg(options::OPT_rdynamic)) | ||
| CmdArgs.push_back("-export-dynamic"); | ||
| - | ||
| - if (!Args.hasArg(options::OPT_shared) && !IsStaticPIE) { | ||
| - CmdArgs.push_back("-dynamic-linker"); | ||
| - CmdArgs.push_back(Args.MakeArgString(Twine(D.DyldPrefix) + | ||
| - ToolChain.getDynamicLinker(Args))); | ||
| - } | ||
| } | ||
|
|
||
| CmdArgs.push_back("-o"); | ||
| -- | ||
| 2.11.0 |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,17 @@ | ||
| diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp | ||
| index c0ee9217e8..bf7737d6fa 100644 | ||
| --- a/lib/Driver/ToolChains.cpp | ||
| +++ b/lib/Driver/ToolChains.cpp | ||
| @@ -1475,7 +1475,17 @@ const char *ToolChain::getClangLinkerDriver( | ||
| @@ -1489,6 +1489,12 @@ | ||
| LinkerDriver = Args.MakeArgString(tool.get()); | ||
| } | ||
|
|
||
| // If there is a linker driver in the toolchain folder, use that instead. | ||
| if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {toolchainPath})) | ||
| - LinkerDriver = Args.MakeArgString(tool.get()); | ||
| + return Args.MakeArgString(tool.get()); | ||
| + } | ||
| + | ||
| + // For Nix, prefer linking using the wrapped system clang, instead of using | ||
| + // 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). | ||
| + if (auto nixCC = llvm::sys::Process::GetEnv("NIX_CC")) { | ||
| + llvm::SmallString<128> binDir(nixCC.getValue()); | ||
| + llvm::sys::path::append(binDir, "bin"); | ||
| + if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {binDir.str()})) | ||
| + return Args.MakeArgString(tool.get()); | ||
| } | ||
|
|
||
| + if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {"@clang@/bin"})) | ||
| + return Args.MakeArgString(tool.get()); | ||
| + | ||
| return LinkerDriver; | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know why this error is triggered? My best guess at the moment is something in swift-crypto, which suggests that it isn't an isolated issue and will probably come up again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. As mentioned in the commit message, SwiftPM should potentially hard‐code Clang for compiling C code or something. But needing a separate
swiftPackages.stdenvsucks and I consider this more a problem of the way we’re handling SwiftPM currently (e.g. a separateswift-cryptopackage could override the compiler itself, if we had one).Since all of this is being rewritten – hopefully for 25.11 – I don’t want to try and do more surgery to clean things up, when a lot of the fundamentals have big issues (like the entanglement between the Swift wrapper and
cc-wrapper). This was just a blocker for removing old LLVMs that already took way more time than I’d like. The fallback option if we don’t like this is to setswiftPackages.stdenvtollvmPackages.stdenv, but it’s not required for e.g.swiftformatorprotoc-gen-swift, at least.