1 { lib, stdenv, version, fetch, cmake, python3, llvm, libcxxabi }:
5 useLLVM = stdenv.hostPlatform.useLLVM or false;
6 bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
7 inherit (stdenv.hostPlatform) isMusl;
12 pname = "compiler-rt";
14 src = fetch "compiler-rt" "1n48p8gjarihkws0i2bay5w9bdwyxyxxbpwyng7ba58jb30dlyq5";
16 nativeBuildInputs = [ cmake python3 llvm ];
17 buildInputs = lib.optional stdenv.hostPlatform.isDarwin libcxxabi;
19 NIX_CFLAGS_COMPILE = [
20 "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
24 "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
25 "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
26 "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
27 ] ++ lib.optionals (useLLVM || bareMetal || isMusl) [
28 "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
29 "-DCOMPILER_RT_BUILD_XRAY=OFF"
30 "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
31 "-DCOMPILER_RT_BUILD_PROFILE=OFF"
32 ] ++ lib.optionals (useLLVM || bareMetal) [
33 "-DCMAKE_C_COMPILER_WORKS=ON"
34 "-DCMAKE_CXX_COMPILER_WORKS=ON"
35 "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
36 "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
37 ] ++ lib.optionals (useLLVM) [
38 "-DCOMPILER_RT_BUILD_BUILTINS=ON"
39 "-DCMAKE_C_FLAGS=-nodefaultlibs"
40 #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
41 "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
42 ] ++ lib.optionals (bareMetal) [
43 "-DCOMPILER_RT_OS_DIR=baremetal"
44 ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
45 # The compiler-rt build infrastructure sniffs supported platforms on Darwin
46 # and finds i386;x86_64;x86_64h. We only build for x86_64, so linking fails
47 # when it tries to use libc++ and libc++api for i386.
48 "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
51 outputs = [ "out" "dev" ];
54 # https://github.com/llvm/llvm-project/commit/947f9692440836dcb8d88b74b69dd379d85974ce
55 ../../common/compiler-rt/glibc.patch
56 ./codesign.patch # Revert compiler-rt commit that makes codesign mandatory
57 ] ++ lib.optional (useLLVM) ./crtbegin-and-end.patch
58 ++ lib.optional stdenv.hostPlatform.isMusl ./sanitizers-nongnu.patch
59 ++ lib.optional stdenv.hostPlatform.isAarch32 ./armv7l.patch;
61 # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
62 # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
63 # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
64 # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
65 # a flag and turn the flag off during the stdenv build.
66 postPatch = lib.optionalString (!stdenv.isDarwin) ''
67 substituteInPlace cmake/builtin-config-ix.cmake \
68 --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
69 '' + lib.optionalString stdenv.isDarwin ''
70 substituteInPlace cmake/config-ix.cmake \
71 --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
72 '' + lib.optionalString (useLLVM) ''
73 substituteInPlace lib/builtins/int_util.c \
74 --replace "#include <stdlib.h>" ""
75 substituteInPlace lib/builtins/clear_cache.c \
76 --replace "#include <assert.h>" ""
77 substituteInPlace lib/builtins/cpu_model.c \
78 --replace "#include <assert.h>" ""
81 # Hack around weird upsream RPATH bug
82 postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin || stdenv.hostPlatform.isWasm) ''
83 ln -s "$out/lib"/*/* "$out/lib"
84 '' + lib.optionalString (useLLVM) ''
85 ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/linux/crtbegin.o
86 ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/linux/crtend.o
87 ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/linux/crtbeginS.o
88 ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/linux/crtendS.o