2024-03-28 11:18:31 -07:00
|
|
|
{ lib
|
|
|
|
, stdenv
|
|
|
|
, llvm_meta
|
|
|
|
, release_version
|
|
|
|
, version
|
|
|
|
, patches ? []
|
|
|
|
, src ? null
|
|
|
|
, monorepoSrc ? null
|
|
|
|
, runCommand
|
|
|
|
, cmake
|
|
|
|
, ninja
|
|
|
|
, python3
|
|
|
|
, xcbuild
|
|
|
|
, libllvm
|
2023-11-27 08:02:52 +02:00
|
|
|
, libcxx
|
2024-03-28 11:18:31 -07:00
|
|
|
, linuxHeaders
|
|
|
|
, libxcrypt
|
2024-05-16 10:48:29 -04:00
|
|
|
|
|
|
|
# Some platforms have switched to using compiler-rt, but still want a
|
|
|
|
# libgcc.a for ABI compat purposes. The use case would be old code that
|
|
|
|
# expects to link `-lgcc` but doesn't care exactly what its contents
|
|
|
|
# are, so long as it provides some builtins.
|
2020-03-09 02:10:06 -04:00
|
|
|
, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
|
2024-05-16 10:48:29 -04:00
|
|
|
|
|
|
|
# In recent releases, the compiler-rt build seems to produce
|
|
|
|
# many `libclang_rt*` libraries, but not a single unified
|
|
|
|
# `libcompiler_rt` library, at least under certain configurations. Some
|
|
|
|
# platforms stil expect this, however, so we symlink one into place.
|
|
|
|
, forceLinkCompilerRt ? stdenv.hostPlatform.isOpenBSD
|
2022-01-09 08:59:00 +00:00
|
|
|
}:
|
2021-06-03 20:14:05 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
useLLVM = stdenv.hostPlatform.useLLVM or false;
|
|
|
|
bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
|
|
|
|
haveLibc = stdenv.cc.libc != null;
|
2023-11-27 08:02:52 +02:00
|
|
|
# TODO: Make this account for GCC having libstdcxx, which will help
|
|
|
|
# use clean up the `cmakeFlags` rats nest below.
|
|
|
|
haveLibcxx = stdenv.cc.libcxx != null;
|
2024-03-28 11:18:31 -07:00
|
|
|
isDarwinStatic = stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic && lib.versionAtLeast release_version "16";
|
|
|
|
inherit (stdenv.hostPlatform) isMusl isAarch64;
|
2024-08-07 01:49:41 -04:00
|
|
|
noSanitizers = !haveLibc || bareMetal || isMusl || isDarwinStatic;
|
2021-06-03 20:14:05 +02:00
|
|
|
|
2022-01-09 08:59:00 +00:00
|
|
|
baseName = "compiler-rt";
|
2024-03-28 11:18:31 -07:00
|
|
|
pname = baseName + lib.optionalString (haveLibc) "-libc";
|
|
|
|
|
|
|
|
src' = if monorepoSrc != null then
|
|
|
|
runCommand "${baseName}-src-${version}" {} ''
|
|
|
|
mkdir -p "$out"
|
|
|
|
cp -r ${monorepoSrc}/cmake "$out"
|
|
|
|
cp -r ${monorepoSrc}/${baseName} "$out"
|
|
|
|
'' else src;
|
2022-01-09 08:59:00 +00:00
|
|
|
|
2023-11-27 08:02:52 +02:00
|
|
|
preConfigure = lib.optionalString (!haveLibc) ''
|
2024-03-28 11:18:31 -07:00
|
|
|
cmakeFlagsArray+=(-DCMAKE_C_FLAGS="-nodefaultlibs -ffreestanding")
|
2022-01-09 08:59:00 +00:00
|
|
|
'';
|
2021-06-03 20:14:05 +02:00
|
|
|
in
|
|
|
|
|
2024-03-28 11:18:31 -07:00
|
|
|
stdenv.mkDerivation ({
|
|
|
|
inherit pname version patches;
|
2021-04-26 19:18:45 +02:00
|
|
|
|
2024-03-28 11:18:31 -07:00
|
|
|
src = src';
|
|
|
|
sourceRoot = if lib.versionOlder release_version "13" then null
|
|
|
|
else "${src'.name}/${baseName}";
|
2021-06-03 20:14:05 +02:00
|
|
|
|
2024-03-28 11:18:31 -07:00
|
|
|
nativeBuildInputs = [ cmake ]
|
|
|
|
++ (lib.optional (lib.versionAtLeast release_version "15") ninja)
|
|
|
|
++ [ python3 libllvm.dev ]
|
2022-08-10 22:11:15 +02:00
|
|
|
++ lib.optional stdenv.isDarwin xcbuild.xcrun;
|
2023-12-14 14:09:07 +01:00
|
|
|
buildInputs =
|
llvmPackages_{12,13,14,15,16,17,git}.{libcxx,libcxxabi}: merge libcxxabi into libcxx (#292043)
- merge libcxxabi into libcxx for LLVM 12, 13, 14, 15, 16, 17, and git.
- remove the link time workaround `-lc++ -lc++abi` from 58 packages as it is no longer required.
- fixes https://github.com/NixOS/nixpkgs/issues/166205
- provides alternative fixes for. https://github.com/NixOS/nixpkgs/issues/269548 https://github.com/NixOS/nix/issues/9640
- pkgsCross.x86_64-freebsd builds work again
This change can be represented in 3 stages
1. merge libcxxabi into libcxx -- files: pkgs/development/compilers/llvm/[12, git]/{libcxx, libcxxabi}
2. update stdenv to account for merge -- files: stdenv.{adapters, cc.wrapper, darwin}
3. remove all references to libcxxabi outside of llvm (about 58 packages modified)
### merging libcxxabi into libcxx
- take the union of the libcxxabi and libcxx cmake flags
- eliminate the libcxx-headers-only package - it was only needed to break libcxx <-> libcxxabi circular dependency
- libcxx.cxxabi is removed. external cxxabi (freebsd) will symlink headers / libs into libcxx.
- darwin will re-export the libcxxabi symbols into libcxx so linking `-lc++` is sufficient.
- linux/freebsd `libc++.so` is a linker script `LINK(libc++.so.1, -lc++abi)` making `-lc++` sufficient.
- libcxx/default.nix [12, 17] are identical except for patches and `LIBCXX_ADDITIONAL_LIBRARIES` (only used in 16+)
- git/libcxx/defaul.nix does not link with -nostdlib when useLLVM is true so flag is removed. this is not much different than before as libcxxabi used -nostdlib where libcxx did not, so libc was linked in anyway.
### stdenv changes
- darwin bootstrap, remove references to libcxxabi and cxxabi
- cc-wrapper: remove c++ link workaround when libcxx.cxxabi doesn't exist (still exists for LLVM pre 12)
- adapter: update overrideLibcxx to account for a pkgs.stdenv that only has libcxx
### 58 package updates
- remove `NIX_LDFLAGS = "-l${stdenv.cc.libcxx.cxxabi.libName}` as no longer needed
- swift, nodejs_v8 remove libcxxabi references in the clang override
https://github.com/NixOS/nixpkgs/pull/292043
2024-03-11 03:53:37 -07:00
|
|
|
lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
|
2021-06-03 20:14:05 +02:00
|
|
|
|
2023-10-02 09:59:18 -07:00
|
|
|
env.NIX_CFLAGS_COMPILE = toString ([
|
2021-06-03 20:14:05 +02:00
|
|
|
"-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
|
2023-10-02 09:59:18 -07:00
|
|
|
] ++ lib.optionals (!haveLibc) [
|
|
|
|
# The compiler got stricter about this, and there is a usellvm patch below
|
|
|
|
# which patches out the assert include causing an implicit definition of
|
|
|
|
# assert. It would be nicer to understand why compiler-rt thinks it should
|
|
|
|
# be able to #include <assert.h> in the first place; perhaps it's in the
|
|
|
|
# wrong, or perhaps there is a way to provide an assert.h.
|
|
|
|
"-Wno-error=implicit-function-declaration"
|
|
|
|
]);
|
2021-06-03 20:14:05 +02:00
|
|
|
|
|
|
|
cmakeFlags = [
|
|
|
|
"-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
|
|
|
|
"-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
|
|
|
|
"-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
|
2023-02-23 13:44:01 +00:00
|
|
|
] ++ lib.optionals (haveLibc && stdenv.hostPlatform.libc == "glibc") [
|
|
|
|
"-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
|
2023-11-27 08:02:52 +02:00
|
|
|
] ++ lib.optionals (useLLVM && haveLibc && stdenv.cc.libcxx == libcxx) [
|
|
|
|
"-DSANITIZER_CXX_ABI=libcxxabi"
|
|
|
|
"-DSANITIZER_CXX_ABI_LIBNAME=libcxxabi"
|
|
|
|
"-DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON"
|
|
|
|
] ++ lib.optionals ((!haveLibc || bareMetal || isMusl || isAarch64) && (lib.versions.major release_version == "13")) [
|
2024-03-28 11:18:31 -07:00
|
|
|
"-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
|
2023-11-27 08:02:52 +02:00
|
|
|
] ++ lib.optionals (useLLVM && haveLibc) [
|
|
|
|
"-DCOMPILER_RT_BUILD_SANITIZERS=ON"
|
2024-08-07 01:49:41 -04:00
|
|
|
] ++ lib.optionals (noSanitizers) [
|
2021-06-03 20:14:05 +02:00
|
|
|
"-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
|
2023-11-27 08:02:52 +02:00
|
|
|
] ++ lib.optionals ((useLLVM && !haveLibcxx) || !haveLibc || bareMetal || isMusl || isDarwinStatic) [
|
2021-06-03 20:14:05 +02:00
|
|
|
"-DCOMPILER_RT_BUILD_XRAY=OFF"
|
|
|
|
"-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
|
2022-01-04 12:30:40 +01:00
|
|
|
"-DCOMPILER_RT_BUILD_MEMPROF=OFF"
|
|
|
|
"-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
|
2023-11-27 08:02:52 +02:00
|
|
|
] ++ lib.optionals (useLLVM && haveLibc) [
|
|
|
|
"-DCOMPILER_RT_BUILD_PROFILE=ON"
|
|
|
|
] ++ lib.optionals (!haveLibc || bareMetal) [
|
2023-10-02 09:59:18 -07:00
|
|
|
"-DCOMPILER_RT_BUILD_PROFILE=OFF"
|
2023-11-27 08:02:52 +02:00
|
|
|
] ++ lib.optionals (!haveLibc || bareMetal || isDarwinStatic) [
|
2023-10-02 09:59:18 -07:00
|
|
|
"-DCMAKE_CXX_COMPILER_WORKS=ON"
|
2023-11-27 08:02:52 +02:00
|
|
|
] ++ lib.optionals (!haveLibc || bareMetal) [
|
2021-06-03 20:14:05 +02:00
|
|
|
"-DCMAKE_C_COMPILER_WORKS=ON"
|
|
|
|
"-DCOMPILER_RT_BAREMETAL_BUILD=ON"
|
|
|
|
"-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
|
2023-11-27 08:02:52 +02:00
|
|
|
] ++ lib.optionals (!haveLibc) [
|
2023-10-02 09:59:18 -07:00
|
|
|
"-DCMAKE_C_FLAGS=-nodefaultlibs"
|
2021-06-03 20:14:05 +02:00
|
|
|
] ++ lib.optionals (useLLVM) [
|
|
|
|
"-DCOMPILER_RT_BUILD_BUILTINS=ON"
|
|
|
|
#https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
|
|
|
|
"-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
|
|
|
|
] ++ lib.optionals (bareMetal) [
|
|
|
|
"-DCOMPILER_RT_OS_DIR=baremetal"
|
2024-03-28 11:18:31 -07:00
|
|
|
] ++ lib.optionals (stdenv.hostPlatform.isDarwin) (lib.optionals (lib.versionAtLeast release_version "16") [
|
2023-10-02 09:59:18 -07:00
|
|
|
"-DCMAKE_LIPO=${lib.getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}lipo"
|
2024-03-28 11:18:31 -07:00
|
|
|
] ++ [
|
2021-06-03 20:14:05 +02:00
|
|
|
"-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
|
|
|
|
"-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
|
|
|
|
"-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
|
2024-03-28 11:18:31 -07:00
|
|
|
] ++ lib.optionals (lib.versionAtLeast release_version "15") [
|
2022-10-07 17:16:05 -05:00
|
|
|
# `COMPILER_RT_DEFAULT_TARGET_ONLY` does not apply to Darwin:
|
|
|
|
# https://github.com/llvm/llvm-project/blob/27ef42bec80b6c010b7b3729ed0528619521a690/compiler-rt/cmake/base-config-ix.cmake#L153
|
|
|
|
"-DCOMPILER_RT_ENABLE_IOS=OFF"
|
2024-03-28 11:18:31 -07:00
|
|
|
]) ++ lib.optionals (lib.versionAtLeast version "19" && stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
|
2024-04-07 21:22:28 -07:00
|
|
|
"-DSANITIZER_MIN_OSX_VERSION=10.10"
|
2024-08-07 01:49:41 -04:00
|
|
|
] ++ lib.optionals (noSanitizers && lib.versionAtLeast release_version "19") [
|
|
|
|
"-DCOMPILER_RT_BUILD_CTX_PROFILE=OFF"
|
2021-06-03 20:14:05 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
outputs = [ "out" "dev" ];
|
|
|
|
|
|
|
|
# TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
|
|
|
|
# to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
|
|
|
|
# can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
|
|
|
|
# get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
|
|
|
|
# a flag and turn the flag off during the stdenv build.
|
|
|
|
postPatch = lib.optionalString (!stdenv.isDarwin) ''
|
|
|
|
substituteInPlace cmake/builtin-config-ix.cmake \
|
|
|
|
--replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
|
|
|
|
'' + lib.optionalString stdenv.isDarwin ''
|
|
|
|
substituteInPlace cmake/config-ix.cmake \
|
|
|
|
--replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
|
2023-11-27 08:02:52 +02:00
|
|
|
'' + lib.optionalString (!haveLibc) ((lib.optionalString (lib.versionAtLeast release_version "18") ''
|
2024-03-16 20:16:06 -07:00
|
|
|
substituteInPlace lib/builtins/aarch64/sme-libc-routines.c \
|
|
|
|
--replace "<stdlib.h>" "<stddef.h>"
|
2024-03-28 11:18:31 -07:00
|
|
|
'') + ''
|
2021-06-03 20:14:05 +02:00
|
|
|
substituteInPlace lib/builtins/int_util.c \
|
|
|
|
--replace "#include <stdlib.h>" ""
|
2024-05-14 13:10:12 -07:00
|
|
|
'' + (if stdenv.hostPlatform.isFreeBSD then
|
|
|
|
# As per above, but in FreeBSD assert is a macro and simply allowing it to be implicitly declared causes Issues!!!!!
|
|
|
|
''
|
2024-05-31 10:15:37 -07:00
|
|
|
substituteInPlace lib/builtins/clear_cache.c lib/builtins/cpu_model${lib.optionalString (lib.versionAtLeast version "18") "/x86"}.c \
|
2024-05-14 13:10:12 -07:00
|
|
|
--replace "#include <assert.h>" "#define assert(e) ((e)?(void)0:__assert(__FUNCTION__,__FILE__,__LINE__,#e))"
|
|
|
|
'' else ''
|
2021-06-03 20:14:05 +02:00
|
|
|
substituteInPlace lib/builtins/clear_cache.c \
|
|
|
|
--replace "#include <assert.h>" ""
|
2024-02-22 18:01:29 +01:00
|
|
|
substituteInPlace lib/builtins/cpu_model${lib.optionalString (lib.versionAtLeast version "18") "/x86"}.c \
|
2021-06-03 20:14:05 +02:00
|
|
|
--replace "#include <assert.h>" ""
|
2024-07-14 01:23:39 -07:00
|
|
|
'')) + lib.optionalString (lib.versionAtLeast release_version "13" && lib.versionOlder release_version "14") ''
|
|
|
|
# https://github.com/llvm/llvm-project/blob/llvmorg-14.0.6/libcxx/utils/merge_archives.py
|
|
|
|
# Seems to only be used in v13 though it's present in v12 and v14, and dropped in v15.
|
|
|
|
substituteInPlace ../libcxx/utils/merge_archives.py \
|
|
|
|
--replace-fail "import distutils.spawn" "from shutil import which as find_executable" \
|
|
|
|
--replace-fail "distutils.spawn." ""
|
|
|
|
'';
|
2021-06-03 20:14:05 +02:00
|
|
|
|
|
|
|
# Hack around weird upsream RPATH bug
|
2023-12-08 15:49:21 +01:00
|
|
|
postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin) ''
|
2021-06-03 20:14:05 +02:00
|
|
|
ln -s "$out/lib"/*/* "$out/lib"
|
2023-12-08 15:49:21 +01:00
|
|
|
'' + lib.optionalString (useLLVM && stdenv.hostPlatform.isLinux) ''
|
2021-06-03 20:14:05 +02:00
|
|
|
ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
|
|
|
|
ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
|
2023-10-02 09:59:18 -07:00
|
|
|
# Note the history of crt{begin,end}S in previous versions of llvm in nixpkg:
|
|
|
|
# The presence of crtbegin_shared has been added and removed; it's possible
|
|
|
|
# people have added/removed it to get it working on their platforms.
|
|
|
|
# Try each in turn for now.
|
|
|
|
ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbeginS.o
|
|
|
|
ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtendS.o
|
2021-06-03 20:14:05 +02:00
|
|
|
ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
|
|
|
|
ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
|
2020-03-09 02:10:06 -04:00
|
|
|
'' + lib.optionalString doFakeLibgcc ''
|
2024-05-16 10:48:29 -04:00
|
|
|
ln -s $out/lib/*/libclang_rt.builtins-*.a $out/lib/libgcc.a
|
|
|
|
'' + lib.optionalString forceLinkCompilerRt ''
|
|
|
|
ln -s $out/lib/*/libclang_rt.builtins-*.a $out/lib/libcompiler_rt.a
|
2021-06-03 20:14:05 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
meta = llvm_meta // {
|
|
|
|
homepage = "https://compiler-rt.llvm.org/";
|
|
|
|
description = "Compiler runtime libraries";
|
|
|
|
longDescription = ''
|
|
|
|
The compiler-rt project provides highly tuned implementations of the
|
|
|
|
low-level code generator support routines like "__fixunsdfdi" and other
|
|
|
|
calls generated when a target doesn't have a short sequence of native
|
|
|
|
instructions to implement a core IR operation. It also provides
|
|
|
|
implementations of run-time libraries for dynamic testing tools such as
|
|
|
|
AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
|
|
|
|
'';
|
|
|
|
# "All of the code in the compiler-rt project is dual licensed under the MIT
|
|
|
|
# license and the UIUC License (a BSD-like license)":
|
|
|
|
license = with lib.licenses; [ mit ncsa ];
|
2023-11-27 08:02:52 +02:00
|
|
|
broken =
|
|
|
|
# compiler-rt requires a Clang stdenv on 32-bit RISC-V:
|
|
|
|
# https://reviews.llvm.org/D43106#1019077
|
|
|
|
(stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang)
|
|
|
|
# emutls wants `<pthread.h>` which isn't avaiable (without exeprimental WASM threads proposal).
|
|
|
|
# `enable_execute_stack.c` Also doesn't sound like something WASM would support.
|
|
|
|
|| (stdenv.hostPlatform.isWasm && haveLibc);
|
2021-06-03 20:14:05 +02:00
|
|
|
};
|
2024-03-28 11:18:31 -07:00
|
|
|
} // (if lib.versionOlder release_version "16" then { inherit preConfigure; } else {}))
|