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

_realname=boost
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.75.0
_boostver=${pkgver//./_}
pkgrel=4
pkgdesc="Free peer-reviewed portable C++ source libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.boost.org/"
license=('custom')
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}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "lndir")
options=('strip' 'buildflags' 'staticlibs')
source=(#https://downloads.sourceforge.net/sourceforge/boost/boost_${_boostver}.tar.bz2
        https://dl.bintray.com/boostorg/release/${pkgver}/source/boost_${_boostver}.tar.bz2
        boost-1.48.0-add-bjam-man-page.patch
        boost-1.58.0-pool.patch
        boost-1.63.0-python-test-PyImport_AppendInittab.patch
        boost-1.57.0-build-optflags.patch
        boost-1.57.0-mpl-print.patch
        boost-1.68.0-pool-test_linking.patch
        boost-1.57.0-python-abi_letters.patch
        boost-1.57.0-python-libpython_dep.patch
        boost-1.60.0-mingw-context.patch
        boost-1.69-old-style-names-layout.patch
        boost-1.70.0-fix-python-install.patch
        boost-1.73.0-context-gas_mingw_symbols.patch
        boost-1.73.0-fix-locale-icu.patch
        boost-1.74-mingw-32bit-enable-tls.patch
        boost-1.75-dont-set-msvcrt-version.patch
        boost-1.75-mingw-clang.patch
        using-mingw-w64-python.patch
        msys2-mingw-folders-bootstrap.patch)
sha256sums=('953db31e016db7bb207f11432bef7df100516eeb746843fa0486a222e3fd49cb'
            'c5eae6354693c6b4e6364d4cf6f06c6c3de4c28486bf812cbd291f4410126cf8'
            '4551ba9edf64c8ccdab4f4890c20e2e8cf6d4ffa1169d251df11e30d25b886b8'
            'b22196b6415f5e1c0fe56b49a12ea7c20073b15a5f31907f363c7be38d70d628'
            '35ff098d1268397e2404bd2db7aa2faa2ec193905b014e6cb36b1bca6e876b01'
            '3a26077616487deff011fea7f042df9b03efce27a586be2ce42d0562b48b7ad5'
            '9d159f713fecbcb2b8faf2534da4cbb223bde6c3e393218aca2c0b3dfc3374c1'
            'cf46e9b791b45937d5af26b0f35dcdfa416cd9b62ef6c29e3064464835634628'
            '01758929643f92530512230d37df9793e6481cd6ce6310e3a79cee5ba287858c'
            '704c2f1528b4584edd67d67159787cc2bd0db8b9321251a22df7b2fa00bd83f7'
            'c2508e60bed41471b396875e0289b4b1d4e021f3d01f4294615c72fcd8567439'
            '21d039cf867a41253f752c5f04f0cdb2ec86f17ad88459918b326fa48e9c1e05'
            'bb84f3c7fc4b856c2bc2dc5c385152905c51ba8bffffa9371370c41168d55fcc'
            '5ef2b1011dc960b7f55d4da89bcd75418eb696dce677b9fd0c78632f4109e7d3'
            '15f25653ec71e061cbd3a611a969541c57ea70c1fe95cb0de11f22cc68c7670f'
            'edc8cb076afe5132f2e5a1393e6e65ef3a8740384f36b7b9aa065d53b1182292'
            '876c64687e4e1c700b5c64971e4cf62792e869b471b8d63cbb721fc057fc612f'
            '0dd6346d369850aad13bf8d9bc2f0abc16d4b0586e34d61fab11f65d3d7fa9d4'
            '5117629ee577de0da800b6923675683ba69422cdbe958e70c9081fdc6886402e')

prepare() {
  cd "${srcdir}/boost_${_boostver}"

  # Add a manual page for bjam, based on the on-line documentation:
  # https://www.boost.org/boost-build2/doc/html/bbv2/overview.html
  rm tools/build/v2/doc/bjam.1 || true
  msg2 "boost-1.48.0-add-bjam-man-page.patch"
  patch -p1 -i ${srcdir}/boost-1.48.0-add-bjam-man-page.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=828856
  # https://bugzilla.redhat.com/show_bug.cgi?id=828857
  # https://svn.boost.org/trac/boost/ticket/6701
  msg2 "boost-1.58.0-pool.patch"
  patch -p0 -i ${srcdir}/boost-1.58.0-pool.patch

  # https://svn.boost.org/trac/boost/ticket/5637
  msg2 "boost-1.57.0-mpl-print.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-mpl-print.patch

  # https://svn.boost.org/trac/boost/ticket/9038
  msg2 "boost-1.68.0-pool-test_linking.patch"
  patch -p1 -i ${srcdir}/boost-1.68.0-pool-test_linking.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=1102667
  msg2 "boost-1.57.0-python-libpython_dep.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-python-libpython_dep.patch
  msg2 "boost-1.57.0-python-abi_letters.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-python-abi_letters.patch
  msg2 "boost-1.60.3-python-test-PyImport_AppendInittab.patch"
  patch -p1 -i ${srcdir}/boost-1.63.0-python-test-PyImport_AppendInittab.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=1190039
  msg2 "boost-1.57.0-build-optflags.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-build-optflags.patch

  # https://svn.boost.org/trac/boost/ticket/7262
  msg2 "boost-1.60.0-mingw-context.patch"
  patch -p1 -i ${srcdir}/boost-1.60.0-mingw-context.patch
  
  # Back old names for libraries (without architecture)
  msg2 "boost-1.69-old-style-names-layout.patch"
  patch -p1 -i ${srcdir}/boost-1.69-old-style-names-layout.patch

  # Use our own build Python
  msg2 "using-mingw-w64-python.patch"
  patch -p1 -i ${srcdir}/using-mingw-w64-python.patch

  msg2 "msys2-mingw-folders-bootstrap.patch"
  patch -p1 -i ${srcdir}/msys2-mingw-folders-bootstrap.patch
  
  # Fix installation of both Python versions
  msg2 "boost-1.70.0-fix-python-install.patch"
  patch -p1 -i ${srcdir}/boost-1.70.0-fix-python-install.patch

  # Correct GAS naming for MinGW
  msg2 "boost-1.73.0-context-gas_mingw_symbols.patch"
  patch -p1 -i ${srcdir}/boost-1.73.0-context-gas_mingw_symbols.patch  
  
  # Fix Boost::locale compilation to use ICU backend
  msg2 "boost-1.73.0-fix-locale-icu.patch"
  patch -p1 -i ${srcdir}/boost-1.73.0-fix-locale-icu.patch
  
  msg2 "boost-1.74-mingw-32bit-enable-tls.patch"
  patch -p1 -i ${srcdir}/boost-1.74-mingw-32bit-enable-tls.patch

  # https://github.com/boostorg/nowide/pull/130
  # https://github.com/boostorg/log/pull/149
  msg2 "boost-1.75-dont-set-msvcrt-version.patch"
  patch -p1 -i ${srcdir}/boost-1.75-dont-set-msvcrt-version.patch

  msg2 "boost-1.75-mingw-clang.patch"
  patch -p1 -i ${srcdir}/boost-1.75-mingw-clang.patch
}

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

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  lndir "${srcdir}/boost_${_boostver}" "${srcdir}/build-${MINGW_CHOST}"

  cd "${srcdir}/build-${MINGW_CHOST}"

  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
  #echo "using gcc : : ${PREFIX_DEPS}/bin/g++.exe : <rc>${PREFIX_DEPS}/bin/windres.exe ;" >> ./tools/build/src/user-config.jam

  ./bootstrap.sh --with-icu=${MINGW_PREFIX} --with-python=${MINGW_PREFIX} --with-toolset=gcc

  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

  ./b2 \
    address-model=${_model} \
    link=shared,static \
    pch=off \
    runtime-link=shared \
    threading=multi \
    threadapi=win32 \
    toolset=gcc \
    variant=${variant} \
    python=${_pyver} \
    --debug-configuration \
    --prefix=${MINGW_PREFIX} \
    --layout=tagged \
    --without-mpi \
    --no-cmake-config \
    -sHAVE_ICU=1 \
    -sICU_PATH=${MINGW_PREFIX} \
    -sICU_LINK="-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt" \
    -sICU_ICUUC_NAME=icuuc \
    -sICU_ICUDT_NAME=icudt \
    -sICU_ICUIN_NAME=icuin \
    -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 \
    -d2 \
    -j$(nproc)
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  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

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

  ./b2 \
      address-model=${_model} \
      link=shared,static \
      runtime-link=shared \
      threading=multi \
      threadapi=win32 \
      toolset=gcc \
      variant=${variant} \
      python=${_pyver} \
      --prefix=${pkgdir}${MINGW_PREFIX} \
      --layout=tagged \
      --without-mpi \
      --no-cmake-config \
      -sHAVE_ICU=1 \
      -sICU_LINK="-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt" \
      -sICU_PATH=${MINGW_PREFIX} \
      -sICU_ICUUC_NAME=icuuc \
      -sICU_ICUDT_NAME=icudt \
      -sICU_ICUIN_NAME=icuin \
      -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 \
      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
}
