# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=libfixmath
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=r154.d308e46
pkgrel=2
pkgdesc="Cross Platform Fixed Point Maths Library (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://github.com/PetteriAimonen/libfixmath"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             'git')
_commit='d308e466e1a09118d03f677c52e5fbf402f6fdd0'
source=("${_realname}"::"git+https://github.com/PetteriAimonen/libfixmath.git#commit=${_commit}"
        "001-no-sanitizers-with-gcc.patch")
sha256sums=('SKIP'
            '35fee365ec8b4593046869f84ace00699d7f40854156e25acc321b17b4dbc82a')

pkgver() {
  cd "${srcdir}/${_realname}"

  printf "r%s.%s" "$(git rev-list --count "${_commit}")" "$(git rev-parse --short "${_commit}")"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -p1 -i "${srcdir}"/001-no-sanitizers-with-gcc.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

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

  "${MINGW_PREFIX}"/bin/ctest
}

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

  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/libfixmath"
  cp "${srcdir}"/${_realname}/libfixmath/*.h "${pkgdir}${MINGW_PREFIX}/include/libfixmath"
  cp "${srcdir}"/${_realname}/libfixmath/*.hpp "${pkgdir}${MINGW_PREFIX}/include/libfixmath"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib"
  cp liblibfixmath.a "${pkgdir}${MINGW_PREFIX}/lib"

  install -Dm644 "${srcdir}/${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
