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

_realname=boost
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-libs")
pkgver=1.86.0
pkgrel=7
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
        boost-1.63.0-python-test-PyImport_AppendInittab.patch
        0011-support-numpy-2.patch)
sha256sums=('2c5ec5edcdff47ff55e27ed9560b0a0b94b07bd07ed9928b476150e16b0efc57'
            'abc6c78a252165df1de504d854d942eb486d88f06a6423d8b99dfec6ab138170'
            'ee45d199c0e578a8d670ee6c6fa46466a53a02690c94f9221c67bbc9ab3387dc'
            'b22196b6415f5e1c0fe56b49a12ea7c20073b15a5f31907f363c7be38d70d628'
            '61b1ad6c2ca16e9f99a4705a2526d2e9594f51e6e7df6f11d5f358b70e26dc01')

# 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 \
    0011-support-numpy-2.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}" == "aarch64" ]]; then
    # boost context does not yet have an implementation for Windows ARM64
    # coroutine and fiber depend on context
    _extra_config+=("-DBOOST_EXCLUDE_LIBRARIES='context;cobalt;log;process;coroutine;fiber'")
  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 \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -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 \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -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;
