# 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=26
pkgdesc="Webkit module for Qt5 (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://github.com/qtwebkit/qtwebkit/wiki"
msys2_references=(
  'archlinux: qt5-webkit'
  "cpe: cpe:/a:qt:qtwebkit"
)
license=(spdx:LGPL-2.0-only AND BSD-3-Clause)
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-location"
         "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
         "${MINGW_PACKAGE_PREFIX}-qt5-sensors"
         "${MINGW_PACKAGE_PREFIX}-qt5-webchannel"
         "${MINGW_PACKAGE_PREFIX}-woff2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ruby"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "dos2unix"
             "gperf")
source=(https://github.com/annulen/webkit/releases/download/qtwebkit-${_pkgver}/${_realname}-${_pkgver}.tar.xz
        0001-fix-build-with-gcc-14.patch
        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
        0300-fix-generating-module-files.patch
        0501-build-gcc9.patch
        0502-clang-compat.patch
        0503-python3.patch
        0504-qwebinspector-crash-workaround.patch
        0601-missing-include.patch
        d92b11fea65364fefa700249bd3340e0cd4c5b31.patch
        6e18844035d9c5e25d770ed817b858c2423d894b.patch
        4e1f209f21c3958e5a8d3a0646dccf9e429ca7e4.patch
        https://github.com/movableink/webkit/commit/df49bfc4.patch
        756e1c8f.patch
        https://salsa.debian.org/qt-kde-team/qt/qt5webkit/-/raw/debian/5.212.0_alpha4-42/debian/patches/offlineasm_ruby_3.2.diff
        https://salsa.debian.org/qt-kde-team/qt/qt5webkit/-/raw/debian/5.212.0_alpha4-42/debian/patches/icu_76.diff)
sha256sums=('9ca126da9273664dd23a3ccd0c9bebceb7bb534bddd743db31caf6a5a6d4a9e6'
            'e77d528e0bd203d4c7a657ff13485d2dc33dc0f262005fc1732f084b586bd05a'
            'ed80d9a31cb1f9565841e3e206d76810881b7fce30210022270bd19694a9c906'
            'b9e39597d140f3fc40d07ae0f0eea0cbabd7b16d8e430d26445ae3063b8ad055'
            '02d0eb76207c4c2d36a50291976f8cc141fcff030c4b77c399402cd10a789b99'
            '38d80f81a998eb107db5696e23da19511ef637be110871d385046a9171af4122'
            'a1ef305a81b4e1b70290d4c3f70bee545f06c17736fe77abde474dba0df8c5c7'
            'c39b65235a61217bf4197285d21765a47a91d2e8c267f4b777c982d56d32b408'
            '7406d9d008e0c657b6d8e6cea20adeb18ad485fc3c749c0fc4ec98dfe1f28e36'
            'c634eedfd1645264626298400c7fbfc9c6874de08280ba841e7a3e9a71195f74'
            'bc65b6e2bca1d8b304b063a4783dbcb845f0554202c7e2035f2cdca9b165d8ea'
            '5ab5de5ee65fcd2a00412c8a2a1b2bbc4281d4ad99bac743757e6e8199a75ebe'
            'f6e85179e3581456cdfa9a00af537b4fb3caf0d66b14becc6592a15deba11175'
            '17bfe3bf5dde380b16198d9f63c7f74032c1fa7e8530047625f0f413d365a722'
            '08031fdddd5e0ce66d5ca8343d0eac80345795a9c2a30610a67ca10acbb3f5a1'
            '491826ff49965c007137e95164fde7d28bd1eb24cf929406c5c6748259d54ea6'
            '83e9ffd3e74f6b533d5a3f5929e7918d31da690c66407cd1119f50d178fa94a0'
            'cc5a2b762426e9cda5a3ae056bba266b5f775ee75c0590913839c255d5f10db0'
            '49d4b19885806f82bff65b09e575488de176e63d91fad4545fda520aec96af82'
            '8e5f2d183c05d740d5eb1c3a191796bfb43743a462c5a0341f8171202f048af7'
            '82716a6d9a491501c6ecc66598adce37278498d9fab1c9610354ebf83d765ad9'
            '8fd479fc2ac56b17fde9392af95fb8ae8af13602e542193f4a64a9a6b8455616'
            '0a2babf4ec4862102ed6caae3026bcc481ab16e66f23a5e8619e3b704b8e3ff4'
            'a85204e2c76252357116364000cb0d415f09779f0c2b228d10e00b7df43421ba')

# 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 \
    0001-fix-build-with-gcc-14.patch \
    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 \
    0015-use-proper-import-suffix-mingw.patch \
    0014-disable-asm-win64.patch \
    0016-separate-debug-into-bin.patch

  # Upstream patches
  apply_patch_with_msg \
    0202-install-private-headers-and-modules.patch

  apply_patch_with_msg \
    0300-fix-generating-module-files.patch \
    0501-build-gcc9.patch \
    0502-clang-compat.patch \
    0503-python3.patch \
    0504-qwebinspector-crash-workaround.patch \
    0601-missing-include.patch

  # Fix build with bison 3.7+
  apply_patch_with_msg \
    d92b11fea65364fefa700249bd3340e0cd4c5b31.patch \
    6e18844035d9c5e25d770ed817b858c2423d894b.patch \
    4e1f209f21c3958e5a8d3a0646dccf9e429ca7e4.patch

  # Fix build with libxml2 2.12.0
  apply_patch_with_msg \
    df49bfc4.patch

  # Fix build with icu 75.1 https://github.com/qtwebkit/qtwebkit/commit/756e1c8f
  apply_patch_with_msg \
    756e1c8f.patch

  # Fix build with ruby 3.2
  apply_patch_with_msg \
    offlineasm_ruby_3.2.diff

  # Fix build with icu 76.1
  apply_patch_with_msg \
    icu_76.diff

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

build() {
  mkdir "${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" "-DSEPARATE_DEBUG_INFO=ON")
  fi

  if [[ "${CARCH}" == "x86_64" ]]; then
    _extra_config+=("-DENABLE_JIT=OFF")
  else
    _extra_config+=("-DENABLE_JIT=ON")
  fi

  CXXFLAGS+=" -w" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    "${_extra_config[@]}" \
    -DPORT=Qt \
    -DUSE_QT_MULTIMEDIA=ON \
    -DENABLE_API_TESTS=OFF \
    ../${_realname}-${_pkgver}

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

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