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