nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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 isGnu;
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
68 # `COMPILER_RT_DEFAULT_TARGET_ONLY` does not apply to Darwin:
69 # https://github.com/llvm/llvm-project/blob/27ef42bec80b6c010b7b3729ed0528619521a690/compiler-rt/cmake/base-config-ix.cmake#L153
70 "-DCOMPILER_RT_ENABLE_IOS=OFF"
71 ];
72
73 outputs = [ "out" "dev" ];
74
75 patches = [
76 ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
77 ./gnu-install-dirs.patch
78 # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
79 # extra `/`.
80 ./normalize-var.patch
81 # Prevent a compilation error on darwin
82 ./darwin-targetconditionals.patch
83 # See: https://github.com/NixOS/nixpkgs/pull/186575
84 ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
85 # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
86 ../../common/compiler-rt/armv7l-15.patch
87 ];
88
89 # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
90 # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
91 # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
92 # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
93 # a flag and turn the flag off during the stdenv build.
94 postPatch = lib.optionalString (!stdenv.isDarwin) ''
95 substituteInPlace cmake/builtin-config-ix.cmake \
96 --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
97 '' + lib.optionalString stdenv.isDarwin ''
98 substituteInPlace cmake/config-ix.cmake \
99 --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
100 '' + lib.optionalString (useLLVM) ''
101 substituteInPlace lib/builtins/int_util.c \
102 --replace "#include <stdlib.h>" ""
103 substituteInPlace lib/builtins/clear_cache.c \
104 --replace "#include <assert.h>" ""
105 substituteInPlace lib/builtins/cpu_model.c \
106 --replace "#include <assert.h>" ""
107 '';
108
109 preConfigure = lib.optionalString (useLLVM && !haveLibc) ''
110 cmakeFlagsArray+=(-DCMAKE_C_FLAGS="-nodefaultlibs -ffreestanding")
111 '';
112
113 # Hack around weird upsream RPATH bug
114 postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin || stdenv.hostPlatform.isWasm) ''
115 ln -s "$out/lib"/*/* "$out/lib"
116 '' + lib.optionalString (useLLVM) ''
117 ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
118 ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
119 ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
120 ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
121 '' + lib.optionalString doFakeLibgcc ''
122 ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
123 '';
124
125 meta = llvm_meta // {
126 homepage = "https://compiler-rt.llvm.org/";
127 description = "Compiler runtime libraries";
128 longDescription = ''
129 The compiler-rt project provides highly tuned implementations of the
130 low-level code generator support routines like "__fixunsdfdi" and other
131 calls generated when a target doesn't have a short sequence of native
132 instructions to implement a core IR operation. It also provides
133 implementations of run-time libraries for dynamic testing tools such as
134 AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
135 '';
136 # "All of the code in the compiler-rt project is dual licensed under the MIT
137 # license and the UIUC License (a BSD-like license)":
138 license = with lib.licenses; [ mit ncsa ];
139 };
140}