1{ lib, stdenv, llvm_meta, version, src, cmake, python3, libllvm, libcxxabi }:
2
3let
4
5 useLLVM = stdenv.hostPlatform.useLLVM or false;
6 bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
7 haveLibc = stdenv.cc.libc != null;
8 inherit (stdenv.hostPlatform) isMusl;
9
10in
11
12stdenv.mkDerivation {
13 pname = "compiler-rt" + lib.optionalString (haveLibc) "-libc";
14 inherit version;
15
16 inherit src;
17 sourceRoot = "source/compiler-rt";
18
19 nativeBuildInputs = [ cmake python3 libllvm.dev ];
20 buildInputs = lib.optional stdenv.hostPlatform.isDarwin libcxxabi;
21
22 NIX_CFLAGS_COMPILE = [
23 "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
24 ];
25
26 cmakeFlags = [
27 "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
28 "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
29 "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
30 ] ++ lib.optionals (useLLVM || bareMetal || isMusl) [
31 "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
32 "-DCOMPILER_RT_BUILD_XRAY=OFF"
33 "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
34 "-DCOMPILER_RT_BUILD_PROFILE=OFF"
35 ] ++ lib.optionals ((useLLVM || bareMetal) && !haveLibc) [
36 "-DCMAKE_C_COMPILER_WORKS=ON"
37 "-DCMAKE_CXX_COMPILER_WORKS=ON"
38 "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
39 "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
40 ] ++ lib.optionals (useLLVM && !haveLibc) [
41 "-DCMAKE_C_FLAGS=-nodefaultlibs"
42 ] ++ lib.optionals (useLLVM) [
43 "-DCOMPILER_RT_BUILD_BUILTINS=ON"
44 #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
45 "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
46 ] ++ lib.optionals (bareMetal) [
47 "-DCOMPILER_RT_OS_DIR=baremetal"
48 ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
49 "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
50 "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
51 "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
52 ];
53
54 outputs = [ "out" "dev" ];
55
56 patches = [
57 ./codesign.patch # Revert compiler-rt commit that makes codesign mandatory
58 ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
59 # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
60 # extra `/`.
61 ./normalize-var.patch
62 ] # Prevent a compilation error on darwin
63 ++ lib.optional stdenv.hostPlatform.isDarwin ./darwin-targetconditionals.patch
64 ++ lib.optional stdenv.hostPlatform.isAarch32 ./armv7l.patch;
65
66 # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
67 # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
68 # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
69 # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
70 # a flag and turn the flag off during the stdenv build.
71 postPatch = lib.optionalString (!stdenv.isDarwin) ''
72 substituteInPlace cmake/builtin-config-ix.cmake \
73 --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
74 '' + lib.optionalString stdenv.isDarwin ''
75 substituteInPlace cmake/builtin-config-ix.cmake \
76 --replace 'set(ARM64 arm64 arm64e)' 'set(ARM64)'
77 substituteInPlace cmake/config-ix.cmake \
78 --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
79 '' + lib.optionalString (useLLVM) ''
80 substituteInPlace lib/builtins/int_util.c \
81 --replace "#include <stdlib.h>" ""
82 substituteInPlace lib/builtins/clear_cache.c \
83 --replace "#include <assert.h>" ""
84 substituteInPlace lib/builtins/cpu_model.c \
85 --replace "#include <assert.h>" ""
86 '';
87
88 # Hack around weird upsream RPATH bug
89 postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin || stdenv.hostPlatform.isWasm) ''
90 ln -s "$out/lib"/*/* "$out/lib"
91 '' + lib.optionalString (useLLVM) ''
92 ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
93 ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
94 ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
95 ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
96 '';
97
98 meta = llvm_meta // {
99 homepage = "https://compiler-rt.llvm.org/";
100 description = "Compiler runtime libraries";
101 longDescription = ''
102 The compiler-rt project provides highly tuned implementations of the
103 low-level code generator support routines like "__fixunsdfdi" and other
104 calls generated when a target doesn't have a short sequence of native
105 instructions to implement a core IR operation. It also provides
106 implementations of run-time libraries for dynamic testing tools such as
107 AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
108 '';
109 # "All of the code in the compiler-rt project is dual licensed under the MIT
110 # license and the UIUC License (a BSD-like license)":
111 license = with lib.licenses; [ mit ncsa ];
112 };
113}