# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=dlfcn
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.0
pkgrel=2
pkgdesc="A wrapper for dlfcn to the Win32 API (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/dlfcn-win32/dlfcn-win32"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('strip' 'staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/dlfcn-win32/dlfcn-win32/archive/v${pkgver}.tar.gz)
sha256sums=('f18a412e84d8b701e61a78252411fe8c72587f52417c1ef21ca93604de1b9c55')

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

build() {
  for SHARED in YES NO; do
    [[ -d "${srcdir}/build-${MINGW_CHOST}-${SHARED}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-${SHARED}"
    cp -rf "$srcdir/dlfcn-win32-${pkgver}" "${srcdir}/build-${MINGW_CHOST}-${SHARED}"
    cd "${srcdir}/build-${MINGW_CHOST}-${SHARED}"
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      ${MINGW_PREFIX}/bin/cmake \
        -G "MSYS Makefiles" \
        -DBUILD_SHARED_LIBS=${SHARED} \
        -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}"  \
        .
    make
  done
}

package() {
  for SHARED in YES NO; do
    cd "${srcdir}/build-${MINGW_CHOST}-${SHARED}"
    make DESTDIR="${pkgdir}" install
  done
  # We do not want transitive dependencies on libdll.dll so remove it.
  # We installed static last so the CMkake files pertain to a static
  # build already.
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/libdl.dll
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/libdl.dll.a
}
