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

# mingw logic lives in gcc.jam, so we always need to use the 'gcc' toolset
_toolset=gcc

_realname=boost
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.85.0
_boostver=${pkgver//./_}
pkgrel=4
pkgdesc="Free peer-reviewed portable C++ source libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' '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"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-python" \
               "${MINGW_PACKAGE_PREFIX}-python-numpy"))
if [[ ${CARCH} != i686 ]]; then
  optdepends=("${MINGW_PACKAGE_PREFIX}-python: For Boost.Python"
              "${MINGW_PACKAGE_PREFIX}-python-numpy: For Boost.Python (NumPy)")
fi
source=(https://boostorg.jfrog.io/artifactory/main/release/${pkgver}/source/boost_${_boostver}.tar.bz2
        boost-1.57.0-build-optflags.patch
        boost-1.57.0-python-abi_letters.patch
        boost-1.57.0-python-libpython_dep.patch
        boost-1.63.0-python-test-PyImport_AppendInittab.patch
        boost-1.68.0-pool-test_linking.patch
        boost-1.70.0-fix-python-install.patch
        using-mingw-w64-python.patch
        msys2-mingw-folders-bootstrap.patch
        boost-1.85.0-container-aliasing.patch
        0010-cmake-prefer-shared-libs-over-static-on-MINGW.patch
        0011-support-numpy-2.patch
        0012-allow-longer-path-on-mingw-w64.patch)
sha256sums=('7009fe1faa1697476bdc7027703a2badb84e849b7b0baad5086b087b971f8617'
            'a23f9afeb620a0e82b6c158d592a555516dd985f82a9e24021f02a29651cb49b'
            'cf46e9b791b45937d5af26b0f35dcdfa416cd9b62ef6c29e3064464835634628'
            '01758929643f92530512230d37df9793e6481cd6ce6310e3a79cee5ba287858c'
            'b22196b6415f5e1c0fe56b49a12ea7c20073b15a5f31907f363c7be38d70d628'
            'c84cbb02321b4df379769f3a3cd03c38c76bdb13abaa3d3f04264ed37c99f2c7'
            '5e5fc9e04482903e1a0e7c6c4e81b57448b8dc86c707d1436d69ddc5010219ff'
            '5c38e08ba63695afa79a29d5d3ea22ef1ecf79f91d48ae922e1a489e83782742'
            '5117629ee577de0da800b6923675683ba69422cdbe958e70c9081fdc6886402e'
            '968844daf8cfb841e12fa24404ae189549ba3bc6e6037651293e4c247b630e90'
            '87e3a33aa402b2f3ea23dc75c6522111ea81b86019aff2ca3779999b82e1acbe'
            '61b1ad6c2ca16e9f99a4705a2526d2e9594f51e6e7df6f11d5f358b70e26dc01'
            'fdc78579f8f057defc59368f22b1304b138b155df13b953b5f10b57c633ed639')

# 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}/boost_${_boostver}"

  # https://bugzilla.redhat.com/show_bug.cgi?id=1190039
  apply_patch_with_msg \
    boost-1.57.0-build-optflags.patch

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

  # https://svn.boost.org/trac/boost/ticket/9038
  apply_patch_with_msg \
    boost-1.68.0-pool-test_linking.patch

  # Fix installation of both Python versions
  apply_patch_with_msg \
    boost-1.70.0-fix-python-install.patch

  # Use our own build Python
  apply_patch_with_msg \
    using-mingw-w64-python.patch

  apply_patch_with_msg \
    msys2-mingw-folders-bootstrap.patch

  # https://github.com/boostorg/container/commit/20ad12f20e661978e90dc7f36d8ab8ac05e5a5a9
  # https://github.com/msys2/MINGW-packages/issues/17977
  apply_patch_with_msg \
    boost-1.85.0-container-aliasing.patch

  # https://github.com/msys2/MINGW-packages/pull/14790#issuecomment-1519141235
  apply_patch_with_msg \
    0010-cmake-prefer-shared-libs-over-static-on-MINGW.patch

  # https://github.com/boostorg/python/commit/0474de0f6cc9
  apply_patch_with_msg \
    0011-support-numpy-2.patch

  # Building boost 1.83.0 fails because It uses response file to build url library
  # That response file uses backslashes for shared and import library which makes
  # the compilers both GCC and Clang to consider them as escape characters
  # boost/b2 default to use response file when the command line exceeds a certain limit
  # This patch increase that limit to 32000 (Cygwin `getconf ARG_MAX`)
  apply_patch_with_msg \
    0012-allow-longer-path-on-mingw-w64.patch
}

setb2args() {
    case ${CARCH} in
    i686|armv7)
      local _model=32
      ;;
    x86_64|aarch64)
      local _model=64
      ;;
    esac

    if check_option "debug" "y"; then
        local variant=debug
    else
        local variant=release
    fi

    # Extract names of ICU libraries from the pkg-config configuration file.
    # Note that the acual order of the libraries in the array is irrelevant.
    # They are all linked in the same way.
    local -a icu_i18n_libs
    local -a icu_uc_libs
    IFS=" " read -r -a icu_i18n_libs <<< "$(pkgconf --libs-only-l icu-i18n)"
    IFS=" " read -r -a icu_uc_libs <<< "$(pkgconf --libs-only-l icu-uc)"
    local -a icu_libs
    icu_libs=("${icu_i18n_libs[@]}" "${icu_uc_libs[@]}")
    local -a icu_libs_unique
    while IFS= read -r -d '' x
    do
        icu_libs_unique+=("$x")
    done < <(printf "%s\0" "${icu_libs[@]}" | sort -uz)
    # Remove -l prefix.
    icu_libs=("${icu_libs_unique[@]/#-l/}")

    b2args="address-model=${_model} \
    link=shared,static \
    pch=off \
    runtime-link=shared \
    threading=multi \
    threadapi=win32 \
    toolset=$_toolset \
    variant=${variant} \
    --prefix=${pkgdir}${MINGW_PREFIX} \
    --layout=tagged-1.66 \
    -sHAVE_ICU=1 \
    -sICU_PATH=${MINGW_PREFIX} \
    -sICU_ICUDT_NAME=${icu_libs[0]} \
    -sICU_ICUIN_NAME=${icu_libs[1]} \
    -sICU_ICUUC_NAME=${icu_libs[2]} \
    -sICONV_PATH=${MINGW_PREFIX} \
    -sICONV_LINK=\"-L${MINGW_PREFIX}/lib -liconv\" \
    -sNO_BZIP2 \
    -sBZIP2_BINARY=bz2 \
    -sBZIP2_INCLUDE=${MINGW_PREFIX}/include \
    -sBZIP2_LIBPATH=${MINGW_PREFIX}/lib \
    -sNO_ZLIB \
    -sZLIB_BINARY=z \
    -sZLIB_INCLUDE=${MINGW_PREFIX}/include \
    -sZLIB_LIBPATH=${MINGW_PREFIX}/lib \
    -q"
    if [[ "${CARCH}" == "aarch64" ]]; then
      # boost context does not yet have an implementation for Windows ARM64
      # coroutine and fiber depend on context
      b2args+=" --without-context --without-coroutine --without-fiber"
    fi
    if [[ ${CARCH} == i686 ]]; then
      b2args+=" --without-python --without-charconv"
    else
      b2args+=" python=${_pyver}"
    fi
}

build() {
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  cp -rf "${srcdir}/boost_${_boostver}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  if [[ ${CARCH} != i686 ]]; then
    local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

    echo "using python : ${_pyver} : ${PREFIX_DEPS}/bin/python.exe : ${PREFIX_DEPS}/include/python${_pyver} : ${PREFIX_DEPS}/lib/python${_pyver}/config-${_pyver} ;" >> ./tools/build/src/user-config.jam
  fi
  # configure b2 to use c++ rather than g++, as the latter no longer exists in
  # clang prefixes
  echo "using gcc : : ${PREFIX_DEPS}/bin/c++.exe ;" >> ./tools/build/src/user-config.jam

  ./tools/build/src/engine/build.sh --cxx="${CXX}" gcc
  cp ./tools/build/src/engine/b2 .

  local -a _bootstrap_args
  if [[ ${CARCH} == i686 ]]; then
    _bootstrap_args+=("--without-libraries=python")
  else
    _bootstrap_args+=("--with-python=${MINGW_PREFIX}")
  fi

  ./bootstrap.sh "${_bootstrap_args[@]}" --with-icu=${MINGW_PREFIX} --with-toolset=$_toolset --with-bjam="$(pwd)/b2"

  setb2args
  ./b2 $b2args -d1 -j$(nproc) \
    cflags="$CFLAGS" \
    cxxflags="$CXXFLAGS -Wno-deprecated-declarations -Wno-ignored-attributes -Wno-unused-command-line-argument" \
    linkflags="$LDFLAGS"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  setb2args
  ./b2 $b2args -d0 install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin

  # install b2 and bjam
  install -Dm755 b2.exe "${pkgdir}${MINGW_PREFIX}"/bin/b2.exe
  cp -rf "${pkgdir}${MINGW_PREFIX}"/bin/b2.exe "${pkgdir}${MINGW_PREFIX}"/bin/bjam.exe
}
