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

_realname=qtwebkit
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=5.212.0-alpha4
pkgver=${_pkgver//-/}
pkgrel=8
pkgdesc="Webkit module for Qt5 (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/annulen/webkit/wiki"
options=(!strip)
license=(GPL3 LGPL3 FDL custom)
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ruby"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "dos2unix"
             "gperf")
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-qt5=5.15.2"
         "${MINGW_PACKAGE_PREFIX}-woff2")
#install=${_realname}-qt5-git-${CARCH}.install
source=(https://github.com/annulen/webkit/releases/download/qtwebkit-${_pkgver}/${_realname}-${_pkgver}.tar.xz
        0003-qtwebkit-mfence-mingw.patch
        0005-fix-icu-find.patch
        0006-python-output-unix-line-endings.patch
        0008-fix-using-msys-perl.patch
        0011-mingw-posix-layout-files.patch
        0014-disable-asm-win64.patch
        0015-use-proper-import-suffix-mingw.patch
        0016-separate-debug-into-bin.patch
        0202-install-private-headers-and-modules.patch
        0203-add-qtmultimedia-dependencies.patch
        0300-fix-generating-module-files.patch
        0501-build-gcc9.patch
        d92b11fea65364fefa700249bd3340e0cd4c5b31.patch)
sha256sums=('9ca126da9273664dd23a3ccd0c9bebceb7bb534bddd743db31caf6a5a6d4a9e6'
            'ed80d9a31cb1f9565841e3e206d76810881b7fce30210022270bd19694a9c906'
            'b9e39597d140f3fc40d07ae0f0eea0cbabd7b16d8e430d26445ae3063b8ad055'
            '16410ad976524b40e7b9d906e67e4f46b86b58db6d7bb61e929f500387d7937f'
            '9ef33dc05e5652362e10d34d75fd2cf9e7952c1c99306eb2402367401f86770b'
            'a1ef305a81b4e1b70290d4c3f70bee545f06c17736fe77abde474dba0df8c5c7'
            'c39b65235a61217bf4197285d21765a47a91d2e8c267f4b777c982d56d32b408'
            '7406d9d008e0c657b6d8e6cea20adeb18ad485fc3c749c0fc4ec98dfe1f28e36'
            'c634eedfd1645264626298400c7fbfc9c6874de08280ba841e7a3e9a71195f74'
            'bc65b6e2bca1d8b304b063a4783dbcb845f0554202c7e2035f2cdca9b165d8ea'
            'de80d99c50c8c23fbc83c4685e4916451535b59d72151c7752e73354aca87390'
            '5ab5de5ee65fcd2a00412c8a2a1b2bbc4281d4ad99bac743757e6e8199a75ebe'
            'f6e85179e3581456cdfa9a00af537b4fb3caf0d66b14becc6592a15deba11175'
            'cc5a2b762426e9cda5a3ae056bba266b5f775ee75c0590913839c255d5f10db0')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"
  
  apply_patch_with_msg \
    0003-qtwebkit-mfence-mingw.patch \
    0005-fix-icu-find.patch \
    0006-python-output-unix-line-endings.patch \
    0008-fix-using-msys-perl.patch \
    0011-mingw-posix-layout-files.patch \
    0014-disable-asm-win64.patch \
    0015-use-proper-import-suffix-mingw.patch \
    0016-separate-debug-into-bin.patch
  
  # Upstream patches
  apply_patch_with_msg \
    0202-install-private-headers-and-modules.patch \
    0203-add-qtmultimedia-dependencies.patch
  
  apply_patch_with_msg \
    0300-fix-generating-module-files.patch \
    0501-build-gcc9.patch
    
  # Fix build with bison 3.7+
  apply_patch_with_msg \
    d92b11fea65364fefa700249bd3340e0cd4c5b31.patch

  # MSYS2 gperf cannot handle \r\n.
  find . -name "*.gperf" -exec dos2unix "{}" \;
}

build() {
  local jit_opt="-DENABLE_JIT=ON"
  if [ "${CARCH}" = "x86_64" ]; then
    local jit_opt="-DENABLE_JIT=OFF"
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST}-release ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}-release
  mkdir ${srcdir}/build-${MINGW_CHOST}-release && cd ${srcdir}/build-${MINGW_CHOST}-release

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DPORT=Qt \
    -DSEPARATE_DEBUG_INFO=ON \
    ${jit_opt} \
    ../${_realname}-${_pkgver}

  make

}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}-release
  make DESTDIR="${pkgdir}" install
}
