# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
  _clangprefix=1
fi

_realname=libc++
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-libunwind")
_pkgver=21.1.8
pkgver=${_pkgver/-/}
pkgrel=1
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://libcxx.llvm.org/"
msys2_references=(
  "cpe: cpe:/a:llvm:llvm"
)
license=("spdx:Apache-2.0 WITH LLVM-exception")
groups=($( (( _clangprefix )) && echo "${MINGW_PACKAGE_PREFIX}-toolchain"))
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-llvm-tools"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-lld"
             $( (( _clangprefix )) && echo "${MINGW_PACKAGE_PREFIX}-compiler-rt")
             "${MINGW_PACKAGE_PREFIX}-python")
if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
  # GNU's strip breaks the library #11553
  options=('!strip')
fi
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${_pkgver}
source=("${_url}/llvm-project-${_pkgver}.src.tar.xz"{,.sig})
sha256sums=('4633a23617fa31a3ea51242586ea7fb1da7140e426bd62fc164261fe036aa142'
            'SKIP')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              '71046D1E9C6656BDD61171873E83BABF4A4F9E85'  # Cullen Rhodes
              'FFB3368980F3E6BB5737145A316C56D064CACBA5'  # Douglas Yung
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta
noextract=(llvm-project-${_pkgver}.src.tar.xz)

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

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if (( _clangprefix )); then
    _extra_config+=(
      -DLIBCXX_USE_COMPILER_RT=ON
      -DLIBCXXABI_USE_COMPILER_RT=ON
      -DLIBCXXABI_USE_LLVM_UNWINDER=ON
      -DLIBCXXABI_ENABLE_STATIC_UNWINDER=ON
      -DLIBUNWIND_USE_COMPILER_RT=ON
    )
  else
    _extra_config+=(-DLIBCXXABI_USE_LLVM_UNWINDER=OFF)
  fi

  # Targeting Win 7 will just lead to libc++ looking
  # up new APIs at runtime, so there is no downside really
  local _win32_winnt
  if [[ ${MSYSTEM} == CLANGARM64 ]]; then
      _win32_winnt=0xA00 # Windows 10
  else
      _win32_winnt=0x601 # Windows 7
  fi

  CC=${MINGW_PREFIX}/bin/clang.exe \
  CXX=${MINGW_PREFIX}/bin/clang++.exe \
  CXXFLAGS+=" -D_WIN32_WINNT=${_win32_winnt} $CPPFLAGS" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DCMAKE_AR="${MINGW_PREFIX}/bin/llvm-ar.exe" \
    -DCMAKE_ASM_COMPILER="${MINGW_PREFIX}/bin/clang.exe" \
    -DCMAKE_RANLIB="${MINGW_PREFIX}/bin/llvm-ranlib.exe" \
    -DLLVM_HOST_TRIPLE="${CARCH}-w64-windows-gnu" \
    -DLLVM_ENABLE_LLD=ON \
    -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
    -DLIBCXX_ENABLE_SHARED=ON \
    -DLIBCXX_ENABLE_STATIC=ON \
    -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON \
    -DLIBCXX_HAS_WIN32_THREAD_API=ON \
    -DLIBCXX_INSTALL_MODULES=ON \
    -DLIBCXX_INCLUDE_BENCHMARKS=OFF \
    -DLIBCXXABI_ENABLE_SHARED=OFF \
    -DLIBCXXABI_ENABLE_STATIC=ON \
    -DLIBCXXABI_HAS_WIN32_THREAD_API=ON \
    -DLIBUNWIND_ENABLE_FRAME_APIS=ON \
    -DLIBUNWIND_ENABLE_SHARED=ON \
    -DLIBUNWIND_ENABLE_STATIC=ON \
    -DENABLE_TIME_ZONE_DATABASE_DEFAULT=ON \
    -DLLVM_INCLUDE_TESTS=OFF \
    "${_extra_config[@]}" \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -Wno-dev \
    ../llvm-project-${_pkgver}.src/runtimes

  ${MINGW_PREFIX}/bin/cmake --build . -- unwind cxxabi cxx cxx_experimental
}

package_libc++() {
  pkgdesc="C++ Standard Library (mingw-w64)"
  url="https://libcxx.llvm.org/"
  provides=($( (( _clangprefix )) && echo \
             "${MINGW_PACKAGE_PREFIX}-gcc-libs" \
             "${MINGW_PACKAGE_PREFIX}-cc-libs" \
             || true))
  depends=($( (( _clangprefix )) && echo \
             "${MINGW_PACKAGE_PREFIX}-libunwind" \
             || echo "${MINGW_PACKAGE_PREFIX}-cc-libs"))

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build "${srcdir}/build-${MSYSTEM}" --target install-cxx install-cxxabi

  install -Dm644 "${srcdir}"/llvm-project-${_pkgver}.src/libcxx/LICENSE.TXT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/libc++/LICENSE
}

package_libunwind() {
  pkgdesc='A new implementation of a stack unwinder for C++ exceptions (mingw-w64)'
  url='https://llvm.org/'
  depends=($( (( _clangprefix )) || echo "${MINGW_PACKAGE_PREFIX}-cc-libs"))

  DESTDIR="${pkgdir}" cmake --build "${srcdir}/build-${MSYSTEM}" --target install-unwind

  install -Dm644 "${srcdir}"/llvm-project-${_pkgver}.src/libunwind/LICENSE.TXT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/libunwind/LICENSE
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
