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

_compiler=clang

pkgbase=llvm
pkgname=("llvm"
         "clang"
         "clang-analyzer"
         "compiler-rt"
         "lld")
_version=20.1.3
_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}
        "0001-LLVM-Cygwin-Fix-symbol-visibility-definition.patch"
        "0002-undef-i386.patch"
        "0003-LLVM-Cygwin-Fix-llvm-config-shared-library-names.patch"
        "0004-LLVM-Cygwin-Fix-Signals-compatibility-with-Cygwin-AP.patch"
        "0102-Clang-Cygwin-Enable-few-conditions-that-are-shared-w.patch"
        "0103-Clang-Cygwin-Enable-TLS.patch"
        "0104-Clang-Cygwin-Fix-symbol-visibility-definition.patch"
        "0106-disable-shared-libs-on-Cygwin-by-default.patch"
        "0107-Clang-Cygwin-Remove-erroneous-define.patch"
        "0108-Clang-Cygwin-dont-use-Bsymbolic-functions.patch"
        "0109-hack-cygwin-allow-multiple-definition-c-index-test.patch"
        "0110-Clang-Driver-add-a-Cygwin-ToolChain.patch"
        "0111-Clang-Driver-use-__cxa_atexit-by-default-on-Cygwin.patch"
        "0112-hack-cygwin-define-_GLIBCXX_USE_CXX11_ABI-to-1-if-no.patch"
        "0201-LLD-MinGW-Implement-dll-search-prefix-option.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"
        )
sha256sums=('e5dc9b9d842c5f79080f67860a084077e163430de1e2cd3a74e8bee86e186751'
            'SKIP'
            '3cddfd12c81a64d2e6036478417e0314278aec3a76e1d197c6fa444a07ed6bfc'
            'SKIP'
            '74ba10db2c9e8938cd7c77f4e4d4fea609d116d7b0eaaf3ef8e8c5db19c0d301'
            'SKIP'
            '4e084130cc51a8b900737cec40578eab867f7e8017121409940d96550784ff12'
            'SKIP'
            'd5423ec180f14df6041058a2b66e321e4420499e111235577e09515a38a03451'
            'SKIP'
            '8397002dd595af572c1b6e3b4276e44a8c273fddc714bb4b2976540db73c5f9c'
            'SKIP'
            '6394d8f111288c918421756037c10c4f8471325992e37c44cf8811b4e3dde00b'
            'SKIP'
            '68053c76fc82309a8a2c6f91ee9344f40d943f5a3078681c9d76388cc901b66b'
            '233010fa91dc741ef6a7fd8b864f92766d37b3ce50c5d39811ca883ea6d1d859'
            '834ed176bc5bad1dabde4558aa38e768b6d454f82081f1cdedb44163f0962be6'
            '6bf8f75ac82b60d13198e206c0cf1bac7cdf1a84dc1db0a86d7686f1c0f346f1'
            'a73568d353fd27ff7cae0bdddbced0d0119274a465befe8a0bda6cae4721da0b'
            'f93d3b3227ee5958880d070451f92129c62a645e6cdc94bac9a13fa0144b3e23'
            'f489cec6ee4a7dcfbe90be4d58fa853d0dfab893d024a8e878074dfab1fd4bfd'
            '312ee451a8f67d3fbc5e723a93a6ad311a6ba95942d32bb8757ba7d2840b53a9'
            'a6d9beedc80cc22bc2e41c2ab101cf519ac7013ec66912b27953224ec48bc1e8'
            'e85fe8c8bb079b2b153ff50254576816196957361c97181ab58883321d02c2bb'
            'e153bc3904747a16b547816d37d7cbd2fc103cb47d694644540202af9b3de6c6'
            '5ee936d5050b5376ba3026578a2e43319fda477c42a091aa66d57c8980638e4f'
            '5a383313e4a2432471b0cd2946786eb3bafd9f6dfc5f2ff005c594b77a82470d'
            '4587f6bd019e69df04fab9b7ba6c9245cd1a26167ce2557530eb185d495f3717'
            '92edbcd4cb2c9d6706a31bd64c25865931025e3c430d23c16900ac39a408785f'
            'e94efca8f2336bbd76265847dc449dcc537b8c6a4f740473884bc70249ca8b02'
            '9df6ecfdd5a18a875442f529fbe08a86eae07986557b0633c3c01404cf6ba3ea')
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; 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-undef-i386.patch
  apply_git_patch_with_msg 0003-LLVM-Cygwin-Fix-llvm-config-shared-library-names.patch
  apply_git_patch_with_msg 0004-LLVM-Cygwin-Fix-Signals-compatibility-with-Cygwin-AP.patch

  # Patch clang
  cd "${srcdir}"/clang
  apply_git_patch_with_msg 0102-Clang-Cygwin-Enable-few-conditions-that-are-shared-w.patch
  apply_git_patch_with_msg 0103-Clang-Cygwin-Enable-TLS.patch
  apply_git_patch_with_msg 0104-Clang-Cygwin-Fix-symbol-visibility-definition.patch
  apply_git_patch_with_msg 0106-disable-shared-libs-on-Cygwin-by-default.patch
  apply_git_patch_with_msg 0107-Clang-Cygwin-Remove-erroneous-define.patch
  apply_git_patch_with_msg 0108-Clang-Cygwin-dont-use-Bsymbolic-functions.patch
  apply_git_patch_with_msg 0109-hack-cygwin-allow-multiple-definition-c-index-test.patch
  apply_git_patch_with_msg 0110-Clang-Driver-add-a-Cygwin-ToolChain.patch
  apply_git_patch_with_msg 0111-Clang-Driver-use-__cxa_atexit-by-default-on-Cygwin.patch
  apply_git_patch_with_msg 0112-hack-cygwin-define-_GLIBCXX_USE_CXX11_ABI-to-1-if-no.patch

  # Patch lld
  cd "${srcdir}"/lld
  apply_git_patch_with_msg 0201-LLD-MinGW-Implement-dll-search-prefix-option.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
}

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
    -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=OFF
    -DLLVM_USE_SYMLINKS=OFF
    -DPython3_EXECUTABLE=/usr/bin/python.exe
  )

  if [ "${_compiler}" == "gcc" ]; then
    common_cmake_args+=(
      -DLIBCLANG_BUILD_STATIC=ON
    )
  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")

  # Unlike Linux, Cygwin does not define it OOTB
  CFLAGS+=" -D_GNU_SOURCE=1"
  CXXFLAGS+=" -D_GNU_SOURCE=1"
  CPPFLAGS+=" -D_GNU_SOURCE=1"

  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
}

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
}
