about summary refs log tree commit diff
path: root/pkgs/development/compilers/llvm
diff options
context:
space:
mode:
authorWeijia Wang <9713184+wegank@users.noreply.github.com>2024-04-13 19:10:53 +0200
committerWeijia Wang <9713184+wegank@users.noreply.github.com>2024-04-13 19:10:53 +0200
commitf3a1652b098bbe33e5ab8a827ff3778b3c2120b8 (patch)
tree243f5d1e82706ad3d727a26692bd1d53d9373776 /pkgs/development/compilers/llvm
parent561b7b74f8e5783f29bb589482b3ed60ce2c1318 (diff)
parentc0c2f2903f310dd5efb86f02b4b5f824183f5173 (diff)
Merge branch 'master' into staging-next
Diffstat (limited to 'pkgs/development/compilers/llvm')
-rw-r--r--pkgs/development/compilers/llvm/12/compiler-rt/default.nix131
-rw-r--r--pkgs/development/compilers/llvm/12/default.nix66
-rw-r--r--pkgs/development/compilers/llvm/12/libcxx/default.nix139
-rw-r--r--pkgs/development/compilers/llvm/13/compiler-rt/default.nix138
-rw-r--r--pkgs/development/compilers/llvm/13/default.nix57
-rw-r--r--pkgs/development/compilers/llvm/13/libcxx/default.nix135
-rw-r--r--pkgs/development/compilers/llvm/14/compiler-rt/default.nix146
-rw-r--r--pkgs/development/compilers/llvm/14/default.nix59
-rw-r--r--pkgs/development/compilers/llvm/15/compiler-rt/default.nix145
-rw-r--r--pkgs/development/compilers/llvm/15/default.nix57
-rw-r--r--pkgs/development/compilers/llvm/15/libcxx/default.nix145
-rw-r--r--pkgs/development/compilers/llvm/16/compiler-rt/default.nix159
-rw-r--r--pkgs/development/compilers/llvm/16/default.nix32
-rw-r--r--pkgs/development/compilers/llvm/16/libcxx/default.nix125
-rw-r--r--pkgs/development/compilers/llvm/17/compiler-rt/default.nix159
-rw-r--r--pkgs/development/compilers/llvm/17/default.nix42
-rw-r--r--pkgs/development/compilers/llvm/17/libcxx/default.nix136
-rw-r--r--pkgs/development/compilers/llvm/18/compiler-rt/default.nix157
-rw-r--r--pkgs/development/compilers/llvm/18/default.nix30
-rw-r--r--pkgs/development/compilers/llvm/18/libcxx/default.nix130
-rw-r--r--pkgs/development/compilers/llvm/common/compiler-rt/default.nix (renamed from pkgs/development/compilers/llvm/git/compiler-rt/default.nix)80
-rw-r--r--pkgs/development/compilers/llvm/common/libcxx/default.nix (renamed from pkgs/development/compilers/llvm/14/libcxx/default.nix)112
-rw-r--r--pkgs/development/compilers/llvm/git/default.nix30
-rw-r--r--pkgs/development/compilers/llvm/git/libcxx/default.nix130
24 files changed, 455 insertions, 2085 deletions
diff --git a/pkgs/development/compilers/llvm/12/compiler-rt/default.nix b/pkgs/development/compilers/llvm/12/compiler-rt/default.nix
deleted file mode 100644
index e11a1b397e764..0000000000000
--- a/pkgs/development/compilers/llvm/12/compiler-rt/default.nix
+++ /dev/null
@@ -1,131 +0,0 @@
-{ lib, stdenv, llvm_meta, version, fetch
-, cmake, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
-, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
-}:
-
-let
-
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-  bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
-  haveLibc = stdenv.cc.libc != null;
-  inherit (stdenv.hostPlatform) isMusl;
-
-in
-
-stdenv.mkDerivation {
-  pname = "compiler-rt" + lib.optionalString (haveLibc) "-libc";
-  inherit version;
-  src = fetch "compiler-rt" "1950rg294izdwkaasi7yjrmadc9mzdd5paf0q63jjcq2m3rdbj5l";
-
-  nativeBuildInputs = [ cmake python3 libllvm.dev ]
-    ++ lib.optional stdenv.isDarwin xcbuild.xcrun;
-  buildInputs =
-    lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
-
-  env.NIX_CFLAGS_COMPILE = toString [
-    "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
-  ];
-
-  cmakeFlags = [
-    "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
-    "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-    "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-  ] ++ lib.optionals (haveLibc && stdenv.hostPlatform.isGnu) [
-    "-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
-  ] ++ lib.optionals (useLLVM || bareMetal || isMusl) [
-    "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
-    "-DCOMPILER_RT_BUILD_XRAY=OFF"
-    "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
-    "-DCOMPILER_RT_BUILD_MEMPROF=OFF"
-  ] ++ lib.optionals (useLLVM || bareMetal) [
-    "-DCOMPILER_RT_BUILD_PROFILE=OFF"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
-    "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
-  ] ++ lib.optionals (useLLVM) [
-    "-DCOMPILER_RT_BUILD_BUILTINS=ON"
-    #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
-    "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
-  ] ++ lib.optionals (bareMetal) [
-    "-DCOMPILER_RT_OS_DIR=baremetal"
-  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
-    "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
-    "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
-    "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
-  ];
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    ../../common/compiler-rt/7-12-codesign.patch # Revert compiler-rt commit that makes codesign mandatory
-    ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
-    ./gnu-install-dirs.patch
-    # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
-    # extra `/`.
-    ./normalize-var.patch
-    ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
-    ./armv7l.patch
-    # Fix build on armv6l
-    ../../common/compiler-rt/armv6-mcr-dmb.patch
-    ../../common/compiler-rt/armv6-sync-ops-no-thumb.patch
-    ../../common/compiler-rt/armv6-no-ldrexd-strexd.patch
-  ];
-
-  # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
-  # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
-  # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
-  # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
-  # a flag and turn the flag off during the stdenv build.
-  postPatch = lib.optionalString (!stdenv.isDarwin) ''
-    substituteInPlace cmake/builtin-config-ix.cmake \
-      --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace cmake/config-ix.cmake \
-      --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
-  '' + lib.optionalString (useLLVM) ''
-    substituteInPlace lib/builtins/int_util.c \
-      --replace "#include <stdlib.h>" ""
-    substituteInPlace lib/builtins/clear_cache.c \
-      --replace "#include <assert.h>" ""
-    substituteInPlace lib/builtins/cpu_model.c \
-      --replace "#include <assert.h>" ""
-  '';
-
-  preConfigure = lib.optionalString (useLLVM && !haveLibc) ''
-    cmakeFlagsArray+=(-DCMAKE_C_FLAGS="-nodefaultlibs -ffreestanding")
-  '';
-
-  # Hack around weird upsream RPATH bug
-  postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin || stdenv.hostPlatform.isWasm) ''
-    ln -s "$out/lib"/*/* "$out/lib"
-  '' + lib.optionalString (useLLVM) ''
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
-    ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
-  '' + lib.optionalString doFakeLibgcc ''
-    ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
-  '';
-
-  meta = llvm_meta // {
-    homepage = "https://compiler-rt.llvm.org/";
-    description = "Compiler runtime libraries";
-    mainProgram = "hwasan_symbolize";
-    longDescription = ''
-      The compiler-rt project provides highly tuned implementations of the
-      low-level code generator support routines like "__fixunsdfdi" and other
-      calls generated when a target doesn't have a short sequence of native
-      instructions to implement a core IR operation. It also provides
-      implementations of run-time libraries for dynamic testing tools such as
-      AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
-    '';
-    # "All of the code in the compiler-rt project is dual licensed under the MIT
-    # license and the UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-    # compiler-rt requires a Clang stdenv on 32-bit RISC-V:
-    # https://reviews.llvm.org/D43106#1019077
-    broken = stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang;
-  };
-}
diff --git a/pkgs/development/compilers/llvm/12/default.nix b/pkgs/development/compilers/llvm/12/default.nix
index d8b673c70f222..7bd82c071538c 100644
--- a/pkgs/development/compilers/llvm/12/default.nix
+++ b/pkgs/development/compilers/llvm/12/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake
 , preLibcCrossHeaders
-, fetchpatch
+, substitute, fetchFromGitHub, fetchpatch
 , libxml2, python3, isl, fetchurl, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
@@ -235,14 +235,44 @@ let
     callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake libxml2 python3 isl release_version version fetch; });
   in {
 
-    compiler-rt-libc = callPackage ./compiler-rt {
+    compiler-rt-libc = callPackage ../common/compiler-rt {
+      src = fetch "compiler-rt" "1950rg294izdwkaasi7yjrmadc9mzdd5paf0q63jjcq2m3rdbj5l";
+      patches = [
+        ../common/compiler-rt/7-12-codesign.patch # Revert compiler-rt commit that makes codesign mandatory
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        ./compiler-rt/armv7l.patch
+        # Fix build on armv6l
+        ../common/compiler-rt/armv6-mcr-dmb.patch
+        ../common/compiler-rt/armv6-sync-ops-no-thumb.patch
+        ../common/compiler-rt/armv6-no-ldrexd-strexd.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
                else stdenv;
     };
 
-    compiler-rt-no-libc = callPackage ./compiler-rt {
+    compiler-rt-no-libc = callPackage ../common/compiler-rt {
+      src = fetch "compiler-rt" "1950rg294izdwkaasi7yjrmadc9mzdd5paf0q63jjcq2m3rdbj5l";
+      patches = [
+        ../common/compiler-rt/7-12-codesign.patch # Revert compiler-rt commit that makes codesign mandatory
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        ./compiler-rt/armv7l.patch
+        # Fix build on armv6l
+        ../common/compiler-rt/armv6-mcr-dmb.patch
+        ../common/compiler-rt/armv6-sync-ops-no-thumb.patch
+        ../common/compiler-rt/armv6-no-ldrexd-strexd.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
@@ -258,7 +288,35 @@ let
 
     libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;
 
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      src = fetchFromGitHub {
+        owner = "llvm";
+        repo = "llvm-project";
+        rev = "refs/tags/llvmorg-${version}";
+        sparseCheckout = [
+          "libcxx"
+          "libcxxabi"
+          "llvm/cmake"
+          "llvm/utils"
+          "runtimes"
+        ];
+        hash = "sha256-etxgXIdWxMTmbZ83Hsc0w6Jt5OSQSUEPVEWqLkHsNBY=";
+      };
+      patches = [
+        (substitute {
+          src = ../common/libcxxabi/wasm.patch;
+          replacements = [
+            "--replace-fail" "/cmake/" "/llvm/cmake/"
+          ];
+        })
+      ] ++ lib.optionals stdenv.hostPlatform.isMusl [
+        (substitute {
+          src = ../common/libcxx/libcxx-0001-musl-hacks.patch;
+          replacements = [
+            "--replace-fail" "/include/" "/libcxx/include/"
+          ];
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/12/libcxx/default.nix b/pkgs/development/compilers/llvm/12/libcxx/default.nix
deleted file mode 100644
index 7c6174711456c..0000000000000
--- a/pkgs/development/compilers/llvm/12/libcxx/default.nix
+++ /dev/null
@@ -1,139 +0,0 @@
-{ lib, stdenv, llvm_meta
-, fetchFromGitHub, runCommand, substitute
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = fetchFromGitHub {
-    owner = "llvm";
-    repo = "llvm-project";
-    rev = "refs/tags/llvmorg-${version}";
-    sparseCheckout = [
-      "libcxx"
-      "libcxxabi"
-      "llvm/cmake"
-      "llvm/utils"
-      "runtimes"
-    ];
-    hash = "sha256-etxgXIdWxMTmbZ83Hsc0w6Jt5OSQSUEPVEWqLkHsNBY=";
-  };
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    (substitute {
-      src = ../../common/libcxxabi/wasm.patch;
-      replacements = [
-        "--replace-fail" "/cmake/" "/llvm/cmake/"
-      ];
-    })
-  ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-    (substitute {
-      src = ../../common/libcxx/libcxx-0001-musl-hacks.patch;
-      replacements = [
-        "--replace-fail" "/include/" "/libcxx/include/"
-      ];
-    })
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include $out/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib $out/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/13/compiler-rt/default.nix b/pkgs/development/compilers/llvm/13/compiler-rt/default.nix
deleted file mode 100644
index 3c83e4e58a178..0000000000000
--- a/pkgs/development/compilers/llvm/13/compiler-rt/default.nix
+++ /dev/null
@@ -1,138 +0,0 @@
-{ lib, stdenv, llvm_meta, version, src
-, cmake, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
-, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
-}:
-
-let
-
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-  bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
-  haveLibc = stdenv.cc.libc != null;
-  inherit (stdenv.hostPlatform) isMusl isAarch64;
-
-in
-
-stdenv.mkDerivation {
-  pname = "compiler-rt" + lib.optionalString (haveLibc) "-libc";
-  inherit version;
-
-  inherit src;
-  sourceRoot = "${src.name}/compiler-rt";
-
-  nativeBuildInputs = [ cmake python3 libllvm.dev ]
-    ++ lib.optional stdenv.isDarwin xcbuild.xcrun;
-  buildInputs =
-    lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
-
-  env.NIX_CFLAGS_COMPILE = toString [
-    "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
-  ];
-
-  cmakeFlags = [
-    "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
-    "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-    "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-  ] ++ lib.optionals (haveLibc && stdenv.hostPlatform.isGnu) [
-    "-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
-  ] ++ lib.optionals (useLLVM || bareMetal || isMusl || isAarch64) [
-    "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
-  ] ++ lib.optionals (useLLVM || bareMetal || isMusl) [
-    "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
-    "-DCOMPILER_RT_BUILD_XRAY=OFF"
-    "-DCOMPILER_RT_BUILD_MEMPROF=OFF"
-    "-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
-  ] ++ lib.optionals (useLLVM || bareMetal) [
-    "-DCOMPILER_RT_BUILD_PROFILE=OFF"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
-    "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
-  ] ++ lib.optionals (useLLVM) [
-    "-DCOMPILER_RT_BUILD_BUILTINS=ON"
-    #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
-    "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
-  ] ++ lib.optionals (bareMetal) [
-    "-DCOMPILER_RT_OS_DIR=baremetal"
-  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
-    "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
-    "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
-    "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
-  ];
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    ./codesign.patch # Revert compiler-rt commit that makes codesign mandatory
-    ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
-    # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
-    # extra `/`.
-    ./normalize-var.patch
-    # Prevent a compilation error on darwin
-    ./darwin-targetconditionals.patch
-    ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
-    ./armv7l.patch
-    # Fix build on armv6l
-    ../../common/compiler-rt/armv6-mcr-dmb.patch
-    ../../common/compiler-rt/armv6-sync-ops-no-thumb.patch
-    ../../common/compiler-rt/armv6-no-ldrexd-strexd.patch
-    ../../common/compiler-rt/armv6-scudo-no-yield.patch
-    ../../common/compiler-rt/armv6-scudo-libatomic.patch
-  ];
-
-  # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
-  # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
-  # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
-  # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
-  # a flag and turn the flag off during the stdenv build.
-  postPatch = lib.optionalString (!stdenv.isDarwin) ''
-    substituteInPlace cmake/builtin-config-ix.cmake \
-      --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace cmake/config-ix.cmake \
-      --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
-  '' + lib.optionalString (useLLVM) ''
-    substituteInPlace lib/builtins/int_util.c \
-      --replace "#include <stdlib.h>" ""
-    substituteInPlace lib/builtins/clear_cache.c \
-      --replace "#include <assert.h>" ""
-    substituteInPlace lib/builtins/cpu_model.c \
-      --replace "#include <assert.h>" ""
-  '';
-
-  preConfigure = lib.optionalString (useLLVM && !haveLibc) ''
-    cmakeFlagsArray+=(-DCMAKE_C_FLAGS="-nodefaultlibs -ffreestanding")
-  '';
-
-  # Hack around weird upsream RPATH bug
-  postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin || stdenv.hostPlatform.isWasm) ''
-    ln -s "$out/lib"/*/* "$out/lib"
-  '' + lib.optionalString (useLLVM) ''
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
-    ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
-  '' + lib.optionalString doFakeLibgcc ''
-    ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
-  '';
-
-  meta = llvm_meta // {
-    homepage = "https://compiler-rt.llvm.org/";
-    description = "Compiler runtime libraries";
-    mainProgram = "hwasan_symbolize";
-    longDescription = ''
-      The compiler-rt project provides highly tuned implementations of the
-      low-level code generator support routines like "__fixunsdfdi" and other
-      calls generated when a target doesn't have a short sequence of native
-      instructions to implement a core IR operation. It also provides
-      implementations of run-time libraries for dynamic testing tools such as
-      AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
-    '';
-    # "All of the code in the compiler-rt project is dual licensed under the MIT
-    # license and the UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-    # compiler-rt requires a Clang stdenv on 32-bit RISC-V:
-    # https://reviews.llvm.org/D43106#1019077
-    broken = stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang;
-  };
-}
diff --git a/pkgs/development/compilers/llvm/13/default.nix b/pkgs/development/compilers/llvm/13/default.nix
index e93fb1bf29440..712c6e160d1ed 100644
--- a/pkgs/development/compilers/llvm/13/default.nix
+++ b/pkgs/development/compilers/llvm/13/default.nix
@@ -1,7 +1,7 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake
 , preLibcCrossHeaders
 , fetchpatch
-, libxml2, python3, isl, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, isl, fetchFromGitHub, substitute, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -271,14 +271,48 @@ in let
     callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake libxml2 python3 isl release_version version src; });
   in {
 
-    compiler-rt-libc = callPackage ./compiler-rt {
+    compiler-rt-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/codesign.patch # Revert compiler-rt commit that makes codesign mandatory
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        ./compiler-rt/armv7l.patch
+        # Fix build on armv6l
+        ../common/compiler-rt/armv6-mcr-dmb.patch
+        ../common/compiler-rt/armv6-sync-ops-no-thumb.patch
+        ../common/compiler-rt/armv6-no-ldrexd-strexd.patch
+        ../common/compiler-rt/armv6-scudo-no-yield.patch
+        ../common/compiler-rt/armv6-scudo-libatomic.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
                else stdenv;
     };
 
-    compiler-rt-no-libc = callPackage ./compiler-rt {
+    compiler-rt-no-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/codesign.patch # Revert compiler-rt commit that makes codesign mandatory
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        ./compiler-rt/armv7l.patch
+        # Fix build on armv6l
+        ../common/compiler-rt/armv6-mcr-dmb.patch
+        ../common/compiler-rt/armv6-sync-ops-no-thumb.patch
+        ../common/compiler-rt/armv6-no-ldrexd-strexd.patch
+        ../common/compiler-rt/armv6-scudo-no-yield.patch
+        ../common/compiler-rt/armv6-scudo-libatomic.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
@@ -294,7 +328,22 @@ in let
 
     libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;
 
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = [
+        (substitute {
+          src = ../common/libcxxabi/wasm.patch;
+          replacements = [
+            "--replace-fail" "/cmake/" "/llvm/cmake/"
+          ];
+        })
+      ] ++ lib.optionals stdenv.hostPlatform.isMusl [
+        (substitute {
+          src = ../common/libcxx/libcxx-0001-musl-hacks.patch;
+          replacements = [
+            "--replace-fail" "/include/" "/libcxx/include/"
+          ];
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
       monorepoSrc = src;
diff --git a/pkgs/development/compilers/llvm/13/libcxx/default.nix b/pkgs/development/compilers/llvm/13/libcxx/default.nix
deleted file mode 100644
index b39b4f37ff1c9..0000000000000
--- a/pkgs/development/compilers/llvm/13/libcxx/default.nix
+++ /dev/null
@@ -1,135 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, substitute
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    (substitute {
-      src = ../../common/libcxxabi/wasm.patch;
-      replacements = [
-        "--replace-fail" "/cmake/" "/llvm/cmake/"
-      ];
-    })
-  ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-    (substitute {
-      src = ../../common/libcxx/libcxx-0001-musl-hacks.patch;
-      replacements = [
-        "--replace-fail" "/include/" "/libcxx/include/"
-      ];
-    })
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/14/compiler-rt/default.nix b/pkgs/development/compilers/llvm/14/compiler-rt/default.nix
deleted file mode 100644
index 3482ed343c5a0..0000000000000
--- a/pkgs/development/compilers/llvm/14/compiler-rt/default.nix
+++ /dev/null
@@ -1,146 +0,0 @@
-{ lib, stdenv, llvm_meta, version
-, monorepoSrc, runCommand
-, cmake, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
-, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
-}:
-
-let
-
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-  bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
-  haveLibc = stdenv.cc.libc != null;
-  inherit (stdenv.hostPlatform) isMusl;
-
-  baseName = "compiler-rt";
-
-  src = runCommand "${baseName}-src-${version}" {} ''
-    mkdir -p "$out"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/${baseName} "$out"
-  '';
-in
-
-stdenv.mkDerivation {
-  pname = baseName + lib.optionalString (haveLibc) "-libc";
-  inherit version;
-
-  inherit src;
-  sourceRoot = "${src.name}/${baseName}";
-
-  nativeBuildInputs = [ cmake python3 libllvm.dev ]
-    ++ lib.optional stdenv.isDarwin xcbuild.xcrun;
-  buildInputs =
-    lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
-
-  env.NIX_CFLAGS_COMPILE = toString [
-    "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
-  ];
-
-  cmakeFlags = [
-    "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
-    "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-    "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-  ] ++ lib.optionals (haveLibc && stdenv.hostPlatform.isGnu) [
-    "-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
-  ] ++ lib.optionals (useLLVM || bareMetal || isMusl) [
-    "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
-    "-DCOMPILER_RT_BUILD_XRAY=OFF"
-    "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
-    "-DCOMPILER_RT_BUILD_MEMPROF=OFF"
-    "-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
-  ] ++ lib.optionals (useLLVM || bareMetal) [
-    "-DCOMPILER_RT_BUILD_PROFILE=OFF"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
-    "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
-  ] ++ lib.optionals (useLLVM) [
-    "-DCOMPILER_RT_BUILD_BUILTINS=ON"
-    #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
-    "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
-  ] ++ lib.optionals (bareMetal) [
-    "-DCOMPILER_RT_OS_DIR=baremetal"
-  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
-    "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
-    "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
-    "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
-  ];
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    ./codesign.patch # Revert compiler-rt commit that makes codesign mandatory
-    ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
-    ./gnu-install-dirs.patch
-    # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
-    # extra `/`.
-    ./normalize-var.patch
-    # Prevent a compilation error on darwin
-    ./darwin-targetconditionals.patch
-    ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
-    ./armv7l.patch
-    # Fix build on armv6l
-    ../../common/compiler-rt/armv6-mcr-dmb.patch
-    ../../common/compiler-rt/armv6-sync-ops-no-thumb.patch
-    ../../common/compiler-rt/armv6-no-ldrexd-strexd.patch
-    ../../common/compiler-rt/armv6-scudo-no-yield.patch
-    ../../common/compiler-rt/armv6-scudo-libatomic.patch
-  ];
-
-  # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
-  # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
-  # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
-  # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
-  # a flag and turn the flag off during the stdenv build.
-  postPatch = lib.optionalString (!stdenv.isDarwin) ''
-    substituteInPlace cmake/builtin-config-ix.cmake \
-      --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace cmake/config-ix.cmake \
-      --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
-  '' + lib.optionalString (useLLVM) ''
-    substituteInPlace lib/builtins/int_util.c \
-      --replace "#include <stdlib.h>" ""
-    substituteInPlace lib/builtins/clear_cache.c \
-      --replace "#include <assert.h>" ""
-    substituteInPlace lib/builtins/cpu_model.c \
-      --replace "#include <assert.h>" ""
-  '';
-
-  preConfigure = lib.optionalString (useLLVM && !haveLibc) ''
-    cmakeFlagsArray+=(-DCMAKE_C_FLAGS="-nodefaultlibs -ffreestanding")
-  '';
-
-  # Hack around weird upsream RPATH bug
-  postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin || stdenv.hostPlatform.isWasm) ''
-    ln -s "$out/lib"/*/* "$out/lib"
-  '' + lib.optionalString (useLLVM) ''
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
-    ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
-  '' + lib.optionalString doFakeLibgcc ''
-    ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
-  '';
-
-  meta = llvm_meta // {
-    homepage = "https://compiler-rt.llvm.org/";
-    description = "Compiler runtime libraries";
-    mainProgram = "hwasan_symbolize";
-    longDescription = ''
-      The compiler-rt project provides highly tuned implementations of the
-      low-level code generator support routines like "__fixunsdfdi" and other
-      calls generated when a target doesn't have a short sequence of native
-      instructions to implement a core IR operation. It also provides
-      implementations of run-time libraries for dynamic testing tools such as
-      AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
-    '';
-    # "All of the code in the compiler-rt project is dual licensed under the MIT
-    # license and the UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-    # compiler-rt requires a Clang stdenv on 32-bit RISC-V:
-    # https://reviews.llvm.org/D43106#1019077
-    broken = stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang;
-  };
-}
diff --git a/pkgs/development/compilers/llvm/14/default.nix b/pkgs/development/compilers/llvm/14/default.nix
index 687deb6ead89b..efab4ca735726 100644
--- a/pkgs/development/compilers/llvm/14/default.nix
+++ b/pkgs/development/compilers/llvm/14/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake
 , preLibcCrossHeaders
-, libxml2, python3, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, fetchFromGitHub, substitute, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -270,14 +270,50 @@ in let
     callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake libxml2 python3 release_version version monorepoSrc; });
   in {
 
-    compiler-rt-libc = callPackage ./compiler-rt {
+    compiler-rt-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/codesign.patch # Revert compiler-rt commit that makes codesign mandatory
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        ./compiler-rt/armv7l.patch
+        # Fix build on armv6l
+        ../common/compiler-rt/armv6-mcr-dmb.patch
+        ../common/compiler-rt/armv6-sync-ops-no-thumb.patch
+        ../common/compiler-rt/armv6-no-ldrexd-strexd.patch
+        ../common/compiler-rt/armv6-scudo-no-yield.patch
+        ../common/compiler-rt/armv6-scudo-libatomic.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
                else stdenv;
     };
 
-    compiler-rt-no-libc = callPackage ./compiler-rt {
+    compiler-rt-no-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/codesign.patch # Revert compiler-rt commit that makes codesign mandatory
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        ./compiler-rt/armv7l.patch
+        # Fix build on armv6l
+        ../common/compiler-rt/armv6-mcr-dmb.patch
+        ../common/compiler-rt/armv6-sync-ops-no-thumb.patch
+        ../common/compiler-rt/armv6-no-ldrexd-strexd.patch
+        ../common/compiler-rt/armv6-scudo-no-yield.patch
+        ../common/compiler-rt/armv6-scudo-libatomic.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
@@ -293,7 +329,22 @@ in let
 
     libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;
 
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = [
+        (substitute {
+          src = ../common/libcxxabi/wasm.patch;
+          replacements = [
+            "--replace-fail" "/cmake/" "/llvm/cmake/"
+          ];
+        })
+      ] ++ lib.optionals stdenv.hostPlatform.isMusl [
+        (substitute {
+          src = ../common/libcxx/libcxx-0001-musl-hacks.patch;
+          replacements = [
+            "--replace-fail" "/include/" "/libcxx/include/"
+          ];
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/15/compiler-rt/default.nix b/pkgs/development/compilers/llvm/15/compiler-rt/default.nix
deleted file mode 100644
index d257c69cbcd05..0000000000000
--- a/pkgs/development/compilers/llvm/15/compiler-rt/default.nix
+++ /dev/null
@@ -1,145 +0,0 @@
-{ lib, stdenv, llvm_meta, version
-, monorepoSrc, runCommand
-, cmake, ninja, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
-, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
-}:
-
-let
-
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-  bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
-  haveLibc = stdenv.cc.libc != null;
-  inherit (stdenv.hostPlatform) isMusl;
-
-  baseName = "compiler-rt";
-
-  src = runCommand "${baseName}-src-${version}" {} ''
-    mkdir -p "$out"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/${baseName} "$out"
-  '';
-in
-
-stdenv.mkDerivation {
-  pname = baseName + lib.optionalString (haveLibc) "-libc";
-  inherit version;
-
-  inherit src;
-  sourceRoot = "${src.name}/${baseName}";
-
-  nativeBuildInputs = [ cmake ninja python3 libllvm.dev ]
-    ++ lib.optional stdenv.isDarwin xcbuild.xcrun;
-  buildInputs =
-    lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
-
-  env.NIX_CFLAGS_COMPILE = toString [
-    "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
-  ];
-
-  cmakeFlags = [
-    "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
-    "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-    "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-  ] ++ lib.optionals (haveLibc && stdenv.hostPlatform.libc == "glibc") [
-    "-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
-  ] ++ lib.optionals (useLLVM || bareMetal || isMusl) [
-    "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
-    "-DCOMPILER_RT_BUILD_XRAY=OFF"
-    "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
-    "-DCOMPILER_RT_BUILD_MEMPROF=OFF"
-    "-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
-  ] ++ lib.optionals (useLLVM || bareMetal) [
-    "-DCOMPILER_RT_BUILD_PROFILE=OFF"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
-    "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
-  ] ++ lib.optionals (useLLVM) [
-    "-DCOMPILER_RT_BUILD_BUILTINS=ON"
-    #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
-    "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
-  ] ++ lib.optionals (bareMetal) [
-    "-DCOMPILER_RT_OS_DIR=baremetal"
-  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
-    "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
-    "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
-    "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
-
-    # `COMPILER_RT_DEFAULT_TARGET_ONLY` does not apply to Darwin:
-    # https://github.com/llvm/llvm-project/blob/27ef42bec80b6c010b7b3729ed0528619521a690/compiler-rt/cmake/base-config-ix.cmake#L153
-    "-DCOMPILER_RT_ENABLE_IOS=OFF"
-  ];
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
-    ./gnu-install-dirs.patch
-    # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
-    # extra `/`.
-    ./normalize-var.patch
-    # Prevent a compilation error on darwin
-    ./darwin-targetconditionals.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/186575
-    ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
-    ../../common/compiler-rt/armv7l-15.patch
-  ];
-
-  # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
-  # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
-  # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
-  # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
-  # a flag and turn the flag off during the stdenv build.
-  postPatch = lib.optionalString (!stdenv.isDarwin) ''
-    substituteInPlace cmake/builtin-config-ix.cmake \
-      --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace cmake/config-ix.cmake \
-      --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
-  '' + lib.optionalString (useLLVM) ''
-    substituteInPlace lib/builtins/int_util.c \
-      --replace "#include <stdlib.h>" ""
-    substituteInPlace lib/builtins/clear_cache.c \
-      --replace "#include <assert.h>" ""
-    substituteInPlace lib/builtins/cpu_model.c \
-      --replace "#include <assert.h>" ""
-  '';
-
-  preConfigure = lib.optionalString (useLLVM && !haveLibc) ''
-    cmakeFlagsArray+=(-DCMAKE_C_FLAGS="-nodefaultlibs -ffreestanding")
-  '';
-
-  # Hack around weird upsream RPATH bug
-  postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin || stdenv.hostPlatform.isWasm) ''
-    ln -s "$out/lib"/*/* "$out/lib"
-  '' + lib.optionalString (useLLVM) ''
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
-    ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
-  '' + lib.optionalString doFakeLibgcc ''
-     ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
-  '';
-
-  meta = llvm_meta // {
-    homepage = "https://compiler-rt.llvm.org/";
-    description = "Compiler runtime libraries";
-    mainProgram = "hwasan_symbolize";
-    longDescription = ''
-      The compiler-rt project provides highly tuned implementations of the
-      low-level code generator support routines like "__fixunsdfdi" and other
-      calls generated when a target doesn't have a short sequence of native
-      instructions to implement a core IR operation. It also provides
-      implementations of run-time libraries for dynamic testing tools such as
-      AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
-    '';
-    # "All of the code in the compiler-rt project is dual licensed under the MIT
-    # license and the UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-    # compiler-rt requires a Clang stdenv on 32-bit RISC-V:
-    # https://reviews.llvm.org/D43106#1019077
-    broken = stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang;
-  };
-}
diff --git a/pkgs/development/compilers/llvm/15/default.nix b/pkgs/development/compilers/llvm/15/default.nix
index 52ab1783b876b..01112e0165e4c 100644
--- a/pkgs/development/compilers/llvm/15/default.nix
+++ b/pkgs/development/compilers/llvm/15/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake, ninja
 , preLibcCrossHeaders
-, libxml2, python3, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, fetchFromGitHub, fetchpatch, substitute, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -284,14 +284,40 @@ in let
     callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake ninja libxml2 python3 release_version version monorepoSrc; });
   in {
 
-    compiler-rt-libc = callPackage ./compiler-rt {
+    compiler-rt-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
                else stdenv;
     };
 
-    compiler-rt-no-libc = callPackage ./compiler-rt {
+    compiler-rt-no-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
@@ -310,7 +336,30 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = [
+        # See:
+        #   - https://reviews.llvm.org/D133566
+        #   - https://github.com/NixOS/nixpkgs/issues/214524#issuecomment-1429146432
+        # !!! Drop in LLVM 16+
+        (fetchpatch {
+          url = "https://github.com/llvm/llvm-project/commit/57c7bb3ec89565c68f858d316504668f9d214d59.patch";
+          hash = "sha256-B07vHmSjy5BhhkGSj3e1E0XmMv5/9+mvC/k70Z29VwY=";
+        })
+        (substitute {
+          src = ../common/libcxxabi/wasm.patch;
+          replacements = [
+            "--replace-fail" "/cmake/" "/llvm/cmake/"
+          ];
+        })
+      ] ++ lib.optionals stdenv.hostPlatform.isMusl [
+        (substitute {
+          src = ../common/libcxx/libcxx-0001-musl-hacks.patch;
+          replacements = [
+            "--replace-fail" "/include/" "/libcxx/include/"
+          ];
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/15/libcxx/default.nix b/pkgs/development/compilers/llvm/15/libcxx/default.nix
deleted file mode 100644
index 89181bcbdbbf2..0000000000000
--- a/pkgs/development/compilers/llvm/15/libcxx/default.nix
+++ /dev/null
@@ -1,145 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, fetchpatch, substitute
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    # See:
-    #   - https://reviews.llvm.org/D133566
-    #   - https://github.com/NixOS/nixpkgs/issues/214524#issuecomment-1429146432
-    # !!! Drop in LLVM 16+
-    (fetchpatch {
-      url = "https://github.com/llvm/llvm-project/commit/57c7bb3ec89565c68f858d316504668f9d214d59.patch";
-      hash = "sha256-B07vHmSjy5BhhkGSj3e1E0XmMv5/9+mvC/k70Z29VwY=";
-    })
-    (substitute {
-      src = ../../common/libcxxabi/wasm.patch;
-      replacements = [
-        "--replace-fail" "/cmake/" "/llvm/cmake/"
-      ];
-    })
-  ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-    (substitute {
-      src = ../../common/libcxx/libcxx-0001-musl-hacks.patch;
-      replacements = [
-        "--replace-fail" "/include/" "/libcxx/include/"
-      ];
-    })
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/16/compiler-rt/default.nix b/pkgs/development/compilers/llvm/16/compiler-rt/default.nix
deleted file mode 100644
index 2a15e5302b463..0000000000000
--- a/pkgs/development/compilers/llvm/16/compiler-rt/default.nix
+++ /dev/null
@@ -1,159 +0,0 @@
-{ lib, stdenv, llvm_meta, version
-, monorepoSrc, runCommand
-, cmake, ninja, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
-, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
-}:
-
-let
-
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-  bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
-  haveLibc = stdenv.cc.libc != null;
-  isDarwinStatic = stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic;
-  inherit (stdenv.hostPlatform) isMusl;
-
-  baseName = "compiler-rt";
-
-  src = runCommand "${baseName}-src-${version}" {} ''
-    mkdir -p "$out"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/${baseName} "$out"
-  '';
-in
-
-stdenv.mkDerivation {
-  pname = baseName + lib.optionalString (haveLibc) "-libc";
-  inherit version;
-
-  inherit src;
-  sourceRoot = "${src.name}/${baseName}";
-
-  nativeBuildInputs = [ cmake ninja python3 libllvm.dev ]
-    ++ lib.optional stdenv.isDarwin xcbuild.xcrun;
-  buildInputs =
-    lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
-
-  env.NIX_CFLAGS_COMPILE = toString ([
-    "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
-  ] ++ lib.optionals (!haveLibc) [
-    # The compiler got stricter about this, and there is a usellvm patch below
-    # which patches out the assert include causing an implicit definition of
-    # assert. It would be nicer to understand why compiler-rt thinks it should
-    # be able to #include <assert.h> in the first place; perhaps it's in the
-    # wrong, or perhaps there is a way to provide an assert.h.
-    "-Wno-error=implicit-function-declaration"
-  ]);
-
-  cmakeFlags = [
-    "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
-    "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-    "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-  ] ++ lib.optionals (haveLibc && stdenv.hostPlatform.libc == "glibc") [
-    "-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
-  ] ++ lib.optionals (useLLVM || bareMetal || isMusl || isDarwinStatic) [
-    "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
-    "-DCOMPILER_RT_BUILD_XRAY=OFF"
-    "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
-    "-DCOMPILER_RT_BUILD_MEMPROF=OFF"
-    "-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
-  ] ++ lib.optionals (useLLVM || bareMetal) [
-     "-DCOMPILER_RT_BUILD_PROFILE=OFF"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isDarwinStatic ) [
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
-    "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
-  ] ++ lib.optionals (useLLVM && !haveLibc) [
-    "-DCMAKE_C_FLAGS=-nodefaultlibs"
-  ] ++ lib.optionals (useLLVM) [
-    "-DCOMPILER_RT_BUILD_BUILTINS=ON"
-    #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
-    "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
-  ] ++ lib.optionals (bareMetal) [
-    "-DCOMPILER_RT_OS_DIR=baremetal"
-  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
-    "-DCMAKE_LIPO=${lib.getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}lipo"
-    "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
-    "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
-    "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
-
-    # `COMPILER_RT_DEFAULT_TARGET_ONLY` does not apply to Darwin:
-    # https://github.com/llvm/llvm-project/blob/27ef42bec80b6c010b7b3729ed0528619521a690/compiler-rt/cmake/base-config-ix.cmake#L153
-    "-DCOMPILER_RT_ENABLE_IOS=OFF"
-  ];
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
-    ./gnu-install-dirs.patch
-    # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
-    # extra `/`.
-    ./normalize-var.patch
-    # Prevent a compilation error on darwin
-    ./darwin-targetconditionals.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/186575
-    ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
-    # ../../common/compiler-rt/armv7l-15.patch
-  ];
-
-  # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
-  # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
-  # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
-  # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
-  # a flag and turn the flag off during the stdenv build.
-  postPatch = lib.optionalString (!stdenv.isDarwin) ''
-    substituteInPlace cmake/builtin-config-ix.cmake \
-      --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace cmake/config-ix.cmake \
-      --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
-  '' + lib.optionalString (useLLVM && !haveLibc) ''
-    substituteInPlace lib/builtins/int_util.c \
-      --replace "#include <stdlib.h>" ""
-    substituteInPlace lib/builtins/clear_cache.c \
-      --replace "#include <assert.h>" ""
-    substituteInPlace lib/builtins/cpu_model.c \
-      --replace "#include <assert.h>" ""
-  '';
-
-  # Hack around weird upsream RPATH bug
-  postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin) ''
-    ln -s "$out/lib"/*/* "$out/lib"
-  '' + lib.optionalString (useLLVM && stdenv.hostPlatform.isLinux) ''
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
-    # Note the history of crt{begin,end}S in previous versions of llvm in nixpkg:
-    # The presence of crtbegin_shared has been added and removed; it's possible
-    # people have added/removed it to get it working on their platforms.
-    # Try each in turn for now.
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtendS.o
-    ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
-  '' + lib.optionalString doFakeLibgcc ''
-     ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
-  '';
-
-  meta = llvm_meta // {
-    homepage = "https://compiler-rt.llvm.org/";
-    description = "Compiler runtime libraries";
-    mainProgram = "hwasan_symbolize";
-    longDescription = ''
-      The compiler-rt project provides highly tuned implementations of the
-      low-level code generator support routines like "__fixunsdfdi" and other
-      calls generated when a target doesn't have a short sequence of native
-      instructions to implement a core IR operation. It also provides
-      implementations of run-time libraries for dynamic testing tools such as
-      AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
-    '';
-    # "All of the code in the compiler-rt project is dual licensed under the MIT
-    # license and the UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-    # compiler-rt requires a Clang stdenv on 32-bit RISC-V:
-    # https://reviews.llvm.org/D43106#1019077
-    broken = stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang;
-  };
-}
diff --git a/pkgs/development/compilers/llvm/16/default.nix b/pkgs/development/compilers/llvm/16/default.nix
index 168c288d49a3c..99ab725273b85 100644
--- a/pkgs/development/compilers/llvm/16/default.nix
+++ b/pkgs/development/compilers/llvm/16/default.nix
@@ -300,14 +300,40 @@ in let
     callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake ninja libxml2 python3 release_version version monorepoSrc; });
   in {
 
-    compiler-rt-libc = callPackage ./compiler-rt {
+    compiler-rt-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        # ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic)
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
                else stdenv;
     };
 
-    compiler-rt-no-libc = callPackage ./compiler-rt {
+    compiler-rt-no-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        # ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
@@ -326,7 +352,7 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/16/libcxx/default.nix b/pkgs/development/compilers/llvm/16/libcxx/default.nix
deleted file mode 100644
index 146424113ec4e..0000000000000
--- a/pkgs/development/compilers/llvm/16/libcxx/default.nix
+++ /dev/null
@@ -1,125 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, fetchpatch
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-    # There's precedent for this in llvm-project/libcxx/cmake/caches.
-    # In a monorepo build you might do the following in the libcxxabi build:
-    #   -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
-    #   -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
-    # libcxx appears to require unwind and doesn't pull it in via other means.
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  sourceRoot = "${src.name}/runtimes";
-
-  outputs = [ "out" "dev" ];
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/17/compiler-rt/default.nix b/pkgs/development/compilers/llvm/17/compiler-rt/default.nix
deleted file mode 100644
index 2a15e5302b463..0000000000000
--- a/pkgs/development/compilers/llvm/17/compiler-rt/default.nix
+++ /dev/null
@@ -1,159 +0,0 @@
-{ lib, stdenv, llvm_meta, version
-, monorepoSrc, runCommand
-, cmake, ninja, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
-, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
-}:
-
-let
-
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-  bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
-  haveLibc = stdenv.cc.libc != null;
-  isDarwinStatic = stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic;
-  inherit (stdenv.hostPlatform) isMusl;
-
-  baseName = "compiler-rt";
-
-  src = runCommand "${baseName}-src-${version}" {} ''
-    mkdir -p "$out"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/${baseName} "$out"
-  '';
-in
-
-stdenv.mkDerivation {
-  pname = baseName + lib.optionalString (haveLibc) "-libc";
-  inherit version;
-
-  inherit src;
-  sourceRoot = "${src.name}/${baseName}";
-
-  nativeBuildInputs = [ cmake ninja python3 libllvm.dev ]
-    ++ lib.optional stdenv.isDarwin xcbuild.xcrun;
-  buildInputs =
-    lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
-
-  env.NIX_CFLAGS_COMPILE = toString ([
-    "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
-  ] ++ lib.optionals (!haveLibc) [
-    # The compiler got stricter about this, and there is a usellvm patch below
-    # which patches out the assert include causing an implicit definition of
-    # assert. It would be nicer to understand why compiler-rt thinks it should
-    # be able to #include <assert.h> in the first place; perhaps it's in the
-    # wrong, or perhaps there is a way to provide an assert.h.
-    "-Wno-error=implicit-function-declaration"
-  ]);
-
-  cmakeFlags = [
-    "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
-    "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-    "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-  ] ++ lib.optionals (haveLibc && stdenv.hostPlatform.libc == "glibc") [
-    "-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
-  ] ++ lib.optionals (useLLVM || bareMetal || isMusl || isDarwinStatic) [
-    "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
-    "-DCOMPILER_RT_BUILD_XRAY=OFF"
-    "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
-    "-DCOMPILER_RT_BUILD_MEMPROF=OFF"
-    "-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
-  ] ++ lib.optionals (useLLVM || bareMetal) [
-     "-DCOMPILER_RT_BUILD_PROFILE=OFF"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isDarwinStatic ) [
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
-    "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
-  ] ++ lib.optionals (useLLVM && !haveLibc) [
-    "-DCMAKE_C_FLAGS=-nodefaultlibs"
-  ] ++ lib.optionals (useLLVM) [
-    "-DCOMPILER_RT_BUILD_BUILTINS=ON"
-    #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
-    "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
-  ] ++ lib.optionals (bareMetal) [
-    "-DCOMPILER_RT_OS_DIR=baremetal"
-  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
-    "-DCMAKE_LIPO=${lib.getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}lipo"
-    "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
-    "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
-    "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
-
-    # `COMPILER_RT_DEFAULT_TARGET_ONLY` does not apply to Darwin:
-    # https://github.com/llvm/llvm-project/blob/27ef42bec80b6c010b7b3729ed0528619521a690/compiler-rt/cmake/base-config-ix.cmake#L153
-    "-DCOMPILER_RT_ENABLE_IOS=OFF"
-  ];
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
-    ./gnu-install-dirs.patch
-    # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
-    # extra `/`.
-    ./normalize-var.patch
-    # Prevent a compilation error on darwin
-    ./darwin-targetconditionals.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/186575
-    ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
-    # ../../common/compiler-rt/armv7l-15.patch
-  ];
-
-  # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
-  # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
-  # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
-  # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
-  # a flag and turn the flag off during the stdenv build.
-  postPatch = lib.optionalString (!stdenv.isDarwin) ''
-    substituteInPlace cmake/builtin-config-ix.cmake \
-      --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace cmake/config-ix.cmake \
-      --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
-  '' + lib.optionalString (useLLVM && !haveLibc) ''
-    substituteInPlace lib/builtins/int_util.c \
-      --replace "#include <stdlib.h>" ""
-    substituteInPlace lib/builtins/clear_cache.c \
-      --replace "#include <assert.h>" ""
-    substituteInPlace lib/builtins/cpu_model.c \
-      --replace "#include <assert.h>" ""
-  '';
-
-  # Hack around weird upsream RPATH bug
-  postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin) ''
-    ln -s "$out/lib"/*/* "$out/lib"
-  '' + lib.optionalString (useLLVM && stdenv.hostPlatform.isLinux) ''
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
-    # Note the history of crt{begin,end}S in previous versions of llvm in nixpkg:
-    # The presence of crtbegin_shared has been added and removed; it's possible
-    # people have added/removed it to get it working on their platforms.
-    # Try each in turn for now.
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtendS.o
-    ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
-  '' + lib.optionalString doFakeLibgcc ''
-     ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
-  '';
-
-  meta = llvm_meta // {
-    homepage = "https://compiler-rt.llvm.org/";
-    description = "Compiler runtime libraries";
-    mainProgram = "hwasan_symbolize";
-    longDescription = ''
-      The compiler-rt project provides highly tuned implementations of the
-      low-level code generator support routines like "__fixunsdfdi" and other
-      calls generated when a target doesn't have a short sequence of native
-      instructions to implement a core IR operation. It also provides
-      implementations of run-time libraries for dynamic testing tools such as
-      AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
-    '';
-    # "All of the code in the compiler-rt project is dual licensed under the MIT
-    # license and the UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-    # compiler-rt requires a Clang stdenv on 32-bit RISC-V:
-    # https://reviews.llvm.org/D43106#1019077
-    broken = stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang;
-  };
-}
diff --git a/pkgs/development/compilers/llvm/17/default.nix b/pkgs/development/compilers/llvm/17/default.nix
index 70571b30b3a6e..2f26632176530 100644
--- a/pkgs/development/compilers/llvm/17/default.nix
+++ b/pkgs/development/compilers/llvm/17/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake, ninja
 , preLibcCrossHeaders
-, libxml2, python3, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, fetchFromGitHub, fetchpatch, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -285,14 +285,40 @@ in let
     callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake ninja libxml2 python3 release_version version monorepoSrc; });
   in {
 
-    compiler-rt-libc = callPackage ./compiler-rt {
+    compiler-rt-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        # ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic)
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
                else stdenv;
     };
 
-    compiler-rt-no-libc = callPackage ./compiler-rt {
+    compiler-rt-no-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        ./compiler-rt/gnu-install-dirs.patch
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # Prevent a compilation error on darwin
+        ./compiler-rt/darwin-targetconditionals.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        # ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
@@ -311,7 +337,15 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
+        # https://github.com/llvm/llvm-project/issues/64226
+        (fetchpatch {
+          name = "0042-mbstate_t-not-defined.patch";
+          url = "https://github.com/macports/macports-ports/raw/acd8acb171f1658596ed1cf25da48d5b932e2d19/lang/llvm-17/files/0042-mbstate_t-not-defined.patch";
+          hash = "sha256-jo+DYA6zuSv9OH3A0bYwY5TlkWprup4OKQ7rfK1WHBI=";
+        })
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/17/libcxx/default.nix b/pkgs/development/compilers/llvm/17/libcxx/default.nix
deleted file mode 100644
index 9ee4fd180d4b6..0000000000000
--- a/pkgs/development/compilers/llvm/17/libcxx/default.nix
+++ /dev/null
@@ -1,136 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, fetchpatch
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-    # There's precedent for this in llvm-project/libcxx/cmake/caches.
-    # In a monorepo build you might do the following in the libcxxabi build:
-    #   -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
-    #   -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
-    # libcxx appears to require unwind and doesn't pull it in via other means.
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
-    # https://github.com/llvm/llvm-project/issues/64226
-    (fetchpatch {
-      name = "0042-mbstate_t-not-defined.patch";
-      url = "https://github.com/macports/macports-ports/raw/acd8acb171f1658596ed1cf25da48d5b932e2d19/lang/llvm-17/files/0042-mbstate_t-not-defined.patch";
-      hash = "sha256-jo+DYA6zuSv9OH3A0bYwY5TlkWprup4OKQ7rfK1WHBI=";
-    })
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/18/compiler-rt/default.nix b/pkgs/development/compilers/llvm/18/compiler-rt/default.nix
deleted file mode 100644
index f51316beb5705..0000000000000
--- a/pkgs/development/compilers/llvm/18/compiler-rt/default.nix
+++ /dev/null
@@ -1,157 +0,0 @@
-{ lib, stdenv, llvm_meta, version
-, monorepoSrc, runCommand
-, cmake, ninja, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
-, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
-}:
-
-let
-
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-  bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
-  haveLibc = stdenv.cc.libc != null;
-  isDarwinStatic = stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic;
-  inherit (stdenv.hostPlatform) isMusl;
-
-  baseName = "compiler-rt";
-
-  src = runCommand "${baseName}-src-${version}" {} ''
-    mkdir -p "$out"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/${baseName} "$out"
-  '';
-in
-
-stdenv.mkDerivation {
-  pname = baseName + lib.optionalString (haveLibc) "-libc";
-  inherit version;
-
-  inherit src;
-  sourceRoot = "${src.name}/${baseName}";
-
-  nativeBuildInputs = [ cmake ninja python3 libllvm.dev ]
-    ++ lib.optional stdenv.isDarwin xcbuild.xcrun;
-  buildInputs =
-    lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
-
-  env.NIX_CFLAGS_COMPILE = toString ([
-    "-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
-  ] ++ lib.optionals (!haveLibc) [
-    # The compiler got stricter about this, and there is a usellvm patch below
-    # which patches out the assert include causing an implicit definition of
-    # assert. It would be nicer to understand why compiler-rt thinks it should
-    # be able to #include <assert.h> in the first place; perhaps it's in the
-    # wrong, or perhaps there is a way to provide an assert.h.
-    "-Wno-error=implicit-function-declaration"
-  ]);
-
-  cmakeFlags = [
-    "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
-    "-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-    "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
-  ] ++ lib.optionals (haveLibc && stdenv.hostPlatform.libc == "glibc") [
-    "-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
-  ] ++ lib.optionals (useLLVM || bareMetal || isMusl || isDarwinStatic) [
-    "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
-    "-DCOMPILER_RT_BUILD_XRAY=OFF"
-    "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
-    "-DCOMPILER_RT_BUILD_MEMPROF=OFF"
-    "-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
-  ] ++ lib.optionals (useLLVM || bareMetal) [
-     "-DCOMPILER_RT_BUILD_PROFILE=OFF"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isDarwinStatic ) [
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCOMPILER_RT_BAREMETAL_BUILD=ON"
-    "-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
-  ] ++ lib.optionals (useLLVM && !haveLibc) [
-    "-DCMAKE_C_FLAGS=-nodefaultlibs"
-  ] ++ lib.optionals (useLLVM) [
-    "-DCOMPILER_RT_BUILD_BUILTINS=ON"
-    #https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
-    "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
-  ] ++ lib.optionals (bareMetal) [
-    "-DCOMPILER_RT_OS_DIR=baremetal"
-  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
-    "-DCMAKE_LIPO=${lib.getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}lipo"
-    "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
-    "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
-    "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
-
-    # `COMPILER_RT_DEFAULT_TARGET_ONLY` does not apply to Darwin:
-    # https://github.com/llvm/llvm-project/blob/27ef42bec80b6c010b7b3729ed0528619521a690/compiler-rt/cmake/base-config-ix.cmake#L153
-    "-DCOMPILER_RT_ENABLE_IOS=OFF"
-  ];
-
-  outputs = [ "out" "dev" ];
-
-  patches = [
-    ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
-    # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
-    # extra `/`.
-    ./normalize-var.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/186575
-    ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
-    # ../../common/compiler-rt/armv7l-15.patch
-  ];
-
-  # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
-  # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
-  # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
-  # get no binary cache for the entire platform. If you really find yourself wanting the TSAN, make this controllable by
-  # a flag and turn the flag off during the stdenv build.
-  postPatch = lib.optionalString (!stdenv.isDarwin) ''
-    substituteInPlace cmake/builtin-config-ix.cmake \
-      --replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace cmake/config-ix.cmake \
-      --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
-  '' + lib.optionalString (useLLVM && !haveLibc) ''
-    substituteInPlace lib/builtins/aarch64/sme-libc-routines.c \
-      --replace "<stdlib.h>" "<stddef.h>"
-    substituteInPlace lib/builtins/int_util.c \
-      --replace "#include <stdlib.h>" ""
-    substituteInPlace lib/builtins/clear_cache.c \
-      --replace "#include <assert.h>" ""
-    substituteInPlace lib/builtins/cpu_model${lib.optionalString (lib.versionAtLeast version "18") "/x86"}.c \
-      --replace "#include <assert.h>" ""
-  '';
-
-  # Hack around weird upsream RPATH bug
-  postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin) ''
-    ln -s "$out/lib"/*/* "$out/lib"
-  '' + lib.optionalString (useLLVM && stdenv.hostPlatform.isLinux) ''
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbegin.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtend.o
-    # Note the history of crt{begin,end}S in previous versions of llvm in nixpkg:
-    # The presence of crtbegin_shared has been added and removed; it's possible
-    # people have added/removed it to get it working on their platforms.
-    # Try each in turn for now.
-    ln -s $out/lib/*/clang_rt.crtbegin-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend-*.o $out/lib/crtendS.o
-    ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
-    ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
-  '' + lib.optionalString doFakeLibgcc ''
-     ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
-  '';
-
-  meta = llvm_meta // {
-    homepage = "https://compiler-rt.llvm.org/";
-    description = "Compiler runtime libraries";
-    longDescription = ''
-      The compiler-rt project provides highly tuned implementations of the
-      low-level code generator support routines like "__fixunsdfdi" and other
-      calls generated when a target doesn't have a short sequence of native
-      instructions to implement a core IR operation. It also provides
-      implementations of run-time libraries for dynamic testing tools such as
-      AddressSanitizer, ThreadSanitizer, MemorySanitizer, and DataFlowSanitizer.
-    '';
-    # "All of the code in the compiler-rt project is dual licensed under the MIT
-    # license and the UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-    # compiler-rt requires a Clang stdenv on 32-bit RISC-V:
-    # https://reviews.llvm.org/D43106#1019077
-    broken = stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang;
-  };
-}
diff --git a/pkgs/development/compilers/llvm/18/default.nix b/pkgs/development/compilers/llvm/18/default.nix
index d5c2140e33e49..4bc56be45b3e5 100644
--- a/pkgs/development/compilers/llvm/18/default.nix
+++ b/pkgs/development/compilers/llvm/18/default.nix
@@ -284,14 +284,34 @@ in let
     callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake ninja libxml2 python3 release_version version monorepoSrc; });
   in {
 
-    compiler-rt-libc = callPackage ./compiler-rt {
+    compiler-rt-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        # ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic)
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
                else stdenv;
     };
 
-    compiler-rt-no-libc = callPackage ./compiler-rt {
+    compiler-rt-no-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        # ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
@@ -310,7 +330,11 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
+        # https://github.com/llvm/llvm-project/issues/64226
+        ./libcxx/0001-darwin-10.12-mbstate_t-fix.patch
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/18/libcxx/default.nix b/pkgs/development/compilers/llvm/18/libcxx/default.nix
deleted file mode 100644
index d6c304c0b476a..0000000000000
--- a/pkgs/development/compilers/llvm/18/libcxx/default.nix
+++ /dev/null
@@ -1,130 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = [
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=OFF"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_ADDITIONAL_LIBRARIES=unwind"
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals (lib.versionAtLeast version "18" && !useLLVM && stdenv.hostPlatform.libc == "glibc" && !stdenv.hostPlatform.isStatic) [
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=gcc_s"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-    # There's precedent for this in llvm-project/libcxx/cmake/caches.
-    # In a monorepo build you might do the following in the libcxxabi build:
-    #   -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
-    #   -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
-    # libcxx appears to require unwind and doesn't pull it in via other means.
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
-    # https://github.com/llvm/llvm-project/issues/64226
-    ./0001-darwin-10.12-mbstate_t-fix.patch
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}
diff --git a/pkgs/development/compilers/llvm/git/compiler-rt/default.nix b/pkgs/development/compilers/llvm/common/compiler-rt/default.nix
index 606ae2868893d..4f44d6396d301 100644
--- a/pkgs/development/compilers/llvm/git/compiler-rt/default.nix
+++ b/pkgs/development/compilers/llvm/common/compiler-rt/default.nix
@@ -1,6 +1,19 @@
-{ lib, stdenv, llvm_meta, version
-, monorepoSrc, runCommand
-, cmake, ninja, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
+{ lib
+, stdenv
+, llvm_meta
+, release_version
+, version
+, patches ? []
+, src ? null
+, monorepoSrc ? null
+, runCommand
+, cmake
+, ninja
+, python3
+, xcbuild
+, libllvm
+, linuxHeaders
+, libxcrypt
 , doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
 }:
 
@@ -9,26 +22,34 @@ let
   useLLVM = stdenv.hostPlatform.useLLVM or false;
   bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
   haveLibc = stdenv.cc.libc != null;
-  isDarwinStatic = stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic;
-  inherit (stdenv.hostPlatform) isMusl;
+  isDarwinStatic = stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic && lib.versionAtLeast release_version "16";
+  inherit (stdenv.hostPlatform) isMusl isAarch64;
 
   baseName = "compiler-rt";
+  pname = baseName + lib.optionalString (haveLibc) "-libc";
+
+  src' = if monorepoSrc != null then
+    runCommand "${baseName}-src-${version}" {} ''
+      mkdir -p "$out"
+      cp -r ${monorepoSrc}/cmake "$out"
+      cp -r ${monorepoSrc}/${baseName} "$out"
+    '' else src;
 
-  src = runCommand "${baseName}-src-${version}" {} ''
-    mkdir -p "$out"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/${baseName} "$out"
+  preConfigure = lib.optionalString (useLLVM && !haveLibc) ''
+    cmakeFlagsArray+=(-DCMAKE_C_FLAGS="-nodefaultlibs -ffreestanding")
   '';
 in
 
-stdenv.mkDerivation {
-  pname = baseName + lib.optionalString (haveLibc) "-libc";
-  inherit version;
+stdenv.mkDerivation ({
+  inherit pname version patches;
 
-  inherit src;
-  sourceRoot = "${src.name}/${baseName}";
+  src = src';
+  sourceRoot = if lib.versionOlder release_version "13" then null
+    else "${src'.name}/${baseName}";
 
-  nativeBuildInputs = [ cmake ninja python3 libllvm.dev ]
+  nativeBuildInputs = [ cmake ]
+    ++ (lib.optional (lib.versionAtLeast release_version "15") ninja)
+    ++ [ python3 libllvm.dev ]
     ++ lib.optional stdenv.isDarwin xcbuild.xcrun;
   buildInputs =
     lib.optional (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isRiscV) linuxHeaders;
@@ -50,6 +71,8 @@ stdenv.mkDerivation {
     "-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
   ] ++ lib.optionals (haveLibc && stdenv.hostPlatform.libc == "glibc") [
     "-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
+  ] ++ lib.optionals ((useLLVM || bareMetal || isMusl || isAarch64) && (lib.versions.major release_version == "13")) [
+    "-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
   ] ++ lib.optionals (useLLVM || bareMetal || isMusl || isDarwinStatic) [
     "-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
     "-DCOMPILER_RT_BUILD_XRAY=OFF"
@@ -58,7 +81,7 @@ stdenv.mkDerivation {
     "-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
   ] ++ lib.optionals (useLLVM || bareMetal) [
      "-DCOMPILER_RT_BUILD_PROFILE=OFF"
-  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isDarwinStatic ) [
+  ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isDarwinStatic) [
     "-DCMAKE_CXX_COMPILER_WORKS=ON"
   ] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
     "-DCMAKE_C_COMPILER_WORKS=ON"
@@ -72,32 +95,22 @@ stdenv.mkDerivation {
     "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
   ] ++ lib.optionals (bareMetal) [
     "-DCOMPILER_RT_OS_DIR=baremetal"
-  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
+  ] ++ lib.optionals (stdenv.hostPlatform.isDarwin) (lib.optionals (lib.versionAtLeast release_version "16") [
     "-DCMAKE_LIPO=${lib.getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}lipo"
+  ] ++ [
     "-DDARWIN_macosx_OVERRIDE_SDK_VERSION=ON"
     "-DDARWIN_osx_ARCHS=${stdenv.hostPlatform.darwinArch}"
     "-DDARWIN_osx_BUILTIN_ARCHS=${stdenv.hostPlatform.darwinArch}"
-
+  ] ++ lib.optionals (lib.versionAtLeast release_version "15") [
     # `COMPILER_RT_DEFAULT_TARGET_ONLY` does not apply to Darwin:
     # https://github.com/llvm/llvm-project/blob/27ef42bec80b6c010b7b3729ed0528619521a690/compiler-rt/cmake/base-config-ix.cmake#L153
     "-DCOMPILER_RT_ENABLE_IOS=OFF"
-  ] ++ lib.optionals (lib.versionAtLeast version "19" && stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
+  ]) ++ lib.optionals (lib.versionAtLeast version "19" && stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
     "-DSANITIZER_MIN_OSX_VERSION=10.10"
   ];
 
   outputs = [ "out" "dev" ];
 
-  patches = [
-    ./X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
-    # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
-    # extra `/`.
-    ./normalize-var.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/186575
-    ../../common/compiler-rt/darwin-plistbuddy-workaround.patch
-    # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
-    # ../../common/compiler-rt/armv7l-15.patch
-  ];
-
   # TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
   # to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
   # can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
@@ -109,16 +122,17 @@ stdenv.mkDerivation {
   '' + lib.optionalString stdenv.isDarwin ''
     substituteInPlace cmake/config-ix.cmake \
       --replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
-  '' + lib.optionalString (useLLVM && !haveLibc) ''
+  '' + lib.optionalString (useLLVM && !haveLibc) ((lib.optionalString (lib.versionAtLeast release_version "18") ''
     substituteInPlace lib/builtins/aarch64/sme-libc-routines.c \
       --replace "<stdlib.h>" "<stddef.h>"
+  '') + ''
     substituteInPlace lib/builtins/int_util.c \
       --replace "#include <stdlib.h>" ""
     substituteInPlace lib/builtins/clear_cache.c \
       --replace "#include <assert.h>" ""
     substituteInPlace lib/builtins/cpu_model${lib.optionalString (lib.versionAtLeast version "18") "/x86"}.c \
       --replace "#include <assert.h>" ""
-  '';
+  '');
 
   # Hack around weird upsream RPATH bug
   postInstall = lib.optionalString (stdenv.hostPlatform.isDarwin) ''
@@ -156,4 +170,4 @@ stdenv.mkDerivation {
     # https://reviews.llvm.org/D43106#1019077
     broken = stdenv.hostPlatform.isRiscV32 && !stdenv.cc.isClang;
   };
-}
+} // (if lib.versionOlder release_version "16" then { inherit preConfigure; } else {}))
diff --git a/pkgs/development/compilers/llvm/14/libcxx/default.nix b/pkgs/development/compilers/llvm/common/libcxx/default.nix
index 79eeadb1c5931..de895125f8832 100644
--- a/pkgs/development/compilers/llvm/14/libcxx/default.nix
+++ b/pkgs/development/compilers/llvm/common/libcxx/default.nix
@@ -1,8 +1,21 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand, substitute
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
+{ lib
+, stdenv
+, llvm_meta
+, release_version
+, monorepoSrc ? null
+, src ? null
+, patches ? []
+, runCommand
+, substitute
+, cmake
+, lndir
+, ninja
+, python3
+, fixDarwinDylibNames
+, version
 , cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
+, libcxxrt
+, libunwind
 , enableShared ? !stdenv.hostPlatform.isStatic
 }:
 
@@ -13,16 +26,39 @@
 assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
 let
   basename = "libcxx";
+  pname = basename;
   cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
   runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
 
   # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
   useLLVM = stdenv.hostPlatform.useLLVM or false;
 
-  cxxabiCMakeFlags = lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
+  src' = if monorepoSrc != null then
+    runCommand "${pname}-src-${version}" {} (''
+      mkdir -p "$out/llvm"
+    '' + (lib.optionalString (lib.versionAtLeast release_version "14") ''
+      cp -r ${monorepoSrc}/cmake "$out"
+    '') + ''
+      cp -r ${monorepoSrc}/libcxx "$out"
+      cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
+      cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
+    '' + (lib.optionalString (lib.versionAtLeast release_version "14") ''
+      cp -r ${monorepoSrc}/third-party "$out"
+    '') + ''
+      cp -r ${monorepoSrc}/runtimes "$out"
+    '' + (lib.optionalString (cxxabi == null) ''
+      cp -r ${monorepoSrc}/libcxxabi "$out"
+    '')) else src;
+
+  cxxabiCMakeFlags = lib.optionals (lib.versionAtLeast release_version "18") [
+    "-DLIBCXXABI_USE_LLVM_UNWINDER=OFF"
+  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) (if lib.versionAtLeast release_version "18" then [
+    "-DLIBCXXABI_ADDITIONAL_LIBRARIES=unwind"
+    "-DLIBCXXABI_USE_COMPILER_RT=ON"
+  ] else [
     "-DLIBCXXABI_USE_COMPILER_RT=ON"
     "-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
+  ]) ++ lib.optionals stdenv.hostPlatform.isWasm [
     "-DLIBCXXABI_ENABLE_THREADS=OFF"
     "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
   ] ++ lib.optionals (!enableShared) [
@@ -35,8 +71,12 @@ let
     "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
   ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
     "-DLIBCXX_HAS_MUSL_LIBC=1"
+  ] ++ lib.optionals (lib.versionAtLeast release_version "18" && !useLLVM && stdenv.hostPlatform.libc == "glibc" && !stdenv.hostPlatform.isStatic) [
+    "-DLIBCXX_ADDITIONAL_LIBRARIES=gcc_s"
   ] ++ lib.optionals useLLVM [
     "-DLIBCXX_USE_COMPILER_RT=ON"
+  ] ++ lib.optionals (useLLVM && lib.versionAtLeast release_version "16") [
+    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
   ] ++ lib.optionals stdenv.hostPlatform.isWasm [
     "-DLIBCXX_ENABLE_THREADS=OFF"
     "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
@@ -62,47 +102,12 @@ let
 
 in
 
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
+stdenv.mkDerivation (rec {
+  inherit pname version cmakeFlags patches;
 
-  patches = [
-    (substitute {
-      src = ../../common/libcxxabi/wasm.patch;
-      replacements = [
-        "--replace-fail" "/cmake/" "/llvm/cmake/"
-      ];
-    })
-  ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-    (substitute {
-      src = ../../common/libcxx/libcxx-0001-musl-hacks.patch;
-      replacements = [
-        "--replace-fail" "/include/" "/libcxx/include/"
-      ];
-    })
-  ];
+  src = src';
 
-  postPatch = ''
-    # fix CMake error when static and LIBCXXABI_USE_LLVM_UNWINDER=ON. aren't
-    # building unwind so don't need to depend on it
-    substituteInPlace libcxx/src/CMakeLists.txt \
-      --replace-fail "add_dependencies(cxx_static unwind)" "# add_dependencies(cxx_static unwind)"
-    cd runtimes
-  '';
+  outputs = [ "out" "dev" ];
 
   preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
     patchShebangs utils/cat_files.py
@@ -119,8 +124,8 @@ stdenv.mkDerivation rec {
   # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
   # support linker scripts so the external cxxabi needs to be symlinked in
   postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
+    lndir ${lib.getDev cxxabi}/include $out/include/c++/v1
+    lndir ${lib.getLib cxxabi}/lib $out/lib
   '';
 
   passthru = {
@@ -138,4 +143,15 @@ stdenv.mkDerivation rec {
     # UIUC License (a BSD-like license)":
     license = with lib.licenses; [ mit ncsa ];
   };
-}
+} // (if (lib.versionOlder release_version "16" || lib.versionAtLeast release_version "17") then {
+  postPatch = (lib.optionalString (lib.versionAtLeast release_version "14" && lib.versionOlder release_version "15") ''
+    # fix CMake error when static and LIBCXXABI_USE_LLVM_UNWINDER=ON. aren't
+    # building unwind so don't need to depend on it
+    substituteInPlace libcxx/src/CMakeLists.txt \
+      --replace-fail "add_dependencies(cxx_static unwind)" "# add_dependencies(cxx_static unwind)"
+  '') + ''
+    cd runtimes
+  '';
+} else {
+  sourceRoot = "${src'.name}/runtimes";
+}))
diff --git a/pkgs/development/compilers/llvm/git/default.nix b/pkgs/development/compilers/llvm/git/default.nix
index d578c2a290e23..1b65ce81055b5 100644
--- a/pkgs/development/compilers/llvm/git/default.nix
+++ b/pkgs/development/compilers/llvm/git/default.nix
@@ -289,14 +289,34 @@ in let
     callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake ninja libxml2 python3 release_version version monorepoSrc; });
   in {
 
-    compiler-rt-libc = callPackage ./compiler-rt {
+    compiler-rt-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        # ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic)
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
                else stdenv;
     };
 
-    compiler-rt-no-libc = callPackage ./compiler-rt {
+    compiler-rt-no-libc = callPackage ../common/compiler-rt {
+      patches = [
+        ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
+        # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
+        # extra `/`.
+        ./compiler-rt/normalize-var.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/186575
+        ../common/compiler-rt/darwin-plistbuddy-workaround.patch
+        # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
+        # ../common/compiler-rt/armv7l-15.patch
+      ];
       inherit llvm_meta;
       stdenv = if stdenv.hostPlatform.useLLVM or false
                then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
@@ -315,7 +335,11 @@ in let
     # `libcxx` requires a fairly modern C++ compiler,
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
-    libcxx = callPackage ./libcxx {
+    libcxx = callPackage ../common/libcxx {
+      patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
+        # https://github.com/llvm/llvm-project/issues/64226
+        ./libcxx/0001-darwin-10.12-mbstate_t-fix.patch
+      ];
       inherit llvm_meta;
       stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
     };
diff --git a/pkgs/development/compilers/llvm/git/libcxx/default.nix b/pkgs/development/compilers/llvm/git/libcxx/default.nix
deleted file mode 100644
index d6c304c0b476a..0000000000000
--- a/pkgs/development/compilers/llvm/git/libcxx/default.nix
+++ /dev/null
@@ -1,130 +0,0 @@
-{ lib, stdenv, llvm_meta
-, monorepoSrc, runCommand
-, cmake, lndir, ninja, python3, fixDarwinDylibNames, version
-, cxxabi ? if stdenv.hostPlatform.isFreeBSD then libcxxrt else null
-, libcxxrt, libunwind
-, enableShared ? !stdenv.hostPlatform.isStatic
-}:
-
-# external cxxabi is not supported on Darwin as the build will not link libcxx
-# properly and not re-export the cxxabi symbols into libcxx
-# https://github.com/NixOS/nixpkgs/issues/166205
-# https://github.com/NixOS/nixpkgs/issues/269548
-assert cxxabi == null || !stdenv.hostPlatform.isDarwin;
-let
-  basename = "libcxx";
-  cxxabiName = "lib${if cxxabi == null then "cxxabi" else cxxabi.libName}";
-  runtimes = [ "libcxx" ] ++ lib.optional (cxxabi == null) "libcxxabi";
-
-  # Note: useLLVM is likely false for Darwin but true under pkgsLLVM
-  useLLVM = stdenv.hostPlatform.useLLVM or false;
-
-  cxxabiCMakeFlags = [
-    "-DLIBCXXABI_USE_LLVM_UNWINDER=OFF"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    "-DLIBCXXABI_ADDITIONAL_LIBRARIES=unwind"
-    "-DLIBCXXABI_USE_COMPILER_RT=ON"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXXABI_ENABLE_THREADS=OFF"
-    "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXXABI_ENABLE_SHARED=OFF"
-  ];
-
-  cxxCMakeFlags = [
-    "-DLIBCXX_CXX_ABI=${cxxabiName}"
-  ] ++ lib.optionals (cxxabi != null) [
-    "-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${lib.getDev cxxabi}/include"
-  ] ++ lib.optionals (stdenv.hostPlatform.isMusl || stdenv.hostPlatform.isWasi) [
-    "-DLIBCXX_HAS_MUSL_LIBC=1"
-  ] ++ lib.optionals (lib.versionAtLeast version "18" && !useLLVM && stdenv.hostPlatform.libc == "glibc" && !stdenv.hostPlatform.isStatic) [
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=gcc_s"
-  ] ++ lib.optionals useLLVM [
-    "-DLIBCXX_USE_COMPILER_RT=ON"
-    # There's precedent for this in llvm-project/libcxx/cmake/caches.
-    # In a monorepo build you might do the following in the libcxxabi build:
-    #   -DLLVM_ENABLE_PROJECTS=libcxxabi;libunwinder
-    #   -DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=On
-    # libcxx appears to require unwind and doesn't pull it in via other means.
-    "-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DLIBCXX_ENABLE_THREADS=OFF"
-    "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
-    "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
-  ] ++ lib.optionals (!enableShared) [
-    "-DLIBCXX_ENABLE_SHARED=OFF"
-  ];
-
-  cmakeFlags = [
-    "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals stdenv.hostPlatform.isWasm [
-    "-DCMAKE_C_COMPILER_WORKS=ON"
-    "-DCMAKE_CXX_COMPILER_WORKS=ON"
-    "-DUNIX=ON" # Required otherwise libc++ fails to detect the correct linker
-  ] ++ cxxCMakeFlags
-    ++ lib.optionals (cxxabi == null) cxxabiCMakeFlags;
-
-in
-
-stdenv.mkDerivation rec {
-  pname = basename;
-  inherit version cmakeFlags;
-
-  src = runCommand "${pname}-src-${version}" {} (''
-    mkdir -p "$out/llvm"
-    cp -r ${monorepoSrc}/cmake "$out"
-    cp -r ${monorepoSrc}/libcxx "$out"
-    cp -r ${monorepoSrc}/llvm/cmake "$out/llvm"
-    cp -r ${monorepoSrc}/llvm/utils "$out/llvm"
-    cp -r ${monorepoSrc}/third-party "$out"
-    cp -r ${monorepoSrc}/runtimes "$out"
-  '' + lib.optionalString (cxxabi == null) ''
-    cp -r ${monorepoSrc}/libcxxabi "$out"
-  '');
-
-  outputs = [ "out" "dev" ];
-
-  patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
-    # https://github.com/llvm/llvm-project/issues/64226
-    ./0001-darwin-10.12-mbstate_t-fix.patch
-  ];
-
-  postPatch = ''
-    cd runtimes
-  '';
-
-  preConfigure = lib.optionalString stdenv.hostPlatform.isMusl ''
-    patchShebangs utils/cat_files.py
-  '';
-
-  nativeBuildInputs = [ cmake ninja python3 ]
-    ++ lib.optional stdenv.isDarwin fixDarwinDylibNames
-    ++ lib.optional (cxxabi != null) lndir;
-
-  buildInputs = [ cxxabi ]
-    ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [ libunwind ];
-
-  # libc++.so is a linker script which expands to multiple libraries,
-  # libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
-  # support linker scripts so the external cxxabi needs to be symlinked in
-  postInstall = lib.optionalString (cxxabi != null) ''
-    lndir ${lib.getDev cxxabi}/include ''${!outputDev}/include/c++/v1
-    lndir ${lib.getLib cxxabi}/lib ''${!outputLib}/lib
-  '';
-
-  passthru = {
-    isLLVM = true;
-  };
-
-  meta = llvm_meta // {
-    homepage = "https://libcxx.llvm.org/";
-    description = "C++ standard library";
-    longDescription = ''
-      libc++ is an implementation of the C++ standard library, targeting C++11,
-      C++14 and above.
-    '';
-    # "All of the code in libc++ is dual licensed under the MIT license and the
-    # UIUC License (a BSD-like license)":
-    license = with lib.licenses; [ mit ncsa ];
-  };
-}