push sheeet
Some checks failed
Periodic Merges (6h) / master → staging-nixos (push) Failing after 12m50s
Periodic Merges (6h) / master → staging-next (push) Failing after 12m54s
Periodic Merges (24h) / merge-base(master,staging) → haskell-updates (push) Failing after 11m54s
Periodic Merges (6h) / staging-next → staging (push) Failing after 12m13s
Periodic Merges (24h) / staging-next-25.05 → staging-25.05 (push) Failing after 13m24s
Periodic Merges (24h) / release-25.05 → staging-next-25.05 (push) Failing after 14m28s

This commit is contained in:
Dark Steveneq
2025-10-09 14:15:47 +02:00
commit 646b892680
49168 changed files with 5897842 additions and 0 deletions

View File

@@ -0,0 +1,828 @@
{
lib,
stdenv,
callPackage,
cmake,
bash,
coreutils,
gnugrep,
perl,
ninja_1_11,
pkg-config,
clang,
bintools,
python312Packages,
git,
fetchpatch,
fetchpatch2,
makeWrapper,
gnumake,
file,
runCommand,
writeShellScriptBin,
# For lldb
libedit,
ncurses,
swig,
libxml2,
# Linux-specific
glibc,
libuuid,
# Darwin-specific
replaceVars,
fixDarwinDylibNames,
xcbuild,
cctools, # libtool
sigtool,
DarwinTools,
apple-sdk_13,
darwinMinVersionHook,
}:
let
apple-sdk_swift = apple-sdk_13; # Use the SDK that was available when Swift shipped.
deploymentVersion =
if lib.versionOlder (targetPlatform.darwinMinVersion or "0") "10.15" then
"10.15"
else
targetPlatform.darwinMinVersion;
# Use Python 3.12 for now because Swift 5.8 depends on Python's PyEval_ThreadsInitialized(), which was removed in 3.13.
python3 = python312Packages.python.withPackages (p: [ p.setuptools ]); # python 3.12 compat.
inherit (stdenv) hostPlatform targetPlatform;
sources = callPackage ../sources.nix { };
# There are apparently multiple naming conventions on Darwin. Swift uses the
# xcrun naming convention. See `configure_sdk_darwin` calls in CMake files.
swiftOs =
if targetPlatform.isDarwin then
{
"macos" = "macosx";
"ios" = "iphoneos";
#iphonesimulator
#appletvos
#appletvsimulator
#watchos
#watchsimulator
}
.${targetPlatform.darwinPlatform}
or (throw "Cannot build Swift for target Darwin platform '${targetPlatform.darwinPlatform}'")
else
targetPlatform.parsed.kernel.name;
# This causes swiftPackages.XCTest to fail to build on aarch64-linux
# as I believe this is because Apple calls the architecture aarch64
# on Linux rather than arm64 when used with macOS.
swiftArch =
if hostPlatform.isDarwin then hostPlatform.darwinArch else targetPlatform.parsed.cpu.name;
# On Darwin, a `.swiftmodule` is a subdirectory in `lib/swift/<OS>`,
# containing binaries for supported archs. On other platforms, binaries are
# installed to `lib/swift/<OS>/<ARCH>`. Note that our setup-hook also adds
# `lib/swift` for convenience.
swiftLibSubdir = "lib/swift/${swiftOs}";
swiftModuleSubdir =
if hostPlatform.isDarwin then "lib/swift/${swiftOs}" else "lib/swift/${swiftOs}/${swiftArch}";
# And then there's also a separate subtree for statically linked modules.
toStaticSubdir = lib.replaceStrings [ "/swift/" ] [ "/swift_static/" ];
swiftStaticLibSubdir = toStaticSubdir swiftLibSubdir;
swiftStaticModuleSubdir = toStaticSubdir swiftModuleSubdir;
# This matches _SWIFT_DEFAULT_COMPONENTS, with specific components disabled.
swiftInstallComponents = [
"autolink-driver"
"compiler"
# "clang-builtin-headers"
"stdlib"
"sdk-overlay"
"static-mirror-lib"
"editor-integration"
# "tools"
# "testsuite-tools"
"toolchain-tools"
"toolchain-dev-tools"
"license"
(if stdenv.hostPlatform.isDarwin then "sourcekit-xpc-service" else "sourcekit-inproc")
"swift-remote-mirror"
"swift-remote-mirror-headers"
];
clangForWrappers = clang.override (prev: {
extraBuildCommands =
prev.extraBuildCommands
# We need to use the resource directory corresponding to Swifts
# version of Clang instead of passing along the one from the
# `cc-wrapper` flags.
+ ''
substituteInPlace $out/nix-support/cc-cflags \
--replace-fail " -resource-dir=$out/resource-root" ""
'';
});
# Build a tool used during the build to create a custom clang wrapper, with
# which we wrap the clang produced by the swift build.
#
# This is used in a `POST_BUILD` for the CMake target, so we rename the
# actual clang to clang-unwrapped, then put the wrapper in place.
#
# We replace the `exec ...` command with `exec -a "$0"` in order to
# preserve $0 for clang. This is because, unlike Nix, we don't have
# separate wrappers for clang/clang++, and clang uses $0 to detect C++.
#
# Similarly, the C++ detection in the wrapper itself also won't work for us,
# so we base it on $0 as well.
makeClangWrapper = writeShellScriptBin "nix-swift-make-clang-wrapper" ''
set -euo pipefail
targetFile="$1"
unwrappedClang="$targetFile-unwrapped"
mv "$targetFile" "$unwrappedClang"
sed < '${clangForWrappers}/bin/clang' > "$targetFile" \
-e 's|^\s*exec|exec -a "$0"|g' \
-e 's|^\[\[ "${clang.cc}/bin/clang" = \*++ ]]|[[ "$0" = *++ ]]|' \
-e "s|${clang.cc}/bin/clang|$unwrappedClang|g" \
-e "s|^\(\s*\)\($unwrappedClang\) \"@\\\$responseFile\"|\1argv0=\$0\n\1${bash}/bin/bash -c \"exec -a '\$argv0' \2 '@\$responseFile'\"|" \
${lib.optionalString (clang.libcxx != null) ''
-e 's|$NIX_CXXSTDLIB_COMPILE_${clang.suffixSalt}|-isystem '$SWIFT_BUILD_ROOT'/libcxx/include/c++/v1|g'
''}
chmod a+x "$targetFile"
'';
# Create a tool used during the build to create a custom swift wrapper for
# each of the swift executables produced by the build.
#
# The build produces several `swift-frontend` executables during
# bootstrapping. Each of these has numerous aliases via symlinks, and the
# executable uses $0 to detect what tool is called.
wrapperParams = {
inherit bintools;
coreutils_bin = lib.getBin coreutils;
gnugrep_bin = gnugrep;
suffixSalt = lib.replaceStrings [ "-" "." ] [ "_" "_" ] targetPlatform.config;
use_response_file_by_default = 1;
swiftDriver = "";
# NOTE: @cc_wrapper@ and @prog@ need to be filled elsewhere.
};
swiftWrapper = runCommand "swift-wrapper.sh" wrapperParams ''
# Make empty to avoid adding the SDKs modules in the bootstrap wrapper. Otherwise, the SDK conflicts with the
# shims the wrapper tries to build.
darwinMinVersion="" substituteAll '${../wrapper/wrapper.sh}' "$out"
'';
makeSwiftcWrapper = writeShellScriptBin "nix-swift-make-swift-wrapper" ''
set -euo pipefail
targetFile="$1"
unwrappedSwift="$targetFile-unwrapped"
mv "$targetFile" "$unwrappedSwift"
sed < '${swiftWrapper}' > "$targetFile" \
-e "s|@prog@|'$unwrappedSwift'|g" \
-e 's|@cc_wrapper@|${clangForWrappers}|g' \
-e 's|exec "$prog"|exec -a "$0" "$prog"|g' \
${lib.optionalString (clang.libcxx != null) ''
-e 's|$NIX_CXXSTDLIB_COMPILE_${clang.suffixSalt}|-isystem '$SWIFT_BUILD_ROOT'/libcxx/include/c++/v1|g'
''}
chmod a+x "$targetFile"
'';
# On Darwin, we need to use BOOTSTRAPPING-WITH-HOSTLIBS because of ABI
# stability, and have to provide the definitions for the system stdlib.
appleSwiftCore = stdenv.mkDerivation {
name = "apple-swift-core";
dontUnpack = true;
buildInputs = [ apple-sdk_swift ];
installPhase = ''
mkdir -p $out/lib/swift
cp -r \
"$SDKROOT/usr/lib/swift/Swift.swiftmodule" \
"$SDKROOT/usr/lib/swift/CoreFoundation.swiftmodule" \
"$SDKROOT/usr/lib/swift/Dispatch.swiftmodule" \
"$SDKROOT/usr/lib/swift/ObjectiveC.swiftmodule" \
"$SDKROOT/usr/lib/swift/libswiftCore.tbd" \
"$SDKROOT/usr/lib/swift/libswiftCoreFoundation.tbd" \
"$SDKROOT/usr/lib/swift/libswiftDispatch.tbd" \
"$SDKROOT/usr/lib/swift/libswiftFoundation.tbd" \
"$SDKROOT/usr/lib/swift/libswiftObjectiveC.tbd" \
$out/lib/swift/
'';
};
# https://github.com/NixOS/nixpkgs/issues/327836
# Fail to build with ninja 1.12 when NIX_BUILD_CORES is low (Hydra or Github Actions).
# Can reproduce using `nix --option cores 2 build -f . swiftPackages.swift-unwrapped`.
# Until we find out the exact cause, follow [swift upstream][1], pin ninja to version
# 1.11.1.
# [1]: https://github.com/swiftlang/swift/pull/72989
ninja = ninja_1_11;
in
stdenv.mkDerivation {
pname = "swift";
inherit (sources) version;
outputs = [
"out"
"lib"
"dev"
"doc"
"man"
];
nativeBuildInputs = [
cmake
git
ninja
perl # pod2man
pkg-config
python3
makeWrapper
makeClangWrapper
makeSwiftcWrapper
]
++ lib.optionals stdenv.hostPlatform.isDarwin [
xcbuild
sigtool # codesign
DarwinTools # sw_vers
fixDarwinDylibNames
cctools.libtool
];
buildInputs = [
# For lldb
python3
swig
libxml2
]
++ lib.optionals stdenv.hostPlatform.isLinux [
libuuid
]
++ lib.optionals stdenv.hostPlatform.isDarwin [
apple-sdk_swift
];
# Will effectively be `buildInputs` when swift is put in `nativeBuildInputs`.
depsTargetTargetPropagated = lib.optionals stdenv.targetPlatform.isDarwin [
apple-sdk_swift
];
# This is a partial reimplementation of our setup hook. Because we reuse
# the Swift wrapper for the Swift build itself, we need to do some of the
# same preparation.
postHook = ''
for pkg in "''${pkgsHostTarget[@]}" '${clang.libc}'; do
for subdir in ${swiftModuleSubdir} ${swiftStaticModuleSubdir} lib/swift; do
if [[ -d "$pkg/$subdir" ]]; then
export NIX_SWIFTFLAGS_COMPILE+=" -I $pkg/$subdir"
fi
done
for subdir in ${swiftLibSubdir} ${swiftStaticLibSubdir} lib/swift; do
if [[ -d "$pkg/$subdir" ]]; then
export NIX_LDFLAGS+=" -L $pkg/$subdir"
fi
done
done
'';
# We setup custom build directories.
dontUseCmakeBuildDir = true;
unpackPhase =
let
copySource = repo: "cp -r ${sources.${repo}} ${repo}";
in
''
mkdir src
cd src
${copySource "swift-cmark"}
${copySource "llvm-project"}
${copySource "swift"}
${copySource "swift-experimental-string-processing"}
${copySource "swift-syntax"}
${lib.optionalString (!stdenv.hostPlatform.isDarwin) (copySource "swift-corelibs-libdispatch")}
chmod -R u+w .
'';
patchPhase = ''
# Just patch all the things for now, we can focus this later.
# TODO: eliminate use of env.
find -type f -print0 | xargs -0 sed -i \
${lib.optionalString stdenv.hostPlatform.isDarwin "-e 's|/usr/libexec/PlistBuddy|${xcbuild}/bin/PlistBuddy|g'"} \
-e 's|/usr/bin/env|${coreutils}/bin/env|g' \
-e 's|/usr/bin/make|${gnumake}/bin/make|g' \
-e 's|/bin/mkdir|${coreutils}/bin/mkdir|g' \
-e 's|/bin/cp|${coreutils}/bin/cp|g' \
-e 's|/usr/bin/file|${file}/bin/file|g'
patch -p1 -d swift -i ${./patches/swift-cmake-3.25-compat.patch}
patch -p1 -d swift -i ${./patches/swift-wrap.patch}
patch -p1 -d swift -i ${./patches/swift-linux-fix-libc-paths.patch}
patch -p1 -d swift -i ${
replaceVars ./patches/swift-linux-fix-linking.patch {
inherit clang;
}
}
patch -p1 -d swift -i ${
replaceVars ./patches/swift-darwin-plistbuddy-workaround.patch {
inherit swiftArch;
}
}
patch -p1 -d swift -i ${
replaceVars ./patches/swift-prevent-sdk-dirs-warning.patch {
inherit (builtins) storeDir;
}
}
# This patch needs to know the lib output location, so must be substituted
# in the same derivation as the compiler.
storeDir="${builtins.storeDir}" \
substituteAll ${./patches/swift-separate-lib.patch} $TMPDIR/swift-separate-lib.patch
patch -p1 -d swift -i $TMPDIR/swift-separate-lib.patch
patch -p1 -d llvm-project/llvm -i ${./patches/llvm-module-cache.patch}
for lldbPatch in ${
lib.escapeShellArgs [
# Fixes for SWIG 4
(fetchpatch2 {
url = "https://github.com/llvm/llvm-project/commit/81fc5f7909a4ef5a8d4b5da2a10f77f7cb01ba63.patch?full_index=1";
stripLen = 1;
hash = "sha256-Znw+C0uEw7lGETQLKPBZV/Ymo2UigZS+Hv/j1mUo7p0=";
})
(fetchpatch2 {
url = "https://github.com/llvm/llvm-project/commit/f0a25fe0b746f56295d5c02116ba28d2f965c175.patch?full_index=1";
stripLen = 1;
hash = "sha256-QzVeZzmc99xIMiO7n//b+RNAvmxghISKQD93U2zOgFI=";
})
(fetchpatch2 {
url = "https://github.com/llvm/llvm-project/commit/ba35c27ec9aa9807f5b4be2a0c33ca9b045accc7.patch?full_index=1";
stripLen = 1;
hash = "sha256-LXl+WbpmWZww5xMDrle3BM2Tw56v8k9LO1f1Z1/wDTs=";
})
(fetchpatch2 {
url = "https://github.com/llvm/llvm-project/commit/9ec115978ea2bdfc60800cd3c21264341cdc8b0a.patch?full_index=1";
stripLen = 1;
hash = "sha256-u0zSejEjfrH3ZoMFm1j+NVv2t5AP9cE5yhsrdTS1dG4=";
})
# Fix the build with modern libc++.
(fetchpatch {
name = "add-cstdio.patch";
url = "https://github.com/llvm/llvm-project/commit/73e15b5edb4fa4a77e68c299a6e3b21e610d351f.patch";
stripLen = 1;
hash = "sha256-eFcvxZaAuBsY/bda1h9212QevrXyvCHw8Cr9ngetDr0=";
})
(fetchpatch {
url = "https://github.com/llvm/llvm-project/commit/68744ffbdd7daac41da274eef9ac0d191e11c16d.patch";
stripLen = 1;
hash = "sha256-QCGhsL/mi7610ZNb5SqxjRGjwJeK2rwtsFVGeG3PUGc=";
})
]
}; do
patch -p1 -d llvm-project/lldb -i $lldbPatch
done
patch -p1 -d llvm-project/clang -i ${./patches/clang-toolchain-dir.patch}
patch -p1 -d llvm-project/clang -i ${./patches/clang-wrap.patch}
patch -p1 -d llvm-project/clang -i ${./patches/clang-purity.patch}
patch -p2 -d llvm-project/clang -i ${
fetchpatch {
name = "clang-cmake-fix-interpreter.patch";
url = "https://github.com/llvm/llvm-project/commit/b5eaf500f2441eff2277ea2973878fb1f171fd0a.patch";
sha256 = "1rma1al0rbm3s3ql6bnvbcighp74lri1lcrwbyacgdqp80fgw1b6";
}
}
# gcc-13 build fixes
patch -p2 -d llvm-project/llvm -i ${
fetchpatch {
name = "gcc-13.patch";
url = "https://github.com/llvm/llvm-project/commit/ff1681ddb303223973653f7f5f3f3435b48a1983.patch";
hash = "sha256-nkRPWx8gNvYr7mlvEUiOAb1rTrf+skCZjAydJVUHrcI=";
}
}
${lib.optionalString stdenv.hostPlatform.isLinux ''
substituteInPlace llvm-project/clang/lib/Driver/ToolChains/Linux.cpp \
--replace 'SysRoot + "/lib' '"${glibc}/lib" "' \
--replace 'SysRoot + "/usr/lib' '"${glibc}/lib" "' \
--replace 'LibDir = "lib";' 'LibDir = "${glibc}/lib";' \
--replace 'LibDir = "lib64";' 'LibDir = "${glibc}/lib";' \
--replace 'LibDir = X32 ? "libx32" : "lib64";' 'LibDir = "${glibc}/lib";'
# uuid.h is not part of glibc, but of libuuid.
sed -i 's|''${GLIBC_INCLUDE_PATH}/uuid/uuid.h|${libuuid.dev}/include/uuid/uuid.h|' \
swift/stdlib/public/Platform/glibc.modulemap.gyb
''}
# Remove tests for cross compilation, which we don't currently support.
rm swift/test/Interop/Cxx/class/constructors-copy-irgen-*.swift
rm swift/test/Interop/Cxx/class/constructors-irgen-*.swift
# TODO: consider fixing and re-adding. This test fails due to a non-standard "install_prefix".
rm swift/validation-test/Python/build_swift.swift
# We cannot handle the SDK location being in "Weird Location" due to Nix isolation.
rm swift/test/DebugInfo/compiler-flags.swift
# TODO: Fix issue with ld.gold invoked from script finding crtbeginS.o and crtendS.o.
rm swift/test/IRGen/ELF-remove-autolink-section.swift
# The following two tests fail because we use don't use the bundled libicu:
# [SOURCE_DIR/utils/build-script] ERROR: can't find source directory for libicu (tried /build/src/icu)
rm swift/validation-test/BuildSystem/default_build_still_performs_epilogue_opts_after_split.test
rm swift/validation-test/BuildSystem/test_early_swift_driver_and_infer.swift
# TODO: This test fails for some unknown reason
rm swift/test/Serialization/restrict-swiftmodule-to-revision.swift
# This test was flaky in ofborg, see #186476
rm swift/test/AutoDiff/compiler_crashers_fixed/issue-56649-missing-debug-scopes-in-pullback-trampoline.swift
patchShebangs .
${lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
patch -p1 -d swift-corelibs-libdispatch -i ${
# Fix the build with modern Clang.
fetchpatch {
url = "https://github.com/swiftlang/swift-corelibs-libdispatch/commit/30bb8019ba79cdae0eb1dc0c967c17996dd5cc0a.patch";
hash = "sha256-wPZQ4wtEWk8HaKMfzjamlU6p/IW5EFiTssY63rGM+ZA=";
}
}
''}
'';
# > clang-15-unwrapped: error: unsupported option '-fzero-call-used-regs=used-gpr' for target 'arm64-apple-macosx10.9.0'
# > clang-15-unwrapped: error: argument unused during compilation: '-fstack-clash-protection' [-Werror,-Wunused-command-line-argument]
hardeningDisable = lib.optionals stdenv.hostPlatform.isAarch64 [
"zerocallusedregs"
"stackclashprotection"
];
configurePhase = ''
export SWIFT_SOURCE_ROOT="$PWD"
mkdir -p ../build
cd ../build
export SWIFT_BUILD_ROOT="$PWD"
'';
# These steps are derived from doing a normal build with.
#
# ./swift/utils/build-toolchain test --dry-run
#
# But dealing with the custom Python build system is far more trouble than
# simply invoking CMake directly. Few variables it passes to CMake are
# actually required or non-default.
#
# Using CMake directly also allows us to split up the already large build,
# and package Swift components separately.
#
# Besides `--dry-run`, another good way to compare build changes between
# Swift releases is to diff the scripts:
#
# git diff swift-5.6.3-RELEASE..swift-5.7-RELEASE -- utils/build*
#
buildPhase = ''
# Helper to build a subdirectory.
#
# Always reset cmakeFlags before calling this. The cmakeConfigurePhase
# amends flags and would otherwise keep expanding it.
function buildProject() {
mkdir -p $SWIFT_BUILD_ROOT/$1
cd $SWIFT_BUILD_ROOT/$1
cmakeDir=$SWIFT_SOURCE_ROOT/''${2-$1}
cmakeConfigurePhase
ninjaBuildPhase
}
cmakeFlags="-GNinja"
buildProject swift-cmark
${lib.optionalString (clang.libcxx != null) ''
# Install the libc++ headers corresponding to the LLVM version of
# Swifts Clang.
cmakeFlags="
-GNinja
-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi
-DLIBCXXABI_INSTALL_INCLUDE_DIR=$dev/include/c++/v1
"
ninjaFlags="install-cxx-headers install-cxxabi-headers"
buildProject libcxx llvm-project/runtimes
unset ninjaFlags
''}
# Some notes:
# - The Swift build just needs Clang.
# - We can further reduce targets to just our targetPlatform.
cmakeFlags="
-GNinja
-DLLVM_BUILD_TOOLS=NO
-DLLVM_ENABLE_PROJECTS=clang
-DLLVM_TARGETS_TO_BUILD=${
{
"x86_64" = "X86";
"aarch64" = "AArch64";
}
.${targetPlatform.parsed.cpu.name}
or (throw "Unsupported CPU architecture: ${targetPlatform.parsed.cpu.name}")
}
"
buildProject llvm llvm-project/llvm
# Ensure that the built Clang can find the runtime libraries by
# copying the symlinks from the main wrapper.
cp -P ${clang}/resource-root/{lib,share} $SWIFT_BUILD_ROOT/llvm/lib/clang/15.0.0/
''
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
# Add appleSwiftCore to the search paths. Adding the whole SDK results in build failures.
OLD_NIX_SWIFTFLAGS_COMPILE="$NIX_SWIFTFLAGS_COMPILE"
OLD_NIX_LDFLAGS="$NIX_LDFLAGS"
export NIX_SWIFTFLAGS_COMPILE=" -I ${appleSwiftCore}/lib/swift"
export NIX_LDFLAGS+=" -L ${appleSwiftCore}/lib/swift"
''
+ ''
# Some notes:
# - BOOTSTRAPPING_MODE defaults to OFF in CMake, but is enabled in standard
# builds, so we enable it as well. On Darwin, we have to use the system
# Swift libs because of ABI-stability, but this may be trouble if the
# builder is an older macOS.
# - Experimental features are OFF by default in CMake, but are enabled in
# official builds, so we do the same. (Concurrency is also required in
# the stdlib. StringProcessing is often implicitely imported, causing
# lots of warnings if missing.)
# - SWIFT_STDLIB_ENABLE_OBJC_INTEROP is set explicitely because its check
# is buggy. (Uses SWIFT_HOST_VARIANT_SDK before initialized.)
# Fixed in: https://github.com/apple/swift/commit/84083afef1de5931904d5c815d53856cdb3fb232
cmakeFlags="
-GNinja
-DBOOTSTRAPPING_MODE=BOOTSTRAPPING${lib.optionalString stdenv.hostPlatform.isDarwin "-WITH-HOSTLIBS"}
-DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING=ON
-DSWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY=ON
-DSWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED=ON
-DSWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING=ON
-DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm
-DClang_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/clang
-DSWIFT_PATH_TO_CMARK_SOURCE=$SWIFT_SOURCE_ROOT/swift-cmark
-DSWIFT_PATH_TO_CMARK_BUILD=$SWIFT_BUILD_ROOT/swift-cmark
-DSWIFT_PATH_TO_LIBDISPATCH_SOURCE=$SWIFT_SOURCE_ROOT/swift-corelibs-libdispatch
-DSWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE=$SWIFT_SOURCE_ROOT/swift-syntax
-DSWIFT_PATH_TO_STRING_PROCESSING_SOURCE=$SWIFT_SOURCE_ROOT/swift-experimental-string-processing
-DSWIFT_INSTALL_COMPONENTS=${lib.concatStringsSep ";" swiftInstallComponents}
-DSWIFT_STDLIB_ENABLE_OBJC_INTEROP=${if stdenv.hostPlatform.isDarwin then "ON" else "OFF"}
-DSWIFT_DARWIN_DEPLOYMENT_VERSION_OSX=${deploymentVersion}
"
buildProject swift
''
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
# Restore search paths to remove appleSwiftCore.
export NIX_SWIFTFLAGS_COMPILE="$OLD_NIX_SWIFTFLAGS_COMPILE"
export NIX_LDFLAGS="$OLD_NIX_LDFLAGS"
''
+ ''
# These are based on flags in `utils/build-script-impl`.
#
# LLDB_USE_SYSTEM_DEBUGSERVER=ON disables the debugserver build on Darwin,
# which requires a special signature.
#
# CMAKE_BUILD_WITH_INSTALL_NAME_DIR ensures we don't use rpath on Darwin.
cmakeFlags="
-GNinja
-DLLDB_SWIFTC=$SWIFT_BUILD_ROOT/swift/bin/swiftc
-DLLDB_SWIFT_LIBS=$SWIFT_BUILD_ROOT/swift/lib/swift
-DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm
-DClang_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/clang
-DSwift_DIR=$SWIFT_BUILD_ROOT/swift/lib/cmake/swift
-DLLDB_ENABLE_CURSES=ON
-DLLDB_ENABLE_LIBEDIT=ON
-DLLDB_ENABLE_PYTHON=ON
-DLLDB_ENABLE_LZMA=OFF
-DLLDB_ENABLE_LUA=OFF
-DLLDB_INCLUDE_TESTS=OFF
-DCMAKE_BUILD_WITH_INSTALL_NAME_DIR=ON
${lib.optionalString stdenv.hostPlatform.isDarwin ''
-DLLDB_USE_SYSTEM_DEBUGSERVER=ON
''}
-DLibEdit_INCLUDE_DIRS=${lib.getInclude libedit}/include
-DLibEdit_LIBRARIES=${lib.getLib libedit}/lib/libedit${stdenv.hostPlatform.extensions.sharedLibrary}
-DCURSES_INCLUDE_DIRS=${lib.getInclude ncurses}/include
-DCURSES_LIBRARIES=${lib.getLib ncurses}/lib/libncurses${stdenv.hostPlatform.extensions.sharedLibrary}
-DPANEL_LIBRARIES=${lib.getLib ncurses}/lib/libpanel${stdenv.hostPlatform.extensions.sharedLibrary}
";
buildProject lldb llvm-project/lldb
${lib.optionalString stdenv.targetPlatform.isDarwin ''
# Need to do a standalone build of concurrency for Darwin back deployment.
# Based on: utils/swift_build_support/swift_build_support/products/backdeployconcurrency.py
cmakeFlags="
-GNinja
-DCMAKE_Swift_COMPILER=$SWIFT_BUILD_ROOT/swift/bin/swiftc
-DSWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE=$SWIFT_SOURCE_ROOT/swift-syntax
-DTOOLCHAIN_DIR=/var/empty
-DSWIFT_NATIVE_LLVM_TOOLS_PATH=${stdenv.cc}/bin
-DSWIFT_NATIVE_CLANG_TOOLS_PATH=${stdenv.cc}/bin
-DSWIFT_NATIVE_SWIFT_TOOLS_PATH=$SWIFT_BUILD_ROOT/swift/bin
-DCMAKE_CROSSCOMPILING=ON
-DBUILD_SWIFT_CONCURRENCY_BACK_DEPLOYMENT_LIBRARIES=ON
-DSWIFT_INCLUDE_TOOLS=OFF
-DSWIFT_BUILD_STDLIB_EXTRA_TOOLCHAIN_CONTENT=OFF
-DSWIFT_BUILD_TEST_SUPPORT_MODULES=OFF
-DSWIFT_BUILD_STDLIB=OFF
-DSWIFT_BUILD_DYNAMIC_STDLIB=OFF
-DSWIFT_BUILD_STATIC_STDLIB=OFF
-DSWIFT_BUILD_REMOTE_MIRROR=OFF
-DSWIFT_BUILD_SDK_OVERLAY=OFF
-DSWIFT_BUILD_DYNAMIC_SDK_OVERLAY=OFF
-DSWIFT_BUILD_STATIC_SDK_OVERLAY=OFF
-DSWIFT_INCLUDE_TESTS=OFF
-DSWIFT_BUILD_PERF_TESTSUITE=OFF
-DSWIFT_HOST_VARIANT_ARCH=${swiftArch}
-DBUILD_STANDALONE=ON
-DSWIFT_INSTALL_COMPONENTS=back-deployment
-DSWIFT_SDKS=${
{
"macos" = "OSX";
"ios" = "IOS";
#IOS_SIMULATOR
#TVOS
#TVOS_SIMULATOR
#WATCHOS
#WATCHOS_SIMULATOR
}
.${targetPlatform.darwinPlatform}
}
-DLLVM_DIR=$SWIFT_BUILD_ROOT/llvm/lib/cmake/llvm
-DSWIFT_DEST_ROOT=$out
-DSWIFT_HOST_VARIANT_SDK=OSX
-DSWIFT_DARWIN_DEPLOYMENT_VERSION_OSX=${deploymentVersion}
-DSWIFT_DARWIN_DEPLOYMENT_VERSION_IOS=13.0
-DSWIFT_DARWIN_DEPLOYMENT_VERSION_MACCATALYST=13.0
-DSWIFT_DARWIN_DEPLOYMENT_VERSION_TVOS=13.0
-DSWIFT_DARWIN_DEPLOYMENT_VERSION_WATCHOS=6.0
"
# This depends on the special Clang build specific to the Swift branch.
# We also need to call a specific Ninja target.
export CC=$SWIFT_BUILD_ROOT/llvm/bin/clang
export CXX=$SWIFT_BUILD_ROOT/llvm/bin/clang++
ninjaFlags="back-deployment"
buildProject swift-concurrency-backdeploy swift
export CC=$NIX_CC/bin/clang
export CXX=$NIX_CC/bin/clang++
unset ninjaFlags
''}
'';
# TODO: ~50 failing tests on x86_64-linux. Other platforms not checked.
doCheck = false;
nativeCheckInputs = [ file ];
# TODO: consider using stress-tester and integration-test.
checkPhase = ''
cd $SWIFT_BUILD_ROOT/swift
checkTarget=check-swift-all
ninjaCheckPhase
unset checkTarget
'';
installPhase = ''
# Undo the clang and swift wrapping we did for the build.
# (This happened via patches to cmake files.)
cd $SWIFT_BUILD_ROOT
mv llvm/bin/clang-15{-unwrapped,}
mv swift/bin/swift-frontend{-unwrapped,}
mkdir $lib
# Install clang binaries only. We hide these with the wrapper, so they are
# for private use by Swift only.
cd $SWIFT_BUILD_ROOT/llvm
installTargets=install-clang
ninjaInstallPhase
unset installTargets
# LLDB is also a private install.
cd $SWIFT_BUILD_ROOT/lldb
ninjaInstallPhase
cd $SWIFT_BUILD_ROOT/swift
ninjaInstallPhase
${lib.optionalString stdenv.hostPlatform.isDarwin ''
cd $SWIFT_BUILD_ROOT/swift-concurrency-backdeploy
installTargets=install-back-deployment
ninjaInstallPhase
unset installTargets
''}
# Separate $lib output here, because specific logic follows.
# Only move the dynamic run-time parts, to keep $lib small. Every Swift
# build will depend on it.
moveToOutput "lib/swift" "$lib"
moveToOutput "lib/libswiftDemangle.*" "$lib"
# This link is here because various tools (swiftpm) check for stdlib
# relative to the swift compiler. It's fine if this is for build-time
# stuff, but we should patch all cases were it would end up in an output.
ln -s $lib/lib/swift $out/lib/swift
# Swift has a separate resource root from Clang, but locates the Clang
# resource root via subdir or symlink.
mv $SWIFT_BUILD_ROOT/llvm/lib/clang/15.0.0 $lib/lib/swift/clang
'';
preFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
# This is cheesy, but helps the patchelf hook remove /build from RPATH.
cd $SWIFT_BUILD_ROOT/..
mv build buildx
'';
postFixup = lib.optionalString stdenv.hostPlatform.isDarwin ''
# These libraries need to use the system install name. The official SDK
# does the same (as opposed to using rpath). Presumably, they are part of
# the stable ABI. Not using the system libraries at run-time is known to
# cause ObjC class conflicts and segfaults.
declare -A systemLibs=(
[libswiftCore.dylib]=1
[libswiftDarwin.dylib]=1
[libswiftSwiftOnoneSupport.dylib]=1
[libswift_Concurrency.dylib]=1
)
for systemLib in "''${!systemLibs[@]}"; do
install_name_tool -id /usr/lib/swift/$systemLib $lib/${swiftLibSubdir}/$systemLib
done
for file in $out/bin/swift-frontend $lib/${swiftLibSubdir}/*.dylib; do
changeArgs=""
for dylib in $(otool -L $file | awk '{ print $1 }'); do
if [[ ''${systemLibs["$(basename $dylib)"]} ]]; then
changeArgs+=" -change $dylib /usr/lib/swift/$(basename $dylib)"
elif [[ "$dylib" = */bootstrapping1/* ]]; then
changeArgs+=" -change $dylib $lib/lib/swift/$(basename $dylib)"
fi
done
if [[ -n "$changeArgs" ]]; then
install_name_tool $changeArgs $file
fi
done
wrapProgram $out/bin/swift-frontend \
--prefix PATH : ${lib.makeBinPath [ cctools.libtool ]}
# Needs to be propagated by the compiler not by its dev output.
moveToOutput nix-support/propagated-target-target-deps "$out"
'';
passthru = {
inherit
swiftOs
swiftArch
swiftModuleSubdir
swiftLibSubdir
swiftStaticModuleSubdir
swiftStaticLibSubdir
;
tests = {
cxx-interop-test = callPackage ../cxx-interop-test { };
};
# Internal attr for the wrapper.
_wrapperParams = wrapperParams;
};
meta = {
description = "Swift Programming Language";
homepage = "https://github.com/apple/swift";
teams = [ lib.teams.swift ];
license = lib.licenses.asl20;
platforms = with lib.platforms; linux ++ darwin;
# Swift doesn't support 32-bit Linux, unknown on other platforms.
badPlatforms = lib.platforms.i686;
timeout = 86400; # 24 hours.
};
}

View File

@@ -0,0 +1,28 @@
From 4add81bba40dcec62c4ea4481be8e35ac53e89d8 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 18 May 2017 11:56:12 -0500
Subject: [PATCH] "purity" patch for 5.0
---
lib/Driver/ToolChains/Gnu.cpp | 7 -------
1 file changed, 7 deletions(-)
diff --git a/lib/Driver/ToolChains/Gnu.cpp b/lib/Driver/ToolChains/Gnu.cpp
index fe3c0191bb..c6a482bece 100644
--- a/lib/Driver/ToolChains/Gnu.cpp
+++ b/lib/Driver/ToolChains/Gnu.cpp
@@ -487,12 +487,6 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
if (!IsStatic) {
if (Args.hasArg(options::OPT_rdynamic))
CmdArgs.push_back("-export-dynamic");
-
- if (!Args.hasArg(options::OPT_shared) && !IsStaticPIE) {
- CmdArgs.push_back("-dynamic-linker");
- CmdArgs.push_back(Args.MakeArgString(Twine(D.DyldPrefix) +
- ToolChain.getDynamicLinker(Args)));
- }
}
CmdArgs.push_back("-o");
--
2.11.0

View File

@@ -0,0 +1,13 @@
Use the Nix include dirs and gcc runtime dir, when no sysroot is configured.
--- a/lib/Driver/ToolChains/Linux.cpp
+++ b/lib/Driver/ToolChains/Linux.cpp
@@ -574,7 +574,7 @@
// Check for configure-time C include directories.
StringRef CIncludeDirs(C_INCLUDE_DIRS);
- if (CIncludeDirs != "") {
+ if (CIncludeDirs != "" && (SysRoot.empty() || SysRoot == "/")) {
SmallVector<StringRef, 5> dirs;
CIncludeDirs.split(dirs, ":");
for (StringRef dir : dirs) {

View File

@@ -0,0 +1,18 @@
Wrap the clang produced during the build
--- a/tools/driver/CMakeLists.txt
+++ b/tools/driver/CMakeLists.txt
@@ -59,6 +59,13 @@ endif()
add_dependencies(clang clang-resource-headers)
+# Nix: wrap the clang build.
+add_custom_command(
+ TARGET clang POST_BUILD
+ COMMAND nix-swift-make-clang-wrapper $<TARGET_FILE:clang>
+ VERBATIM
+)
+
if(NOT CLANG_LINKS_TO_CREATE)
set(CLANG_LINKS_TO_CREATE clang++ clang-cl clang-cpp)
endif()

View File

@@ -0,0 +1,30 @@
The compiler fails if LLVM modules are enabled and it cannot write its module
cache. This patch detects and rejects the fake, non-existant $HOME used in Nix
builds.
We could simply return false in `cache_directory`, but that completely disables
module caching, and may unnecessarily slow down builds. Instead, let it use
'/tmp/.cache'.
--- a/lib/Support/Unix/Path.inc
+++ b/lib/Support/Unix/Path.inc
@@ -1380,6 +1380,9 @@ bool user_config_directory(SmallVectorImpl<char> &result) {
if (!home_directory(result)) {
return false;
}
+ if (std::equal(result.begin(), result.end(), "/homeless-shelter")) {
+ return false;
+ }
append(result, ".config");
return true;
}
@@ -1401,6 +1404,9 @@ bool cache_directory(SmallVectorImpl<char> &result) {
if (!home_directory(result)) {
return false;
}
+ if (std::equal(result.begin(), result.end(), "/homeless-shelter")) {
+ system_temp_directory(true/*ErasedOnReboot*/, result);
+ }
append(result, ".cache");
return true;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,17 @@
CMake tries to read a list field from SDKSettings.plist, but the output of
facebook/xcbuild PlistBuddy is incompatible with Apple's.
Simply set the supported architectures to the one target architecture we're
building for.
--- a/cmake/modules/SwiftConfigureSDK.cmake
+++ b/cmake/modules/SwiftConfigureSDK.cmake
@@ -189,7 +189,7 @@ macro(configure_sdk_darwin
endif()
# Remove any architectures not supported by the SDK.
- remove_sdk_unsupported_archs(${name} ${xcrun_name} ${SWIFT_SDK_${prefix}_PATH} SWIFT_SDK_${prefix}_ARCHITECTURES)
+ set(SWIFT_SDK_${prefix}_ARCHITECTURES "@swiftArch@")
list_intersect(
"${SWIFT_DARWIN_MODULE_ARCHS}" # lhs

View File

@@ -0,0 +1,48 @@
This code injects an LLVM modulemap for glibc and libstdc++ by overriding
specific VFS paths. In order to do that, it needs to know the actual locations
of glibc and libstdc++, but it only searches `-sysroot` and fails. Here we
patch it to also consider `-idirafter` and `-isystem` as added by cc-wrapper.
--- a/lib/ClangImporter/ClangIncludePaths.cpp
+++ b/lib/ClangImporter/ClangIncludePaths.cpp
@@ -120,6 +120,7 @@ static clang::driver::Driver createClangDriver(const ASTContext &ctx) {
/// \return a path without dots (`../`, './').
static llvm::Optional<Path>
findFirstIncludeDir(const llvm::opt::InputArgList &args,
+ const llvm::opt::ArgList &DriverArgs,
const ArrayRef<const char *> expectedFileNames) {
// C++ stdlib paths are added as `-internal-isystem`.
std::vector<std::string> includeDirs =
@@ -128,6 +129,14 @@ findFirstIncludeDir(const llvm::opt::InputArgList &args,
llvm::append_range(includeDirs,
args.getAllArgValues(
clang::driver::options::OPT_internal_externc_isystem));
+ // Nix adds the C stdlib include path using `-idirafter`.
+ llvm::append_range(includeDirs,
+ DriverArgs.getAllArgValues(
+ clang::driver::options::OPT_idirafter));
+ // Nix adds the C++ stdlib include path using `-isystem`.
+ llvm::append_range(includeDirs,
+ DriverArgs.getAllArgValues(
+ clang::driver::options::OPT_isystem));
for (const auto &includeDir : includeDirs) {
Path dir(includeDir);
@@ -193,7 +202,7 @@ getGlibcFileMapping(ASTContext &ctx) {
// Ideally we would check that all of the headers referenced from the
// modulemap are present.
Path glibcDir;
- if (auto dir = findFirstIncludeDir(parsedIncludeArgs,
+ if (auto dir = findFirstIncludeDir(parsedIncludeArgs, clangDriverArgs,
{"inttypes.h", "unistd.h", "stdint.h"})) {
glibcDir = dir.value();
} else {
@@ -251,7 +260,7 @@ getLibStdCxxFileMapping(ASTContext &ctx) {
auto parsedStdlibArgs = parseClangDriverArgs(clangDriver, stdlibArgStrings);
Path cxxStdlibDir;
- if (auto dir = findFirstIncludeDir(parsedStdlibArgs,
+ if (auto dir = findFirstIncludeDir(parsedStdlibArgs, clangDriverArgs,
{"cstdlib", "string", "vector"})) {
cxxStdlibDir = dir.value();
} else {

View File

@@ -0,0 +1,17 @@
diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp
index c0ee9217e8..bf7737d6fa 100644
--- a/lib/Driver/ToolChains.cpp
+++ b/lib/Driver/ToolChains.cpp
@@ -1489,6 +1489,12 @@
LinkerDriver = Args.MakeArgString(tool.get());
}
+ // For Nix, prefer linking using the wrapped Nixpkgs clang, instead of using
+ // the unwrapped clang packaged with swift. The latter is unable to link, but
+ // we still want to use it for other purposes (clang importer).
+ if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {"@clang@/bin"}))
+ return Args.MakeArgString(tool.get());
+
return LinkerDriver;
}

View File

@@ -0,0 +1,39 @@
Prevents a user-visible warning on every compilation:
ld: warning: directory not found for option '-L.../MacOSX11.0.sdk/usr/lib/swift'
--- a/lib/Driver/ToolChains.cpp
+++ b/lib/Driver/ToolChains.cpp
@@ -1455,9 +1455,11 @@ void ToolChain::getRuntimeLibraryPaths(SmallVectorImpl<std::string> &runtimeLibP
runtimeLibPaths.push_back(std::string(scratchPath.str()));
}
+ if (!SDKPath.startswith("@storeDir@")) {
scratchPath = SDKPath;
llvm::sys::path::append(scratchPath, "usr", "lib", "swift");
runtimeLibPaths.push_back(std::string(scratchPath.str()));
+ }
}
}
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -185,7 +185,9 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
RuntimeLibraryImportPaths.push_back(std::string(LibPath.str()));
}
- LibPath = SearchPathOpts.getSDKPath();
+ auto SDKPath = SearchPathOpts.getSDKPath();
+ if (!SDKPath.startswith("@storeDir@")) {
+ LibPath = SDKPath;
llvm::sys::path::append(LibPath, "usr", "lib", "swift");
if (!Triple.isOSDarwin()) {
// Use the non-architecture suffixed form with directory-layout
@@ -200,6 +202,7 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
}
RuntimeLibraryImportPaths.push_back(std::string(LibPath.str()));
+ }
}
SearchPathOpts.setRuntimeLibraryImportPaths(RuntimeLibraryImportPaths);
}

View File

@@ -0,0 +1,26 @@
Patch paths to use the separate 'lib' output. One of the things this patch
fixes is the output of `swift -frontend -print-target-info`, which swiftpm uses
to set rpath on Linux.
The check if the executable path starts with 'out' is necessary for
bootstrapping, or the compiler will fail when run from the build directory.
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -49,11 +49,16 @@ swift::CompilerInvocation::CompilerInvocation() {
void CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
StringRef mainExecutablePath, bool shared,
llvm::SmallVectorImpl<char> &runtimeResourcePath) {
+ if (mainExecutablePath.startswith("@storeDir@")) {
+ auto libPath = StringRef("@lib@");
+ runtimeResourcePath.append(libPath.begin(), libPath.end());
+ } else {
runtimeResourcePath.append(mainExecutablePath.begin(),
mainExecutablePath.end());
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /swift
llvm::sys::path::remove_filename(runtimeResourcePath); // Remove /bin
+ }
appendSwiftLibDir(runtimeResourcePath, shared);
}

View File

@@ -0,0 +1,46 @@
Wrap the swift compiler produced during the build
--- a/tools/driver/CMakeLists.txt
+++ b/tools/driver/CMakeLists.txt
@@ -16,6 +16,13 @@ if(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*")
swiftDriverTool
libswiftStub)
+ # Nix: wrap the swift build.
+ add_custom_command(
+ TARGET swift-frontend-bootstrapping0 POST_BUILD
+ COMMAND nix-swift-make-swift-wrapper $<TARGET_FILE:swift-frontend-bootstrapping0>
+ VERBATIM
+ )
+
swift_create_post_build_symlink(swift-frontend-bootstrapping0
SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}"
DESTINATION "swiftc${CMAKE_EXECUTABLE_SUFFIX}"
@@ -34,6 +41,13 @@ if(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*")
swiftDriverTool
libswift-bootstrapping1)
+ # Nix: wrap the swift build.
+ add_custom_command(
+ TARGET swift-frontend-bootstrapping1 POST_BUILD
+ COMMAND nix-swift-make-swift-wrapper $<TARGET_FILE:swift-frontend-bootstrapping1>
+ VERBATIM
+ )
+
swift_create_post_build_symlink(swift-frontend-bootstrapping1
SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}"
DESTINATION "swiftc${CMAKE_EXECUTABLE_SUFFIX}"
@@ -50,6 +64,13 @@ target_link_libraries(swift-frontend
swiftDriverTool
libswift)
+# Nix: wrap the swift build.
+add_custom_command(
+ TARGET swift-frontend POST_BUILD
+ COMMAND nix-swift-make-swift-wrapper $<TARGET_FILE:swift-frontend>
+ VERBATIM
+)
+
# Create a `swift-driver` executable adjacent to the `swift-frontend` executable
# to ensure that `swiftc` forwards to the standalone driver when invoked.
swift_create_early_driver_copies(swift-frontend)