# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=boost
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-libs")
pkgver=1.87.0
pkgrel=2
pkgdesc="Free peer-reviewed portable C++ source libraries"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.boost.org/"
msys2_repository_url="https://github.com/boostorg/boost"
msys2_references=(
  'archlinux: boost'
  "cpe: cpe:/a:boost:boost"
)
license=('spdx:BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-python" \
               "${MINGW_PACKAGE_PREFIX}-python-numpy"))
source=(https://github.com/boostorg/boost/releases/download/boost-${pkgver}/boost-${pkgver}-cmake.tar.xz
        0001-Fix-building-with-cmake-on-MINGW.patch
        0002-No-Arch-in-library-name-tag.patch
        0003-fix-build-context-with-CMake-on-arm64.patch
        boost-1.63.0-python-test-PyImport_AppendInittab.patch)
sha256sums=('7da75f171837577a52bbf217e17f8ea576c7c246e4594d617bfde7fafd408be5'
            'abc6c78a252165df1de504d854d942eb486d88f06a6423d8b99dfec6ab138170'
            'ee45d199c0e578a8d670ee6c6fa46466a53a02690c94f9221c67bbc9ab3387dc'
            'ac6f996db2d5e2e5870b1a2d26c3b7d022f09d5be94945451d90875e8d4ecda4'
            'b22196b6415f5e1c0fe56b49a12ea7c20073b15a5f31907f363c7be38d70d628')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://bugzilla.redhat.com/show_bug.cgi?id=1102667
  apply_patch_with_msg \
    boost-1.63.0-python-test-PyImport_AppendInittab.patch

  apply_patch_with_msg \
    0001-Fix-building-with-cmake-on-MINGW.patch \
    0002-No-Arch-in-library-name-tag.patch \
    0003-fix-build-context-with-CMake-on-arm64.patch
}

build() {
  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 [[ "${CARCH}" != "i686" ]]; then
    _extra_config+=('-DBOOST_ENABLE_PYTHON=ON')
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS="OFF" \
      -DBOOST_INSTALL_LAYOUT=tagged \
      -DBOOST_STACKTRACE_ENABLE_FROM_EXCEPTION=OFF \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DBUILD_TESTING=OFF \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}-static

  ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS="ON" \
      -DBOOST_INSTALL_LAYOUT=tagged \
      -DBOOST_STACKTRACE_ENABLE_FROM_EXCEPTION=OFF \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DBUILD_TESTING=OFF \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}

  ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}
}

package_boost() {
  pkgdesc+=" (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-boost-libs")
  if [[ ${CARCH} != i686 ]]; then
    optdepends=("${MINGW_PACKAGE_PREFIX}-python: For Boost.Python"
                "${MINGW_PACKAGE_PREFIX}-python-numpy: For Boost.Python (NumPy)")
  fi

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-static
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE_1_0.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # split runtime libraries
  mkdir -p dest${MINGW_PREFIX}/bin
  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.dll dest${MINGW_PREFIX}/bin/
}

package_boost-libs() {
  pkgdesc+=" (runtime libraries) (mingw-w64)"

  mv dest/* "${pkgdir}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE_1_0.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-libs/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;
