# Maintainer: Mateusz Mikuła <oss@mateuszmikula.dev>

_compiler=clang

pkgbase=llvm
pkgname=("llvm"
         "clang"
         "clang-analyzer"
         "compiler-rt"
         "lld")
_version=20.1.8
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
pkgrel=2
pkgdesc="C language family frontend for LLVM"
arch=('i686' 'x86_64')
url="https://llvm.org/"
msys2_references=(
  "cpe: cpe:/a:llvm:llvm"
)
license=("spdx:Apache-2.0 WITH LLVM-exception")
makedepends=('cmake'
             "$_compiler"
             'ninja'
             'pkgconf'
             'python'
             'git'
             'libzstd-devel'
             'zlib-devel'
             )
_url=https://github.com/llvm/llvm-project/releases/download/${_tag}
source=("${_url}/llvm-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/clang-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/compiler-rt-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/lld-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/cmake-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/libunwind-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/runtimes-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/third-party-${pkgver}.src.tar.xz"{,.sig}
        "0001-LLVM-Cygwin-Fix-symbol-visibility-definition.patch"
        "0002-LLVM-Cygwin-Remove-special-case-for-CXX-extensions-o.patch"
        "0003-LLVM-Cygwin-Fix-shared-library-name-136599.patch"
        "0004-LLVM-Cygwin-Fix-Signals-compatibility-with-Cygwin-AP.patch"
        "0005-LLVM-TargetParser-Handle-msys-targets-the-same-as-cy.patch"
        "0006-LLVM-Cygwin-Define-_GNU_SOURCE-on-Cygwin-as-well.-13.patch"
        "0007-Cygwin-Emit-COMDAT-name-correctly-for-Cygwin-138621.patch"
        "0008-LLVM-Cygwin-add-workaround-for-blocking-connect-acce.patch"
        "0009-Support-Cygwin-Fix-handling-of-Process-symbol-lookup.patch"
        "0010-Cygwin-Define-LLVM_ABI-for-Cygwin-143222.patch"
        "0011-Cygwin-CYGWIN-is-not-WIN32-in-current-CMake-143130.patch"
        "0012-Cygwin-Don-t-use-version-script-for-Cygwin-target-14.patch"
        "0013-LLVM-Tests-remove-exeext-from-lli-child-target-param.patch"
        "0014-LLVM-Support-Cygwin-Add-threading-support-for-Cygwin.patch"
        "0015-LLVM-Cygwin-Enable-dynamic-linking-of-libLLVM-146440.patch"
        "0016-LLVM-Cygwin-Enable-conditions-that-are-shared-with-M.patch"
        "0017-llvm-objcopy-COFF-Ignore-associative-sections-in-exe.patch"
        "0018-lli-Fix-crash-with-no-process-syms-on-MinGW-151386.patch"
        "0019-llvm-Enable-building-Analysis-plugins-on-Cygwin-1513.patch"
        "0020-LLVM-adjust-lit.cfg.py-for-Cygwin-151416.patch"
        "0021-LLVM-Support-Fix-tests-on-Cygwin-151417.patch"
        "0022-LLVM-lit-add-system-cygwin-feature-152780.patch"
        "0023-hack-tablegentest.patch"
        "0101-Cygwin-Global-symbols-should-be-external-by-default-.patch"
        "0102-Clang-Cygwin-Enable-few-conditions-that-are-shared-w.patch"
        "0103-Cygwin-Enable-TLS-on-Cygwin-target-138618.patch"
        "0104-Clang-Cygwin-Fix-symbol-visibility-definition-138118.patch"
        "0105-Cygwin-RTTI-and-VTable-should-be-dllexport-ed-139798.patch"
        "0106-Clang-Cygwin-Disable-shared-libs-on-Cygwin-by-defaul.patch"
        "0107-Clang-Cygwin-Remove-erroneous-_WIN32-define-and-clea.patch"
        "0108-Clang-Cygwin-don-t-use-Bsymbolic-functions-138217.patch"
        "0109-hack-cygwin-allow-multiple-definition-in-c-index-tes.patch"
        "0110-Clang-Driver-add-a-Cygwin-ToolChain-135691.patch"
        "0111-Clang-Driver-use-__cxa_atexit-by-default-on-Cygwin.-.patch"
        "0112-Clang-CMake-use-CMakePushCheckState-138783.patch"
        "0113-CMake-respect-LLVMConfig.cmake-s-LLVM_DEFINITIONS.patch"
        "0114-Cygwin-Internal-class-in-explicitly-instantiation-de.patch"
        "0115-Cygwin-Template-instantiations-should-be-exported-by.patch"
        "0116-Clang-Cygwin-va_list-must-be-treated-like-normal-Win.patch"
        "0117-Clang-Cygwin-wint_t-is-unsigned-int-143117.patch"
        "0118-scan-build-Windows-Fix-driver-name-transformation-in.patch"
        "0119-libclang-Add-missing-dllexport-annotation-147108.patch"
        "0120-libclang-Cygwin-Use-__declspec-dllexport-for-libclan.patch"
        "0121-libclang-Cygwin-Use-LLVM_EXPORTED_SYMBOL_FILE-.def-f.patch"
        "0122-Tests-Add-system-cygwin-feature-and-use-it.-152611.patch"
        "0201-LLD-Cygwin-Implement-dll-search-prefix-143263.patch"
        "0202-LLD-COFF-Ensure-.bss-is-merged-at-the-end-of-a-secti.patch"
        "0203-LLD-COFF-add-__-data-bss-_-start-end-__-symbols-for-.patch"
        "0204-LLD-MinGW-Fall-back-to-using-default-target-if-no-m-.patch"
        "0205-LLD-CMake-fix-testing-standalone-build-without-insta.patch"
        "0206-LLD-Cygwin-Add-libcygwin-and-libmsys-2.0-to-exclude-.patch"
        )
sha256sums=('e1363888216b455184dbb8a74a347bf5612f56a3f982369e1cba6c7e0726cde1'
            'SKIP'
            'b7a1b7b0af7b9c7596af6bd46e36d11321926eaa66a7a7dc957ab0a1375ee4b0'
            'SKIP'
            '15277402f6fd63397c0917a5c7171cda82d16d226094b828c1ed0f58f73b9c69'
            'SKIP'
            '8281462d62cae67dfe5fdbb4da0d82bc62e0a86d4d4f758b0b601d44f09fd987'
            'SKIP'
            '3319203cfd1172bbac50f06fa68e318af84dcb5d65353310c0586354069d6634'
            'SKIP'
            '0bced9d701e300f8fe6599523367e214c1f928ac559afceece58f47018e9c4a7'
            'SKIP'
            '86b375ab217619d018f6875b7c71106ffab5ab21c425f414a11468c2a8262819'
            'SKIP'
            '9a4e452a8163732d417db067a89190fcda823cb3aa33199e834ac7c028923f4b'
            'SKIP'
            'b59cbff2a7c102a018a8cc773d67ac6694837bd2a8efbef2f362ef7471f7cce5'
            '17c42bd648aca828a8710ca03e47c617cc93f1827d56f4955bb3d071feb32709'
            '40c0aa890514e68cdefc321ff6a06b65e469473160c31624331477704095e418'
            'f1b2a62c40b27270f396a72ee9e80c06d17f8f36942250d68e5b002dcf438260'
            '7aa4f78e66d7492762c4c578989fe625edc620366f4fc608f5ea432117dc1f82'
            '5c9776d8aaae83607dddca8d341ea94d434f720a8ea35f15450c13812f1c045f'
            '9e07a313f9a30278f566bce045309ba2a5dc070247ba969eb3ff162dc3958648'
            '9a37d8954bacd38dbb971a9c7814955605b9e5d549d0190c641c90878f1c0349'
            '7edd21d8ad3f6a5961de07bd69ab890db2d0f68c0b8c1c033a2f6a4393211ce9'
            '2b28cb44accbcdc2baee97ff965f747ee9403414ba859e4b465aa75f06bdceec'
            '677ba2a562716183a69b868d7f75b64e9109e6fc8d99dae196964f5573e40219'
            '1070f3a7ce03c472c9e2155464c9907f8ba27fa15df707396e59c9085b49c0ab'
            '9194df20a37702827037646fa93b4169554fb050a4b80efd5b942407ac44b55a'
            '84a771685a0a3220218de157069047e1fcb8d921b14d9ecc75a0cbd912cafff7'
            '45e79dfe7dcab1202274021040f3d7637e992b56a5a895d0ccbaff60626147ad'
            'b2c5441adcd95a8b25c83aa454bf0f06af117612fe5525aede8d2b5d7eff2470'
            'af5cea6aab4c86d3c137ca21d28c5eeaec918d68a045530ab8f4b45e8506290f'
            'e62dddc3e533cd2729f4ed8f51b898eb4214f2b9db15062f818c063fab85aee0'
            '499eba68cbb2a3a50a1e9a1ba211651bd5db7cab48b5154ca3cb37a44c2b2a0c'
            'd382b9f7012df9702e9c52c04d63620a0febb607b8656bef06f8bb98e59e73b9'
            'a7a17329a247e4bdc4d9b27f860c757ae153a754b0e0ce018a8a72ff06a803a8'
            '5431019c3a6fae561b675c60545d80278d914b78e12f9ae5c59ab0ce7dc6508d'
            '7cb70e14a5c6e87ef28427356ad3d366d58c367b2039d2a3bf92fa7dee33d79d'
            'c1e29c6aa62c075975b76c38fe0d60fe765f2527611f4336d60152afd1d0be98'
            '13e4b046406ffde1af3567e204a38fd7955d09ee4c493b2f3ec6bb48b0112dcd'
            '0db6ad5d5709f8f9031f57a9d33fa7c92ab95461c82e7b0eff72eec026f5169b'
            'c12a74c2d03f9ff5f8b98e546e2a3be99f74ff6d7ee64e0a95eefb20c8b4b3b9'
            'f0ddc7573db6d57aab0fa3cac787e2f39d061bfe38b01e1ec30d1c691142b38f'
            'cc4d53a46d80a0b5e7b193b888b3da6f87e523156e977ca08fdad17b466c288e'
            'a666029b966bb3ec4035d60743c78df56f1898bbd64126ac7e191a1d35976420'
            '6b65a05a9642888009080fb0295934de7ead96366cd76a9824bc9a941318718a'
            '771d26add61c8ac4e4f501d4852346e3e6807e1781cd33955f10816a5b4244c1'
            'c625e5a8c4803c169f3de0bf2ce666a2e67f70a2e126110b365562f58d82d9f7'
            'ba70c891d3653873a64d5eff93f3958f2dd09f7fa43560fdf4b3e18c38d47e70'
            'a9c6a0dda178d8a0e5e29c68eba177c8055242159ab8dee98fca6a78546825f1'
            'b2e44bcb7014f1384bfd12e55b138e53efdf7aaafe35bb912f60a30d92a7e1cb'
            '7c3d364c3bd23097c44dd5eb3583dcaf6b8e7f6790411a61da149aad74d3e94b'
            '019d1be80860be7f1d46a9da86e28eec926b7df21da8181a3812416e08f8c59e'
            '57dd8f7e859c9baf47c7ec1f92a081cdfb5304b2edc3828b01dfcc59e29ad6ac'
            'e7d764fad2d93fc76900365ae3baa659563ac5c4f89bb2fe4bdc63fdb453d0f9'
            '497ca7968b639cead910b0b383df099f5d5539531d6a6ff0f8ecd71d7ff6cd4c'
            '103c2907fe19dc0bf1909268cee356a183ce1c9bd5b1d38a9e9de92475530b6f'
            '2956145e72166ad997261d38ecca376e4fa3485ef4eb6a2eb83db9f77cce1d24'
            'e926774591c7806d3b58cf541c88c526ec239b92f5c42fe2e02cab4b270e4cd6'
            '8d4172d88163efa7b9902b8a01e9f052bbb04374cd8b36aba3382bb82193af8f'
            'd232026664efe2c1f076909e7cb77f35d530af4613c3212cde1801ca20c54b09'
            'c383fa5f2d8824992091ea203d8236494fa5d76f2701af2c4df5dd30a49e62fa'
            'f6fd632ff804176fc1c02122a4f4a522d2c82c024db7b66bc3838b15466b5b69'
            '1f5e5fb086d7cd043e2a9d6f3965c2a8520393b5fcc4d2a354d89d6e2fcabaf1'
            '517afbd61320b240636b425c0acca67ab35b7ddaa563cc2bceeb0f909de906d3'
            'd7903409e1611f9bd00987ebdd9e81dd0cc6a2d44f87241d6e0e145f1dc9f682')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta
noextract=(clang-${pkgver}.src.tar.xz)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

revert_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Reverting ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

apply_git_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp2 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"
  plain "Extracting clang-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  tar -xJf "${srcdir}"/clang-${pkgver}.src.tar.xz -C "${srcdir}" || true

  # Rename Directories
  # We need libunwind for LLD: https://github.com/llvm/llvm-project/issues/48572
  for pkg in llvm clang compiler-rt lld cmake libunwind runtimes third-party; do
    mv ${pkg}-$pkgver.src ${pkg}
  done

  # Patch llvm
  cd "${srcdir}"/llvm
  apply_git_patch_with_msg 0001-LLVM-Cygwin-Fix-symbol-visibility-definition.patch
  apply_git_patch_with_msg 0002-LLVM-Cygwin-Remove-special-case-for-CXX-extensions-o.patch
  apply_git_patch_with_msg 0003-LLVM-Cygwin-Fix-shared-library-name-136599.patch
  apply_git_patch_with_msg 0004-LLVM-Cygwin-Fix-Signals-compatibility-with-Cygwin-AP.patch
  apply_git_patch_with_msg 0005-LLVM-TargetParser-Handle-msys-targets-the-same-as-cy.patch
  apply_git_patch_with_msg 0006-LLVM-Cygwin-Define-_GNU_SOURCE-on-Cygwin-as-well.-13.patch
  apply_git_patch_with_msg 0007-Cygwin-Emit-COMDAT-name-correctly-for-Cygwin-138621.patch
  apply_git_patch_with_msg 0008-LLVM-Cygwin-add-workaround-for-blocking-connect-acce.patch
  apply_git_patch_with_msg 0009-Support-Cygwin-Fix-handling-of-Process-symbol-lookup.patch
  apply_git_patch_with_msg 0010-Cygwin-Define-LLVM_ABI-for-Cygwin-143222.patch
  apply_git_patch_with_msg 0011-Cygwin-CYGWIN-is-not-WIN32-in-current-CMake-143130.patch
  apply_git_patch_with_msg 0012-Cygwin-Don-t-use-version-script-for-Cygwin-target-14.patch
  apply_git_patch_with_msg 0013-LLVM-Tests-remove-exeext-from-lli-child-target-param.patch
  apply_git_patch_with_msg 0014-LLVM-Support-Cygwin-Add-threading-support-for-Cygwin.patch
  apply_git_patch_with_msg 0015-LLVM-Cygwin-Enable-dynamic-linking-of-libLLVM-146440.patch
  apply_git_patch_with_msg 0016-LLVM-Cygwin-Enable-conditions-that-are-shared-with-M.patch
  apply_git_patch_with_msg 0017-llvm-objcopy-COFF-Ignore-associative-sections-in-exe.patch
  apply_git_patch_with_msg 0018-lli-Fix-crash-with-no-process-syms-on-MinGW-151386.patch
  apply_git_patch_with_msg 0019-llvm-Enable-building-Analysis-plugins-on-Cygwin-1513.patch
  apply_git_patch_with_msg 0020-LLVM-adjust-lit.cfg.py-for-Cygwin-151416.patch
  apply_git_patch_with_msg 0021-LLVM-Support-Fix-tests-on-Cygwin-151417.patch
  apply_git_patch_with_msg 0022-LLVM-lit-add-system-cygwin-feature-152780.patch
  apply_git_patch_with_msg 0023-hack-tablegentest.patch

  # Patch clang
  cd "${srcdir}"/clang
  apply_git_patch_with_msg 0101-Cygwin-Global-symbols-should-be-external-by-default-.patch
  apply_git_patch_with_msg 0102-Clang-Cygwin-Enable-few-conditions-that-are-shared-w.patch
  apply_git_patch_with_msg 0103-Cygwin-Enable-TLS-on-Cygwin-target-138618.patch
  apply_git_patch_with_msg 0104-Clang-Cygwin-Fix-symbol-visibility-definition-138118.patch
  apply_git_patch_with_msg 0105-Cygwin-RTTI-and-VTable-should-be-dllexport-ed-139798.patch
  apply_git_patch_with_msg 0106-Clang-Cygwin-Disable-shared-libs-on-Cygwin-by-defaul.patch
  apply_git_patch_with_msg 0107-Clang-Cygwin-Remove-erroneous-_WIN32-define-and-clea.patch
  apply_git_patch_with_msg 0108-Clang-Cygwin-don-t-use-Bsymbolic-functions-138217.patch
  apply_git_patch_with_msg 0109-hack-cygwin-allow-multiple-definition-in-c-index-tes.patch
  apply_git_patch_with_msg 0110-Clang-Driver-add-a-Cygwin-ToolChain-135691.patch
  apply_git_patch_with_msg 0111-Clang-Driver-use-__cxa_atexit-by-default-on-Cygwin.-.patch
  apply_git_patch_with_msg 0112-Clang-CMake-use-CMakePushCheckState-138783.patch
  apply_git_patch_with_msg 0113-CMake-respect-LLVMConfig.cmake-s-LLVM_DEFINITIONS.patch
  apply_git_patch_with_msg 0114-Cygwin-Internal-class-in-explicitly-instantiation-de.patch
  apply_git_patch_with_msg 0115-Cygwin-Template-instantiations-should-be-exported-by.patch
  apply_git_patch_with_msg 0116-Clang-Cygwin-va_list-must-be-treated-like-normal-Win.patch
  apply_git_patch_with_msg 0117-Clang-Cygwin-wint_t-is-unsigned-int-143117.patch
  apply_git_patch_with_msg 0118-scan-build-Windows-Fix-driver-name-transformation-in.patch
  apply_git_patch_with_msg 0119-libclang-Add-missing-dllexport-annotation-147108.patch
  apply_git_patch_with_msg 0120-libclang-Cygwin-Use-__declspec-dllexport-for-libclan.patch
  apply_git_patch_with_msg 0121-libclang-Cygwin-Use-LLVM_EXPORTED_SYMBOL_FILE-.def-f.patch
  apply_git_patch_with_msg 0122-Tests-Add-system-cygwin-feature-and-use-it.-152611.patch


  # Patch lld
  cd "${srcdir}"/lld
  apply_git_patch_with_msg 0201-LLD-Cygwin-Implement-dll-search-prefix-143263.patch
  apply_git_patch_with_msg 0202-LLD-COFF-Ensure-.bss-is-merged-at-the-end-of-a-secti.patch
  apply_git_patch_with_msg 0203-LLD-COFF-add-__-data-bss-_-start-end-__-symbols-for-.patch
  apply_git_patch_with_msg 0204-LLD-MinGW-Fall-back-to-using-default-target-if-no-m-.patch
  apply_git_patch_with_msg 0205-LLD-CMake-fix-testing-standalone-build-without-insta.patch
  apply_git_patch_with_msg 0206-LLD-Cygwin-Add-libcygwin-and-libmsys-2.0-to-exclude-.patch
}

build() {
  local -a platform_config
  local -a common_cmake_args

  if check_option "debug" "y"; then
    common_cmake_args+=(-DCMAKE_BUILD_TYPE=Debug
                        -DLLVM_ENABLE_ASSERTIONS=ON)
    VERBOSE="VERBOSE=1"
  else
    common_cmake_args+=(-DCMAKE_BUILD_TYPE=Release
                        -DLLVM_ENABLE_ASSERTIONS=OFF
                        -DLLVM_ENABLE_DUMP=ON)
  fi
  common_cmake_args+=(-Wno-dev
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCOMPILER_RT_BUILD_ORC=OFF
    -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
    -DCOMPILER_RT_INCLUDE_TESTS=OFF
    -DCOMPILER_RT_OS_DIR=cygwin
    -DLLVM_DEFAULT_TARGET_TRIPLE=${CARCH}-pc-windows-cygnus
    -DLLVM_ENABLE_THREADS=ON
    -DLLVM_HOST_TRIPLE=${CARCH}-pc-windows-cygnus
    -DLLVM_INCLUDE_BENCHMARKS=OFF
    -DLLVM_INCLUDE_TESTS=ON
    -DLLVM_USE_SYMLINKS=OFF
    -DPython3_EXECUTABLE=/usr/bin/python.exe
  )

  if [ "${_compiler}" == "gcc" ]; then
    common_cmake_args+=(
      -DLIBCLANG_BUILD_STATIC=ON
      -DLLVM_ENABLE_PIC=OFF
    )
  elif [ "${_compiler}" == "clang" ]; then
    export CC='clang'
    export CXX='clang++'
    common_cmake_args+=(
      -DLLVM_BUILD_LLVM_DYLIB=ON
      -DLLVM_LINK_LLVM_DYLIB=ON
    )
  else
    error "Unsupported compiler: $_compiler"
    exit 1
  fi

  local _projects="clang;lld"

  platform_config+=(-DLLVM_TARGETS_TO_BUILD="AArch64;ARM;X86")

  cmake \
    -GNinja \
    -B build \
    -DLLVM_ENABLE_PROJECTS="${_projects}" \
    -DLLVM_ENABLE_RUNTIMES="compiler-rt" \
    -DLLVM_ENABLE_RTTI=ON \
    -DLLD_DEFAULT_LD_LLD_IS_MINGW=ON \
    "${common_cmake_args[@]}" \
    "${platform_config[@]}" \
    llvm

  cmake --build build
}

check() {
  cd "${srcdir}/build"
  MSYS=winsymlinks:sys ln -sf msys-clang-20.1.dll bin/libclang.so
  MSYS=winsymlinks:sys PATH="${srcdir}/build/bin:$PATH" ninja -k0 check-all || true
}

package_clang() {
  pkgdesc="C language family frontend for LLVM"
  url="https://clang.llvm.org/"
  depends=("gcc" "llvm=${pkgver}")
  optdepends=("compiler-rt: for -rtlib=compiler-rt")

  # Disable automatic installation of components that go into subpackages
  # -i.orig to check what has been removed in-case it starts dropping more than it should
  sed -i.orig '/\(extra\|scan-build\|scan-build-py\|scan-view\)\/cmake_install.cmake/d' "${srcdir}"/build/tools/clang/tools/cmake_install.cmake
  DESTDIR="${pkgdir}" cmake --install "${srcdir}"/build/tools/clang

  install -Dm644 "${srcdir}"/clang/LICENSE.TXT "${pkgdir}"/usr/share/licenses/clang/LICENSE

  # Install Python bindings
  site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "${pkgdir}"/${site_packages}
  cp -a "${srcdir}"/clang/bindings/python/clang "${pkgdir}"/${site_packages}
  python -m compileall -o 0 -o 1 "${pkgdir}"/${site_packages}
}

package_clang-analyzer() {
  pkgdesc="A source code analysis framework"
  url="https://clang-analyzer.llvm.org/"
  depends=("clang=${pkgver}" "python")

  local _analyzer
  for _analyzer in scan-build scan-build-py scan-view; do
    DESTDIR="${pkgdir}" cmake --install "${srcdir}"/build/tools/clang/tools/${_analyzer}
  done

  # Compile Python scripts
  python -m compileall -o 0 -o 1 "${pkgdir}"/usr/lib/libscanbuild

  install -Dm644 "${srcdir}"/clang/LICENSE.TXT "${pkgdir}"/usr/share/licenses/clang-analyzer/LICENSE
}

package_compiler-rt() {
  pkgdesc="Runtime libraries for Clang and LLVM"
  url="https://compiler-rt.llvm.org/"
  depends=("gcc-libs")

  DESTDIR="${pkgdir}" cmake --install "${srcdir}"/build/runtimes/builtins-bins
  DESTDIR="${pkgdir}" cmake --install "${srcdir}"/build/runtimes/runtimes-bins

  install -Dm644 "${srcdir}"/compiler-rt/LICENSE.TXT "${pkgdir}"/usr/share/licenses/compiler-rt/LICENSE
}

package_lld() {
  pkgdesc="Linker tools for LLVM"
  url="https://lld.llvm.org/"
  depends=("gcc-libs" "llvm=${pkgver}")

  DESTDIR="${pkgdir}" cmake --install "${srcdir}"/build/tools/lld

  install -Dm644 "${srcdir}"/lld/LICENSE.TXT "${pkgdir}"/usr/share/licenses/lld/LICENSE
}

package_llvm() {
  pkgdesc="Low Level Virtual Machine"
  depends=("gcc-libs" "libzstd" "zlib")

  # Disable automatic installation of components that go into subpackages
  # -i.orig to check what has been removed in-case it starts dropping more than it should
  sed -i.orig '/\(clang\|lld\)\/cmake_install.cmake/d' "${srcdir}"/build/tools/cmake_install.cmake
  sed -i.orig '/\(builtins\|runtimes\)-bins\/\+cmake_install.cmake/d' "${srcdir}"/build/runtimes/cmake_install.cmake
  DESTDIR="${pkgdir}" cmake --install "${srcdir}"/build

  install -Dm644 "${srcdir}"/llvm/LICENSE.TXT "${pkgdir}"/usr/share/licenses/llvm/LICENSE

  # Install CMake stuff
  install -d "${pkgdir}"/usr/share/llvm/cmake/{modules,platforms}
  install -Dm644 "${srcdir}"/llvm/cmake/modules/*.cmake "${pkgdir}"/usr/share/llvm/cmake/modules/
  install -Dm644 "${srcdir}"/llvm/cmake/platforms/*.cmake "${pkgdir}"/usr/share/llvm/cmake/platforms/

  # fix cmake files.
  sed -e "s|${srcdir}/build|/usr|g" -i ${pkgdir}/usr/lib/cmake/llvm/LLVMConfig.cmake
}
