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