# Maintainer: Hans-Kristian Arntzen <post@arntzen-software.no>
# Contributor: Joshua Ashton <joshua@froggi.es>
#
# Based off the AUR package available here: https://aur.archlinux.org/packages/renderdoc
#
# Holo: Use clang instead of GCC. RenderDoc is not really tested with GCC.
# Holo: Apply a small post-cmake hack to workaround Arch forcing LTCG for Qt. This breaks clang.
# Holo: Apply experimental wayland support which is required for Gamescope WSI layer to work,
# but should only apply to Deck. Should never be enabled by default in upstream PKGBUILD.
# Holo: Back-port a patch from v1.x which fixes replay when using gamescope WSI layer.
# Only relevant for Deck.

# This so that our renderdoc android apks get updated in lockstep
source ../renderdoc/renderdoc_common.pkgbuild

pkgname=renderdoc
pkgver=${_pkgver:-1}
pkgrel=${_pkgrel:-1}
pkgdesc="OpenGL and Vulkan debugging tool"
arch=(x86_64 aarch64)
url="https://github.com/baldurk/renderdoc"
license=("MIT")
makedepends=("pcre" "cmake" "python" "clang")
depends=("libx11" "libxcb" "mesa" "libgl" "qt5-base" "qt5-svg" "qt5-x11extras" "xcb-util-keysyms")
source=("git+https://gitlab.steamos.cloud/deckard/renderdoc.git#commit=${_commit}")
validpgpkeys=('1B039DB9A4718A2D699DE031AC612C3120C34695')
sha512sums=('SKIP')

# Baldur recommends not using LTO.
options=(!lto)

build() {
  # Baldur recommends clang over GCC.
  # For Gamescope WSI layer, we require Wayland to work,
  # so we have to enable the experimental feature despite its dubious nature.
  cmake -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DBUILD_VERSION_STABLE=ON \
        -DCMAKE_C_COMPILER=clang \
        -DCMAKE_CXX_COMPILER=clang++ \
        -DENABLE_UNSUPPORTED_EXPERIMENTAL_POSSIBLY_BROKEN_WAYLAND=ON \
        -DBUILD_VERSION_DIST_CONTACT="sabae@valvesoftware.com" \
        -DBUILD_VERSION_DIST_NAME="SteamOS" \
        -DBUILD_VERSION_DIST_VER="${pkgver}" \
        -B"${srcdir}/renderdoc"/build \
        -H"${srcdir}/renderdoc"
  # For some reason, LTCG is enabled by default in qmake.
  # This seems to be an Arch Linux default for whatever reason, but it breaks renderdoc build.
  echo "CONFIG-=ltcg" >> "${srcdir}/renderdoc"/build/qrenderdoc/qrenderdoc_cmake.pri
  cmake --build "${srcdir}/renderdoc/build"
}

package() {
  make DESTDIR="${pkgdir}" -C "${srcdir}/renderdoc/build" install
  mkdir -p "${pkgdir}/usr/share/licenses/$pkgname"
  install -Dm644 "${srcdir}/renderdoc/LICENSE.md" "${pkgdir}/usr/share/licenses/renderdoc/LICENSE"
}
