lol

swift: compiler only build & darwin support

+1391 -566
+680
pkgs/development/compilers/swift/compiler/default.nix
··· 1 + { lib 2 + , stdenv 3 + , cmake 4 + , coreutils 5 + , gnugrep 6 + , perl 7 + , ninja 8 + , pkg-config 9 + , clang 10 + , bintools 11 + , python3 12 + , git 13 + , fetchFromGitHub 14 + , fetchpatch 15 + , makeWrapper 16 + , gnumake 17 + , file 18 + , runCommand 19 + , writeShellScriptBin 20 + # For lldb 21 + , libedit 22 + , ncurses 23 + , swig 24 + , libxml2 25 + # Linux-specific 26 + , glibc 27 + , libuuid 28 + # Darwin-specific 29 + , substituteAll 30 + , fixDarwinDylibNames 31 + , runCommandLocal 32 + , xcbuild 33 + , cctools # libtool 34 + , sigtool 35 + , DarwinTools 36 + , CoreServices 37 + , Foundation 38 + , Combine 39 + , CLTools_Executables 40 + }: 41 + 42 + let 43 + 44 + inherit (stdenv) hostPlatform targetPlatform; 45 + 46 + # The Swift toolchain script builds projects with separate repos. By convention, some of them share 47 + # the same version with the main Swift compiler project per release. 48 + version = "5.7"; 49 + 50 + fetchSwiftRelease = { repo, hash }: 51 + fetchFromGitHub { 52 + owner = "apple"; 53 + inherit repo hash; 54 + rev = "swift-${version}-RELEASE"; 55 + name = "${repo}-${version}-src"; 56 + }; 57 + 58 + # Names in this set match the directory the source is unpacked to. 59 + sources = { 60 + cmark = fetchSwiftRelease { 61 + repo = "swift-cmark"; 62 + hash = "sha256-f0BoTs4HYdx/aJ9HIGCWMalhl8PvClWD6R4QK3qSgAw="; 63 + }; 64 + llvm-project = fetchSwiftRelease { 65 + repo = "llvm-project"; 66 + hash = "sha256-uW6dEAFaDOlHXnq8lFYxrKNLRPEukauZJxX4UCpWpIY="; 67 + }; 68 + swift = fetchSwiftRelease { 69 + repo = "swift"; 70 + hash = "sha256-n8WVQYinAyIj4wmQnDhvPsH+t8ydANkGbjFJ6blfHOY="; 71 + }; 72 + swift-experimental-string-processing = fetchSwiftRelease { 73 + repo = "swift-experimental-string-processing"; 74 + hash = "sha256-Ar9fQWi8bYSvGErrS0SWrxIxwEwCjsYIZcWweZ8bV28="; 75 + }; 76 + } 77 + // lib.optionalAttrs (!stdenv.isDarwin) { 78 + swift-corelibs-libdispatch = fetchSwiftRelease { 79 + repo = "swift-corelibs-libdispatch"; 80 + hash = "sha256-1qbXiC1k9+T+L6liqXKg6EZXqem6KEEx8OctuL4Kb2o="; 81 + }; 82 + }; 83 + 84 + # Tools invoked by swift at run-time. 85 + runtimeDeps = lib.optionals stdenv.isDarwin [ 86 + # libtool is used for static linking. This is part of cctools, but adding 87 + # that as a build input puts an unwrapped linker in PATH, and breaks 88 + # builds. This small derivation exposes just libtool. 89 + # NOTE: The same applies to swift-driver, but that is currently always 90 + # invoked via the old `swift` / `swiftc`. May change in the future. 91 + (runCommandLocal "libtool" { } '' 92 + mkdir -p $out/bin 93 + ln -s ${cctools}/bin/libtool $out/bin/libtool 94 + '') 95 + ]; 96 + 97 + # There are apparently multiple naming conventions on Darwin. Swift uses the 98 + # xcrun naming convention. See `configure_sdk_darwin` calls in CMake files. 99 + swiftOs = if targetPlatform.isDarwin 100 + then { 101 + "macos" = "macosx"; 102 + "ios" = "iphoneos"; 103 + #iphonesimulator 104 + #appletvos 105 + #appletvsimulator 106 + #watchos 107 + #watchsimulator 108 + }.${targetPlatform.darwinPlatform} 109 + or (throw "Cannot build Swift for target Darwin platform '${targetPlatform.darwinPlatform}'") 110 + else targetPlatform.parsed.kernel.name; 111 + 112 + # Apple Silicon uses a different CPU name in the target triple. 113 + swiftArch = if stdenv.isDarwin && stdenv.isAarch64 then "arm64" 114 + else targetPlatform.parsed.cpu.name; 115 + 116 + # On Darwin, a `.swiftmodule` is a subdirectory in `lib/swift/<OS>`, 117 + # containing binaries for supported archs. On other platforms, binaries are 118 + # installed to `lib/swift/<OS>/<ARCH>`. Note that our setup-hook also adds 119 + # `lib/swift` for convenience. 120 + swiftLibSubdir = "lib/swift/${swiftOs}"; 121 + swiftModuleSubdir = if hostPlatform.isDarwin 122 + then "lib/swift/${swiftOs}" 123 + else "lib/swift/${swiftOs}/${swiftArch}"; 124 + 125 + # And then there's also a separate subtree for statically linked modules. 126 + toStaticSubdir = lib.replaceStrings [ "/swift/" ] [ "/swift_static/" ]; 127 + swiftStaticLibSubdir = toStaticSubdir swiftLibSubdir; 128 + swiftStaticModuleSubdir = toStaticSubdir swiftModuleSubdir; 129 + 130 + # This matches _SWIFT_DEFAULT_COMPONENTS, with specific components disabled. 131 + swiftInstallComponents = [ 132 + "autolink-driver" 133 + "compiler" 134 + # "clang-builtin-headers" 135 + "stdlib" 136 + "sdk-overlay" 137 + "parser-lib" 138 + "static-mirror-lib" 139 + "editor-integration" 140 + # "tools" 141 + # "testsuite-tools" 142 + "toolchain-tools" 143 + "toolchain-dev-tools" 144 + "license" 145 + (if stdenv.isDarwin then "sourcekit-xpc-service" else "sourcekit-inproc") 146 + "swift-remote-mirror" 147 + "swift-remote-mirror-headers" 148 + ]; 149 + 150 + # Build a tool used during the build to create a custom clang wrapper, with 151 + # which we wrap the clang produced by the swift build. 152 + # 153 + # This is used in a `POST_BUILD` for the CMake target, so we rename the 154 + # actual clang to clang-unwrapped, then put the wrapper in place. 155 + # 156 + # We replace the `exec ...` command with `exec -a "$0"` in order to 157 + # preserve $0 for clang. This is because, unlike Nix, we don't have 158 + # separate wrappers for clang/clang++, and clang uses $0 to detect C++. 159 + # 160 + # Similarly, the C++ detection in the wrapper itself also won't work for us, 161 + # so we base it on $0 as well. 162 + makeClangWrapper = writeShellScriptBin "nix-swift-make-clang-wrapper" '' 163 + set -euo pipefail 164 + 165 + targetFile="$1" 166 + unwrappedClang="$targetFile-unwrapped" 167 + 168 + mv "$targetFile" "$unwrappedClang" 169 + sed < '${clang}/bin/clang' > "$targetFile" \ 170 + -e 's|^\s*exec|exec -a "$0"|g' \ 171 + -e 's|^\[\[ "${clang.cc}/bin/clang" = \*++ ]]|[[ "$0" = *++ ]]|' \ 172 + -e "s|${clang.cc}/bin/clang|$unwrappedClang|g" 173 + chmod a+x "$targetFile" 174 + ''; 175 + 176 + # Create a tool used during the build to create a custom swift wrapper for 177 + # each of the swift executables produced by the build. 178 + # 179 + # The build produces several `swift-frontend` executables during 180 + # bootstrapping. Each of these has numerous aliases via symlinks, and the 181 + # executable uses $0 to detect what tool is called. 182 + wrapperParams = { 183 + inherit bintools; 184 + default_cc_wrapper = clang; # Instead of `@out@` in the original. 185 + coreutils_bin = lib.getBin coreutils; 186 + gnugrep_bin = gnugrep; 187 + suffixSalt = lib.replaceStrings ["-" "."] ["_" "_"] targetPlatform.config; 188 + use_response_file_by_default = 1; 189 + # NOTE: @prog@ needs to be filled elsewhere. 190 + }; 191 + swiftWrapper = runCommand "swift-wrapper.sh" wrapperParams '' 192 + substituteAll '${../wrapper/wrapper.sh}' "$out" 193 + ''; 194 + makeSwiftcWrapper = writeShellScriptBin "nix-swift-make-swift-wrapper" '' 195 + set -euo pipefail 196 + 197 + targetFile="$1" 198 + unwrappedSwift="$targetFile-unwrapped" 199 + 200 + mv "$targetFile" "$unwrappedSwift" 201 + sed < '${swiftWrapper}' > "$targetFile" \ 202 + -e "s|@prog@|'$unwrappedSwift'|g" \ 203 + -e 's|exec "$prog"|exec -a "$0" "$prog"|g' 204 + chmod a+x "$targetFile" 205 + ''; 206 + 207 + in stdenv.mkDerivation { 208 + pname = "swift"; 209 + inherit version; 210 + 211 + outputs = [ "out" "lib" "dev" "doc" "man" ]; 212 + 213 + nativeBuildInputs = [ 214 + cmake 215 + git 216 + ninja 217 + perl # pod2man 218 + pkg-config 219 + python3 220 + makeWrapper 221 + makeClangWrapper 222 + makeSwiftcWrapper 223 + ] 224 + ++ lib.optionals stdenv.isDarwin [ 225 + xcbuild 226 + sigtool # codesign 227 + DarwinTools # sw_vers 228 + fixDarwinDylibNames 229 + ]; 230 + 231 + buildInputs = [ 232 + # For lldb 233 + python3 234 + swig 235 + libxml2 236 + ] 237 + ++ lib.optionals stdenv.isLinux [ 238 + libuuid 239 + ] 240 + ++ lib.optionals stdenv.isDarwin [ 241 + CoreServices 242 + Foundation 243 + Combine 244 + ]; 245 + 246 + # This is a partial reimplementation of our setup hook. Because we reuse 247 + # the Swift wrapper for the Swift build itself, we need to do some of the 248 + # same preparation. 249 + postHook = '' 250 + for pkg in "''${pkgsHostTarget[@]}" '${clang.libc}'; do 251 + for subdir in ${swiftModuleSubdir} ${swiftStaticModuleSubdir} lib/swift; do 252 + if [[ -d "$pkg/$subdir" ]]; then 253 + export NIX_SWIFTFLAGS_COMPILE+=" -I $pkg/$subdir" 254 + fi 255 + done 256 + for subdir in ${swiftLibSubdir} ${swiftStaticLibSubdir} lib/swift; do 257 + if [[ -d "$pkg/$subdir" ]]; then 258 + export NIX_LDFLAGS+=" -L $pkg/$subdir" 259 + fi 260 + done 261 + done 262 + ''; 263 + 264 + # We invoke cmakeConfigurePhase multiple times, but only need this once. 265 + dontFixCmake = true; 266 + # We setup custom build directories. 267 + dontUseCmakeBuildDir = true; 268 + 269 + unpackPhase = '' 270 + mkdir src 271 + cd src 272 + 273 + ${lib.concatStrings (lib.mapAttrsToList (dir: src: '' 274 + cp -r ${src} ${dir} 275 + '') sources)} 276 + 277 + chmod -R u+w . 278 + ''; 279 + 280 + patchPhase = '' 281 + # Just patch all the things for now, we can focus this later. 282 + # TODO: eliminate use of env. 283 + find -type f -print0 | xargs -0 sed -i \ 284 + ${lib.optionalString stdenv.isDarwin 285 + "-e 's|/usr/libexec/PlistBuddy|${xcbuild}/bin/PlistBuddy|g'"} \ 286 + -e 's|/usr/bin/env|${coreutils}/bin/env|g' \ 287 + -e 's|/usr/bin/make|${gnumake}/bin/make|g' \ 288 + -e 's|/bin/mkdir|${coreutils}/bin/mkdir|g' \ 289 + -e 's|/bin/cp|${coreutils}/bin/cp|g' \ 290 + -e 's|/usr/bin/file|${file}/bin/file|g' 291 + 292 + patch -p1 -d swift -i ${./patches/swift-wrap.patch} 293 + patch -p1 -d swift -i ${./patches/swift-nix-resource-root.patch} 294 + patch -p1 -d swift -i ${./patches/swift-linux-fix-linking.patch} 295 + patch -p1 -d swift -i ${./patches/swift-darwin-fix-bootstrap.patch} 296 + patch -p1 -d swift -i ${substituteAll { 297 + src = ./patches/swift-darwin-plistbuddy-workaround.patch; 298 + inherit swiftArch; 299 + }} 300 + patch -p1 -d swift -i ${substituteAll { 301 + src = ./patches/swift-prevent-sdk-dirs-warning.patch; 302 + inherit (builtins) storeDir; 303 + }} 304 + substituteInPlace swift/cmake/modules/SwiftConfigureSDK.cmake \ 305 + --replace '/usr/include' "${stdenv.cc.libc_dev}/include" 306 + 307 + # This patch needs to know the lib output location, so must be substituted 308 + # in the same derivation as the compiler. 309 + storeDir="${builtins.storeDir}" \ 310 + substituteAll ${./patches/swift-separate-lib.patch} $TMPDIR/swift-separate-lib.patch 311 + patch -p1 -d swift -i $TMPDIR/swift-separate-lib.patch 312 + 313 + patch -p1 -d llvm-project/llvm -i ${./patches/llvm-module-cache.patch} 314 + 315 + patch -p1 -d llvm-project/clang -i ${./patches/clang-toolchain-dir.patch} 316 + patch -p1 -d llvm-project/clang -i ${./patches/clang-wrap.patch} 317 + patch -p1 -d llvm-project/clang -i ${../../llvm/14/clang/purity.patch} 318 + patch -p2 -d llvm-project/clang -i ${fetchpatch { 319 + name = "clang-cmake-fix-interpreter.patch"; 320 + url = "https://github.com/llvm/llvm-project/commit/b5eaf500f2441eff2277ea2973878fb1f171fd0a.patch"; 321 + sha256 = "1rma1al0rbm3s3ql6bnvbcighp74lri1lcrwbyacgdqp80fgw1b6"; 322 + }} 323 + 324 + ${lib.optionalString stdenv.isLinux '' 325 + substituteInPlace llvm-project/clang/lib/Driver/ToolChains/Linux.cpp \ 326 + --replace 'SysRoot + "/lib' '"${glibc}/lib" "' \ 327 + --replace 'SysRoot + "/usr/lib' '"${glibc}/lib" "' \ 328 + --replace 'LibDir = "lib";' 'LibDir = "${glibc}/lib";' \ 329 + --replace 'LibDir = "lib64";' 'LibDir = "${glibc}/lib";' \ 330 + --replace 'LibDir = X32 ? "libx32" : "lib64";' 'LibDir = "${glibc}/lib";' 331 + 332 + # uuid.h is not part of glibc, but of libuuid. 333 + sed -i 's|''${GLIBC_INCLUDE_PATH}/uuid/uuid.h|${libuuid.dev}/include/uuid/uuid.h|' \ 334 + swift/stdlib/public/Platform/glibc.modulemap.gyb 335 + ''} 336 + 337 + # Remove tests for cross compilation, which we don't currently support. 338 + rm swift/test/Interop/Cxx/class/constructors-copy-irgen.swift 339 + rm swift/test/Interop/Cxx/class/constructors-irgen.swift 340 + 341 + # TODO: consider fixing and re-adding. This test fails due to a non-standard "install_prefix". 342 + rm swift/validation-test/Python/build_swift.swift 343 + 344 + # We cannot handle the SDK location being in "Weird Location" due to Nix isolation. 345 + rm swift/test/DebugInfo/compiler-flags.swift 346 + 347 + # TODO: Fix issue with ld.gold invoked from script finding crtbeginS.o and crtendS.o. 348 + rm swift/test/IRGen/ELF-remove-autolink-section.swift 349 + 350 + # The following two tests fail because we use don't use the bundled libicu: 351 + # [SOURCE_DIR/utils/build-script] ERROR: can't find source directory for libicu (tried /build/src/icu) 352 + rm swift/validation-test/BuildSystem/default_build_still_performs_epilogue_opts_after_split.test 353 + rm swift/validation-test/BuildSystem/test_early_swift_driver_and_infer.swift 354 + 355 + # TODO: This test fails for some unknown reason 356 + rm swift/test/Serialization/restrict-swiftmodule-to-revision.swift 357 + 358 + # This test was flaky in ofborg, see #186476 359 + rm swift/test/AutoDiff/compiler_crashers_fixed/sr14290-missing-debug-scopes-in-pullback-trampoline.swift 360 + 361 + patchShebangs . 362 + 363 + ${lib.optionalString (!stdenv.isDarwin) '' 364 + # NOTE: This interferes with ABI stability on Darwin, which uses the system 365 + # libraries in the hardcoded path /usr/lib/swift. 366 + fixCmakeFiles . 367 + ''} 368 + ''; 369 + 370 + configurePhase = '' 371 + export SWIFT_SOURCE_ROOT="$PWD" 372 + mkdir -p ../build 373 + cd ../build 374 + export SWIFT_BUILD_ROOT="$PWD" 375 + 376 + # Most builds set a target, but LLDB doesn't. Harmless on non-Darwin. 377 + export MACOSX_DEPLOYMENT_TARGET=10.15 378 + ''; 379 + 380 + # These steps are derived from doing a normal build with. 381 + # 382 + # ./swift/utils/build-toolchain test --dry-run 383 + # 384 + # But dealing with the custom Python build system is far more trouble than 385 + # simply invoking CMake directly. Few variables it passes to CMake are 386 + # actually required or non-default. 387 + # 388 + # Using CMake directly also allows us to split up the already large build, 389 + # and package Swift components separately. 390 + # 391 + # Besides `--dry-run`, another good way to compare build changes between 392 + # Swift releases is to diff the scripts: 393 + # 394 + # git diff swift-5.6.3-RELEASE..swift-5.7-RELEASE -- utils/build* 395 + # 396 + buildPhase = '' 397 + # Helper to build a subdirectory. 398 + # 399 + # Always reset cmakeFlags before calling this. The cmakeConfigurePhase 400 + # amends flags and would otherwise keep expanding it. 401 + function buildProject() { 402 + mkdir -p $SWIFT_BUILD_ROOT/$1 403 + cd $SWIFT_BUILD_ROOT/$1 404 + 405 + cmakeDir=$SWIFT_SOURCE_ROOT/''${2-$1} 406 + cmakeConfigurePhase 407 + 408 + ninjaBuildPhase 409 + } 410 + 411 + cmakeFlags="-GNinja" 412 + buildProject cmark 413 + 414 + # Some notes: 415 + # - The Swift build just needs Clang. 416 + # - We can further reduce targets to just our targetPlatform. 417 + cmakeFlags=" 418 + -GNinja 419 + -DLLVM_ENABLE_PROJECTS=clang 420 + -DLLVM_TARGETS_TO_BUILD=${{ 421 + "x86_64" = "X86"; 422 + "aarch64" = "AArch64"; 423 + }.${targetPlatform.parsed.cpu.name}} 424 + " 425 + buildProject llvm llvm-project/llvm 426 + 427 + # Some notes: 428 + # - Building with libswift defaults to OFF in CMake, but is enabled in 429 + # standard builds, so we enable it as well. 430 + # - Experimental features are OFF by default in CMake, but some are 431 + # required to build the stdlib. 432 + # - SWIFT_STDLIB_ENABLE_OBJC_INTEROP is set explicitely because its check 433 + # is buggy. (Uses SWIFT_HOST_VARIANT_SDK before initialized.) 434 + # Fixed in: https://github.com/apple/swift/commit/84083afef1de5931904d5c815d53856cdb3fb232 435 + cmakeFlags=" 436 + -GNinja 437 + -DBOOTSTRAPPING_MODE=BOOTSTRAPPING 438 + -DSWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY=ON 439 + -DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm 440 + -DClang_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/clang 441 + -DSWIFT_PATH_TO_CMARK_SOURCE=$SWIFT_SOURCE_ROOT/cmark 442 + -DSWIFT_PATH_TO_CMARK_BUILD=$SWIFT_BUILD_ROOT/cmark 443 + -DSWIFT_PATH_TO_LIBDISPATCH_SOURCE=$SWIFT_SOURCE_ROOT/swift-corelibs-libdispatch 444 + -DEXPERIMENTAL_STRING_PROCESSING_SOURCE_DIR=$SWIFT_SOURCE_ROOT/swift-experimental-string-processing 445 + -DSWIFT_INSTALL_COMPONENTS=${lib.concatStringsSep ";" swiftInstallComponents} 446 + -DSWIFT_STDLIB_ENABLE_OBJC_INTEROP=${if stdenv.isDarwin then "ON" else "OFF"} 447 + " 448 + buildProject swift 449 + 450 + # These are based on flags in `utils/build-script-impl`. 451 + # 452 + # LLDB_USE_SYSTEM_DEBUGSERVER=ON disables the debugserver build on Darwin, 453 + # which requires a special signature. 454 + # 455 + # CMAKE_BUILD_WITH_INSTALL_NAME_DIR ensures we don't use rpath on Darwin. 456 + # 457 + # NOTE: On Darwin, we only want ncurses in the linker search path, because 458 + # headers are part of libsystem. Adding its headers to the search path 459 + # causes strange mixing and errors. Note that libedit propagates ncurses, 460 + # so we add both manually here, instead of relying on setup hooks. 461 + # TODO: Find a better way to prevent this conflict. 462 + cmakeFlags=" 463 + -GNinja 464 + -DLLDB_SWIFTC=$SWIFT_BUILD_ROOT/swift/bin/swiftc 465 + -DLLDB_SWIFT_LIBS=$SWIFT_BUILD_ROOT/swift/lib/swift 466 + -DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm 467 + -DClang_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/clang 468 + -DSwift_DIR=$SWIFT_BUILD_ROOT/swift/lib/cmake/swift 469 + -DLLDB_ENABLE_CURSES=ON 470 + -DLLDB_ENABLE_LIBEDIT=ON 471 + -DLLDB_ENABLE_PYTHON=ON 472 + -DLLDB_ENABLE_LZMA=OFF 473 + -DLLDB_ENABLE_LUA=OFF 474 + -DLLDB_INCLUDE_TESTS=OFF 475 + -DCMAKE_BUILD_WITH_INSTALL_NAME_DIR=ON 476 + ${lib.optionalString stdenv.isDarwin '' 477 + -DLLDB_USE_SYSTEM_DEBUGSERVER=ON 478 + ''} 479 + -DLibEdit_INCLUDE_DIRS=${libedit.dev}/include 480 + -DLibEdit_LIBRARIES=${libedit}/lib/libedit${stdenv.hostPlatform.extensions.sharedLibrary} 481 + -DCURSES_INCLUDE_DIRS=${if stdenv.isDarwin then "/var/empty" else ncurses.dev}/include 482 + -DCURSES_LIBRARIES=${ncurses}/lib/libncurses${stdenv.hostPlatform.extensions.sharedLibrary} 483 + -DPANEL_LIBRARIES=${ncurses}/lib/libpanel${stdenv.hostPlatform.extensions.sharedLibrary} 484 + "; 485 + buildProject lldb llvm-project/lldb 486 + 487 + ${lib.optionalString stdenv.isDarwin '' 488 + # Need to do a standalone build of concurrency for Darwin back deployment. 489 + # Based on: utils/swift_build_support/swift_build_support/products/backdeployconcurrency.py 490 + cmakeFlags=" 491 + -GNinja 492 + -DCMAKE_Swift_COMPILER=$SWIFT_BUILD_ROOT/swift/bin/swiftc 493 + 494 + -DTOOLCHAIN_DIR=/var/empty 495 + -DSWIFT_NATIVE_LLVM_TOOLS_PATH=${stdenv.cc}/bin 496 + -DSWIFT_NATIVE_CLANG_TOOLS_PATH=${stdenv.cc}/bin 497 + -DSWIFT_NATIVE_SWIFT_TOOLS_PATH=$SWIFT_BUILD_ROOT/swift/bin 498 + 499 + -DCMAKE_CROSSCOMPILING=ON 500 + 501 + -DBUILD_SWIFT_CONCURRENCY_BACK_DEPLOYMENT_LIBRARIES=ON 502 + -DSWIFT_INCLUDE_TOOLS=OFF 503 + -DSWIFT_BUILD_STDLIB_EXTRA_TOOLCHAIN_CONTENT=OFF 504 + -DSWIFT_BUILD_TEST_SUPPORT_MODULES=OFF 505 + -DSWIFT_BUILD_STDLIB=OFF 506 + -DSWIFT_BUILD_DYNAMIC_STDLIB=OFF 507 + -DSWIFT_BUILD_STATIC_STDLIB=OFF 508 + -DSWIFT_BUILD_REMOTE_MIRROR=OFF 509 + -DSWIFT_BUILD_SDK_OVERLAY=OFF 510 + -DSWIFT_BUILD_DYNAMIC_SDK_OVERLAY=OFF 511 + -DSWIFT_BUILD_STATIC_SDK_OVERLAY=OFF 512 + -DSWIFT_INCLUDE_TESTS=OFF 513 + -DSWIFT_BUILD_PERF_TESTSUITE=OFF 514 + 515 + -DSWIFT_HOST_VARIANT_ARCH=${swiftArch} 516 + -DBUILD_STANDALONE=ON 517 + 518 + -DSWIFT_INSTALL_COMPONENTS=back-deployment 519 + 520 + -DSWIFT_SDKS=${{ 521 + "macos" = "OSX"; 522 + "ios" = "IOS"; 523 + #IOS_SIMULATOR 524 + #TVOS 525 + #TVOS_SIMULATOR 526 + #WATCHOS 527 + #WATCHOS_SIMULATOR 528 + }.${targetPlatform.darwinPlatform}} 529 + 530 + -DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm 531 + 532 + -DSWIFT_DEST_ROOT=$out 533 + -DSWIFT_HOST_VARIANT_SDK=OSX 534 + 535 + -DSWIFT_DARWIN_DEPLOYMENT_VERSION_OSX=10.15 536 + -DSWIFT_DARWIN_DEPLOYMENT_VERSION_IOS=13.0 537 + -DSWIFT_DARWIN_DEPLOYMENT_VERSION_MACCATALYST=13.0 538 + -DSWIFT_DARWIN_DEPLOYMENT_VERSION_TVOS=13.0 539 + -DSWIFT_DARWIN_DEPLOYMENT_VERSION_WATCHOS=6.0 540 + " 541 + 542 + # This depends on the special Clang build specific to the Swift branch. 543 + # We also need to call a specific Ninja target. 544 + export CC=$SWIFT_BUILD_ROOT/llvm/bin/clang 545 + export CXX=$SWIFT_BUILD_ROOT/llvm/bin/clang++ 546 + ninjaFlags="back-deployment" 547 + 548 + buildProject swift-concurrency-backdeploy swift 549 + 550 + export CC=$NIX_CC/bin/clang 551 + export CXX=$NIX_CC/bin/clang++ 552 + unset ninjaFlags 553 + ''} 554 + ''; 555 + 556 + # TODO: ~50 failing tests on x86_64-linux. Other platforms not checked. 557 + doCheck = false; 558 + checkInputs = [ file ]; 559 + # TODO: consider using stress-tester and integration-test. 560 + checkPhase = '' 561 + cd $SWIFT_BUILD_ROOT/swift 562 + checkTarget=check-swift-all 563 + ninjaCheckPhase 564 + unset checkTarget 565 + ''; 566 + 567 + installPhase = '' 568 + # Undo the clang and swift wrapping we did for the build. 569 + # (This happened via patches to cmake files.) 570 + cd $SWIFT_BUILD_ROOT 571 + mv llvm/bin/clang-14{-unwrapped,} 572 + mv swift/bin/swift-frontend{-unwrapped,} 573 + 574 + mkdir $out $lib 575 + 576 + # Install clang binaries only. We hide these with the wrapper, so they are 577 + # for private use by Swift only. 578 + cd $SWIFT_BUILD_ROOT/llvm 579 + installTargets=install-clang 580 + ninjaInstallPhase 581 + unset installTargets 582 + 583 + # LLDB is also a private install. 584 + cd $SWIFT_BUILD_ROOT/lldb 585 + ninjaInstallPhase 586 + 587 + cd $SWIFT_BUILD_ROOT/swift 588 + ninjaInstallPhase 589 + 590 + ${lib.optionalString stdenv.isDarwin '' 591 + cd $SWIFT_BUILD_ROOT/swift-concurrency-backdeploy 592 + installTargets=install-back-deployment 593 + ninjaInstallPhase 594 + unset installTargets 595 + ''} 596 + 597 + # Separate $lib output here, because specific logic follows. 598 + # Only move the dynamic run-time parts, to keep $lib small. Every Swift 599 + # build will depend on it. 600 + moveToOutput "lib/swift" "$lib" 601 + moveToOutput "lib/libswiftDemangle.*" "$lib" 602 + 603 + # This link is here because various tools (swiftpm) check for stdlib 604 + # relative to the swift compiler. It's fine if this is for build-time 605 + # stuff, but we should patch all cases were it would end up in an output. 606 + ln -s $lib/lib/swift $out/lib/swift 607 + 608 + # Swift has a separate resource root from Clang, but locates the Clang 609 + # resource root via subdir or symlink. Provide a default here, but we also 610 + # patch Swift to prefer NIX_CC if set. 611 + ln -s ${clang}/resource-root $lib/lib/swift/clang 612 + 613 + ${lib.optionalString stdenv.isDarwin '' 614 + # Install required library for ObjC interop. 615 + # TODO: Is there no source code for this available? 616 + cp -r ${CLTools_Executables}/usr/lib/arc $out/lib/arc 617 + ''} 618 + ''; 619 + 620 + preFixup = lib.optionalString stdenv.isLinux '' 621 + # This is cheesy, but helps the patchelf hook remove /build from RPATH. 622 + cd $NIX_BUILD_TOP 623 + mv build buildx 624 + ''; 625 + 626 + postFixup = lib.optionalString stdenv.isDarwin '' 627 + # These libraries need to use the system install name. The official SDK 628 + # does the same (as opposed to using rpath). Presumably, they are part of 629 + # the stable ABI. Not using the system libraries at run-time is known to 630 + # cause ObjC class conflicts and segfaults. 631 + declare -A systemLibs=( 632 + [libswiftCore.dylib]=1 633 + [libswiftDarwin.dylib]=1 634 + [libswiftSwiftOnoneSupport.dylib]=1 635 + [libswift_Concurrency.dylib]=1 636 + ) 637 + 638 + for systemLib in "''${!systemLibs[@]}"; do 639 + install_name_tool -id /usr/lib/swift/$systemLib $lib/${swiftLibSubdir}/$systemLib 640 + done 641 + 642 + for file in $out/bin/swift-frontend $lib/${swiftLibSubdir}/*.dylib; do 643 + changeArgs="" 644 + for dylib in $(otool -L $file | awk '{ print $1 }'); do 645 + if [[ ''${systemLibs["$(basename $dylib)"]} ]]; then 646 + changeArgs+=" -change $dylib /usr/lib/swift/$(basename $dylib)" 647 + elif [[ "$dylib" = */bootstrapping1/* ]]; then 648 + changeArgs+=" -change $dylib $lib/lib/swift/$(basename $dylib)" 649 + fi 650 + done 651 + if [[ -n "$changeArgs" ]]; then 652 + install_name_tool $changeArgs $file 653 + fi 654 + done 655 + 656 + wrapProgram $out/bin/swift-frontend \ 657 + --prefix PATH : ${lib.makeBinPath runtimeDeps} 658 + ''; 659 + 660 + passthru = { 661 + inherit 662 + swiftOs swiftArch 663 + swiftModuleSubdir swiftLibSubdir 664 + swiftStaticModuleSubdir swiftStaticLibSubdir; 665 + 666 + # Internal attr for the wrapper. 667 + _wrapperParams = wrapperParams; 668 + }; 669 + 670 + meta = { 671 + description = "The Swift Programming Language"; 672 + homepage = "https://github.com/apple/swift"; 673 + maintainers = with lib.maintainers; [ dtzWill trepetti dduan trundle stephank ]; 674 + license = lib.licenses.asl20; 675 + platforms = with lib.platforms; linux ++ darwin; 676 + # Swift doesn't support 32-bit Linux, unknown on other platforms. 677 + badPlatforms = lib.platforms.i686; 678 + timeout = 86400; # 24 hours. 679 + }; 680 + }
+18
pkgs/development/compilers/swift/compiler/patches/clang-wrap.patch
··· 1 + Wrap the clang produced during the build 2 + 3 + --- a/tools/driver/CMakeLists.txt 4 + +++ b/tools/driver/CMakeLists.txt 5 + @@ -59,6 +59,13 @@ endif() 6 + 7 + add_dependencies(clang clang-resource-headers) 8 + 9 + +# Nix: wrap the clang build. 10 + +add_custom_command( 11 + + TARGET clang POST_BUILD 12 + + COMMAND nix-swift-make-clang-wrapper $<TARGET_FILE:clang> 13 + + VERBATIM 14 + +) 15 + + 16 + if(NOT CLANG_LINKS_TO_CREATE) 17 + set(CLANG_LINKS_TO_CREATE clang++ clang-cl clang-cpp) 18 + endif()
+30
pkgs/development/compilers/swift/compiler/patches/llvm-module-cache.patch
··· 1 + The compiler fails if LLVM modules are enabled and it cannot write its module 2 + cache. This patch detects and rejects the fake, non-existant $HOME used in Nix 3 + builds. 4 + 5 + We could simply return false in `cache_directory`, but that completely disables 6 + module caching, and may unnecessarily slow down builds. Instead, let it use 7 + '/tmp/.cache'. 8 + 9 + --- a/lib/Support/Unix/Path.inc 10 + +++ b/lib/Support/Unix/Path.inc 11 + @@ -1380,6 +1380,9 @@ bool user_config_directory(SmallVectorImpl<char> &result) { 12 + if (!home_directory(result)) { 13 + return false; 14 + } 15 + + if (std::equal(result.begin(), result.end(), "/homeless-shelter")) { 16 + + return false; 17 + + } 18 + append(result, ".config"); 19 + return true; 20 + } 21 + @@ -1401,6 +1404,9 @@ bool cache_directory(SmallVectorImpl<char> &result) { 22 + if (!home_directory(result)) { 23 + return false; 24 + } 25 + + if (std::equal(result.begin(), result.end(), "/homeless-shelter")) { 26 + + system_temp_directory(true/*ErasedOnReboot*/, result); 27 + + } 28 + append(result, ".cache"); 29 + return true; 30 + }
+20
pkgs/development/compilers/swift/compiler/patches/swift-darwin-fix-bootstrap.patch
··· 1 + This patch fixes dylib references during bootstrapping. It's possible 2 + `LIBSWIFT_BUILD_MODE=BOOTSTRAPPING` is not really well tested on Darwin, 3 + because official builds don't use it. 4 + 5 + In the near future, Swift will require an existing Swift toolchain to 6 + bootstrap, and we will likely have to replace this any way. 7 + 8 + --- a/stdlib/cmake/modules/AddSwiftStdlib.cmake 9 + +++ b/stdlib/cmake/modules/AddSwiftStdlib.cmake 10 + @@ -1035,6 +1035,10 @@ function(add_swift_target_library_single target name) 11 + set(install_name_dir "${SWIFTLIB_SINGLE_DARWIN_INSTALL_NAME_DIR}") 12 + endif() 13 + 14 + + if(DEFINED SWIFTLIB_SINGLE_BOOTSTRAPPING) 15 + + set(install_name_dir "${lib_dir}/${output_sub_dir}") 16 + + endif() 17 + + 18 + set_target_properties("${target}" 19 + PROPERTIES 20 + INSTALL_NAME_DIR "${install_name_dir}")
+17
pkgs/development/compilers/swift/compiler/patches/swift-darwin-plistbuddy-workaround.patch
··· 1 + CMake tries to read a list field from SDKSettings.plist, but the output of 2 + facebook/xcbuild PlistBuddy is incompatible with Apple's. 3 + 4 + Simply set the supported architectures to the one target architecture we're 5 + building for. 6 + 7 + --- a/cmake/modules/SwiftConfigureSDK.cmake 8 + +++ b/cmake/modules/SwiftConfigureSDK.cmake 9 + @@ -189,7 +189,7 @@ macro(configure_sdk_darwin 10 + endif() 11 + 12 + # Remove any architectures not supported by the SDK. 13 + - remove_sdk_unsupported_archs(${name} ${xcrun_name} ${SWIFT_SDK_${prefix}_PATH} SWIFT_SDK_${prefix}_ARCHITECTURES) 14 + + set(SWIFT_SDK_${prefix}_ARCHITECTURES "@swiftArch@") 15 + 16 + list_intersect( 17 + "${SWIFT_DARWIN_MODULE_ARCHS}" # lhs
+21
pkgs/development/compilers/swift/compiler/patches/swift-linux-fix-linking.patch
··· 1 + --- a/lib/Driver/ToolChains.cpp 2 + +++ b/lib/Driver/ToolChains.cpp 3 + @@ -1475,7 +1475,17 @@ const char *ToolChain::getClangLinkerDriver( 4 + 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 12 + + // the unwrapped clang packaged with swift. The latter is unable to link, but 13 + + // 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 + 21 + return LinkerDriver;
+67
pkgs/development/compilers/swift/compiler/patches/swift-nix-resource-root.patch
··· 1 + Swift normally looks for the Clang resource dir in a subdir/symlink of its own 2 + resource dir. We provide a symlink to the Swift build-time Clang as a default 3 + there, but we also here patch two checks to try locate it via NIX_CC. 4 + 5 + The first (ClangImporter.cpp) happens when Swift code imports C modules. The 6 + second (ToolChains.cpp) happens when Swift is used to link the final product. 7 + 8 + --- a/lib/ClangImporter/ClangImporter.cpp 9 + +++ b/lib/ClangImporter/ClangImporter.cpp 10 + @@ -68,6 +68,7 @@ 11 + #include "llvm/Support/FileSystem.h" 12 + #include "llvm/Support/Memory.h" 13 + #include "llvm/Support/Path.h" 14 + +#include "llvm/Support/Process.h" 15 + #include "llvm/Support/YAMLParser.h" 16 + #include "llvm/Support/YAMLTraits.h" 17 + #include <algorithm> 18 + @@ -809,6 +810,17 @@ importer::addCommonInvocationArguments( 19 + 20 + const std::string &overrideResourceDir = importerOpts.OverrideResourceDir; 21 + if (overrideResourceDir.empty()) { 22 + + // Prefer the Clang resource directory from NIX_CC, to allow swapping in a 23 + + // different stdenv. 24 + + // TODO: Figure out how to provide a user override for this. Probably a 25 + + // niche use case, though, and for now a user can unset NIX_CC to work 26 + + // around it if necessary. 27 + + if (auto nixCC = llvm::sys::Process::GetEnv("NIX_CC")) { 28 + + llvm::SmallString<128> resourceDir(nixCC.getValue()); 29 + + llvm::sys::path::append(resourceDir, "resource-root"); 30 + + invocationArgStrs.push_back("-resource-dir"); 31 + + invocationArgStrs.push_back(std::string(resourceDir.str())); 32 + + } else { 33 + llvm::SmallString<128> resourceDir(searchPathOpts.RuntimeResourcePath); 34 + 35 + // Adjust the path to refer to our copy of the Clang resource directory 36 + @@ -824,6 +836,7 @@ importer::addCommonInvocationArguments( 37 + // Set the Clang resource directory to the path we computed. 38 + invocationArgStrs.push_back("-resource-dir"); 39 + invocationArgStrs.push_back(std::string(resourceDir.str())); 40 + + } // nixCC 41 + } else { 42 + invocationArgStrs.push_back("-resource-dir"); 43 + invocationArgStrs.push_back(overrideResourceDir); 44 + --- a/lib/Driver/ToolChains.cpp 45 + +++ b/lib/Driver/ToolChains.cpp 46 + @@ -1372,10 +1372,20 @@ void ToolChain::getClangLibraryPath(const ArgList &Args, 47 + SmallString<128> &LibPath) const { 48 + const llvm::Triple &T = getTriple(); 49 + 50 + + // Nix: We provide a `clang` symlink in the default Swift resource root, but 51 + + // prefer detecting the Clang resource root via NIX_CC, to allow swapping in 52 + + // a different stdenv. However, always honor a user-provided `-resource-dir`. 53 + + auto nixCC = llvm::sys::Process::GetEnv("NIX_CC"); 54 + + if (nixCC && !Args.hasArgNoClaim(options::OPT_resource_dir)) { 55 + + LibPath.assign(nixCC.getValue()); 56 + + llvm::sys::path::append(LibPath, "resource-root"); 57 + + } else { 58 + getResourceDirPath(LibPath, Args, /*Shared=*/true); 59 + // Remove platform name. 60 + llvm::sys::path::remove_filename(LibPath); 61 + - llvm::sys::path::append(LibPath, "clang", "lib", 62 + + llvm::sys::path::append(LibPath, "clang"); 63 + + } // nixCC 64 + + llvm::sys::path::append(LibPath, "lib", 65 + T.isOSDarwin() ? "darwin" 66 + : getPlatformNameForTriple(T)); 67 + }
+39
pkgs/development/compilers/swift/compiler/patches/swift-prevent-sdk-dirs-warning.patch
··· 1 + Prevents a user-visible warning on every compilation: 2 + 3 + ld: warning: directory not found for option '-L.../MacOSX11.0.sdk/usr/lib/swift' 4 + 5 + --- a/lib/Driver/ToolChains.cpp 6 + +++ b/lib/Driver/ToolChains.cpp 7 + @@ -1455,9 +1455,11 @@ void ToolChain::getRuntimeLibraryPaths(SmallVectorImpl<std::string> &runtimeLibP 8 + runtimeLibPaths.push_back(std::string(scratchPath.str())); 9 + } 10 + 11 + + if (!SDKPath.startswith("@storeDir@")) { 12 + scratchPath = SDKPath; 13 + llvm::sys::path::append(scratchPath, "usr", "lib", "swift"); 14 + runtimeLibPaths.push_back(std::string(scratchPath.str())); 15 + + } 16 + } 17 + } 18 + 19 + --- a/lib/Frontend/CompilerInvocation.cpp 20 + +++ b/lib/Frontend/CompilerInvocation.cpp 21 + @@ -185,7 +185,9 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts, 22 + RuntimeLibraryImportPaths.push_back(std::string(LibPath.str())); 23 + } 24 + 25 + - LibPath = SearchPathOpts.getSDKPath(); 26 + + auto SDKPath = SearchPathOpts.getSDKPath(); 27 + + if (!SDKPath.startswith("@storeDir@")) { 28 + + LibPath = SDKPath; 29 + llvm::sys::path::append(LibPath, "usr", "lib", "swift"); 30 + if (!Triple.isOSDarwin()) { 31 + // Use the non-architecture suffixed form with directory-layout 32 + @@ -200,6 +202,7 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts, 33 + llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple)); 34 + } 35 + RuntimeLibraryImportPaths.push_back(std::string(LibPath.str())); 36 + + } 37 + } 38 + SearchPathOpts.setRuntimeLibraryImportPaths(RuntimeLibraryImportPaths); 39 + }
+26
pkgs/development/compilers/swift/compiler/patches/swift-separate-lib.patch
··· 1 + Patch paths to use the separate 'lib' output. One of the things this patch 2 + fixes is the output of `swift -frontend -print-target-info`, which swiftpm uses 3 + to set rpath on Linux. 4 + 5 + The check if the executable path starts with 'out' is necessary for 6 + bootstrapping, or the compiler will fail when run from the build directory. 7 + 8 + --- a/lib/Frontend/CompilerInvocation.cpp 9 + +++ b/lib/Frontend/CompilerInvocation.cpp 10 + @@ -49,11 +49,16 @@ swift::CompilerInvocation::CompilerInvocation() { 11 + void CompilerInvocation::computeRuntimeResourcePathFromExecutablePath( 12 + StringRef mainExecutablePath, bool shared, 13 + llvm::SmallVectorImpl<char> &runtimeResourcePath) { 14 + + if (mainExecutablePath.startswith("@storeDir@")) { 15 + + auto libPath = StringRef("@lib@"); 16 + + runtimeResourcePath.append(libPath.begin(), libPath.end()); 17 + + } else { 18 + runtimeResourcePath.append(mainExecutablePath.begin(), 19 + mainExecutablePath.end()); 20 + 21 + llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /swift 22 + llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /bin 23 + + } 24 + appendSwiftLibDir(runtimeResourcePath, shared); 25 + } 26 +
+46
pkgs/development/compilers/swift/compiler/patches/swift-wrap.patch
··· 1 + Wrap the swift compiler produced during the build 2 + 3 + --- a/tools/driver/CMakeLists.txt 4 + +++ b/tools/driver/CMakeLists.txt 5 + @@ -16,6 +16,13 @@ if(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*") 6 + swiftDriverTool 7 + libswiftStub) 8 + 9 + + # Nix: wrap the swift build. 10 + + add_custom_command( 11 + + TARGET swift-frontend-bootstrapping0 POST_BUILD 12 + + COMMAND nix-swift-make-swift-wrapper $<TARGET_FILE:swift-frontend-bootstrapping0> 13 + + VERBATIM 14 + + ) 15 + + 16 + swift_create_post_build_symlink(swift-frontend-bootstrapping0 17 + SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" 18 + DESTINATION "swiftc${CMAKE_EXECUTABLE_SUFFIX}" 19 + @@ -34,6 +41,13 @@ if(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*") 20 + swiftDriverTool 21 + libswift-bootstrapping1) 22 + 23 + + # Nix: wrap the swift build. 24 + + add_custom_command( 25 + + TARGET swift-frontend-bootstrapping1 POST_BUILD 26 + + COMMAND nix-swift-make-swift-wrapper $<TARGET_FILE:swift-frontend-bootstrapping1> 27 + + VERBATIM 28 + + ) 29 + + 30 + swift_create_post_build_symlink(swift-frontend-bootstrapping1 31 + SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}" 32 + DESTINATION "swiftc${CMAKE_EXECUTABLE_SUFFIX}" 33 + @@ -50,6 +64,13 @@ target_link_libraries(swift-frontend 34 + swiftDriverTool 35 + libswift) 36 + 37 + +# Nix: wrap the swift build. 38 + +add_custom_command( 39 + + TARGET swift-frontend POST_BUILD 40 + + COMMAND nix-swift-make-swift-wrapper $<TARGET_FILE:swift-frontend> 41 + + VERBATIM 42 + +) 43 + + 44 + # Create a `swift-driver` executable adjacent to the `swift-frontend` executable 45 + # to ensure that `swiftc` forwards to the standalone driver when invoked. 46 + swift_create_early_driver_copies(swift-frontend)
+48 -461
pkgs/development/compilers/swift/default.nix
··· 1 - { lib, stdenv 2 - , cmake 3 - , coreutils 4 - , glibc 5 - , gccForLibs 6 - , which 7 - , perl 8 - , libedit 9 - , ninja 10 - , pkg-config 11 - , sqlite 12 - , libxml2 13 - , clang_13 14 - , python3 15 - , ncurses 16 - , libuuid 17 - , libxcrypt 18 - , icu 19 - , libgcc 20 - , libblocksruntime 21 - , curl 22 - , rsync 23 - , git 24 - , libgit2 25 - , fetchFromGitHub 26 - , makeWrapper 27 - , gnumake 28 - , file 1 + { lib 2 + , pkgs 3 + , newScope 4 + , darwin 5 + , llvmPackages_latest 6 + , overrideCC 29 7 }: 30 8 31 9 let 32 - # The Swift toolchain script builds projects with separate repos. By convention, some of them share 33 - # the same version with the main Swift compiler project per release. We fetch these with 34 - # `fetchSwiftRelease`. The rest have their own versions locked to each Swift release, as defined in the 35 - # Swift compiler repo: 36 - # utils/update_checkout/update_checkout-config.json. 37 - # 38 - # ... among projects listed in that file, we provide our own: 39 - # - CMake 40 - # - ninja 41 - # - icu 42 - # 43 - # ... we'd like to include the following in the future: 44 - # - stress-tester 45 - # - integration-tests 46 - 47 - versions = { 48 - swift = "5.6.2"; 49 - yams = "4.0.2"; 50 - argumentParser = "1.0.3"; 51 - format = "release/5.6"; 52 - crypto = "1.1.5"; 53 - nio = "2.31.2"; 54 - nio-ssl = "2.15.0"; 55 - }; 10 + self = rec { 56 11 57 - fetchAppleRepo = { repo, rev, sha256 }: 58 - fetchFromGitHub { 59 - owner = "apple"; 60 - inherit repo rev sha256; 61 - name = "${repo}-${rev}-src"; 62 - }; 12 + callPackage = newScope self; 63 13 64 - fetchSwiftRelease = { repo, sha256, fetchSubmodules ? false }: 65 - fetchFromGitHub { 66 - owner = "apple"; 67 - inherit repo sha256 fetchSubmodules; 68 - rev = "swift-${versions.swift}-RELEASE"; 69 - name = "${repo}-${versions.swift}-src"; 70 - }; 14 + # Current versions of Swift on Darwin require macOS SDK 10.15 at least. 15 + # Re-export this so we can rely on the minimum Swift SDK elsewhere. 16 + apple_sdk = pkgs.darwin.apple_sdk_11_0; 71 17 72 - sources = { 73 - # Projects that share `versions.swift` for each release. 18 + # Our current Clang on Darwin is v11, but we need at least v12. The 19 + # following applies the newer Clang with the same libc overrides as 20 + # `apple_sdk.stdenv`. 21 + # 22 + # If 'latest' becomes an issue, recommend replacing it with v14, which is 23 + # currently closest to the official Swift builds. 24 + clang = if pkgs.stdenv.isDarwin 25 + then 26 + llvmPackages_latest.clang.override rec { 27 + libc = apple_sdk.Libsystem; 28 + bintools = pkgs.bintools.override { inherit libc; }; 29 + } 30 + else 31 + llvmPackages_latest.clang; 74 32 75 - swift = fetchSwiftRelease { 76 - repo = "swift"; 77 - sha256 = "sha256-wiRXAXWEksJuy+YQQ+B7tzr2iLkSVkgV6o+wIz7yKJA="; 78 - }; 79 - cmark = fetchSwiftRelease { 80 - repo = "swift-cmark"; 81 - sha256 = "sha256-f0BoTs4HYdx/aJ9HIGCWMalhl8PvClWD6R4QK3qSgAw="; 82 - }; 83 - llbuild = fetchSwiftRelease { 84 - repo = "swift-llbuild"; 85 - sha256 = "sha256-SQ6V0zVshIYMjayx+ZpYuLijgQ89tqRnPlXBPf2FYqM="; 33 + # Overrides that create a useful environment for swift packages, allowing 34 + # packaging with `swiftPackages.callPackage`. These are similar to 35 + # `apple_sdk_11_0.callPackage`, with our clang on top. 36 + inherit (clang) bintools; 37 + stdenv = overrideCC pkgs.stdenv clang; 38 + darwin = pkgs.darwin.overrideScope (_: prev: { 39 + inherit apple_sdk; 40 + inherit (apple_sdk) Libsystem LibsystemCross libcharset libunwind objc4 configd IOKit Security; 41 + CF = apple_sdk.CoreFoundation; 42 + }); 43 + xcodebuild = pkgs.xcbuild.override { 44 + inherit (apple_sdk.frameworks) CoreServices CoreGraphics ImageIO; 45 + inherit stdenv; 46 + sdkVer = "10.15"; 86 47 }; 87 - driver = fetchSwiftRelease { 88 - repo = "swift-driver"; 89 - sha256 = "sha256-D5/C4Rbv5KIsKpy6YbuMxGIGaQkn80PD4Cp0l6bPKzY="; 90 - }; 91 - toolsSupportCore = fetchSwiftRelease { 92 - repo = "swift-tools-support-core"; 93 - sha256 = "sha256-FbtQCq1sSlzrskCrgzD4iYuo5eGaXrAUUxoNX/BiOfg="; 94 - }; 95 - swiftpm = fetchSwiftRelease { 96 - repo = "swift-package-manager"; 97 - sha256 = "sha256-esO4Swz3UYngbVgxoV+fkhSC0AU3IaxVjWkgK/s3x68="; 98 - }; 99 - syntax = fetchSwiftRelease { 100 - repo = "swift-syntax"; 101 - sha256 = "sha256-C9FPCtq49BvKXtTWWeReYWNrU70pHzT2DhAv3NiTbPU="; 102 - }; 103 - corelibsXctest = fetchSwiftRelease { 104 - repo = "swift-corelibs-xctest"; 105 - sha256 = "sha256-0hizfnKJaUUA+jXuXzXWk72FmlSyc+UGEf7BTLdJrx4="; 106 - }; 107 - corelibsFoundation = fetchSwiftRelease { 108 - repo = "swift-corelibs-foundation"; 109 - sha256 = "sha256-8sCL8Ia6yb6bRsJZ52gUJH0jN3lwClM573G8jgUdEhw="; 110 - }; 111 - corelibsLibdispatch = fetchSwiftRelease { 112 - repo = "swift-corelibs-libdispatch"; 113 - sha256 = "sha256-1tIskUMnfblnvZaFDQPUMBfWTmBYG98s7rEww7PwZO8="; 114 - fetchSubmodules = true; 115 - }; 116 - indexstoreDb = fetchSwiftRelease { 117 - repo = "indexstore-db"; 118 - sha256 = "sha256-/PO4eMiASZN3pjFjBQ1r8vYwGRn6xm3SWaB2HDZlkPs="; 119 - }; 120 - sourcekitLsp = fetchSwiftRelease { 121 - repo = "sourcekit-lsp"; 122 - sha256 = "sha256-ttgUC4ZHD3P/xLHllEbACtHVrJ6HXqeVWccXcoPMkts="; 123 - }; 124 - llvmProject = fetchSwiftRelease { 125 - repo = "llvm-project"; 126 - sha256 = "sha256-YVs3lKV2RlaovpYkdGO+vzypolrmXmbKBBP4+osNMYw="; 127 - }; 128 - docc = fetchSwiftRelease { 129 - repo = "swift-docc"; 130 - sha256 = "sha256-rWiaNamZoHTO1bKpubxuT7m1IBOl7amT5M71mNauilY="; 131 - }; 132 - docc-render-artifact = fetchSwiftRelease { 133 - repo = "swift-docc-render-artifact"; 134 - sha256 = "sha256-AX+rtDLhq8drk7N6/hoH3fQioudmmTCnEhR45bME8uU="; 135 - }; 136 - docc-symbolkit = fetchSwiftRelease { 137 - repo = "swift-docc-symbolkit"; 138 - sha256 = "sha256-Xy1TQ5ucDW+MnkeOvVznsATBmwcQ3p1x+ofQ22ofk+o="; 139 - }; 140 - lmdb = fetchSwiftRelease { 141 - repo = "swift-lmdb"; 142 - sha256 = "sha256-i2GkWRWq1W5j8rF4PiHwWgT4Dur5FCY2o44HvUU3vtQ="; 143 - }; 144 - markdown = fetchSwiftRelease { 145 - repo = "swift-markdown"; 146 - sha256 = "sha256-XtFSBiNHhmULjS4OqSpMgUetLu3peRg7l6HpjwVsTj8="; 48 + xcbuild = xcodebuild; 49 + 50 + swift-unwrapped = callPackage ./compiler { 51 + inherit (darwin) DarwinTools cctools sigtool; 52 + inherit (apple_sdk) CLTools_Executables; 53 + inherit (apple_sdk.frameworks) CoreServices Foundation Combine; 147 54 }; 148 55 149 - cmark-gfm = fetchAppleRepo { 150 - repo = "swift-cmark"; 151 - rev = "swift-${versions.swift}-RELEASE-gfm"; 152 - sha256 = "sha256-g28iKmMR2W0r1urf8Fk1HBxAp5OlonNYSVN3Ril66tQ="; 56 + swift = callPackage ./wrapper { 57 + swift = swift-unwrapped; 153 58 }; 154 59 155 - # Projects that have their own versions during each release 156 - 157 - argumentParser = fetchAppleRepo { 158 - repo = "swift-argument-parser"; 159 - rev = "${versions.argumentParser}"; 160 - sha256 = "sha256-vNqkuAwSZNCWvwe6E5BqbXQdIbmIia0dENmmSQ9P8Mo="; 161 - }; 162 - format = fetchAppleRepo { 163 - repo = "swift-format"; 164 - rev = "${versions.format}"; 165 - sha256 = "sha256-1f5sIrv9IbPB7Vnahq1VwH8gT41dcjWldRwvVEaMdto="; 166 - }; 167 - crypto = fetchAppleRepo { 168 - repo = "swift-crypto"; 169 - rev = "${versions.crypto}"; 170 - sha256 = "sha256-jwxXQuOF+CnpLMwTZ2z52Fgx2b97yWzXiPTx0Ye8KCQ="; 171 - }; 172 - nio = fetchAppleRepo { 173 - repo = "swift-nio"; 174 - rev = versions.nio; 175 - sha256 = "sha256-FscOA/S7on31QCR/MZFjg4ZB3FGJ+rdptZ6MRZJXexE="; 176 - }; 177 - nio-ssl = fetchAppleRepo { 178 - repo = "swift-nio-ssl"; 179 - rev = versions.nio-ssl; 180 - sha256 = "sha256-5QGkmkCOXhG3uOdf0bd3Fo1MFekB8/WcveBXGhtVZKo="; 181 - }; 182 - yams = fetchFromGitHub { 183 - owner = "jpsim"; 184 - repo = "Yams"; 185 - rev = versions.yams; 186 - sha256 = "sha256-cTkCAwxxLc35laOon1ZXXV8eAxX02oDolJyPauhZado="; 187 - name = "Yams-${versions.yams}-src"; 188 - }; 189 60 }; 190 61 191 - devInputs = [ 192 - curl 193 - glibc 194 - icu 195 - libblocksruntime 196 - libedit 197 - libgcc 198 - libuuid 199 - libxcrypt 200 - libxml2 201 - ncurses 202 - sqlite 203 - ]; 204 - 205 - python = (python3.withPackages (ps: [ps.six])); 206 - 207 - cmakeFlags = [ 208 - "-DGLIBC_INCLUDE_PATH=${stdenv.cc.libc.dev}/include" 209 - "-DC_INCLUDE_DIRS=${lib.makeSearchPathOutput "dev" "include" devInputs}:${libxml2.dev}/include/libxml2" 210 - "-DGCC_INSTALL_PREFIX=${gccForLibs}" 211 - ]; 212 - 213 - in 214 - stdenv.mkDerivation { 215 - pname = "swift"; 216 - version = versions.swift; 217 - 218 - nativeBuildInputs = [ 219 - cmake 220 - git 221 - makeWrapper 222 - ninja 223 - perl 224 - pkg-config 225 - python 226 - rsync 227 - which 228 - ]; 229 - buildInputs = devInputs ++ [ 230 - clang_13 231 - ]; 232 - 233 - # TODO: Revisit what needs to be propagated and how. 234 - propagatedBuildInputs = [ 235 - libgcc 236 - libgit2 237 - python 238 - ]; 239 - propagatedUserEnvPkgs = [ git pkg-config ]; 240 - 241 - hardeningDisable = [ "format" ]; # for LLDB 242 - 243 - unpackPhase = '' 244 - mkdir src 245 - cd src 246 - export SWIFT_SOURCE_ROOT=$PWD 247 - 248 - cp -r ${sources.swift} swift 249 - cp -r ${sources.cmark} cmark 250 - cp -r ${sources.llbuild} llbuild 251 - cp -r ${sources.argumentParser} swift-argument-parser 252 - cp -r ${sources.driver} swift-driver 253 - cp -r ${sources.toolsSupportCore} swift-tools-support-core 254 - cp -r ${sources.swiftpm} swiftpm 255 - cp -r ${sources.syntax} swift-syntax 256 - cp -r ${sources.corelibsXctest} swift-corelibs-xctest 257 - cp -r ${sources.corelibsFoundation} swift-corelibs-foundation 258 - cp -r ${sources.corelibsLibdispatch} swift-corelibs-libdispatch 259 - cp -r ${sources.yams} yams 260 - cp -r ${sources.indexstoreDb} indexstore-db 261 - cp -r ${sources.sourcekitLsp} sourcekit-lsp 262 - cp -r ${sources.format} swift-format 263 - cp -r ${sources.crypto} swift-crypto 264 - cp -r ${sources.llvmProject} llvm-project 265 - cp -r ${sources.cmark-gfm} swift-cmark-gfm 266 - cp -r ${sources.docc} swift-docc 267 - cp -r ${sources.docc-render-artifact} swift-docc-render-artifact 268 - cp -r ${sources.docc-symbolkit} swift-docc-symbolkit 269 - cp -r ${sources.lmdb} swift-lmdb 270 - cp -r ${sources.markdown} swift-markdown 271 - cp -r ${sources.nio} swift-nio 272 - cp -r ${sources.nio-ssl} swift-nio-ssl 273 - 274 - chmod -R u+w . 275 - ''; 276 - 277 - patchPhase = '' 278 - # Just patch all the things for now, we can focus this later. 279 - patchShebangs $SWIFT_SOURCE_ROOT 280 - 281 - # TODO: eliminate use of env. 282 - find -type f -print0 | xargs -0 sed -i \ 283 - -e 's|/usr/bin/env|${coreutils}/bin/env|g' \ 284 - -e 's|/usr/bin/make|${gnumake}/bin/make|g' \ 285 - -e 's|/bin/mkdir|${coreutils}/bin/mkdir|g' \ 286 - -e 's|/bin/cp|${coreutils}/bin/cp|g' \ 287 - -e 's|/usr/bin/file|${file}/bin/file|g' 288 - 289 - # Build configuration patches. 290 - patch -p1 -d swift -i ${./patches/0001-build-presets-linux-don-t-require-using-Ninja.patch} 291 - patch -p1 -d swift -i ${./patches/0002-build-presets-linux-allow-custom-install-prefix.patch} 292 - patch -p1 -d swift -i ${./patches/0003-build-presets-linux-don-t-build-extra-libs.patch} 293 - patch -p1 -d swift -i ${./patches/0004-build-presets-linux-plumb-extra-cmake-options.patch} 294 - patch -p1 -d swift -i ${./patches/0007-build-presets-linux-os-stdlib.patch} 295 - substituteInPlace swift/cmake/modules/SwiftConfigureSDK.cmake \ 296 - --replace '/usr/include' "${stdenv.cc.libc.dev}/include" 297 - sed -i swift/utils/build-presets.ini \ 298 - -e 's/^test-installable-package$/# \0/' \ 299 - -e 's/^test$/# \0/' \ 300 - -e 's/^validation-test$/# \0/' \ 301 - -e 's/^long-test$/# \0/' \ 302 - -e 's/^stress-test$/# \0/' \ 303 - -e 's/^test-optimized$/# \0/' \ 304 - -e 's/^swift-install-components=autolink.*$/\0;editor-integration/' 305 - 306 - # LLVM toolchain patches. 307 - patch -p1 -d llvm-project/clang -i ${./patches/0005-clang-toolchain-dir.patch} 308 - patch -p1 -d llvm-project/clang -i ${./patches/0006-clang-purity.patch} 309 - substituteInPlace llvm-project/clang/lib/Driver/ToolChains/Linux.cpp \ 310 - --replace 'SysRoot + "/lib' '"${glibc}/lib" "' \ 311 - --replace 'SysRoot + "/usr/lib' '"${glibc}/lib" "' \ 312 - --replace 'LibDir = "lib";' 'LibDir = "${glibc}/lib";' \ 313 - --replace 'LibDir = "lib64";' 'LibDir = "${glibc}/lib";' \ 314 - --replace 'LibDir = X32 ? "libx32" : "lib64";' 'LibDir = "${glibc}/lib";' 315 - 316 - # Substitute ncurses for curses in llbuild. 317 - sed -i 's/curses/ncurses/' llbuild/*/*/CMakeLists.txt 318 - sed -i 's/curses/ncurses/' llbuild/*/*/*/CMakeLists.txt 319 - 320 - # uuid.h is not part of glibc, but of libuuid. 321 - sed -i 's|''${GLIBC_INCLUDE_PATH}/uuid/uuid.h|${libuuid.dev}/include/uuid/uuid.h|' swift/stdlib/public/Platform/glibc.modulemap.gyb 322 - 323 - # Support library build script patches. 324 - PREFIX=''${out/#\/} 325 - substituteInPlace swift/utils/swift_build_support/swift_build_support/products/benchmarks.py \ 326 - --replace \ 327 - "'--toolchain', toolchain_path," \ 328 - "'--toolchain', '/build/install/$PREFIX'," 329 - substituteInPlace swift/benchmark/scripts/build_script_helper.py \ 330 - --replace \ 331 - "swiftbuild_path = os.path.join(args.toolchain, \"usr\", \"bin\", \"swift-build\")" \ 332 - "swiftbuild_path = os.path.join(args.toolchain, \"bin\", \"swift-build\")" 333 - substituteInPlace swift-corelibs-xctest/build_script.py \ 334 - --replace usr "$PREFIX" 335 - 336 - # Can be removed in later swift-docc versions, see 337 - # https://github.com/apple/swift-docc/commit/bff70b847008f91ac729cfd299a85481eef3f581 338 - substituteInPlace swift-docc/build-script-helper.py \ 339 - --replace \ 340 - "subprocess.check_output(cmd, env=env).strip(), 'docc')" \ 341 - "subprocess.check_output(cmd, env=env).strip().decode(), 'docc')" 342 - 343 - # Can be removed in later Swift versions, see 344 - # https://github.com/apple/swift/pull/58755 345 - substituteInPlace swift/utils/process-stats-dir.py \ 346 - --replace \ 347 - "type=argparse.FileType('wb', 0)," \ 348 - "type=argparse.FileType('w', 0)," 349 - 350 - # Apply Python 3 fix, see 351 - # https://github.com/apple/swift/commit/ec6bc595092974628b27b114a472e84162261bbd 352 - substituteInPlace swift/utils/swift_build_support/swift_build_support/productpipeline_list_builder.py \ 353 - --replace \ 354 - "filter(lambda x: x is not None, pipeline)" \ 355 - "[p for p in pipeline if p is not None]" 356 - ''; 357 - 358 - configurePhase = '' 359 - cd .. 360 - 361 - mkdir build install 362 - export SWIFT_BUILD_ROOT=$PWD/build 363 - export SWIFT_INSTALL_DIR=$PWD/install 364 - 365 - export INSTALLABLE_PACKAGE=$PWD/swift.tar.gz 366 - export NIX_ENFORCE_PURITY= 367 - 368 - cd $SWIFT_BUILD_ROOT 369 - ''; 370 - 371 - buildPhase = '' 372 - # Explicitly include C++ headers to prevent errors where stdlib.h is not found from cstdlib. 373 - export NIX_CFLAGS_COMPILE="$(< ${clang_13}/nix-support/libcxx-cxxflags) $NIX_CFLAGS_COMPILE" 374 - 375 - # During the Swift build, a full local LLVM build is performed and the resulting clang is 376 - # invoked. This compiler is not using the Nix wrappers, so it needs some help to find things. 377 - export NIX_LDFLAGS_BEFORE="-rpath ${gccForLibs.lib}/lib -L${gccForLibs.lib}/lib $NIX_LDFLAGS_BEFORE" 378 - 379 - # However, we want to use the wrapped compiler whenever possible. 380 - export CC="${clang_13}/bin/clang" 381 - 382 - $SWIFT_SOURCE_ROOT/swift/utils/build-script \ 383 - --preset=buildbot_linux \ 384 - installable_package=$INSTALLABLE_PACKAGE \ 385 - install_prefix=$out \ 386 - install_destdir=$SWIFT_INSTALL_DIR \ 387 - extra_cmake_options="${lib.concatStringsSep "," cmakeFlags}" 388 - ''; 389 - 390 - doCheck = true; 391 - 392 - checkInputs = [ file ]; 393 - 394 - checkPhase = '' 395 - # Remove compiler build system tests which fail due to our modified default build profile and 396 - # nixpkgs-provided version of CMake. 397 - rm $SWIFT_SOURCE_ROOT/swift/validation-test/BuildSystem/infer_implies_install_all.test 398 - rm $SWIFT_SOURCE_ROOT/swift/validation-test/BuildSystem/infer_dumps_deps_if_verbose_build.test 399 - 400 - # This test apparently requires Python 2 (strings are assumed to be bytes-like), but the build 401 - # process overall now otherwise requires Python 3 (which is what we have updated to). A fix PR 402 - # has been submitted upstream. 403 - rm $SWIFT_SOURCE_ROOT/swift/validation-test/SIL/verify_all_overlays.py 404 - 405 - # TODO: consider fixing and re-adding. This test fails due to a non-standard "install_prefix". 406 - rm $SWIFT_SOURCE_ROOT/swift/validation-test/Python/build_swift.swift 407 - 408 - # We cannot handle the SDK location being in "Weird Location" due to Nix isolation. 409 - rm $SWIFT_SOURCE_ROOT/swift/test/DebugInfo/compiler-flags.swift 410 - 411 - # TODO: Fix issue with ld.gold invoked from script finding crtbeginS.o and crtendS.o. 412 - rm $SWIFT_SOURCE_ROOT/swift/test/IRGen/ELF-remove-autolink-section.swift 413 - 414 - # The following two tests fail because we use don't use the bundled libicu: 415 - # [SOURCE_DIR/utils/build-script] ERROR: can't find source directory for libicu (tried /build/src/icu) 416 - rm $SWIFT_SOURCE_ROOT/swift/validation-test/BuildSystem/default_build_still_performs_epilogue_opts_after_split.test 417 - rm $SWIFT_SOURCE_ROOT/swift/validation-test/BuildSystem/test_early_swift_driver_and_infer.swift 418 - 419 - # TODO: This test fails for some unknown reason 420 - rm $SWIFT_SOURCE_ROOT/swift/test/Serialization/restrict-swiftmodule-to-revision.swift 421 - 422 - # This test was flaky in ofborg, see #186476 423 - rm $SWIFT_SOURCE_ROOT/swift/test/AutoDiff/compiler_crashers_fixed/sr14290-missing-debug-scopes-in-pullback-trampoline.swift 424 - 425 - # TODO: consider using stress-tester and integration-test. 426 - 427 - # Match the wrapped version of Swift to be installed. 428 - export LIBRARY_PATH=${lib.makeLibraryPath [icu libgcc libuuid]}:$l 429 - 430 - checkTarget=check-swift-all-${stdenv.hostPlatform.parsed.kernel.name}-${stdenv.hostPlatform.parsed.cpu.name} 431 - ninjaFlags='-C buildbot_linux/swift-${stdenv.hostPlatform.parsed.kernel.name}-${stdenv.hostPlatform.parsed.cpu.name}' 432 - ninjaCheckPhase 433 - ''; 434 - 435 - installPhase = '' 436 - mkdir -p $out 437 - 438 - # Extract the generated tarball into the store. 439 - tar xf $INSTALLABLE_PACKAGE -C $out --strip-components=3 ''${out/#\/} 440 - find $out -type d -empty -delete 441 - 442 - # Fix installation weirdness, also present in Apple’s official tarballs. 443 - mv $out/local/include/indexstore $out/include 444 - rmdir $out/local/include $out/local 445 - rm -r $out/bin/sdk-module-lists $out/bin/swift-api-checker.py 446 - 447 - wrapProgram $out/bin/swift \ 448 - --set CC $out/bin/clang \ 449 - --suffix C_INCLUDE_PATH : $out/lib/swift/clang/include \ 450 - --suffix CPLUS_INCLUDE_PATH : $out/lib/swift/clang/include \ 451 - --suffix LIBRARY_PATH : ${lib.makeLibraryPath [icu libgcc libuuid]} \ 452 - --suffix PATH : ${lib.makeBinPath [ stdenv.cc.bintools ]} 453 - 454 - wrapProgram $out/bin/swiftc \ 455 - --set CC $out/bin/clang \ 456 - --suffix C_INCLUDE_PATH : $out/lib/swift/clang/include \ 457 - --suffix CPLUS_INCLUDE_PATH : $out/lib/swift/clang/include \ 458 - --suffix LIBRARY_PATH : ${lib.makeLibraryPath [icu libgcc libuuid]} \ 459 - --suffix PATH : ${lib.makeBinPath [ stdenv.cc.bintools ]} 460 - ''; 461 - 462 - # Hack to avoid build and install directories in RPATHs. 463 - preFixup = "rm -rf $SWIFT_BUILD_ROOT $SWIFT_INSTALL_DIR"; 464 - 465 - meta = with lib; { 466 - description = "The Swift Programming Language"; 467 - homepage = "https://github.com/apple/swift"; 468 - maintainers = with maintainers; [ dtzWill trepetti dduan trundle ]; 469 - license = licenses.asl20; 470 - # Swift doesn't support 32-bit Linux, unknown on other platforms. 471 - platforms = platforms.linux; 472 - badPlatforms = platforms.i686; 473 - timeout = 86400; # 24 hours. 474 - }; 475 - } 62 + in self
-13
pkgs/development/compilers/swift/patches/0001-build-presets-linux-don-t-require-using-Ninja.patch
··· 1 - Don't build Ninja, we use our own. 2 - 3 - --- a/utils/build-presets.ini 4 - +++ b/utils/build-presets.ini 5 - @@ -779,7 +779,7 @@ swiftpm 6 - 7 - dash-dash 8 - 9 - -build-ninja 10 - +# build-ninja 11 - install-llvm 12 - install-swift 13 - install-lldb
-13
pkgs/development/compilers/swift/patches/0002-build-presets-linux-allow-custom-install-prefix.patch
··· 1 - Use custom install prefix. 2 - 3 - --- a/utils/build-presets.ini 4 - +++ b/utils/build-presets.ini 5 - @@ -788,7 +788,7 @@ 6 - install-swiftpm 7 - install-xctest 8 - install-libicu 9 - -install-prefix=/usr 10 - +install-prefix=%(install_prefix)s 11 - install-libcxx 12 - install-sourcekit-lsp 13 - build-swift-static-stdlib
-23
pkgs/development/compilers/swift/patches/0003-build-presets-linux-don-t-build-extra-libs.patch
··· 1 - Disable targets, where we use Nix packages. 2 - 3 - --- a/utils/build-presets.ini 4 - +++ b/utils/build-presets.ini 5 - @@ -818,8 +818,6 @@ 6 - swiftpm 7 - swift-driver 8 - xctest 9 - -libicu 10 - -libcxx 11 - swiftdocc 12 - 13 - # build-ninja 14 - @@ -830,9 +828,7 @@ 15 - install-swiftpm 16 - install-swift-driver 17 - install-xctest 18 - -install-libicu 19 - install-prefix=%(install_prefix)s 20 - -install-libcxx 21 - install-sourcekit-lsp 22 - install-swiftdocc 23 - build-swift-static-stdlib
-13
pkgs/development/compilers/swift/patches/0004-build-presets-linux-plumb-extra-cmake-options.patch
··· 1 - Plumb extra-cmake-options. 2 - 3 - --- a/utils/build-presets.ini 4 - +++ b/utils/build-presets.ini 5 - @@ -812,6 +812,8 @@ 6 - # Path to the .tar.gz package we would create. 7 - installable-package=%(installable_package)s 8 - 9 - +extra-cmake-options=%(extra_cmake_options)s 10 - + 11 - [preset: buildbot_linux] 12 - mixin-preset=mixin_linux_installation 13 - build-subdir=buildbot_linux
pkgs/development/compilers/swift/patches/0005-clang-toolchain-dir.patch pkgs/development/compilers/swift/compiler/patches/clang-toolchain-dir.patch
-16
pkgs/development/compilers/swift/patches/0006-clang-purity.patch
··· 1 - Apply the "purity" patch (updated for 5.4.2). 2 - 3 - --- a/lib/Driver/ToolChains/Gnu.cpp 4 - +++ b/lib/Driver/ToolChains/Gnu.cpp 5 - @@ -488,11 +488,5 @@ 6 - if (Args.hasArg(options::OPT_rdynamic)) 7 - CmdArgs.push_back("-export-dynamic"); 8 - - 9 - - if (!Args.hasArg(options::OPT_shared) && !IsStaticPIE) { 10 - - CmdArgs.push_back("-dynamic-linker"); 11 - - CmdArgs.push_back(Args.MakeArgString(Twine(D.DyldPrefix) + 12 - - ToolChain.getDynamicLinker(Args))); 13 - - } 14 - } 15 - 16 - CmdArgs.push_back("-o");
-13
pkgs/development/compilers/swift/patches/0007-build-presets-linux-os-stdlib.patch
··· 1 - Use os-stdlib in tests. 2 - 3 - --- a/utils/build-presets.ini 4 - +++ b/utils/build-presets.ini 5 - @@ -872,7 +872,7 @@ 6 - indexstore-db 7 - sourcekit-lsp 8 - swiftdocc 9 - -lit-args=-v --time-tests 10 - +lit-args=-v --time-tests --param use_os_stdlib 11 - 12 - # rdar://problem/31454823 13 - lldb-test-swift-only
+50
pkgs/development/compilers/swift/wrapper/default.nix
··· 1 + { stdenv 2 + , swift 3 + , wrapperParams ? swift._wrapperParams 4 + }: 5 + 6 + stdenv.mkDerivation (wrapperParams // { 7 + pname = "swift-wrapper"; 8 + inherit (swift) version meta; 9 + 10 + outputs = [ "out" "man" ]; 11 + 12 + # Setup hook variables. 13 + inherit swift; 14 + inherit (swift) 15 + swiftOs swiftArch 16 + swiftModuleSubdir swiftLibSubdir 17 + swiftStaticModuleSubdir swiftStaticLibSubdir; 18 + 19 + passAsFile = [ "buildCommand" ]; 20 + buildCommand = '' 21 + mkdir -p $out/bin $out/nix-support 22 + 23 + # Symlink all Swift binaries first. 24 + # NOTE: This specifically omits clang binaries. We want to hide these for 25 + # private use by Swift only. 26 + ln -s -t $out/bin/ $swift/bin/swift* 27 + 28 + # Replace specific binaries with wrappers. 29 + for executable in swift swiftc swift-frontend; do 30 + export prog=$swift/bin/$executable 31 + rm $out/bin/$executable 32 + substituteAll '${./wrapper.sh}' $out/bin/$executable 33 + chmod a+x $out/bin/$executable 34 + done 35 + 36 + ln -s ${swift.man} $man 37 + 38 + # This link is here because various tools (swiftpm) check for stdlib 39 + # relative to the swift compiler. It's fine if this is for build-time 40 + # stuff, but we should patch all cases were it would end up in an output. 41 + ln -s ${swift.lib}/lib $out/lib 42 + 43 + substituteAll ${./setup-hook.sh} $out/nix-support/setup-hook 44 + ''; 45 + 46 + passthru = { 47 + inherit swift; 48 + inherit (swift) swiftOs swiftArch swiftModuleSubdir swiftLibSubdir; 49 + }; 50 + })
+28
pkgs/development/compilers/swift/wrapper/setup-hook.sh
··· 1 + # Add import paths for build inputs. 2 + swiftWrapper_addImports () { 3 + # Include subdirectories following both the Swift platform convention, and 4 + # a simple `lib/swift` for Nix convenience. 5 + for subdir in @swiftModuleSubdir@ @swiftStaticModuleSubdir@ lib/swift; do 6 + if [[ -d "$1/$subdir" ]]; then 7 + export NIX_SWIFTFLAGS_COMPILE+=" -I $1/$subdir" 8 + fi 9 + done 10 + for subdir in @swiftLibSubdir@ @swiftStaticLibSubdir@ lib/swift; do 11 + if [[ -d "$1/$subdir" ]]; then 12 + export NIX_LDFLAGS+=" -L $1/$subdir" 13 + fi 14 + done 15 + } 16 + 17 + addEnvHooks "$targetOffset" swiftWrapper_addImports 18 + 19 + # Use a postHook here because we rely on NIX_CC, which is set by the cc-wrapper 20 + # setup hook, so delay until we're sure it was run. 21 + swiftWrapper_postHook () { 22 + # On Darwin, libc also contains Swift modules. 23 + if [[ -e "$NIX_CC/nix-support/orig-libc" ]]; then 24 + swiftWrapper_addImports "$(<$NIX_CC/nix-support/orig-libc)" 25 + fi 26 + } 27 + 28 + postHooks+=(swiftWrapper_postHook)
+266
pkgs/development/compilers/swift/wrapper/wrapper.sh
··· 1 + #! @shell@ 2 + # NOTE: This wrapper is derived from cc-wrapper.sh, and is hopefully somewhat 3 + # diffable with the original, so changes can be merged if necessary. 4 + set -eu -o pipefail +o posix 5 + shopt -s nullglob 6 + 7 + if (( "${NIX_DEBUG:-0}" >= 7 )); then 8 + set -x 9 + fi 10 + 11 + cc_wrapper="${NIX_CC:-@default_cc_wrapper@}" 12 + 13 + source $cc_wrapper/nix-support/utils.bash 14 + 15 + expandResponseParams "$@" 16 + 17 + # Check if we should wrap this Swift invocation at all, and how. Specifically, 18 + # there are some internal tools we don't wrap, plus swift-frontend doesn't link 19 + # and doesn't understand linker flags. This follows logic in 20 + # `lib/DriverTool/driver.cpp`. 21 + prog=@prog@ 22 + progName="$(basename "$prog")" 23 + firstArg="${params[0]:-}" 24 + isFrontend=0 25 + isRepl=0 26 + 27 + # These checks follow `shouldRunAsSubcommand`. 28 + if [[ "$progName" == swift ]]; then 29 + case "$firstArg" in 30 + "" | -* | *.* | */* | repl) 31 + ;; 32 + *) 33 + exec "swift-$firstArg" "${params[@]:1}" 34 + ;; 35 + esac 36 + fi 37 + 38 + # These checks follow the first part of `run_driver`. 39 + # 40 + # NOTE: The original function short-circuits, but we can't here, because both 41 + # paths must be wrapped. So we use an 'isFrontend' flag instead. 42 + case "$firstArg" in 43 + -frontend) 44 + isFrontend=1 45 + # Ensure this stays the first argument. 46 + params=( "${params[@]:1}" ) 47 + extraBefore+=( "-frontend" ) 48 + ;; 49 + -modulewrap) 50 + # Don't wrap this integrated tool. 51 + exec "$prog" "${params[@]}" 52 + ;; 53 + repl) 54 + isRepl=1 55 + params=( "${params[@]:1}" ) 56 + ;; 57 + --driver-mode=*) 58 + ;; 59 + *) 60 + if [[ "$progName" == swift-frontend ]]; then 61 + isFrontend=1 62 + fi 63 + ;; 64 + esac 65 + 66 + path_backup="$PATH" 67 + 68 + # That @-vars are substituted separately from bash evaluation makes 69 + # shellcheck think this, and others like it, are useless conditionals. 70 + # shellcheck disable=SC2157 71 + if [[ -n "@coreutils_bin@" && -n "@gnugrep_bin@" ]]; then 72 + PATH="@coreutils_bin@/bin:@gnugrep_bin@/bin" 73 + fi 74 + 75 + # Parse command line options and set several variables. 76 + # For instance, figure out if linker flags should be passed. 77 + # GCC prints annoying warnings when they are not needed. 78 + isCxx=0 79 + dontLink=$isFrontend 80 + 81 + for p in "${params[@]}"; do 82 + case "$p" in 83 + -enable-cxx-interop) 84 + isCxx=1 ;; 85 + esac 86 + done 87 + 88 + # NOTE: We don't modify these for Swift, but sourced scripts may use them. 89 + cxxInclude=1 90 + cxxLibrary=1 91 + cInclude=1 92 + 93 + linkType=$(checkLinkType "${params[@]}") 94 + 95 + # Optionally filter out paths not refering to the store. 96 + if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then 97 + kept=() 98 + nParams=${#params[@]} 99 + declare -i n=0 100 + while (( "$n" < "$nParams" )); do 101 + p=${params[n]} 102 + p2=${params[n+1]:-} # handle `p` being last one 103 + n+=1 104 + 105 + skipNext=false 106 + path="" 107 + case "$p" in 108 + -[IL]/*) path=${p:2} ;; 109 + -[IL]) path=$p2 skipNext=true ;; 110 + esac 111 + 112 + if [[ -n $path ]] && badPath "$path"; then 113 + skip "$path" 114 + $skipNext && n+=1 115 + continue 116 + fi 117 + 118 + kept+=("$p") 119 + done 120 + # Old bash empty array hack 121 + params=(${kept+"${kept[@]}"}) 122 + fi 123 + 124 + # Flirting with a layer violation here. 125 + if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then 126 + source @bintools@/nix-support/add-flags.sh 127 + fi 128 + 129 + # Put this one second so libc ldflags take priority. 130 + if [ -z "${NIX_CC_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then 131 + source $cc_wrapper/nix-support/add-flags.sh 132 + fi 133 + 134 + if [[ "$isCxx" = 1 ]]; then 135 + if [[ "$cxxInclude" = 1 ]]; then 136 + NIX_CFLAGS_COMPILE_@suffixSalt@+=" $NIX_CXXSTDLIB_COMPILE_@suffixSalt@" 137 + fi 138 + if [[ "$cxxLibrary" = 1 ]]; then 139 + NIX_CFLAGS_LINK_@suffixSalt@+=" $NIX_CXXSTDLIB_LINK_@suffixSalt@" 140 + fi 141 + fi 142 + 143 + source $cc_wrapper/nix-support/add-hardening.sh 144 + 145 + # Add the flags for the C compiler proper. 146 + addCFlagsToList() { 147 + declare -n list="$1" 148 + shift 149 + 150 + for ((i = 1; i <= $#; i++)); do 151 + local val="${!i}" 152 + case "$val" in 153 + # Pass through using -Xcc, but also convert to Swift -I. 154 + # These have slightly different meaning for Clang, but Swift 155 + # doesn't have exact equivalents. 156 + -isystem | -idirafter) 157 + i=$((i + 1)) 158 + list+=("-Xcc" "$val" "-Xcc" "${!i}" "-I" "${!i}") 159 + ;; 160 + # Simple rename. 161 + -iframework) 162 + i=$((i + 1)) 163 + list+=("-Fsystem" "${!i}") 164 + ;; 165 + # Pass through verbatim. 166 + -I | -Fsystem) 167 + i=$((i + 1)) 168 + list+=("${val}" "${!i}") 169 + ;; 170 + -I* | -L* | -F*) 171 + list+=("${val}") 172 + ;; 173 + # Pass through using -Xcc. 174 + *) 175 + list+=("-Xcc" "$val") 176 + ;; 177 + esac 178 + done 179 + } 180 + for i in ${NIX_SWIFTFLAGS_COMPILE:-}; do 181 + extraAfter+=("$i") 182 + done 183 + for i in ${NIX_SWIFTFLAGS_COMPILE_BEFORE:-}; do 184 + extraBefore+=("$i") 185 + done 186 + addCFlagsToList extraAfter $NIX_CFLAGS_COMPILE_@suffixSalt@ 187 + addCFlagsToList extraBefore ${hardeningCFlags[@]+"${hardeningCFlags[@]}"} $NIX_CFLAGS_COMPILE_BEFORE_@suffixSalt@ 188 + 189 + if [ "$dontLink" != 1 ]; then 190 + 191 + # Add the flags that should only be passed to the compiler when 192 + # linking. 193 + addCFlagsToList extraAfter $(filterRpathFlags "$linkType" $NIX_CFLAGS_LINK_@suffixSalt@) 194 + 195 + # Add the flags that should be passed to the linker (and prevent 196 + # `ld-wrapper' from adding NIX_LDFLAGS_@suffixSalt@ again). 197 + for i in $(filterRpathFlags "$linkType" $NIX_LDFLAGS_BEFORE_@suffixSalt@); do 198 + extraBefore+=("-Xlinker" "$i") 199 + done 200 + if [[ "$linkType" == dynamic && -n "$NIX_DYNAMIC_LINKER_@suffixSalt@" ]]; then 201 + extraBefore+=("-Xlinker" "-dynamic-linker=$NIX_DYNAMIC_LINKER_@suffixSalt@") 202 + fi 203 + for i in $(filterRpathFlags "$linkType" $NIX_LDFLAGS_@suffixSalt@); do 204 + if [ "${i:0:3}" = -L/ ]; then 205 + extraAfter+=("$i") 206 + else 207 + extraAfter+=("-Xlinker" "$i") 208 + fi 209 + done 210 + export NIX_LINK_TYPE_@suffixSalt@=$linkType 211 + fi 212 + 213 + # TODO: If we ever need to expand functionality of this hook, it may no longer 214 + # be compatible with Swift. Right now, it is only used on Darwin to force 215 + # -target, which also happens to work with Swift. 216 + if [[ -e $cc_wrapper/nix-support/add-local-cc-cflags-before.sh ]]; then 217 + source $cc_wrapper/nix-support/add-local-cc-cflags-before.sh 218 + fi 219 + 220 + # May need to transform the triple injected by the above. 221 + for ((i = 1; i < ${#extraBefore[@]}; i++)); do 222 + if [[ "${extraBefore[i]}" = -target ]]; then 223 + i=$((i + 1)) 224 + # On Darwin only, need to change 'aarch64' to 'arm64'. 225 + extraBefore[i]="${extraBefore[i]/aarch64-apple-/arm64-apple-}" 226 + # On Darwin, Swift requires the triple to be annotated with a version. 227 + # TODO: Assumes macOS. 228 + extraBefore[i]="${extraBefore[i]/-apple-darwin/-apple-macosx${MACOSX_DEPLOYMENT_TARGET:-11.0}}" 229 + break 230 + fi 231 + done 232 + 233 + # As a very special hack, if the arguments are just `-v', then don't 234 + # add anything. This is to prevent `gcc -v' (which normally prints 235 + # out the version number and returns exit code 0) from printing out 236 + # `No input files specified' and returning exit code 1. 237 + if [ "$*" = -v ]; then 238 + extraAfter=() 239 + extraBefore=() 240 + fi 241 + 242 + # Optionally print debug info. 243 + if (( "${NIX_DEBUG:-0}" >= 1 )); then 244 + # Old bash workaround, see ld-wrapper for explanation. 245 + echo "extra flags before to $prog:" >&2 246 + printf " %q\n" ${extraBefore+"${extraBefore[@]}"} >&2 247 + echo "original flags to $prog:" >&2 248 + printf " %q\n" ${params+"${params[@]}"} >&2 249 + echo "extra flags after to $prog:" >&2 250 + printf " %q\n" ${extraAfter+"${extraAfter[@]}"} >&2 251 + fi 252 + 253 + PATH="$path_backup" 254 + # Old bash workaround, see above. 255 + 256 + if (( "${NIX_CC_USE_RESPONSE_FILE:-@use_response_file_by_default@}" >= 1 )); then 257 + exec "$prog" @<(printf "%q\n" \ 258 + ${extraBefore+"${extraBefore[@]}"} \ 259 + ${params+"${params[@]}"} \ 260 + ${extraAfter+"${extraAfter[@]}"}) 261 + else 262 + exec "$prog" \ 263 + ${extraBefore+"${extraBefore[@]}"} \ 264 + ${params+"${params[@]}"} \ 265 + ${extraAfter+"${extraAfter[@]}"} 266 + fi
+33 -13
pkgs/os-specific/darwin/apple-sdk-11.0/default.nix
··· 3 3 , xar, cpio, python3, pbzx }: 4 4 5 5 let 6 - MacOSX-SDK = stdenvNoCC.mkDerivation rec { 7 - pname = "MacOSX-SDK"; 8 - version = "11.0.0"; 9 - 10 - # https://swscan.apple.com/content/catalogs/others/index-11-10.15-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog 11 - src = fetchurl { 12 - url = "http://swcdn.apple.com/content/downloads/46/21/001-89745-A_56FM390IW5/v1um2qppgfdnam2e9cdqcqu2r6k8aa3lis/CLTools_macOSNMOS_SDK.pkg"; 13 - sha256 = "0n425smj4q1vxbza8fzwnk323fyzbbq866q32w288c44hl5yhwsf"; 14 - }; 15 - 6 + mkSusDerivation = args: stdenvNoCC.mkDerivation (args // { 16 7 dontBuild = true; 17 8 darwinDontCodeSign = true; 18 9 ··· 24 15 pbzx $src | cpio -idm 25 16 ''; 26 17 18 + passthru = { 19 + inherit (args) version; 20 + }; 21 + }); 22 + 23 + MacOSX-SDK = mkSusDerivation { 24 + pname = "MacOSX-SDK"; 25 + version = "11.0.0"; 26 + 27 + # https://swscan.apple.com/content/catalogs/others/index-11-10.15-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog 28 + src = fetchurl { 29 + url = "http://swcdn.apple.com/content/downloads/46/21/001-89745-A_56FM390IW5/v1um2qppgfdnam2e9cdqcqu2r6k8aa3lis/CLTools_macOSNMOS_SDK.pkg"; 30 + sha256 = "0n425smj4q1vxbza8fzwnk323fyzbbq866q32w288c44hl5yhwsf"; 31 + }; 32 + 27 33 installPhase = '' 28 34 cd Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk 29 35 30 36 mkdir $out 31 37 cp -r System usr $out/ 32 38 ''; 39 + }; 33 40 34 - passthru = { 35 - inherit version; 41 + CLTools_Executables = mkSusDerivation { 42 + pname = "CLTools_Executables"; 43 + version = "11.0.0"; 44 + 45 + # https://swscan.apple.com/content/catalogs/others/index-11-10.15-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog 46 + src = fetchurl { 47 + url = "http://swcdn.apple.com/content/downloads/46/21/001-89745-A_56FM390IW5/v1um2qppgfdnam2e9cdqcqu2r6k8aa3lis/CLTools_Executables.pkg"; 48 + sha256 = "0nvb1qx7l81l2wcl8wvgbpsg5rcn51ylhivqmlfr2hrrv3zrrpl0"; 36 49 }; 50 + 51 + installPhase = '' 52 + cd Library/Developer/CommandLineTools 53 + 54 + mkdir $out 55 + cp -r Library usr $out/ 56 + ''; 37 57 }; 38 58 39 59 callPackage = newScope (packages // pkgs.darwin // { inherit MacOSX-SDK; }); ··· 43 63 44 64 # TODO: this is nice to be private. is it worth the callPackage above? 45 65 # Probably, I don't think that callPackage costs much at all. 46 - inherit MacOSX-SDK; 66 + inherit MacOSX-SDK CLTools_Executables; 47 67 48 68 Libsystem = callPackage ./libSystem.nix {}; 49 69 LibsystemCross = pkgs.darwin.Libsystem;
+2 -1
pkgs/top-level/all-packages.nix
··· 15041 15041 15042 15042 svdtools = callPackage ../development/embedded/svdtools { }; 15043 15043 15044 - swift = callPackage ../development/compilers/swift { }; 15044 + swiftPackages = recurseIntoAttrs (callPackage ../development/compilers/swift { }); 15045 + inherit (swiftPackages) swift; 15045 15046 15046 15047 swiProlog = callPackage ../development/compilers/swi-prolog { 15047 15048 openssl = openssl_1_1;