# Maintainer: Simon Sobisch https://github.com/GitMensch

_realname=libipt
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgbase="mingw-w64-${_realname}"
pkgver=2.1.2
pkgrel=1
pkgdesc='Intel(R) Processor Trace decoder library (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/intel/libipt'
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             #'pandoc' # Only required for building the manpage, we likely don't want that
             'git')
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('713d3e76b6c3073b122a9f5b6c025bc301a0436582f132caf782814363acf60f')

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 \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DDEVBUILD=OFF \
      -DFEATURE_THREADS=ON \
      -DFEATURE_ELF=ON \
      -DMAN=OFF \
      -DPTDUMP=ON \
      -DPTTC=ON \
      -DPTUNIT=ON \
      -DPTXED=OFF \
      -DSIDEBAND=ON \
      -DCMAKE_C_FLAGS="-D__STDC_NO_THREADS__" \
      ../${_realname}-${pkgver}

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

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

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

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

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/README" \
    "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
