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