# Contributor: Evangelos Foutras <evangelos@foutrelis.com>
# Contributor: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Marius Knaust <marius.knaust@gmail.com>
# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: Stéphane Gaudreault <stephane@archlinux.org>
# Contributor: kevin <kevin@archlinux.org>
# Contributor: Giovanni Scafora <giovanni@archlinux.org>
# Contributor: Kritoke <kritoke@gamebox.net>
# Contributor: Luca Roccia <little_rock@users.sourceforge.net>

_pkgname=boost
pkgname=static-compat-$_pkgname
pkgver=1.78.0
pkgrel=1
_srcname=boost_${pkgver//./_}
pkgdesc="Free peer-reviewed portable C++ source libraries"
arch=('x86_64')
url="https://www.boost.org/"
license=('custom')
options=('staticlibs')
depends=('static-compat-zlib' 'static-compat-zstd' 'static-compat-bzip2')
makedepends=('static-compat-environment')
source=(https://boostorg.jfrog.io/artifactory/main/release/$pkgver/source/$_srcname.tar.gz
        $_pkgname-b2-fix-lib-install.patch::https://github.com/bfgroup/b2/commit/78fd284a42ca.patch
        $_pkgname-ublas-c++20-iterator.patch::https://github.com/boostorg/ublas/commit/a31e5cffa85f.patch)
sha256sums=('94ced8b72956591c4775ae2207a9763d3600b30d9d7446562c552f0a14a63be7'
            'd233b16920ad7da8bb6e02bf5fb4e429592057b4f4db28d4babf3850e837a8db'
            'aa38addb40d5f44b4a8472029b475e7e6aef1c460509eb7d8edf03491dc1b5ee')

prepare() {
  cd $_srcname

  # https://github.com/bfgroup/b2/issues/104
  patch -Np1 -d tools/build <../$_pkgname-b2-fix-lib-install.patch

  # https://github.com/boostorg/ublas/pull/97
  patch -Np2 -i ../$_pkgname-ublas-c++20-iterator.patch
}

build() {
  source static-compat-environment
  local JOBS="$(sed 's/.*\(-j *[0-9]\+\).*/\1/' <<<$MAKEFLAGS)"

  pushd $_srcname/tools/build
  ./bootstrap.sh --cxxflags="$CXXFLAGS $LDFLAGS"
  ./b2 install --prefix="$srcdir"/fakeinstall
  ln -s b2 "$srcdir"/fakeinstall/bin/bjam
  popd

  cd $_srcname
  ./bootstrap.sh --with-toolset=gcc

  # boostbook is needed by quickbook
  install -dm755 "$srcdir"/fakeinstall/share/boostbook
  cp -a tools/boostbook/{xsl,dtd} "$srcdir"/fakeinstall/share/boostbook/

  # install to $srcdir/fakeinstall in preparation for split packaging
  ./b2 install \
    variant=release \
    debug-symbols=off \
    threading=multi \
    runtime-link=static \
    link=static \
    toolset=gcc \
    --without-python --without-mpi --without-graph_parallel \
    cflags="$CPPFLAGS $CFLAGS -fPIC -O3 -ffat-lto-objects" \
    cxxflags="$CPPFLAGS $CXXFLAGS -fPIC -O3 -ffat-lto-objects" \
    linkflags="$LDFLAGS" \
    --layout=system \
    $JOBS \
    \
    --prefix="$srcdir"/fakeinstall
}

package() {
  source static-compat-environment

  install -d "$pkgdir/$static_compat_prefix"/lib
  cp -a fakeinstall/lib/*.a "$pkgdir/$static_compat_prefix"/lib/
  cp -a fakeinstall/lib/cmake "$pkgdir/$static_compat_prefix"/lib/
  cp -a fakeinstall/{bin,include,share/boost-build} "$pkgdir/$static_compat_prefix"/

  install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname" $_srcname/LICENSE_1_0.txt
}
