# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Yuui Tanabe <yuuitanabe@163.com>
# Contributor: Oscar Fuentes <ofv@wanadoo.es>
# Contributor: Adrian Pop <adrian.pop@liu.se>


# We can switch to clang when it fully works with mingw-w64
_compiler=gcc
_generator="MSYS Makefiles"
#_generator="Ninja"
if [ "${_generator}" = "Ninja" ]; then
  _make_cmd="ninja"
else
  _make_cmd="make"
fi
_realname=clang
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-clang-analyzer"
         "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
         "${MINGW_PACKAGE_PREFIX}-compiler-rt"
         "${MINGW_PACKAGE_PREFIX}-libc++"
         "${MINGW_PACKAGE_PREFIX}-libc++abi"
         "${MINGW_PACKAGE_PREFIX}-libunwind"
         "${MINGW_PACKAGE_PREFIX}-lld"
         "${MINGW_PACKAGE_PREFIX}-lldb"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-openmp"
         "${MINGW_PACKAGE_PREFIX}-polly")
pkgver=10.0.0
pkgrel=3
pkgdesc="C language family frontend for LLVM (mingw-w64)"
arch=('any')
url="https://llvm.org/"
license=("custom:University of Illinois/NCSA Open Source License")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake>=3.4.3"
             "${MINGW_PACKAGE_PREFIX}-z3"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "tar"
             "groff"
             $([[ "$_compiler" == "clang" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-clang")
             $([[ "$_generator" == "Ninja" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-ninja")
             )
depends=("${MINGW_PACKAGE_PREFIX}-gcc"
         "${MINGW_PACKAGE_PREFIX}-uasm")
options=('!debug' 'strip')
#_url=https://releases.llvm.org
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver}
source=(${_url}/llvm-${pkgver}.src.tar.xz{,.sig}
        ${_url}/openmp-${pkgver}.src.tar.xz{,.sig}
        ${_url}/clang-${pkgver}.src.tar.xz{,.sig}
        ${_url}/compiler-rt-${pkgver}.src.tar.xz{,.sig}
        ${_url}/test-suite-${pkgver}.src.tar.xz{,.sig}
        ${_url}/libcxx-${pkgver}.src.tar.xz{,.sig}
        ${_url}/libcxxabi-${pkgver}.src.tar.xz{,.sig}
        ${_url}/clang-tools-extra-${pkgver}.src.tar.xz{,.sig}
        ${_url}/lld-${pkgver}.src.tar.xz{,.sig}
        ${_url}/lldb-${pkgver}.src.tar.xz{,.sig}
        ${_url}/libunwind-${pkgver}.src.tar.xz{,.sig}
        ${_url}/polly-${pkgver}.src.tar.xz{,.sig}
        "0002-Fix-GetHostTriple-for-mingw-w64-in-msys.patch"
        "0004-llvm-config-look-for-unversioned-shared-lib-on-win32.patch"
        "0005-add-pthread-as-system-lib-for-mingw.patch"
        "0006-add-coff-exported-flag.patch"
        "0007-getLoadAddressWithOffset-assertion.patch"
        "0008-enable-emutls-for-mingw.patch"
        "0009-empty-target-prefix-only-msvc.patch"
        "0010-mbig-obj-for-all.patch"
        "0012-fix-testplugin-linking.patch"
        "0013-Add-Z3-to-system-libraries-list-if-enabled.patch"
        "0101-Allow-build-static-clang-library-for-mingw.patch"
        "0102-fix-libclang-name-for-mingw.patch"
        "0104-link-pthread-with-mingw.patch"
        "0105-build-libclang-cpp-fix.patch"
        "0106-fix-libunwind-extension.patch"
        "0301-Fix-def-file-exporting-of-symbols-containing-periods.patch"
        "0502-Add-missing-declarations-for-linking-to-psapi.patch"
        "0503-lldb-find-mingw-python.patch"
        "0600-unwind-rename-__personality_routine-to-_Unwind_Personality_Fn.patch"
        "0504-fix-lldb-library-name-mingw.patch"
        "0801-Don-t-build-LLVMPolly-on-WIN32.patch"
        "0901-openmp-kpm-lock.patch"
        "0902-libomp-weak-mingw.patch"
        "0903-Fix-implib-for-MinGW.patch")
# Some patch notes :)
#0001-0099 -> llvm
#0101-0199 -> clang
#0201-0299 -> rt
#0301-0399 -> lld
#0401-0499 -> libc++
#0501-0599 -> lldb
#0601-0699 -> libunwind
#0701-0799 -> libc++abi
#0801-0899 -> polly
#0901-0999 -> openmp
sha256sums=('df83a44b3a9a71029049ec101fb0077ecbbdf5fe41e395215025779099a98fdf'
            'SKIP'
            '3b9ff29a45d0509a1e9667a0feb43538ef402ea8cfc7df3758a01f20df08adfa'
            'SKIP'
            '885b062b00e903df72631c5f98b9579ed1ed2790f74e5646b4234fa084eacb21'
            'SKIP'
            '6a7da64d3a0a7320577b68b9ca4933bdcab676e898b759850e827333c3282c75'
            'SKIP'
            '2bcf0f76263572bf45bf7c552e3eae886ec2f315bbc529f3a68603bfc1d13b33'
            'SKIP'
            '270f8a3f176f1981b0f6ab8aa556720988872ec2b48ed3b605d0ced8d09156c7'
            'SKIP'
            'e71bac75a88c9dde455ad3f2a2b449bf745eafd41d2d8432253b2964e0ca14e1'
            'SKIP'
            'acdf8cf6574b40e6b1dabc93e76debb84a9feb6f22970126b04d4ba18b92911c'
            'SKIP'
            'b9a0d7c576eeef05bc06d6e954938a01c5396cee1d1e985891e0b1cf16e3d708'
            'SKIP'
            'dd1ffcb42ed033f5167089ec4c6ebe84fbca1db4a9eaebf5c614af09d89eb135'
            'SKIP'
            '09dc5ecc4714809ecf62908ae8fe8635ab476880455287036a2730966833c626'
            'SKIP'
            '35fba6ed628896fe529be4c10407f1b1c8a7264d40c76bced212180e701b4d97'
            'SKIP'
            '9b6d3ecb0ef4a38d34aefaefff8c6257ff22d366d84630020d7f079dc8065d97'
            '1f318c0370357fdf9c54ae6d31bad761b0caa58ac099998937b636309ecb6590'
            '7f0c64cd87b61e894be632f180ae5291e1aa9f1d9d382608f659067eeeda7146'
            '2c2431997e664c5b42b359f4134a4773578753e5e25c505ba30de42d357f3057'
            '1887ea21fcd591a50dd10559774e872ce1183177e672b028133d9b669e3cac32'
            'e1c5f6fa15d6ce9af24dde2b038f0c2fb26a18f2f1200c65405adca6f8d15a6a'
            '33400d16d5f6671a8fd60345c3ae44b9777a7d600061957889d14305eb2ad709'
            '1c9efccd40a0e7834c3aa9d819aa25cfdd2cec389d1bd3e8a89bc9ff670a0129'
            'c486e1d45f6fb2a24823d776d2b47d6930530d423df73cc635f863265210c294'
            'a2f0e8090a9450abe51e43ba9e9c8ff4b2a4024ba394deea4b8aff547e30fc33'
            'b2e5fd29bf666998d495a321690c97fe239bb998abcf0739418ac0ea725fecd6'
            '01b029f2a21bd998ce374a90d41d214c891dfbb611dfbd9ca147517cd2c228ea'
            '53646dd01af2862473e9719c5223366486268891ccbff86413943a432a8342e9'
            'a60f7328d84628a56a9f626e4dc26ffd0c35292c79eeba62ac3d4f25aef2fe5c'
            'ab28a1f6afe2cbd3849da9db12346a3ecdcfa6ca3bd546963c3ac6b874b7b33b'
            'b0958bfb21d011e60f93f943dc616a362b4fbe2ebd4c38955cdc97389a88b5ab'
            '6ee9d0bd6abe0a3642bb3078fc285b56f945ba6443a9d305d3f54f1c63f0672a'
            'd1021748be52cfd447cbfff5f1bfcbb540f7d9e80af7c1bb3f2cc4c0c3a79eab'
            '12939edc95f503ad03a19355c34a617af645b9731e32b8de7c70054241f1aa96'
            'bed7ae642486a8fd0db5afb0b9afcd8a87605de318ad891bf4c9378a2eb09dc2'
            'bc394e597f8939b6f6630bd88c990f951738aaadacded2f3be71c658e9608fe7'
            'db8dc554f7780609ee4eb4bc9e3817d4a9409b622f6471c8db06c92705805ead'
            'f7958638e73273ea6a8df4a4e8726a0904d049fdaea7e84c33db5dec1af08fa8'
            'a633c38149772f90cc34f83b144185746b5a47f7d3d10a97cb060c31d140ab7e')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A') # Tom Stellard
noextract=(clang-${pkgver}.src.tar.xz
           libcxx-${pkgver}.src.tar.xz
           lldb-${pkgver}.src.tar.xz
           test-suite-${pkgver}.src.tar.xz)

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

prepare() {
  plain "Extracting clang-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/clang-${pkgver} ]] && rm -rf ${srcdir}/clang-${pkgver}
  [[ -d ${srcdir}/clang-${pkgver} ]] || tar -xJvf ${srcdir}/clang-${pkgver}.src.tar.xz -C ${srcdir} || true

  plain "Extracting libcxx-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/libcxx-${pkgver} ]] && rm -rf ${srcdir}/libcxx-${pkgver}
  [[ -d ${srcdir}/libcxx-${pkgver} ]] || tar -xJvf ${srcdir}/libcxx-${pkgver}.src.tar.xz -C ${srcdir} || true

  plain "Extracting lldb-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/lldb-${pkgver} ]] && rm -rf ${srcdir}/lldb-${pkgver}
  [[ -d ${srcdir}/lldb-${pkgver} ]] || tar -xJvf ${srcdir}/lldb-${pkgver}.src.tar.xz -C ${srcdir} || true

  cd "${srcdir}/llvm-${pkgver}.src"
  apply_patch_with_msg \
      "0002-Fix-GetHostTriple-for-mingw-w64-in-msys.patch" \
      "0004-llvm-config-look-for-unversioned-shared-lib-on-win32.patch" \
      "0005-add-pthread-as-system-lib-for-mingw.patch" \
      "0008-enable-emutls-for-mingw.patch" \
      "0009-empty-target-prefix-only-msvc.patch" \
      "0010-mbig-obj-for-all.patch" \
      "0012-fix-testplugin-linking.patch" \
      "0013-Add-Z3-to-system-libraries-list-if-enabled.patch"

  # https://bugs.llvm.org/show_bug.cgi?id=25493
  apply_patch_with_msg \
      "0006-add-coff-exported-flag.patch"
  # https://bugs.llvm.org/show_bug.cgi?id=30584
  apply_patch_with_msg \
      "0007-getLoadAddressWithOffset-assertion.patch"

  cd "${srcdir}/clang-${pkgver}.src"
  apply_patch_with_msg \
      "0101-Allow-build-static-clang-library-for-mingw.patch" \
      "0102-fix-libclang-name-for-mingw.patch" \
      "0104-link-pthread-with-mingw.patch" \
      "0105-build-libclang-cpp-fix.patch" \
      "0106-fix-libunwind-extension.patch"

  cd "${srcdir}/lld-${pkgver}.src"
  apply_patch_with_msg \
      "0301-Fix-def-file-exporting-of-symbols-containing-periods.patch"

  cd "${srcdir}/lldb-${pkgver}.src"
  apply_patch_with_msg \
      "0502-Add-missing-declarations-for-linking-to-psapi.patch" \
      "0503-lldb-find-mingw-python.patch" \
      "0504-fix-lldb-library-name-mingw.patch"

  cd "${srcdir}/libunwind-${pkgver}.src"
  apply_patch_with_msg \
      "0600-unwind-rename-__personality_routine-to-_Unwind_Personality_Fn.patch" \

  cd "${srcdir}/polly-${pkgver}.src"
  # With LLVM_LINK_LLVM_DYLIB=ON it's build before LLVM and fails with undefined references to LLVM functions
  apply_patch_with_msg \
      "0801-Don-t-build-LLVMPolly-on-WIN32.patch"

  cd "${srcdir}/openmp-${pkgver}.src"
  apply_patch_with_msg \
      "0901-openmp-kpm-lock.patch" \
      "0902-libomp-weak-mingw.patch" \
      "0903-Fix-implib-for-MinGW.patch"

  # At the present, clang must reside inside the LLVM source code tree to build
  # See https://bugs.llvm.org/show_bug.cgi?id=4840

  cd "${srcdir}"
  rm -rf clang clang-tools-extra lld lldb polly compiler-rt libcxx \
         libcxxabi libunwind openmp test-suite | true
  mv "${srcdir}/clang-${pkgver}.src"             clang
  mv "${srcdir}/clang-tools-extra-${pkgver}.src" clang-tools-extra
  mv "${srcdir}/lld-${pkgver}.src"               lld
  mv "${srcdir}/lldb-${pkgver}.src"              lldb
  mv "${srcdir}/polly-${pkgver}.src"             polly
  mv "${srcdir}/compiler-rt-${pkgver}.src"       compiler-rt
  mv "${srcdir}/libcxxabi-${pkgver}.src"         libcxxabi
  mv "${srcdir}/libcxx-${pkgver}.src"            libcxx
  mv "${srcdir}/libunwind-${pkgver}.src"         libunwind
  mv "${srcdir}/openmp-${pkgver}.src"            openmp
  #mv "${srcdir}/testsuite-${pkgver}.src"         test-suite
}

build() {
  cd "${srcdir}"

  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH} && cd build-${CARCH}

  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
    VERBOSE="VERBOSE=1"
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  # Include location of libffi headers in CPPFLAGS
  FFI_INCLUDE_DIR="$(pkg-config --cflags libffi)"
  FFI_INCLUDE_DIR=$(echo $FFI_INCLUDE_DIR | sed 's|-I||g')

  # "Ninja" cant install each component seperately
  # https://github.com/martine/ninja/issues/932

  if [ "${_compiler}" == "clang" ]; then
    #export CC='clang -stdlib=libc++'
    #export CXX='clang++ -stdlib=libc++'
    export CC='clang'
    export CXX='clang++'
  fi

  export ASM_COMPILER="${MINGW_PREFIX}/bin/uasm.exe"
  if [ "${CARCH}" == "x86_64" ]; then
    export ASM_COMPILER_FLAGS=-win64
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"$_generator" \
    -DCLANG_ANALYZER_BUILD_Z3=ON \
    -DCMAKE_ASM_MASM_COMPILER="${ASM_COMPILER}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DFFI_INCLUDE_DIR="${FFI_INCLUDE_DIR}" \
    -DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON \
    -DLIBCLANG_BUILD_STATIC=ON \
    -DLIBOMP_ASMFLAGS="${ASM_COMPILER_FLAGS}" \
    -DLIBOMP_FORTRAN_MODULES=ON \
    -DLLDB_ENABLE_LIBEDIT=OFF \
    -DLLDB_RELOCATABLE_PYTHON=ON \
    -DLLDB_USE_SYSTEM_SIX=ON \
    -DLLVM_BUILD_LLVM_DYLIB=ON \
    -DLLVM_BUILD_STATIC=OFF \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;compiler-rt;lld;lldb;openmp;polly" \
    -DLLVM_ENABLE_SPHINX=ON \
    -DLLVM_ENABLE_THREADS=ON \
    -DLLVM_INCLUDE_EXAMPLES=OFF \
    -DLLVM_LINK_LLVM_DYLIB=OFF \
    -DLLVM_POLLY_LINK_INTO_TOOLS=OFF \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    "${extra_config[@]}" \
    ../llvm-${pkgver}.src

  # sed away the bad windows style command line parameters (they are translated by msys from /bad to c:\msys\bad
  sed -i.orig 's/\/c  \/Fo/-c -Fo/' projects/openmp/runtime/src/CMakeFiles/omp.dir/build.make
  sed -i.orig 's/\/safeseh \/coff/-safeseh -coff/' projects/openmp/runtime/src/CMakeFiles/omp.dir/build.make

  ${_make_cmd}

  # 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\|lldb\|polly\)\/cmake_install.cmake/d' tools/cmake_install.cmake
  sed -i.orig '/\(extra\|scan-build\|scan-view\)\/cmake_install.cmake/d' tools/clang/tools/cmake_install.cmake
  sed -i.orig '/\(compiler-rt\|libcxxabi\|libcxx\|openmp\|libunwind\)\/cmake_install.cmake/d' projects/cmake_install.cmake

  # Use newly built compiler because GCC/binutils doesn't play nicely
  export CC="${srcdir}/build-${CARCH}/bin/clang"
  export CXX="${srcdir}/build-${CARCH}/bin/clang++"
  # Force win32 threads for libc++abi
  export CXXFLAGS="${CXXFLAGS} -D_LIBCPP_HAS_THREAD_API_WIN32"
  COMMON_CMAKE_ARGS=(-DCMAKE_AR=${srcdir}/build-${CARCH}/bin/llvm-ar
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}
    -DCMAKE_RANLIB=${srcdir}/build-${CARCH}/bin/llvm-ranlib
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib
    -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=ON
    -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON
    -DLIBCXX_HAS_WIN32_THREAD_API=ON
    -DLIBCXX_USE_COMPILER_RT=ON
    -DLIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS=OFF
    -DLIBCXXABI_ENABLE_SHARED=OFF
    -DLIBCXXABI_ENABLE_STATIC=ON
    -DLIBCXXABI_USE_COMPILER_RT=ON
    -DLIBCXXABI_USE_LLVM_UNWINDER=ON
    -DLIBUNWIND_ENABLE_SHARED=ON
    -DLIBUNWIND_USE_COMPILER_RT=ON
    -DLLVM_ENABLE_LLD=ON
    -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi;libunwind"
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe)

  cd "${srcdir}"

  [[ -d build-libcxx-shared-${CARCH} ]] && rm -rf build-libcxx-shared-${CARCH}
  mkdir build-libcxx-shared-${CARCH} && cd build-libcxx-shared-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"$_generator" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} -D_LIBCPP_BUILDING_LIBRARY -U_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS" \
    -DLIBCXX_ENABLE_SHARED=ON \
    -DLIBCXX_ENABLE_STATIC=OFF \
    -DLIBUNWIND_ENABLE_STATIC=OFF \
    "${COMMON_CMAKE_ARGS[@]}" \
    ${extra_config[@]} \
    ../llvm-${pkgver}.src

  ${_make_cmd} cxx

  cd "${srcdir}"

  [[ -d build-libcxx-static-${CARCH} ]] && rm -rf build-libcxx-static-${CARCH}
  mkdir build-libcxx-static-${CARCH} && cd build-libcxx-static-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"$_generator" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS" \
    -DLIBCXX_ENABLE_SHARED=OFF \
    -DLIBCXX_ENABLE_STATIC=ON \
    -DLIBUNWIND_ENABLE_STATIC=ON \
    "${COMMON_CMAKE_ARGS[@]}" \
    ${extra_config[@]} \
    ../llvm-${pkgver}.src

  ${_make_cmd} unwind cxxabi cxx
}

#check() {
#  cd "${srcdir}"/build-${CARCH}
#  # Remove || true once testcase doesn't cause failures.
#  make check || true
#  make check-clang || true
#  make check-polly || true
#}

package_clang() {
  pkgdesc="C language family frontend for LLVM (mingw-w64)"
  url="https://clang.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gcc"
           "${MINGW_PACKAGE_PREFIX}-z3")

  cd "${srcdir}/clang"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang DESTDIR="${pkgdir}" install

  # Install static libraries
  install -Dm644 ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/libclang.a
  install -Dm644 ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/libclang_static.a
}

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

  cd "${srcdir}/clang"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/scan-build DESTDIR="${pkgdir}" install
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/scan-view DESTDIR="${pkgdir}" install

  # Use Python 2
  sed -i \
    -e 's|env python$|&2|' \
    -e 's|/usr/bin/python$|&2|' \
    "${pkgdir}"${MINGW_PREFIX}/bin/scan-view

  # Compile Python scripts
  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}"${MINGW_PREFIX}/bin/clang-analyzer
  ${MINGW_PREFIX}/bin/python -O -m compileall "${pkgdir}"${MINGW_PREFIX}/lib/clang-analyzer
  install -Dm644 LICENSE.TXT "${pkgdir}"${MINGW_PREFIX}/share/licenses/clang-analyzer/LICENSE
}

package_clang-tools-extra() {
  pkgdesc="Extra tools built using Clang's tooling APIs (mingw-w64)"
  url="https://clang.llvm.org/"

  cd "${srcdir}/clang-tools-extra"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/extra DESTDIR="${pkgdir}" install
}

package_compiler-rt() {
  pkgdesc="Runtime libraries for Clang and LLVM (mingw-w64)"
  url="https://compiler-rt.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}")

  cd "${srcdir}"/compiler-rt
  ${_make_cmd} -C ../build-${CARCH}/projects/compiler-rt DESTDIR="${pkgdir}" install
}

package_libcxx() {
  pkgdesc="C++ Standard Library (mingw-w64)"
  url="https://libcxx.llvm.org/"
  depends="${MINGW_PACKAGE_PREFIX}-libunwind"

  cd "${srcdir}/libcxx"
  ${_make_cmd} -C ../build-libcxx-shared-${CARCH}/projects/libcxx DESTDIR="${pkgdir}" install
  ${_make_cmd} -C ../build-libcxx-static-${CARCH}/projects/libcxx DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/build-libcxx-static-${CARCH}/lib/libc++experimental.a" "${pkgdir}${MINGW_PREFIX}/lib/libc++experimental.a"
}

package_openmp() {
  pkgdesc="OpenMP library (mingw-w64)"
  url="https://openmp.llvm.org/"

  cd "${srcdir}/openmp"
  ${_make_cmd} -C ../build-${CARCH}/projects/openmp DESTDIR="${pkgdir}" install
}

package_libcxxabi() {
  pkgdesc="C++ Standard Library Support (mingw-w64)"
  url="https://libcxxabi.llvm.org/"
  depends="${MINGW_PACKAGE_PREFIX}-libunwind"

  cd "${srcdir}/libcxxabi"
  ${_make_cmd} -C ../build-libcxx-static-${CARCH}/projects/libcxxabi DESTDIR="${pkgdir}" install
}

package_libunwind() {
  pkgdesc='A new implementation of a stack unwinder for C++ exceptions (mingw-w64)'
  url='https://llvm.org/'

  cd "${srcdir}/libunwind"
  ${_make_cmd} -C ../build-libcxx-static-${CARCH}/projects/libunwind DESTDIR="${pkgdir}" install

  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  install "${srcdir}/build-libcxx-static-${CARCH}/lib/libunwind.dll" "${pkgdir}${MINGW_PREFIX}/bin/libunwind.dll"
}

package_lld() {
  pkgdesc="Linker tools for LLVM (mingw-w64)"
  url="https://lld.llvm.org/"

  cd "${srcdir}/lld"
  ${_make_cmd} -C ../build-${CARCH}/tools/lld DESTDIR="${pkgdir}" install
}

package_lldb() {
  pkgdesc="Next generation, high-performance debugger (mingw-w64)"
  url="https://lldb.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-llvm"
           "${MINGW_PACKAGE_PREFIX}-lua"
           "${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-readline"
           "${MINGW_PACKAGE_PREFIX}-swig")

  cd "${srcdir}/lldb"

  ${_make_cmd} -C ../build-${CARCH}/tools/lldb DESTDIR="${pkgdir}" install

  # Compile Python scripts
   local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/lldb"
  ${MINGW_PREFIX}/bin/python -O -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/lldb"

  install -Dm644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/lldb/LICENSE"
}

package_llvm() {
  pkgdesc="Low Level Virtual Machine (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libffi"
           "${MINGW_PACKAGE_PREFIX}-z3"
           "${MINGW_PACKAGE_PREFIX}-gcc-libs") # "compiler-rt"

  cd "${srcdir}"/llvm-${pkgver}.src

  ${_make_cmd} -C ../build-${CARCH} DESTDIR="${pkgdir}" install

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

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

  # FileCheck is needed to build rust.
  install -Dm755 "${srcdir}"/build-${CARCH}/bin/FileCheck.exe "${pkgdir}${MINGW_PREFIX}/bin/FileCheck.exe"
  # fox cmake files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${pkgdir}/${MINGW_PREFIX}/lib/cmake/llvm/LLVMExports.cmake
}

package_polly() {
  pkgdesc="Polly - Polyhedral optimizations for LLVM (mingw-w64)"
  url="https://polly.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}")

  cd "${srcdir}"/polly
  ${_make_cmd} -C ../build-${CARCH}/tools/polly DESTDIR="${pkgdir}" install
  # fox cmake files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  local SRC_WIN=$(cygpath -wm ${srcdir})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}/|g" \
    -e "s|${SRC_WIN}/build-${CARCH}/lib|${MINGW_PREFIX}/lib|g" \
    -i "${pkgdir}/${MINGW_PREFIX}/lib/cmake/polly/PollyConfig.cmake"
}

# Wrappers
package_mingw-w64-i686-clang(){
  package_clang
}

package_mingw-w64-i686-clang-analyzer(){
  package_clang-analyzer
}

package_mingw-w64-i686-clang-tools-extra(){
  package_clang-tools-extra
}

package_mingw-w64-i686-compiler-rt(){
  package_compiler-rt
}

package_mingw-w64-i686-libc++abi(){
  package_libcxxabi
}

package_mingw-w64-i686-libc++(){
  package_libcxx
}

package_mingw-w64-i686-openmp(){
  package_openmp
}

package_mingw-w64-i686-lld(){
  package_lld
}

package_mingw-w64-i686-lldb(){
  package_lldb
}

package_mingw-w64-i686-libunwind(){
  package_libunwind
}

package_mingw-w64-i686-llvm(){
  package_llvm
}

package_mingw-w64-i686-polly(){
  package_polly
}

package_mingw-w64-x86_64-clang(){
  package_clang
}

package_mingw-w64-x86_64-clang-analyzer(){
  package_clang-analyzer
}

package_mingw-w64-x86_64-clang-tools-extra(){
  package_clang-tools-extra
}

package_mingw-w64-x86_64-compiler-rt(){
  package_compiler-rt
}

package_mingw-w64-x86_64-libc++abi(){
  package_libcxxabi
}

package_mingw-w64-x86_64-libc++(){
  package_libcxx
}

package_mingw-w64-x86_64-openmp(){
  package_openmp
}

package_mingw-w64-x86_64-lld(){
  package_lld
}

package_mingw-w64-x86_64-lldb(){
  package_lldb
}

package_mingw-w64-x86_64-libunwind(){
  package_libunwind
}

package_mingw-w64-x86_64-llvm(){
  package_llvm
}

package_mingw-w64-x86_64-polly(){
  package_polly
}
