1{
2 lib,
3 stdenv,
4 callPackage,
5 cmake,
6 bash,
7 coreutils,
8 gnugrep,
9 perl,
10 ninja_1_11,
11 pkg-config,
12 clang,
13 bintools,
14 python312Packages,
15 git,
16 fetchpatch,
17 fetchpatch2,
18 makeWrapper,
19 gnumake,
20 file,
21 runCommand,
22 writeShellScriptBin,
23 # For lldb
24 libedit,
25 ncurses,
26 swig,
27 libxml2,
28 # Linux-specific
29 glibc,
30 libuuid,
31 # Darwin-specific
32 replaceVars,
33 fixDarwinDylibNames,
34 xcbuild,
35 cctools, # libtool
36 sigtool,
37 DarwinTools,
38 apple-sdk_13,
39 darwinMinVersionHook,
40}:
41
42let
43 apple-sdk_swift = apple-sdk_13; # Use the SDK that was available when Swift shipped.
44
45 deploymentVersion =
46 if lib.versionOlder (targetPlatform.darwinMinVersion or "0") "10.15" then
47 "10.15"
48 else
49 targetPlatform.darwinMinVersion;
50
51 # Use Python 3.12 for now because Swift 5.8 depends on Python's PyEval_ThreadsInitialized(), which was removed in 3.13.
52 python3 = python312Packages.python.withPackages (p: [ p.setuptools ]); # python 3.12 compat.
53
54 inherit (stdenv) hostPlatform targetPlatform;
55
56 sources = callPackage ../sources.nix { };
57
58 # There are apparently multiple naming conventions on Darwin. Swift uses the
59 # xcrun naming convention. See `configure_sdk_darwin` calls in CMake files.
60 swiftOs =
61 if targetPlatform.isDarwin then
62 {
63 "macos" = "macosx";
64 "ios" = "iphoneos";
65 #iphonesimulator
66 #appletvos
67 #appletvsimulator
68 #watchos
69 #watchsimulator
70 }
71 .${targetPlatform.darwinPlatform}
72 or (throw "Cannot build Swift for target Darwin platform '${targetPlatform.darwinPlatform}'")
73 else
74 targetPlatform.parsed.kernel.name;
75
76 # This causes swiftPackages.XCTest to fail to build on aarch64-linux
77 # as I believe this is because Apple calls the architecture aarch64
78 # on Linux rather than arm64 when used with macOS.
79 swiftArch =
80 if hostPlatform.isDarwin then hostPlatform.darwinArch else targetPlatform.parsed.cpu.name;
81
82 # On Darwin, a `.swiftmodule` is a subdirectory in `lib/swift/<OS>`,
83 # containing binaries for supported archs. On other platforms, binaries are
84 # installed to `lib/swift/<OS>/<ARCH>`. Note that our setup-hook also adds
85 # `lib/swift` for convenience.
86 swiftLibSubdir = "lib/swift/${swiftOs}";
87 swiftModuleSubdir =
88 if hostPlatform.isDarwin then "lib/swift/${swiftOs}" else "lib/swift/${swiftOs}/${swiftArch}";
89
90 # And then there's also a separate subtree for statically linked modules.
91 toStaticSubdir = lib.replaceStrings [ "/swift/" ] [ "/swift_static/" ];
92 swiftStaticLibSubdir = toStaticSubdir swiftLibSubdir;
93 swiftStaticModuleSubdir = toStaticSubdir swiftModuleSubdir;
94
95 # This matches _SWIFT_DEFAULT_COMPONENTS, with specific components disabled.
96 swiftInstallComponents = [
97 "autolink-driver"
98 "compiler"
99 # "clang-builtin-headers"
100 "stdlib"
101 "sdk-overlay"
102 "static-mirror-lib"
103 "editor-integration"
104 # "tools"
105 # "testsuite-tools"
106 "toolchain-tools"
107 "toolchain-dev-tools"
108 "license"
109 (if stdenv.hostPlatform.isDarwin then "sourcekit-xpc-service" else "sourcekit-inproc")
110 "swift-remote-mirror"
111 "swift-remote-mirror-headers"
112 ];
113
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
126 # Build a tool used during the build to create a custom clang wrapper, with
127 # which we wrap the clang produced by the swift build.
128 #
129 # This is used in a `POST_BUILD` for the CMake target, so we rename the
130 # actual clang to clang-unwrapped, then put the wrapper in place.
131 #
132 # We replace the `exec ...` command with `exec -a "$0"` in order to
133 # preserve $0 for clang. This is because, unlike Nix, we don't have
134 # separate wrappers for clang/clang++, and clang uses $0 to detect C++.
135 #
136 # Similarly, the C++ detection in the wrapper itself also won't work for us,
137 # so we base it on $0 as well.
138 makeClangWrapper = writeShellScriptBin "nix-swift-make-clang-wrapper" ''
139 set -euo pipefail
140
141 targetFile="$1"
142 unwrappedClang="$targetFile-unwrapped"
143
144 mv "$targetFile" "$unwrappedClang"
145 sed < '${clangForWrappers}/bin/clang' > "$targetFile" \
146 -e 's|^\s*exec|exec -a "$0"|g' \
147 -e 's|^\[\[ "${clang.cc}/bin/clang" = \*++ ]]|[[ "$0" = *++ ]]|' \
148 -e "s|${clang.cc}/bin/clang|$unwrappedClang|g" \
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 ''}
153 chmod a+x "$targetFile"
154 '';
155
156 # Create a tool used during the build to create a custom swift wrapper for
157 # each of the swift executables produced by the build.
158 #
159 # The build produces several `swift-frontend` executables during
160 # bootstrapping. Each of these has numerous aliases via symlinks, and the
161 # executable uses $0 to detect what tool is called.
162 wrapperParams = {
163 inherit bintools;
164 coreutils_bin = lib.getBin coreutils;
165 gnugrep_bin = gnugrep;
166 suffixSalt = lib.replaceStrings [ "-" "." ] [ "_" "_" ] targetPlatform.config;
167 use_response_file_by_default = 1;
168 swiftDriver = "";
169 # NOTE: @cc_wrapper@ and @prog@ need to be filled elsewhere.
170 };
171 swiftWrapper = runCommand "swift-wrapper.sh" wrapperParams ''
172 # Make empty to avoid adding the SDK’s modules in the bootstrap wrapper. Otherwise, the SDK conflicts with the
173 # shims the wrapper tries to build.
174 darwinMinVersion="" substituteAll '${../wrapper/wrapper.sh}' "$out"
175 '';
176 makeSwiftcWrapper = writeShellScriptBin "nix-swift-make-swift-wrapper" ''
177 set -euo pipefail
178
179 targetFile="$1"
180 unwrappedSwift="$targetFile-unwrapped"
181
182 mv "$targetFile" "$unwrappedSwift"
183 sed < '${swiftWrapper}' > "$targetFile" \
184 -e "s|@prog@|'$unwrappedSwift'|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 ''}
190 chmod a+x "$targetFile"
191 '';
192
193 # On Darwin, we need to use BOOTSTRAPPING-WITH-HOSTLIBS because of ABI
194 # stability, and have to provide the definitions for the system stdlib.
195 appleSwiftCore = stdenv.mkDerivation {
196 name = "apple-swift-core";
197 dontUnpack = true;
198
199 buildInputs = [ apple-sdk_swift ];
200
201 installPhase = ''
202 mkdir -p $out/lib/swift
203 cp -r \
204 "$SDKROOT/usr/lib/swift/Swift.swiftmodule" \
205 "$SDKROOT/usr/lib/swift/CoreFoundation.swiftmodule" \
206 "$SDKROOT/usr/lib/swift/Dispatch.swiftmodule" \
207 "$SDKROOT/usr/lib/swift/ObjectiveC.swiftmodule" \
208 "$SDKROOT/usr/lib/swift/libswiftCore.tbd" \
209 "$SDKROOT/usr/lib/swift/libswiftCoreFoundation.tbd" \
210 "$SDKROOT/usr/lib/swift/libswiftDispatch.tbd" \
211 "$SDKROOT/usr/lib/swift/libswiftFoundation.tbd" \
212 "$SDKROOT/usr/lib/swift/libswiftObjectiveC.tbd" \
213 $out/lib/swift/
214 '';
215 };
216
217 # https://github.com/NixOS/nixpkgs/issues/327836
218 # Fail to build with ninja 1.12 when NIX_BUILD_CORES is low (Hydra or Github Actions).
219 # Can reproduce using `nix --option cores 2 build -f . swiftPackages.swift-unwrapped`.
220 # Until we find out the exact cause, follow [swift upstream][1], pin ninja to version
221 # 1.11.1.
222 # [1]: https://github.com/swiftlang/swift/pull/72989
223 ninja = ninja_1_11;
224
225in
226stdenv.mkDerivation {
227 pname = "swift";
228 inherit (sources) version;
229
230 outputs = [
231 "out"
232 "lib"
233 "dev"
234 "doc"
235 "man"
236 ];
237
238 nativeBuildInputs = [
239 cmake
240 git
241 ninja
242 perl # pod2man
243 pkg-config
244 python3
245 makeWrapper
246 makeClangWrapper
247 makeSwiftcWrapper
248 ]
249 ++ lib.optionals stdenv.hostPlatform.isDarwin [
250 xcbuild
251 sigtool # codesign
252 DarwinTools # sw_vers
253 fixDarwinDylibNames
254 cctools.libtool
255 ];
256
257 buildInputs = [
258 # For lldb
259 python3
260 swig
261 libxml2
262 ]
263 ++ lib.optionals stdenv.hostPlatform.isLinux [
264 libuuid
265 ]
266 ++ lib.optionals stdenv.hostPlatform.isDarwin [
267 apple-sdk_swift
268 ];
269
270 # Will effectively be `buildInputs` when swift is put in `nativeBuildInputs`.
271 depsTargetTargetPropagated = lib.optionals stdenv.targetPlatform.isDarwin [
272 apple-sdk_swift
273 ];
274
275 # This is a partial reimplementation of our setup hook. Because we reuse
276 # the Swift wrapper for the Swift build itself, we need to do some of the
277 # same preparation.
278 postHook = ''
279 for pkg in "''${pkgsHostTarget[@]}" '${clang.libc}'; do
280 for subdir in ${swiftModuleSubdir} ${swiftStaticModuleSubdir} lib/swift; do
281 if [[ -d "$pkg/$subdir" ]]; then
282 export NIX_SWIFTFLAGS_COMPILE+=" -I $pkg/$subdir"
283 fi
284 done
285 for subdir in ${swiftLibSubdir} ${swiftStaticLibSubdir} lib/swift; do
286 if [[ -d "$pkg/$subdir" ]]; then
287 export NIX_LDFLAGS+=" -L $pkg/$subdir"
288 fi
289 done
290 done
291 '';
292
293 # We invoke cmakeConfigurePhase multiple times, but only need this once.
294 dontFixCmake = true;
295 # We setup custom build directories.
296 dontUseCmakeBuildDir = true;
297
298 unpackPhase =
299 let
300 copySource = repo: "cp -r ${sources.${repo}} ${repo}";
301 in
302 ''
303 mkdir src
304 cd src
305
306 ${copySource "swift-cmark"}
307 ${copySource "llvm-project"}
308 ${copySource "swift"}
309 ${copySource "swift-experimental-string-processing"}
310 ${copySource "swift-syntax"}
311 ${lib.optionalString (!stdenv.hostPlatform.isDarwin) (copySource "swift-corelibs-libdispatch")}
312
313 chmod -R u+w .
314 '';
315
316 patchPhase = ''
317 # Just patch all the things for now, we can focus this later.
318 # TODO: eliminate use of env.
319 find -type f -print0 | xargs -0 sed -i \
320 ${lib.optionalString stdenv.hostPlatform.isDarwin "-e 's|/usr/libexec/PlistBuddy|${xcbuild}/bin/PlistBuddy|g'"} \
321 -e 's|/usr/bin/env|${coreutils}/bin/env|g' \
322 -e 's|/usr/bin/make|${gnumake}/bin/make|g' \
323 -e 's|/bin/mkdir|${coreutils}/bin/mkdir|g' \
324 -e 's|/bin/cp|${coreutils}/bin/cp|g' \
325 -e 's|/usr/bin/file|${file}/bin/file|g'
326
327 patch -p1 -d swift -i ${./patches/swift-cmake-3.25-compat.patch}
328 patch -p1 -d swift -i ${./patches/swift-wrap.patch}
329 patch -p1 -d swift -i ${./patches/swift-linux-fix-libc-paths.patch}
330 patch -p1 -d swift -i ${
331 replaceVars ./patches/swift-linux-fix-linking.patch {
332 inherit clang;
333 }
334 }
335 patch -p1 -d swift -i ${
336 replaceVars ./patches/swift-darwin-plistbuddy-workaround.patch {
337 inherit swiftArch;
338 }
339 }
340 patch -p1 -d swift -i ${
341 replaceVars ./patches/swift-prevent-sdk-dirs-warning.patch {
342 inherit (builtins) storeDir;
343 }
344 }
345
346 # This patch needs to know the lib output location, so must be substituted
347 # in the same derivation as the compiler.
348 storeDir="${builtins.storeDir}" \
349 substituteAll ${./patches/swift-separate-lib.patch} $TMPDIR/swift-separate-lib.patch
350 patch -p1 -d swift -i $TMPDIR/swift-separate-lib.patch
351
352 patch -p1 -d llvm-project/llvm -i ${./patches/llvm-module-cache.patch}
353
354 for lldbPatch in ${
355 lib.escapeShellArgs [
356 # Fixes for SWIG 4
357 (fetchpatch2 {
358 url = "https://github.com/llvm/llvm-project/commit/81fc5f7909a4ef5a8d4b5da2a10f77f7cb01ba63.patch?full_index=1";
359 stripLen = 1;
360 hash = "sha256-Znw+C0uEw7lGETQLKPBZV/Ymo2UigZS+Hv/j1mUo7p0=";
361 })
362 (fetchpatch2 {
363 url = "https://github.com/llvm/llvm-project/commit/f0a25fe0b746f56295d5c02116ba28d2f965c175.patch?full_index=1";
364 stripLen = 1;
365 hash = "sha256-QzVeZzmc99xIMiO7n//b+RNAvmxghISKQD93U2zOgFI=";
366 })
367 (fetchpatch2 {
368 url = "https://github.com/llvm/llvm-project/commit/ba35c27ec9aa9807f5b4be2a0c33ca9b045accc7.patch?full_index=1";
369 stripLen = 1;
370 hash = "sha256-LXl+WbpmWZww5xMDrle3BM2Tw56v8k9LO1f1Z1/wDTs=";
371 })
372 (fetchpatch2 {
373 url = "https://github.com/llvm/llvm-project/commit/9ec115978ea2bdfc60800cd3c21264341cdc8b0a.patch?full_index=1";
374 stripLen = 1;
375 hash = "sha256-u0zSejEjfrH3ZoMFm1j+NVv2t5AP9cE5yhsrdTS1dG4=";
376 })
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 })
390 ]
391 }; do
392 patch -p1 -d llvm-project/lldb -i $lldbPatch
393 done
394
395 patch -p1 -d llvm-project/clang -i ${./patches/clang-toolchain-dir.patch}
396 patch -p1 -d llvm-project/clang -i ${./patches/clang-wrap.patch}
397 patch -p1 -d llvm-project/clang -i ${./patches/clang-purity.patch}
398 patch -p2 -d llvm-project/clang -i ${
399 fetchpatch {
400 name = "clang-cmake-fix-interpreter.patch";
401 url = "https://github.com/llvm/llvm-project/commit/b5eaf500f2441eff2277ea2973878fb1f171fd0a.patch";
402 sha256 = "1rma1al0rbm3s3ql6bnvbcighp74lri1lcrwbyacgdqp80fgw1b6";
403 }
404 }
405
406 # gcc-13 build fixes
407 patch -p2 -d llvm-project/llvm -i ${
408 fetchpatch {
409 name = "gcc-13.patch";
410 url = "https://github.com/llvm/llvm-project/commit/ff1681ddb303223973653f7f5f3f3435b48a1983.patch";
411 hash = "sha256-nkRPWx8gNvYr7mlvEUiOAb1rTrf+skCZjAydJVUHrcI=";
412 }
413 }
414
415 ${lib.optionalString stdenv.hostPlatform.isLinux ''
416 substituteInPlace llvm-project/clang/lib/Driver/ToolChains/Linux.cpp \
417 --replace 'SysRoot + "/lib' '"${glibc}/lib" "' \
418 --replace 'SysRoot + "/usr/lib' '"${glibc}/lib" "' \
419 --replace 'LibDir = "lib";' 'LibDir = "${glibc}/lib";' \
420 --replace 'LibDir = "lib64";' 'LibDir = "${glibc}/lib";' \
421 --replace 'LibDir = X32 ? "libx32" : "lib64";' 'LibDir = "${glibc}/lib";'
422
423 # uuid.h is not part of glibc, but of libuuid.
424 sed -i 's|''${GLIBC_INCLUDE_PATH}/uuid/uuid.h|${libuuid.dev}/include/uuid/uuid.h|' \
425 swift/stdlib/public/Platform/glibc.modulemap.gyb
426 ''}
427
428 # Remove tests for cross compilation, which we don't currently support.
429 rm swift/test/Interop/Cxx/class/constructors-copy-irgen-*.swift
430 rm swift/test/Interop/Cxx/class/constructors-irgen-*.swift
431
432 # TODO: consider fixing and re-adding. This test fails due to a non-standard "install_prefix".
433 rm swift/validation-test/Python/build_swift.swift
434
435 # We cannot handle the SDK location being in "Weird Location" due to Nix isolation.
436 rm swift/test/DebugInfo/compiler-flags.swift
437
438 # TODO: Fix issue with ld.gold invoked from script finding crtbeginS.o and crtendS.o.
439 rm swift/test/IRGen/ELF-remove-autolink-section.swift
440
441 # The following two tests fail because we use don't use the bundled libicu:
442 # [SOURCE_DIR/utils/build-script] ERROR: can't find source directory for libicu (tried /build/src/icu)
443 rm swift/validation-test/BuildSystem/default_build_still_performs_epilogue_opts_after_split.test
444 rm swift/validation-test/BuildSystem/test_early_swift_driver_and_infer.swift
445
446 # TODO: This test fails for some unknown reason
447 rm swift/test/Serialization/restrict-swiftmodule-to-revision.swift
448
449 # This test was flaky in ofborg, see #186476
450 rm swift/test/AutoDiff/compiler_crashers_fixed/issue-56649-missing-debug-scopes-in-pullback-trampoline.swift
451
452 patchShebangs .
453
454 ${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
463 # NOTE: This interferes with ABI stability on Darwin, which uses the system
464 # libraries in the hardcoded path /usr/lib/swift.
465 fixCmakeFiles .
466 ''}
467 '';
468
469 # > clang-15-unwrapped: error: unsupported option '-fzero-call-used-regs=used-gpr' for target 'arm64-apple-macosx10.9.0'
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 ];
475
476 configurePhase = ''
477 export SWIFT_SOURCE_ROOT="$PWD"
478 mkdir -p ../build
479 cd ../build
480 export SWIFT_BUILD_ROOT="$PWD"
481 '';
482
483 # These steps are derived from doing a normal build with.
484 #
485 # ./swift/utils/build-toolchain test --dry-run
486 #
487 # But dealing with the custom Python build system is far more trouble than
488 # simply invoking CMake directly. Few variables it passes to CMake are
489 # actually required or non-default.
490 #
491 # Using CMake directly also allows us to split up the already large build,
492 # and package Swift components separately.
493 #
494 # Besides `--dry-run`, another good way to compare build changes between
495 # Swift releases is to diff the scripts:
496 #
497 # git diff swift-5.6.3-RELEASE..swift-5.7-RELEASE -- utils/build*
498 #
499 buildPhase = ''
500 # Helper to build a subdirectory.
501 #
502 # Always reset cmakeFlags before calling this. The cmakeConfigurePhase
503 # amends flags and would otherwise keep expanding it.
504 function buildProject() {
505 mkdir -p $SWIFT_BUILD_ROOT/$1
506 cd $SWIFT_BUILD_ROOT/$1
507
508 cmakeDir=$SWIFT_SOURCE_ROOT/''${2-$1}
509 cmakeConfigurePhase
510
511 ninjaBuildPhase
512 }
513
514 cmakeFlags="-GNinja"
515 buildProject swift-cmark
516
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
530 # Some notes:
531 # - The Swift build just needs Clang.
532 # - We can further reduce targets to just our targetPlatform.
533 cmakeFlags="
534 -GNinja
535 -DLLVM_BUILD_TOOLS=NO
536 -DLLVM_ENABLE_PROJECTS=clang
537 -DLLVM_TARGETS_TO_BUILD=${
538 {
539 "x86_64" = "X86";
540 "aarch64" = "AArch64";
541 }
542 .${targetPlatform.parsed.cpu.name}
543 or (throw "Unsupported CPU architecture: ${targetPlatform.parsed.cpu.name}")
544 }
545 "
546 buildProject llvm llvm-project/llvm
547
548 # Ensure that the built Clang can find the runtime libraries by
549 # copying the symlinks from the main wrapper.
550 cp -P ${clang}/resource-root/{lib,share} $SWIFT_BUILD_ROOT/llvm/lib/clang/15.0.0/
551
552 ''
553 + lib.optionalString stdenv.hostPlatform.isDarwin ''
554 # Add appleSwiftCore to the search paths. Adding the whole SDK results in build failures.
555 OLD_NIX_SWIFTFLAGS_COMPILE="$NIX_SWIFTFLAGS_COMPILE"
556 OLD_NIX_LDFLAGS="$NIX_LDFLAGS"
557 export NIX_SWIFTFLAGS_COMPILE=" -I ${appleSwiftCore}/lib/swift"
558 export NIX_LDFLAGS+=" -L ${appleSwiftCore}/lib/swift"
559 ''
560 + ''
561
562 # Some notes:
563 # - BOOTSTRAPPING_MODE defaults to OFF in CMake, but is enabled in standard
564 # builds, so we enable it as well. On Darwin, we have to use the system
565 # Swift libs because of ABI-stability, but this may be trouble if the
566 # builder is an older macOS.
567 # - Experimental features are OFF by default in CMake, but are enabled in
568 # official builds, so we do the same. (Concurrency is also required in
569 # the stdlib. StringProcessing is often implicitely imported, causing
570 # lots of warnings if missing.)
571 # - SWIFT_STDLIB_ENABLE_OBJC_INTEROP is set explicitely because its check
572 # is buggy. (Uses SWIFT_HOST_VARIANT_SDK before initialized.)
573 # Fixed in: https://github.com/apple/swift/commit/84083afef1de5931904d5c815d53856cdb3fb232
574 cmakeFlags="
575 -GNinja
576 -DBOOTSTRAPPING_MODE=BOOTSTRAPPING${lib.optionalString stdenv.hostPlatform.isDarwin "-WITH-HOSTLIBS"}
577 -DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING=ON
578 -DSWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY=ON
579 -DSWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED=ON
580 -DSWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING=ON
581 -DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm
582 -DClang_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/clang
583 -DSWIFT_PATH_TO_CMARK_SOURCE=$SWIFT_SOURCE_ROOT/swift-cmark
584 -DSWIFT_PATH_TO_CMARK_BUILD=$SWIFT_BUILD_ROOT/swift-cmark
585 -DSWIFT_PATH_TO_LIBDISPATCH_SOURCE=$SWIFT_SOURCE_ROOT/swift-corelibs-libdispatch
586 -DSWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE=$SWIFT_SOURCE_ROOT/swift-syntax
587 -DSWIFT_PATH_TO_STRING_PROCESSING_SOURCE=$SWIFT_SOURCE_ROOT/swift-experimental-string-processing
588 -DSWIFT_INSTALL_COMPONENTS=${lib.concatStringsSep ";" swiftInstallComponents}
589 -DSWIFT_STDLIB_ENABLE_OBJC_INTEROP=${if stdenv.hostPlatform.isDarwin then "ON" else "OFF"}
590 -DSWIFT_DARWIN_DEPLOYMENT_VERSION_OSX=${deploymentVersion}
591 "
592 buildProject swift
593
594 ''
595 + lib.optionalString stdenv.hostPlatform.isDarwin ''
596 # Restore search paths to remove appleSwiftCore.
597 export NIX_SWIFTFLAGS_COMPILE="$OLD_NIX_SWIFTFLAGS_COMPILE"
598 export NIX_LDFLAGS="$OLD_NIX_LDFLAGS"
599 ''
600 + ''
601
602 # These are based on flags in `utils/build-script-impl`.
603 #
604 # LLDB_USE_SYSTEM_DEBUGSERVER=ON disables the debugserver build on Darwin,
605 # which requires a special signature.
606 #
607 # CMAKE_BUILD_WITH_INSTALL_NAME_DIR ensures we don't use rpath on Darwin.
608 cmakeFlags="
609 -GNinja
610 -DLLDB_SWIFTC=$SWIFT_BUILD_ROOT/swift/bin/swiftc
611 -DLLDB_SWIFT_LIBS=$SWIFT_BUILD_ROOT/swift/lib/swift
612 -DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm
613 -DClang_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/clang
614 -DSwift_DIR=$SWIFT_BUILD_ROOT/swift/lib/cmake/swift
615 -DLLDB_ENABLE_CURSES=ON
616 -DLLDB_ENABLE_LIBEDIT=ON
617 -DLLDB_ENABLE_PYTHON=ON
618 -DLLDB_ENABLE_LZMA=OFF
619 -DLLDB_ENABLE_LUA=OFF
620 -DLLDB_INCLUDE_TESTS=OFF
621 -DCMAKE_BUILD_WITH_INSTALL_NAME_DIR=ON
622 ${lib.optionalString stdenv.hostPlatform.isDarwin ''
623 -DLLDB_USE_SYSTEM_DEBUGSERVER=ON
624 ''}
625 -DLibEdit_INCLUDE_DIRS=${lib.getInclude libedit}/include
626 -DLibEdit_LIBRARIES=${lib.getLib libedit}/lib/libedit${stdenv.hostPlatform.extensions.sharedLibrary}
627 -DCURSES_INCLUDE_DIRS=${lib.getInclude ncurses}/include
628 -DCURSES_LIBRARIES=${lib.getLib ncurses}/lib/libncurses${stdenv.hostPlatform.extensions.sharedLibrary}
629 -DPANEL_LIBRARIES=${lib.getLib ncurses}/lib/libpanel${stdenv.hostPlatform.extensions.sharedLibrary}
630 ";
631 buildProject lldb llvm-project/lldb
632
633 ${lib.optionalString stdenv.targetPlatform.isDarwin ''
634 # Need to do a standalone build of concurrency for Darwin back deployment.
635 # Based on: utils/swift_build_support/swift_build_support/products/backdeployconcurrency.py
636 cmakeFlags="
637 -GNinja
638 -DCMAKE_Swift_COMPILER=$SWIFT_BUILD_ROOT/swift/bin/swiftc
639 -DSWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE=$SWIFT_SOURCE_ROOT/swift-syntax
640
641 -DTOOLCHAIN_DIR=/var/empty
642 -DSWIFT_NATIVE_LLVM_TOOLS_PATH=${stdenv.cc}/bin
643 -DSWIFT_NATIVE_CLANG_TOOLS_PATH=${stdenv.cc}/bin
644 -DSWIFT_NATIVE_SWIFT_TOOLS_PATH=$SWIFT_BUILD_ROOT/swift/bin
645
646 -DCMAKE_CROSSCOMPILING=ON
647
648 -DBUILD_SWIFT_CONCURRENCY_BACK_DEPLOYMENT_LIBRARIES=ON
649 -DSWIFT_INCLUDE_TOOLS=OFF
650 -DSWIFT_BUILD_STDLIB_EXTRA_TOOLCHAIN_CONTENT=OFF
651 -DSWIFT_BUILD_TEST_SUPPORT_MODULES=OFF
652 -DSWIFT_BUILD_STDLIB=OFF
653 -DSWIFT_BUILD_DYNAMIC_STDLIB=OFF
654 -DSWIFT_BUILD_STATIC_STDLIB=OFF
655 -DSWIFT_BUILD_REMOTE_MIRROR=OFF
656 -DSWIFT_BUILD_SDK_OVERLAY=OFF
657 -DSWIFT_BUILD_DYNAMIC_SDK_OVERLAY=OFF
658 -DSWIFT_BUILD_STATIC_SDK_OVERLAY=OFF
659 -DSWIFT_INCLUDE_TESTS=OFF
660 -DSWIFT_BUILD_PERF_TESTSUITE=OFF
661
662 -DSWIFT_HOST_VARIANT_ARCH=${swiftArch}
663 -DBUILD_STANDALONE=ON
664
665 -DSWIFT_INSTALL_COMPONENTS=back-deployment
666
667 -DSWIFT_SDKS=${
668 {
669 "macos" = "OSX";
670 "ios" = "IOS";
671 #IOS_SIMULATOR
672 #TVOS
673 #TVOS_SIMULATOR
674 #WATCHOS
675 #WATCHOS_SIMULATOR
676 }
677 .${targetPlatform.darwinPlatform}
678 }
679
680 -DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm
681
682 -DSWIFT_DEST_ROOT=$out
683 -DSWIFT_HOST_VARIANT_SDK=OSX
684
685 -DSWIFT_DARWIN_DEPLOYMENT_VERSION_OSX=${deploymentVersion}
686 -DSWIFT_DARWIN_DEPLOYMENT_VERSION_IOS=13.0
687 -DSWIFT_DARWIN_DEPLOYMENT_VERSION_MACCATALYST=13.0
688 -DSWIFT_DARWIN_DEPLOYMENT_VERSION_TVOS=13.0
689 -DSWIFT_DARWIN_DEPLOYMENT_VERSION_WATCHOS=6.0
690 "
691
692 # This depends on the special Clang build specific to the Swift branch.
693 # We also need to call a specific Ninja target.
694 export CC=$SWIFT_BUILD_ROOT/llvm/bin/clang
695 export CXX=$SWIFT_BUILD_ROOT/llvm/bin/clang++
696 ninjaFlags="back-deployment"
697
698 buildProject swift-concurrency-backdeploy swift
699
700 export CC=$NIX_CC/bin/clang
701 export CXX=$NIX_CC/bin/clang++
702 unset ninjaFlags
703 ''}
704 '';
705
706 # TODO: ~50 failing tests on x86_64-linux. Other platforms not checked.
707 doCheck = false;
708 nativeCheckInputs = [ file ];
709 # TODO: consider using stress-tester and integration-test.
710 checkPhase = ''
711 cd $SWIFT_BUILD_ROOT/swift
712 checkTarget=check-swift-all
713 ninjaCheckPhase
714 unset checkTarget
715 '';
716
717 installPhase = ''
718 # Undo the clang and swift wrapping we did for the build.
719 # (This happened via patches to cmake files.)
720 cd $SWIFT_BUILD_ROOT
721 mv llvm/bin/clang-15{-unwrapped,}
722 mv swift/bin/swift-frontend{-unwrapped,}
723
724 mkdir $lib
725
726 # Install clang binaries only. We hide these with the wrapper, so they are
727 # for private use by Swift only.
728 cd $SWIFT_BUILD_ROOT/llvm
729 installTargets=install-clang
730 ninjaInstallPhase
731 unset installTargets
732
733 # LLDB is also a private install.
734 cd $SWIFT_BUILD_ROOT/lldb
735 ninjaInstallPhase
736
737 cd $SWIFT_BUILD_ROOT/swift
738 ninjaInstallPhase
739
740 ${lib.optionalString stdenv.hostPlatform.isDarwin ''
741 cd $SWIFT_BUILD_ROOT/swift-concurrency-backdeploy
742 installTargets=install-back-deployment
743 ninjaInstallPhase
744 unset installTargets
745 ''}
746
747 # Separate $lib output here, because specific logic follows.
748 # Only move the dynamic run-time parts, to keep $lib small. Every Swift
749 # build will depend on it.
750 moveToOutput "lib/swift" "$lib"
751 moveToOutput "lib/libswiftDemangle.*" "$lib"
752
753 # This link is here because various tools (swiftpm) check for stdlib
754 # relative to the swift compiler. It's fine if this is for build-time
755 # stuff, but we should patch all cases were it would end up in an output.
756 ln -s $lib/lib/swift $out/lib/swift
757
758 # Swift has a separate resource root from Clang, but locates the Clang
759 # resource root via subdir or symlink.
760 mv $SWIFT_BUILD_ROOT/llvm/lib/clang/15.0.0 $lib/lib/swift/clang
761 '';
762
763 preFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
764 # This is cheesy, but helps the patchelf hook remove /build from RPATH.
765 cd $SWIFT_BUILD_ROOT/..
766 mv build buildx
767 '';
768
769 postFixup = lib.optionalString stdenv.hostPlatform.isDarwin ''
770 # These libraries need to use the system install name. The official SDK
771 # does the same (as opposed to using rpath). Presumably, they are part of
772 # the stable ABI. Not using the system libraries at run-time is known to
773 # cause ObjC class conflicts and segfaults.
774 declare -A systemLibs=(
775 [libswiftCore.dylib]=1
776 [libswiftDarwin.dylib]=1
777 [libswiftSwiftOnoneSupport.dylib]=1
778 [libswift_Concurrency.dylib]=1
779 )
780
781 for systemLib in "''${!systemLibs[@]}"; do
782 install_name_tool -id /usr/lib/swift/$systemLib $lib/${swiftLibSubdir}/$systemLib
783 done
784
785 for file in $out/bin/swift-frontend $lib/${swiftLibSubdir}/*.dylib; do
786 changeArgs=""
787 for dylib in $(otool -L $file | awk '{ print $1 }'); do
788 if [[ ''${systemLibs["$(basename $dylib)"]} ]]; then
789 changeArgs+=" -change $dylib /usr/lib/swift/$(basename $dylib)"
790 elif [[ "$dylib" = */bootstrapping1/* ]]; then
791 changeArgs+=" -change $dylib $lib/lib/swift/$(basename $dylib)"
792 fi
793 done
794 if [[ -n "$changeArgs" ]]; then
795 install_name_tool $changeArgs $file
796 fi
797 done
798
799 wrapProgram $out/bin/swift-frontend \
800 --prefix PATH : ${lib.makeBinPath [ cctools.libtool ]}
801
802 # Needs to be propagated by the compiler not by its dev output.
803 moveToOutput nix-support/propagated-target-target-deps "$out"
804 '';
805
806 passthru = {
807 inherit
808 swiftOs
809 swiftArch
810 swiftModuleSubdir
811 swiftLibSubdir
812 swiftStaticModuleSubdir
813 swiftStaticLibSubdir
814 ;
815
816 tests = {
817 cxx-interop-test = callPackage ../cxx-interop-test { };
818 };
819
820 # Internal attr for the wrapper.
821 _wrapperParams = wrapperParams;
822 };
823
824 meta = {
825 description = "Swift Programming Language";
826 homepage = "https://github.com/apple/swift";
827 teams = [ lib.teams.swift ];
828 license = lib.licenses.asl20;
829 platforms = with lib.platforms; linux ++ darwin;
830 # Swift doesn't support 32-bit Linux, unknown on other platforms.
831 badPlatforms = lib.platforms.i686;
832 timeout = 86400; # 24 hours.
833 };
834}