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