# Maintainer:  Chris Severance aur.severach aATt spamgourmet dott com
# Contributor: Antonio Rojas <arojas@archlinux.org>
# Contributor: Felix Yan <felixonmars@archlinux.org>
# Contributor: Andrea Scarpino <andrea@archlinux.org>

# BUILD-CLEAN-CHROOT

pkgname=qt5-webengine
_basever=5.15.18
pkgver=5.15.19
pkgrel=6
arch=('x86_64')
#url='https://www.qt.io'
url='https://wiki.qt.io/QtWebEngine/ChromiumVersions'
license=('GFDL-1.3-only' 'GPL-2.0-or-later' 'GPL-3.0-only' 'LGPL-2.0-or-later' 'LGPL-3.0-only') # qtwebengine
#license+=('GPL-3.0-only WITH QT-exception' 'LicenseRef-Qt-Commercial') # qtwebengine
license+=('Apache-2.0' 'EPL-1.0' 'GPL-2.0-only' 'LGPL-2.1-only' 'LGPL-2.1-or-later' 'MPL-1.1' 'MPL-2.0') # qtwebengine-chromium
#license+=('AFL-2.0' 'APSL-2.0' 'BSD-2-Clause' 'BSD-3-Clause' 'BSD-3-Clause-Modification' 'BSD-4-Clause' 'CC-BY-4.0' 'ClArtistic' 'GPL-2.0-or-later' 'GPL-3.0-only' 'ISC' 'LGPL-2.0-or-later' 'libpng-2.0' 'MIT' 'OpenSSL' 'SGI-B-2.0' 'SSLeay-standalone') # qtwebengine-chromium
pkgdesc='Provides support for web applications using the Chromium browser project'
depends=('qt5-webchannel' 'qt5-location' 'libxcomposite' 'libxrandr' 'pciutils' 'libxdamage'
         'libevent' 'snappy' 'nss' 'libxslt' 'minizip' 'libvpx' 'libxtst')
#depends+=('ffmpeg' 'ttf-font')
depends+=('qt5-base' 'qt5-tools' 'qt5-declarative' 'gcc-libs' 'glib2' 'glibc' 'alsa-lib' 'dbus' 'expat' 'fontconfig' 'freetype2' 'harfbuzz' 'icu' 'lcms2' 'libglvnd' 'libjpeg-turbo' 'libpng' 'libwebp' 'libx11' 'libxcb' 'libxext' 'libxfixes' 'libxkbcommon' 'libxml2' 'libxrender' 'nspr' 'opus' 'zlib')
makedepends=('git' 'python' 'gperf' 'jsoncpp' 'ninja' 'poppler' 'pipewire' 'nodejs' 'python-html5lib')
makedepends+=('perl' 'libxcursor' 'libxkbfile' 'libpulse' 'libxss')
optdepends=('pipewire: WebRTC desktop sharing under Wayland')
options=('!lto' '!debug') # dreieck
_srcdir="kde-${pkgname/5-/}"
_srcdir87='qtwebengine-chromium'
source=(
  "${_srcdir}::git+https://code.qt.io/qt/qtwebengine.git#tag=v${pkgver}-lts"
  "git+https://code.qt.io/qt/${_srcdir87}.git"
  '0000-qt5-webengine-pipewire-0.3@chromium.patch' # Port to pipewire 0.3
  '0001-qt5-webengine-icu-75@chromium.patch'       # Fix build with ICU 75
  '0002-qt5-webengine-ninja-1.12@chromium.patch'   # Fix build with ninja 1.12
  #'0003-qt5-webengine-gcc-15@chromium.patch'       # Fix build with GCC 15 (Fedora)
  '0003a-qt5-webengine-gcc-15@chromium.patch'      # Fix build with GCC 15 (Fedora)
  #'0004-qt5-webengine-gcc-15@webengine.patch'      # Fix build with GCC 15 (Fedora)
  '0005-python3.12-six@chromium.patch'             # Fix build with python 3.12 - patch from Debian
  '0006-qt5-webengine-icu-78@chromium.patch'       # Fix build with ICU78
  '0007-qt5-webengine-glibc-2.43-SYS_SECCOMP-as-enum@chromium.patch'
)
md5sums=('1d1c4743f5b9f6601681c81f0ed953d5'
         '5fbf73d6c48235a94620231755481628'
         'a21aa28d235877e7dcc35055d67d417d'
         'ce4f3cb4a30f88136b1d3d1bcef40448'
         'f1ed8795356458457a311d0e3101ba28'
         '37f21a98d8c06e4180b90da1d320cb7f'
         '58145f2e58d3bce2b28c427cba7bad68'
         'bdddda5a578704eb56f5ceeaab49bd2f'
         '7c88da9ed56bd99eee3cdba5f197102c')
sha256sums=('33cd20f4a87874f34ef3f9c8d767e006498acc3e6cff89a9c64a9e3387bbfaa5'
            'bef1d9d0ed81778b31cbb5f592a66007bd60d1919f836529f9cd057edd9aacc1'
            '5e3a3c4711d964d5152a04059a2b5c1d14bb13dd29bce370120f60e85b476b6f'
            '7cac28ba784d24b4abf6414079548ada165343af507ecd8e23cbe7e4f63ae52f'
            '6672741b64d896dc555c8ee42ca2329c4f20d5f406095a69fe72da44b3a142f4'
            '54b3b1032e004d5829eade2c290df4c6793eb15f3f728a2e92828afbe0f7de43'
            '3459c151665842ff15130354db50c8a7c6fadc6fec583b85e818c42215ccce91'
            '7374653d1d6ee2289adce311d1cfc6b4d607dfd3cd593160e6ffab6e90eed559'
            'fdb6653d92b897d7ea2d8fa669155a7cf9b6aa0500341ce4f67319a0052bcea2')

if : ; then
  # I can't find any archive links on code.qt.io so we use the github mirror.
  _srcdir="qtwebengine-${pkgver}-lts"
  source[0]="${_srcdir}.tgz::https://github.com/qt/qtwebengine/archive/refs/tags/v${_srcdir#*-}.tar.gz"
  #_srcdir='qtwebengine'
  #source[0]="git+https://github.com/qt/${_srcdir}.git#tag=v${pkgver}-lts"
  _srcdir87='qtwebengine-chromium-87-based'
  _date87='20250423'
  source[1]="${_srcdir87}-${_date87}.tgz::https://github.com/qt/qtwebengine-chromium/archive/refs/heads/87-based.tar.gz"
else
  md5sums[0]='SKIP'
  md5sums[1]='SKIP'
  sha256sums[0]='SKIP'
  sha256sums[1]='SKIP'
fi

_fn_patch() {
  local -
  local _pt _ptf=() _pts=() _ptd
  for _pt in "${source[@]}"; do
    set +u; msg2 "Patch ${_pt}"; set -u
    if [[ "${_pt}" =~ ^[^@]+@([^.]+).patch$ ]]; then
      _ptd=()
      case "${BASH_REMATCH[1]}" in
      'chromium') _ptd=(-d "${srcdir}/$2");;
      'webengine') _ptd=(-d "${srcdir}/$1");;
      esac
      if patch "${_ptd[@]}" -Nup1 -i "${srcdir}/${_pt}"; then
        _pts+=("${_pt}")
      else
        _ptf+=("${_pt}")
      fi
    fi
  done
  if [ "${#_ptf[@]}" -gt 0 ]; then
     if [ "${#_pts[@]}" -gt 0 ]; then
       printf 'Patch success %s\n' "${_pts[@]}"
       printf 'Warning: Some old patches may need to be removed even if they are successful\n'
     fi
     printf 'Patch failed %s\n' "${_ptf[@]}"
     set +x
     false
  fi
  #cp -pr "${_srcdir}" 'a'; ln -s "${_srcdir}" 'b';  false
  #cp -pr "${_srcdir87}" 'a'; ln -s "${_srcdir87}" 'b';  false
  #diff -pNaru5 'a' 'b' > "0000-$RANDOM@domain.patch"
}

prepare() {
  mkdir -p build

  cd "${_srcdir}"
  if [ "${source[1]/git+/}" != "${source[1]}" ]; then
    git submodule init
    git submodule set-url 'src/3rdparty' "${srcdir}/${_srcdir87}"
    git submodule set-branch --branch '87-based' 'src/3rdparty'
    git -c protocol.file.allow='always' submodule update
    _fn_patch "${_srcdir}" "${_srcdir}/src/3rdparty"
  else
    _fn_patch "${_srcdir}" "${_srcdir87}"
    pushd "${srcdir}/${_srcdir87}" > /dev/null
    local _d
    for _d in *; do
      rm -rf "${srcdir}/${_srcdir}/src/3rdparty/${_d}"
      mv "${_d}" "${srcdir}/${_srcdir}/src/3rdparty/" # Neither patch nor git-apply work with symlinks
      #ln -srf "${_d}" "${srcdir}/${_srcdir}/src/3rdparty/"
    done
    popd > /dev/null
  fi

  # Why does the qtwebengine git download compile and the archive download doesn't?
  # The files are identical except for .git*.

  # This is what you should see a few minutes into a working compile (some lines skipped)
  #[187/187] LINK gn
  #( test -e Makefile.core_headers || /usr/bin/qmake -o Makefile.core_headers ...
  #Project MESSAGE: perl -w /usr/bin/syncqt.pl -module QtWebEngineCore -version 5.15.19 ...
  #<srcbase> = /build/qt5-webengine/src/kde-qtwebengine.
  #<bldbase> = /build/qt5-webengine/src/build.
  #<outbase> = /build/qt5-webengine/src/build.
  #QtWebEngineCore: created fwd-include header(s) for <srcbase>/src/core/api/ { qtwebenginecoreglobal.h (1), ...
  #QtWebEngineCore: created version header
  #QtWebEngineCore: created master header
  #QtWebEngineCore: created headers.pri file

  # Without this folder most of the above lines disappear and compile fails many hours in with
  # 'fatal error: QtWebEngineCore/qtwebenginecoreglobal.h: No such file or directory'
  # The git download always includes this folder. The archive download doesn't.
  mkdir -p '.git'

  # Fix build with python 3.13
  sed -e '/import pipes/d' -i src/3rdparty/chromium/build/android/gyp/util/build_utils.py
  # Force building with the bundled re2 version (gwuensch, https://bugs.gentoo.org/913923#c3)
  sed -e 's/\(use_system_re2=\)true/\1false/' -i 'src/core/config/linux.pri'

}

build() {
  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2} -Wno-error"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2} -Wno-error"

  cd build
  qmake "../${_srcdir}" CONFIG+='force_debug_info' -- \
    -proprietary-codecs \
    -webp \
    -webengine-icu \
    -spellchecker \
    -webengine-kerberos \
    -webengine-webrtc-pipewire \
    -webengine-python-version python3
  make
}

package() {
  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  cd build
  make INSTALL_ROOT="${pkgdir}" install

  # Drop QMAKE_PRL_BUILD_DIR because reference the build dir
  find "${pkgdir}/usr/lib" -type f -name '*.prl' \
    -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;

  install -Dm644 "${srcdir}"/${_srcdir}/src/3rdparty/chromium/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE.chromium

  # Fix cmake dependency versions
  sed -e "s|${pkgver}\ |$_basever |" -i "${pkgdir}"/usr/lib/cmake/*/*Config.cmake
}
