# Maintainer: d0p1 <contact@d0p1.eu>

_realname=liboqs
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.15.0
pkgrel=1
pkgdesc="C library for prototyping and experimenting with quantum-resistant cryptography (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://openquantumsafe.org/'
msys2_repository_url="https://github.com/open-quantum-safe/liboqs"
msys2_references=(
  'anitya: 364436'
  'aur: liboqs'
)
license=('spdx:MIT')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc"
)
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-pytest"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-xdist"
  "${MINGW_PACKAGE_PREFIX}-python-yaml"
)
source=("https://github.com/open-quantum-safe/liboqs/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-aarch64-detection.patch")
sha256sums=('3983f7cd1247f37fb76a040e6fd684894d44a84cecdcfbdb90559b3216684b5c'
            '99a7133701b106e6e0d4d9dece250a2e63c1082c7c6835747ad69387bd29f958')

apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

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

  apply_patch_with_msg \
    001-aarch64-detection.patch
}

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

  if [[ ${CC} == clang ]]; then
    export CFLAGS+=" -Wno-unknown-warning-option"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DBUILD_SHARED_LIBS=ON \
      -DOQS_BUILD_ONLY_LIB=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

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