pkgname=mingw-w64-clang-aarch64-boost
pkgver=1.86.0
_boostver=${pkgver//./_}
pkgrel=1
pkgdesc="Free peer-reviewed portable C++ source libraries (mingw-w64-clang-aarch64)"
arch=('any')
url="http://www.boost.org/"
license=('custom')
depends=('mingw-w64-clang-aarch64-zstd' 'mingw-w64-clang-aarch64-bzip2' 'mingw-w64-clang-aarch64-dlfcn')
makedepends=('mingw-w64-clang-aarch64-cmake' 'ninja')
options=('!strip' '!buildflags' 'staticlibs')
source=(https://github.com/boostorg/boost/releases/download/boost-${pkgver}/boost-${pkgver}-cmake.tar.xz
       0001-Fix-building-with-cmake-on-MINGW.patch)
sha256sums=('2c5ec5edcdff47ff55e27ed9560b0a0b94b07bd07ed9928b476150e16b0efc57'
            'abc6c78a252165df1de504d854d942eb486d88f06a6423d8b99dfec6ab138170')

_architectures='aarch64-w64-mingw32'

_configurations=()
[[ $NO_SHARED_LIBS ]] || _configurations+=('shared')
[[ $NO_STATIC_LIBS ]] || _configurations+=('static')

prepare() {
  cd "$srcdir/boost-$pkgver"

  # process_handle_windows.cpp:(.text+0x25): undefined reference to `_imp__WSACleanup@0'
  sed -i -e "/if \[ os.name \] = NT/d" -e "/{/d" -e "/}/d" libs/process/build/Jamfile
  sed -i -e "46ilib ws2_32 ;" -e "57i<target-os>windows:<library>ws2_32" libs/process/build/Jamfile

  # from_exception.cpp:333:23: error: 'current_exception' is not a member of 'std'
  sed -i "160i#include <exception>" libs/stacktrace/src/from_exception.cpp
  sed -i "160i\ \ \ \ <target-os>windows:<library>dl" libs/stacktrace/build/Jamfile.v2

  patch -p1 -i ../0001-Fix-building-with-cmake-on-MINGW.patch
}

build() {
  for _arch in "${_architectures[@]}"; do
    source mingw-clang-env $_arch
    declare -A _config_flags=(
      [shared]="
          -DCMAKE_INSTALL_PREFIX=/usr/$_arch
          -DBUILD_SHARED_LIBS:BOOL=ON
      "
      [static]="
          -DCMAKE_INSTALL_PREFIX=/usr/$_arch
          -DBUILD_SHARED_LIBS:BOOL=OFF
      "
    )
    for _cfg in "${_configurations[@]}"; do
      msg2 "${_arch}-${_cfg}"

      if [[ $_arch =~ aarch64-.* ]]; then
        # skip context and anything depending on it for now due to:
        # ld.lld: error: undefined symbol: __declspec(dllimport) make_fcontext
        # ld.lld: error: undefined symbol: __declspec(dllimport) ontop_fcontext
        _arch_config=("-DBOOST_EXCLUDE_LIBRARIES='context;cobalt;log;process;coroutine;fiber'")
      fi

      ${_arch}-cmake -G Ninja -B "build-${_arch}-${_cfg}" -S "$srcdir/boost-$pkgver" \
        -DBOOST_INSTALL_LAYOUT=system \
        ${_config_flags[$_cfg]} ${_arch_config[$_cfg]}
      VERBOSE=1 cmake --build "build-${_arch}-${_cfg}"
    done
  done
}

package() {
  for _arch in "${_architectures[@]}"; do
    source mingw-clang-env $_arch
    for _cfg in "${_configurations[@]}"; do
      msg2 "${_arch}-${_cfg}"
      DESTDIR="$pkgdir" cmake --install "build-${_arch}-${_cfg}"
    done
    $STRIP --strip-unneeded "$pkgdir"/usr/$_arch/bin/*.dll
    #$STRIP -g "$pkgdir"/usr/$_arch/static/lib/*.a
    $STRIP -g "$pkgdir"/usr/$_arch/lib/*.a
  done
  install -Dm644 "$srcdir/boost-$pkgver/LICENSE_1_0.txt" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

