# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Andrew Smeltzov <andrew.smeltzov@gmail.com>

_realname=shiboken2
pkgbase=mingw-w64-${_realname}-qt5
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-qt5)
pkgdesc="CPython bindings generator for C++ libraries (mingw-w64)"
pkgver=5.15.2
pkgrel=3
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://doc.qt.io/qtforpython-5/"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-libxslt")
options=('staticlibs' 'strip')
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/pyside-setup-opensource-src-${pkgver}.tar.xz
        001-shiboken.patch
        002-cmake-relative-paths.patch
        003-llvm-paths.patch
        004-shibokenmodule-name.patch)
sha256sums=('b306504b0b8037079a8eab772ee774b9e877a2d84bab2dbefbe4fa6f83941418'
            'd7150cd5c554e5da68f5a53e08b46af02e45c5fc3481e99a1289b194e3220015'
            'e8fce91fee692a3ef906c841575e53e141a58c0c97c63fd1d94bed5a823190f9'
            '0cb712ba34d9951b0c29dc3b71f9c82882064331bd54a4cf0f4c792837932497'
            'f163bc1f81b3982b8445f06e7de4e0398c84a1935c5be7b6dc0e88f4ed794d5f')

prepare() {
  cd ${srcdir}/pyside-setup-opensource-src-${pkgver}
  patch -p1 -i ${srcdir}/001-shiboken.patch
  patch -p1 -i ${srcdir}/002-cmake-relative-paths.patch
  patch -p1 -i ${srcdir}/003-llvm-paths.patch
  patch -p1 -i ${srcdir}/004-shibokenmodule-name.patch
}

build() {
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_C_COMPILER=${MINGW_PREFIX}/bin/clang.exe \
    -DCMAKE_CXX_COMPILER=${MINGW_PREFIX}/bin/clang++.exe \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_TESTS=OFF \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    ../pyside-setup-opensource-src-${pkgver}/sources/shiboken2

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

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  local _PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${_f}"
  done

  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/Shiboken2-${pkgver}/*.cmake; do
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done
}
