# Maintainer (arch): Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Eric Bélanger <eric@archlinux.org>
# Contributor: Andreas Grapentin <andreas@grapentin.org>
# Contributor: Jesús E. <heckyel@parabola.nu>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Luke R. <g4jc@openmailbox.org>
# Contributor: Freemor <freemor@fremor.ca>
# Contributor: bill-auger <bill-auger@programmer.net>
# Maintainer (archarm): Kevin Mihelich <kevin@archlinuxarm.org>
# Maintainer: Parabola Hackers <dev@lists.parabola.nu>


# parabola changes and rationale:
#  - remove geoclue2 support
#  - pinned to 'icu' sodep
# archarm changes:
#  - build v7 with vfpv3, neon is broken

# NOTE: i686 currently fails to build (FS32#310)


pkgbase=webkit2gtk
pkgname=(
  webkit2gtk
  webkit2gtk-docs
)
pkgver=2.42.5
pkgrel=2
pkgrel+=.nonprism1
pkgdesc="Web content engine for GTK"
pkgdesc+=", without geoclue2 support"
url="https://webkitgtk.org"
arch=(x86_64)
arch+=(armv7h i686)
license=(custom)
depends=(
  at-spi2-core
  atk
  bubblewrap
  cairo
  enchant
  fontconfig
  freetype2
  glib2
  gst-plugins-bad-libs
  gst-plugins-base-libs
  gstreamer
  gtk3
  harfbuzz
  harfbuzz-icu
  hyphen
  icu
  libavif
  libdrm
  libegl
  libepoxy
  libgcrypt
  libgl
  libgles
  libjpeg
  libjxl
  libmanette
  libpng
  libseccomp
  libsecret
  libsoup
  libsystemd
  libtasn1
  libwebp
  libwpe
  libx11
  libxcomposite
  libxml2
  libxslt
  libxt
  mesa
  openjpeg2
  sqlite
  wayland
  woff2
  wpebackend-fdo
  xdg-dbus-proxy
  zlib
)
makedepends=(
  clang
  cmake
  gi-docgen
  gobject-introspection
  gperf
  gst-plugins-bad
  lld
  ninja
  python
  ruby
  systemd
  unifdef
  wayland-protocols
)
options=(debug)
source=(
  $url/releases/webkitgtk-$pkgver.tar.xz{,.asc}
  GTK-MiniBrowser-should-hide-the-toolbar-when-using-full-screen.patch
  GTK-Disable-DMABuf-renderer-for-NVIDIA-proprietary-drivers.patch
)
source_i686=(webkitgtk-2.36.3-no-sse2.arch32.patch)
sha256sums=('b64278c1f20b8cfdbfb5ff573c37d871aba74a1db26d9b39f74e8953fe61e749'
            'SKIP'
            'a921d6be1303e9f23474971f381886fd291ec5bb1a7ff1e85acede8cfb88bef2'
            '655f3b2c96355ac83c4fa1fc6048e3256bbfdbfb9727e1e18c5af12613536206')
sha256sums_i686=('58a724b7f1092a9049186a3b8c61689c31558b6c2b587c827cec04219397adac')
b2sums=('4b772d91d3e2c3fc3a48455f43016d2cc1699b18fcc78253d1527eecabbfc1f47cd3bf3c512508fd683d29de68f980511d2057e4c65979bab38083a2da5cb516'
        'SKIP'
        'd440d82c769f1b35caf5464dc850cdf1c896224205c90c17d8b0a44aee62e4b1383e11306936aaca067fde8836770d346d5122d7b05c91a5c7c1741c89c65e2f'
        'daa782d4d40cc12a05c02b2494e879333f66f32820f1a1b89b7ab68f62fd53043b116ecb5ef476004095a7c7b924b12695b7e87e21dd547f66e72fa02a972f0d')
validpgpkeys=(
  'D7FCF61CF9A2DEAB31D81BD3F3D322D0EC4582C3'  # Carlos Garcia Campos <cgarcia@igalia.com>
  '5AA3BC334FD7E3369E7C77B291C559DBE4C9123B'  # Adrián Pérez de Castro <aperez@igalia.com>
)

prepare() {
  cd webkitgtk-$pkgver

  # Requested by eworm
  # https://github.com/WebKit/WebKit/pull/17909
  patch -Np1 -i ../GTK-MiniBrowser-should-hide-the-toolbar-when-using-full-screen.patch

  # https://bugs.archlinux.org/task/79783
  # https://github.com/WebKit/WebKit/pull/18614
  patch -Np1 -i ../GTK-Disable-DMABuf-renderer-for-NVIDIA-proprietary-drivers.patch


  ## arch-specific patching ##

  case "$CARCH" in
  i686)
    options+=(!debug)

    # disable SSE2 on 486 and 686
    echo "applying webkitgtk-2.36.3-no-sse2.arch32.patch"
    patch -Rp1 -i "$srcdir"/webkitgtk-2.36.3-no-sse2.arch32.patch
    ;;
  esac
}

build() {
  local cmake_options=(
    -DPORT=GTK
    -DCMAKE_BUILD_TYPE=Release
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_INSTALL_LIBDIR=lib
    -DCMAKE_INSTALL_LIBEXECDIR=lib
    -DCMAKE_SKIP_RPATH=ON
    -DUSE_AVIF=ON
    -DUSE_SOUP2=ON
    -DENABLE_DOCUMENTATION=ON
    -DENABLE_MINIBROWSER=ON
  )

  # GCC with LTO fails to link libjavascriptcoregtk
  #     /usr/bin/ld: /tmp/ccXxyWZV.ltrans0.ltrans.o: in function `ipint_table_size_validate':
  #     <artificial>:(.text+0x49f0f): undefined reference to `ipint_extern_table_size'
  #     /usr/bin/ld: /tmp/ccXxyWZV.ltrans0.ltrans.o: in function `ipint_table_fill_validate':
  #     <artificial>:(.text+0x4a019): undefined reference to `ipint_extern_table_fill'
  #     collect2: error: ld returned 1 exit status
  export CC=clang CXX=clang++
  LDFLAGS+=" -fuse-ld=lld"

  # JITted code crashes when CET is used
  CFLAGS+=' -fcf-protection=none'
  CXXFLAGS+=' -fcf-protection=none'

  # Produce minimal debug info: 4.3 GB of debug data makes the
  # build too slow and is too much to package for debuginfod
  CFLAGS+=' -g1'
  CXXFLAGS+=' -g1'


  ## parabola config ##

  # clang-16: error: unknown argument: '-fvar-tracking-assignments'
  CFLAGS="${CFLAGS/-fvar-tracking-assignments/}"
  CXXFLAGS="${CXXFLAGS/-fvar-tracking-assignments/}"


  ## arch-specific config ##

  case "${CARCH}" in
  armv7h)
    export -n CC CXX
    LDFLAGS="${LDFLAGS/ -fuse-ld=lld/}"
    [[ $CARCH == "armv7h" ]] && CFLAGS=`echo $CFLAGS | sed -e 's/neon/vfpv3/'` && CXXFLAGS="$CFLAGS"
    ;;
  i686)
    # linker runs out of memory on 32-bit
    LDFLAGS+=" -Wl,--no-keep-memory"

    # do not build JIT code on non-pentium4 (breaks in micro-optimizations)
    cmake_options+=( -DENABLE_JIT=OFF -DENABLE_C_LOOP=ON -DENABLE_SAMPLING_PROFILER=OFF )

    # Could NOT find GIDocgen (missing: GIDocgen_VERSION) (found version "")
    # ENABLE_INTROSPECTION is needed for gi-docgen.
    cmake_options+=( -DENABLE_DOCUMENTATION=OFF )

    # Package 'gstreamer-webrtc-1.0' has version '1.16.2', required version is '>= 1.18.4'
    # GStreamerTranscoder >= 1.20 is needed for USE_GSTREAMER_TRANSCODER.
    cmake_options+=( -DUSE_GSTREAMER_TRANSCODER=OFF )
    ;;
  esac
  local parabola_options=( -DENABLE_GEOLOCATION=OFF )
  cmake_options+=( ${parabola_options[@]} )


  ## upstream ##

  cmake -S webkitgtk-$pkgver -B build -G Ninja "${cmake_options[@]}"
  cmake --build build
}

package_webkit2gtk() {
  depends+=(
    libWPEBackend-fdo-1.0.so
    libwpe-1.0.so
  )
  depends+=( libicui18n.so libicuuc.so )
  provides+=(
    libjavascriptcoregtk-4.0.so
    libwebkit2gtk-4.0.so
  )
  optdepends=(
    # 'geoclue: Geolocation support' # anti-feature
    'gst-libav: nonfree media decoding'
    'gst-plugins-bad: media decoding'
    'gst-plugins-good: media decoding'
  )

  DESTDIR="$pkgdir" cmake --install build

  rm -r "$pkgdir/usr/bin"

  mkdir -p doc/usr/share
  mv {"$pkgdir",doc}/usr/share/gtk-doc

  cd webkitgtk-$pkgver
  find Source -name 'COPYING*' -or -name 'LICENSE*' -print0 | sort -z |
    while IFS= read -d $'\0' -r _f; do
      echo "### $_f ###"
      cat "$_f"
      echo
    done |
    install -Dm644 /dev/stdin "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_webkit2gtk-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv doc/* "$pkgdir"
}

