# Author: mosra <mosra@centrum.cz>
pkgname=magnum-bindings
pkgver=dev
pkgrel=1
pkgdesc="C++11/C++14 graphics middleware for games and data visualization — bindings"
arch=('i686' 'x86_64')
url="https://magnum.graphics"
license=('MIT')
depends=('corrade' 'magnum' 'python')
makedepends=('cmake' 'ninja' 'pybind11' 'lcov' 'python-coverage')
provides=('magnum-bindings-git')

_rootdir=$startdir/../../

build() {
    mkdir -p "$_rootdir/build-coverage"
    cd "$_rootdir/build-coverage"

    # Disable optimization (saves A LOT of compilation time)
    newcxxflags=$(echo $CXXFLAGS | sed s/-O.//g | sed s/-D_FORTIFY_SOURCE=.//g)
    export CXXFLAGS="$newcxxflags"

    # Colored output is enabled implicitly. If Ninja detects it's outputting to
    # a pipe, it strips it away from the output, alternatively you can set the
    # GCC_COLORS= (empty) env variable to temporarily disable colors. The
    # inverse, i.e. preserving colors when Ninja outputs to a pipe can be done
    # with CLICOLOR_FORCE=1: https://github.com/ninja-build/ninja/issues/2196
    cmake .. \
        -DCMAKE_CXX_FLAGS="--coverage" \
        -DCMAKE_BUILD_TYPE=Debug \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_COLOR_DIAGNOSTICS=ON \
        -DPYTHON_EXECUTABLE=$(which python) \
        -DMAGNUM_WITH_PYTHON=ON \
        -DMAGNUM_BUILD_TESTS=ON \
        -G Ninja
    ninja
}

check() {
    # Python coverage
    cd "$_rootdir/src/python/corrade"
    CMAKE_BINARY_DIR="$_rootdir/build-coverage" coverage run -m unittest -v || true
    cp .coverage ../.coverage.corrade

    cd "$_rootdir/src/python/magnum"
    CMAKE_BINARY_DIR="$_rootdir/build-coverage" coverage run -m unittest -v || true
    cp .coverage ../.coverage.magnum

    cd ..
    coverage combine
    coverage html

    # C++ coverage
    cd "$_rootdir/build-coverage"

    rm -rf coverage
    mkdir coverage
    # Keep in sync with package/ci/circleci.yml, please
    # TODO figure out a way to avoid adding --ignore-errors mismatch etc
    lcov --ignore-errors mismatch,inconsistent --directory . --capture --output-file coverage.info
    lcov --ignore-errors inconsistent --extract coverage.info "*/src/python/*" "*/src/Corrade/*" "*/src/Magnum/*" --output-file coverage.info
    genhtml --no-function-coverage --missed --output-directory ./coverage coverage.info
}

package() {
    echo -e "Open \n    file://${_rootdir}build-coverage/coverage/index.html\n    file://${_rootdir}src/python/htmlcov/index.html\nto see the results." && false
}
