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

_realname=boost
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-libs")
pkgver=1.89.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}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         $([[ ${CARCH} == i686 ]] || echo "${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-include-missing-cstdint.patch
        0004-fix-installing-dll.patch
        boost-1.63.0-python-test-PyImport_AppendInittab.patch)
sha256sums=('67acec02d0d118b5de9eb441f5fb707b3a1cdd884be00ca24b9a73c995511f74'
            'fb32ea452e02196552dae815eb44be5b18d849b0f6e24ecf5cf7be15e42ad85b'
            'ee45d199c0e578a8d670ee6c6fa46466a53a02690c94f9221c67bbc9ab3387dc'
            'fb575db0a68861f11e5bc788d8c0a172e7ffdb5569376eb848ca0f91670d6ec5'
            'b08150904cd3868971aaf8f0654056fd9c0a53fa84a88cdc2311ce37d15c18bb'
            '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-include-missing-cstdint.patch \
    0004-fix-installing-dll.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;
