at 23.05-pre 129 lines 5.4 kB view raw
1{ lib, stdenv, llvm_meta, version 2, monorepoSrc, runCommand 3, cmake, python3, xcbuild, libllvm, libcxxabi 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 NIX_CFLAGS_COMPILE = [ 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 (useLLVM || bareMetal || isMusl) [ 43 "-DCOMPILER_RT_BUILD_SANITIZERS=OFF" 44 "-DCOMPILER_RT_BUILD_XRAY=OFF" 45 "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF" 46 "-DCOMPILER_RT_BUILD_PROFILE=OFF" 47 "-DCOMPILER_RT_BUILD_MEMPROF=OFF" 48 "-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary 49 ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [ 50 "-DCMAKE_C_COMPILER_WORKS=ON" 51 "-DCMAKE_CXX_COMPILER_WORKS=ON" 52 "-DCOMPILER_RT_BAREMETAL_BUILD=ON" 53 "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}" 54 ] ++ lib.optionals (useLLVM && !haveLibc) [ 55 "-DCMAKE_C_FLAGS=-nodefaultlibs" 56 ] ++ lib.optionals (useLLVM) [ 57 "-DCOMPILER_RT_BUILD_BUILTINS=ON" 58 #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program 59 "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY" 60 ] ++ lib.optionals (bareMetal) [ 61 "-DCOMPILER_RT_OS_DIR=baremetal" 62 ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [ 63 "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON" 64 "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}" 65 "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}" 66 ]; 67 68 outputs = [ "out" "dev" ]; 69 70 patches = [ 71 ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config 72 ./gnu-install-dirs.patch 73 # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the 74 # extra `/`. 75 ./normalize-var.patch 76 # Prevent a compilation error on darwin 77 ./darwin-targetconditionals.patch 78 ../../common/compiler-rt/darwin-plistbuddy-workaround.patch 79 ./armv7l.patch 80 ]; 81 82 # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks 83 # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra 84 # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd 85 # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by 86 # a flag and turn the flag off during the stdenv build. 87 postPatch = lib.optionalString (!stdenv.isDarwin) '' 88 substituteInPlace cmake/builtin-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_shared-*.o $out/lib/crtbeginS.o 109 ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o 110 '' + lib.optionalString doFakeLibgcc '' 111 ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a 112 ''; 113 114 meta = llvm_meta // { 115 homepage = "https://compiler-rt.llvm.org/"; 116 description = "Compiler runtime libraries"; 117 longDescription = '' 118 The compiler-rt project provides highly tuned implementations of the 119 low-level code generator support routines like "__fixunsdfdi" and other 120 calls generated when a target doesn't have a short sequence of native 121 instructions to implement a core IR operation. It also provides 122 implementations of run-time libraries for dynamic testing tools such as 123 AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer. 124 ''; 125 # "All of the code in the compiler-rt project is dual licensed under the MIT 126 # license and the UIUC License (a BSD-like license)": 127 license = with lib.licenses; [ mit ncsa ]; 128 }; 129}