nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at python-updates 292 lines 12 kB view raw
1{ 2 lib, 3 stdenv, 4 llvm_meta, 5 release_version, 6 version, 7 src ? null, 8 monorepoSrc ? null, 9 runCommand, 10 cmake, 11 ninja, 12 python3, 13 jq, 14 libcxx, 15 linuxHeaders, 16 freebsd, 17 18 # Some platforms have switched to using compiler-rt, but still want a 19 # libgcc.a for ABI compat purposes. The use case would be old code that 20 # expects to link `-lgcc` but doesn't care exactly what its contents 21 # are, so long as it provides some builtins. 22 doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD, 23 24 # In recent releases, the compiler-rt build seems to produce 25 # many `libclang_rt*` libraries, but not a single unified 26 # `libcompiler_rt` library, at least under certain configurations. Some 27 # platforms still expect this, however, so we symlink one into place. 28 forceLinkCompilerRt ? stdenv.hostPlatform.isOpenBSD, 29 devExtraCmakeFlags ? [ ], 30 getVersionFile, 31 fetchpatch, 32}: 33 34let 35 36 useLLVM = stdenv.hostPlatform.useLLVM or false; 37 bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none"; 38 haveLibc = stdenv.cc.libc != null; 39 # TODO: Make this account for GCC having libstdcxx, which will help 40 # use clean up the `cmakeFlags` rats nest below. 41 haveLibcxx = stdenv.cc.libcxx != null; 42 isDarwinStatic = stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic; 43 inherit (stdenv.hostPlatform) isMusl isAarch64 isWindows; 44 noSanitizers = !haveLibc || bareMetal || isMusl || isDarwinStatic || isWindows; 45in 46 47stdenv.mkDerivation (finalAttrs: { 48 pname = "compiler-rt${lib.optionalString haveLibc "-libc"}"; 49 inherit version; 50 51 src = 52 if monorepoSrc != null then 53 runCommand "compiler-rt-src-${version}" { inherit (monorepoSrc) passthru; } ( 54 '' 55 mkdir -p "$out" 56 cp -r ${monorepoSrc}/cmake "$out" 57 '' 58 + lib.optionalString (lib.versionAtLeast release_version "21") '' 59 cp -r ${monorepoSrc}/third-party "$out" 60 '' 61 + '' 62 cp -r ${monorepoSrc}/compiler-rt "$out" 63 cp -r ${monorepoSrc}/llvm "$out" 64 '' 65 ) 66 else 67 src; 68 69 sourceRoot = "${finalAttrs.src.name}/compiler-rt"; 70 71 patches = [ 72 (getVersionFile "compiler-rt/X86-support-extension.patch") # Add support for i486 i586 i686 by reusing i386 config 73 # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the 74 # extra `/`. 75 (getVersionFile "compiler-rt/normalize-var.patch") 76 # Fix build on armv6l 77 ./armv6-no-ldrexd-strexd.patch 78 # See: https://github.com/NixOS/nixpkgs/pull/186575 79 ./darwin-plistbuddy-workaround.patch 80 ] 81 ++ [ 82 (getVersionFile "compiler-rt/armv6-scudo-libatomic.patch") 83 ] 84 ++ lib.optional (lib.versions.major release_version == "19") (fetchpatch { 85 url = "https://github.com/llvm/llvm-project/pull/99837/commits/14ae0a660a38e1feb151928a14f35ff0f4487351.patch"; 86 hash = "sha256-JykABCaNNhYhZQxCvKiBn54DZ5ZguksgCHnpdwWF2no="; 87 relative = "compiler-rt"; 88 }) 89 ++ lib.optional (lib.strings.versionOlder (lib.versions.major release_version) "20") (fetchpatch { 90 url = "https://github.com/llvm/llvm-project/commit/59978b21ad9c65276ee8e14f26759691b8a65763.patch"; 91 hash = "sha256-ys5SMLfO3Ay9nCX9GV5yRCQ6pLsseFu/ZY6Xd6OL4p0="; 92 relative = "compiler-rt"; 93 }); 94 95 nativeBuildInputs = [ 96 cmake 97 python3 98 ninja 99 ] 100 ++ lib.optionals stdenv.hostPlatform.isDarwin [ jq ]; 101 buildInputs = 102 lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders 103 ++ lib.optional (stdenv.hostPlatform.isFreeBSD) freebsd.include; 104 105 env = { 106 NIX_CFLAGS_COMPILE = toString ( 107 [ 108 "-DSCUDO_DEFAULT_OPTIONS=delete_size_mismatch=false:dealloc_type_mismatch=false" 109 ] 110 ++ lib.optionals (!haveLibc) [ 111 # The compiler got stricter about this, and there is a usellvm patch below 112 # which patches out the assert include causing an implicit definition of 113 # assert. It would be nicer to understand why compiler-rt thinks it should 114 # be able to #include <assert.h> in the first place; perhaps it's in the 115 # wrong, or perhaps there is a way to provide an assert.h. 116 "-Wno-error=implicit-function-declaration" 117 ] 118 ); 119 120 # Work around clang’s trying to invoke unprefixed-ld on Darwin when `-target` is passed. 121 NIX_CFLAGS_LINK = lib.optionalString (stdenv.hostPlatform.isDarwin) "--ld-path=${stdenv.cc.bintools}/bin/${stdenv.cc.targetPrefix}ld"; 122 }; 123 124 cmakeFlags = [ 125 (lib.cmakeBool "COMPILER_RT_DEFAULT_TARGET_ONLY" true) 126 (lib.cmakeFeature "CMAKE_C_COMPILER_TARGET" stdenv.hostPlatform.config) 127 (lib.cmakeFeature "CMAKE_ASM_COMPILER_TARGET" stdenv.hostPlatform.config) 128 ] 129 ++ lib.optionals (useLLVM && haveLibc && stdenv.cc.libcxx == libcxx) [ 130 (lib.cmakeFeature "SANITIZER_CXX_ABI" "libcxxabi") 131 (lib.cmakeFeature "SANITIZER_CXX_ABI_LIBNAME" "libcxxabi") 132 (lib.cmakeBool "COMPILER_RT_USE_BUILTINS_LIBRARY" true) 133 ] 134 ++ lib.optionals (useLLVM && haveLibc) [ 135 (lib.cmakeBool "COMPILER_RT_BUILD_SANITIZERS" true) 136 (lib.cmakeBool "COMPILER_RT_BUILD_PROFILE" true) 137 ] 138 ++ lib.optionals noSanitizers [ 139 (lib.cmakeBool "COMPILER_RT_BUILD_SANITIZERS" false) 140 ] 141 ++ lib.optionals ((useLLVM && !haveLibcxx) || !haveLibc || bareMetal || isMusl || isDarwinStatic) [ 142 (lib.cmakeBool "COMPILER_RT_BUILD_XRAY" false) 143 (lib.cmakeBool "COMPILER_RT_BUILD_LIBFUZZER" false) 144 (lib.cmakeBool "COMPILER_RT_BUILD_MEMPROF" false) 145 (lib.cmakeBool "COMPILER_RT_BUILD_ORC" false) # may be possible to build with musl if necessary 146 ] 147 ++ lib.optionals (!haveLibc || bareMetal) [ 148 (lib.cmakeBool "COMPILER_RT_BUILD_PROFILE" false) 149 (lib.cmakeBool "CMAKE_C_COMPILER_WORKS" true) 150 (lib.cmakeBool "COMPILER_RT_BAREMETAL_BUILD" true) 151 (lib.cmakeFeature "CMAKE_SIZEOF_VOID_P" (toString (stdenv.hostPlatform.parsed.cpu.bits / 8))) 152 ] 153 ++ lib.optionals (!haveLibc || bareMetal || isDarwinStatic) [ 154 (lib.cmakeBool "CMAKE_CXX_COMPILER_WORKS" true) 155 ] 156 ++ lib.optionals (!haveLibc) [ 157 (lib.cmakeFeature "CMAKE_C_FLAGS" "-nodefaultlibs") 158 ] 159 ++ lib.optionals useLLVM [ 160 (lib.cmakeBool "COMPILER_RT_BUILD_BUILTINS" true) 161 #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program 162 (lib.cmakeFeature "CMAKE_TRY_COMPILE_TARGET_TYPE" "STATIC_LIBRARY") 163 ] 164 ++ lib.optionals bareMetal [ 165 (lib.cmakeFeature "COMPILER_RT_OS_DIR" "baremetal") 166 ] 167 ++ lib.optionals (stdenv.hostPlatform.isDarwin) ( 168 [ 169 (lib.cmakeFeature "CMAKE_LIPO" "${lib.getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}lipo") 170 ] 171 ++ lib.optionals (!haveLibcxx) [ 172 # Darwin fails to detect that the compiler supports the `-g` flag when there is no libc++ during the 173 # compiler-rt bootstrap, which prevents compiler-rt from building. The `-g` flag is required by the 174 # Darwin support, so force it to be enabled during the first stage of the compiler-rt bootstrap. 175 (lib.cmakeBool "COMPILER_RT_HAS_G_FLAG" true) 176 ] 177 ++ [ 178 (lib.cmakeFeature "DARWIN_osx_ARCHS" stdenv.hostPlatform.darwinArch) 179 (lib.cmakeFeature "DARWIN_osx_BUILTIN_ARCHS" stdenv.hostPlatform.darwinArch) 180 (lib.cmakeFeature "SANITIZER_MIN_OSX_VERSION" stdenv.hostPlatform.darwinMinVersion) 181 # `COMPILER_RT_DEFAULT_TARGET_ONLY` does not apply to Darwin: 182 # https://github.com/llvm/llvm-project/blob/27ef42bec80b6c010b7b3729ed0528619521a690/compiler-rt/cmake/base-config-ix.cmake#L153 183 (lib.cmakeBool "COMPILER_RT_ENABLE_IOS" false) 184 ] 185 ) 186 ++ lib.optionals (noSanitizers && lib.versionAtLeast release_version "19") [ 187 (lib.cmakeBool "COMPILER_RT_BUILD_CTX_PROFILE" false) 188 ] 189 ++ 190 lib.optional (stdenv.hostPlatform.isAarch64 && !haveLibc) 191 # Fixes https://github.com/NixOS/nixpkgs/issues/393603 192 (lib.cmakeBool "COMPILER_RT_DISABLE_AARCH64_FMV" true) 193 ++ devExtraCmakeFlags; 194 195 outputs = [ 196 "out" 197 "dev" 198 ]; 199 200 postPatch = 201 lib.optionalString (!stdenv.hostPlatform.isDarwin) '' 202 substituteInPlace cmake/builtin-config-ix.cmake \ 203 --replace-fail 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)' 204 '' 205 + lib.optionalString (!haveLibc) ( 206 (lib.optionalString (lib.versions.major release_version == "18") '' 207 substituteInPlace lib/builtins/aarch64/sme-libc-routines.c \ 208 --replace-fail "<stdlib.h>" "<stddef.h>" 209 '') 210 + '' 211 substituteInPlace lib/builtins/int_util.c \ 212 --replace-fail "#include <stdlib.h>" "" 213 '' 214 + (lib.optionalString (!stdenv.hostPlatform.isFreeBSD) 215 # On FreeBSD, assert/static_assert are macros and allowing them to be implicitly declared causes link errors. 216 # see description above for why we're nuking assert.h normally but that doesn't work here. 217 # instead, we add the freebsd.include dependency explicitly 218 '' 219 substituteInPlace lib/builtins/clear_cache.c \ 220 --replace-fail "#include <assert.h>" "" 221 substituteInPlace lib/builtins/cpu_model/x86.c \ 222 --replace-fail "#include <assert.h>" "" 223 '' 224 ) 225 ) 226 + 227 lib.optionalString (lib.versionAtLeast release_version "19") 228 # codesign in sigtool doesn't support the various options used by the build 229 # and is present in the bootstrap-tools. Removing find_program prevents the 230 # build from trying to use it and failing. 231 '' 232 substituteInPlace cmake/Modules/AddCompilerRT.cmake \ 233 --replace-fail 'find_program(CODESIGN codesign)' "" 234 ''; 235 236 preConfigure = lib.optionalString stdenv.hostPlatform.isDarwin '' 237 cmakeFlagsArray+=( 238 "-DDARWIN_macosx_CACHED_SYSROOT=$SDKROOT" 239 "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=$(jq -r .Version "$SDKROOT/SDKSettings.json")" 240 ) 241 ''; 242 243 # Hack around weird upstream RPATH bug 244 postInstall = 245 lib.optionalString (stdenv.hostPlatform.isDarwin) '' 246 ln -s "$out/lib"/*/* "$out/lib" 247 '' 248 + lib.optionalString (useLLVM && stdenv.hostPlatform.isLinux) '' 249 ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o 250 ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o 251 # Note the history of crt{begin,end}S in previous versions of llvm in nixpkg: 252 # The presence of crtbegin_shared has been added and removed; it's possible 253 # people have added/removed it to get it working on their platforms. 254 # Try each in turn for now. 255 ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbeginS.o 256 ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtendS.o 257 ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o 258 ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o 259 '' 260 + lib.optionalString doFakeLibgcc '' 261 ln -s $out/lib/*/libclang_rt.builtins-*.a $out/lib/libgcc.a 262 '' 263 + lib.optionalString forceLinkCompilerRt '' 264 ln -s $out/lib/*/libclang_rt.builtins-*.a $out/lib/libcompiler_rt.a 265 ''; 266 267 meta = llvm_meta // { 268 homepage = "https://compiler-rt.llvm.org/"; 269 description = "Compiler runtime libraries"; 270 longDescription = '' 271 The compiler-rt project provides highly tuned implementations of the 272 low-level code generator support routines like "__fixunsdfdi" and other 273 calls generated when a target doesn't have a short sequence of native 274 instructions to implement a core IR operation. It also provides 275 implementations of run-time libraries for dynamic testing tools such as 276 AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer. 277 ''; 278 # "All of the code in the compiler-rt project is dual licensed under the MIT 279 # license and the UIUC License (a BSD-like license)": 280 license = with lib.licenses; [ 281 mit 282 ncsa 283 ]; 284 broken = 285 # compiler-rt requires a Clang stdenv on 32-bit RISC-V: 286 # https://reviews.llvm.org/D43106#1019077 287 (stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang) 288 # emutls wants `<pthread.h>` which isn't available (without experimental WASM threads proposal). 289 # `enable_execute_stack.c` Also doesn't sound like something WASM would support. 290 || (stdenv.hostPlatform.isWasm && haveLibc); 291 }; 292})