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
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 } ;
0 commit comments