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