# Maintainer: aur.chaotic.cx

_pkgname="tracy-profiler"
pkgname="$_pkgname-git"
pkgver=0.13.1.r1347.gb0d75ac
pkgrel=1
pkgdesc="Real-time, nanosecond resolution frame profiler"
url="https://github.com/wolfpld/tracy"
license=('BSD-3-Clause')
arch=('x86_64')

depends=(
  'capstone'
  'dbus'
  'freetype2'
  'glfw'
  'libglvnd'
  'libxkbcommon'
  'pugixml'
  'wayland'
)
makedepends=(
  'cmake'
  'git'
  'libxrandr'
  'ninja'
  'wayland-protocols'
)
optdepends=(
  'libxrandr: for legacy X11 profiler'
  'xdg-desktop-portal: file dialogs'
)

options=('!lto')

provides=('tracy' "$_pkgname")
conflicts=('tracy' "$_pkgname")

_pkgsrc="$_pkgname"
source=("$_pkgsrc"::"git+$url.git")
sha256sums=('SKIP')

prepare() {
  # prepare legacy module
  ln -sf profiler "$_pkgsrc"/profiler_x11

  # ensure march=native disabled
  sed -e '/add_compile_options.*=native/d' -i "$_pkgsrc/cmake/config.cmake"

  # fix for capstone 5.x
  sed -E \
    -e 's&\b(detail)\.aarch64\b\b&\1.arm64&g' \
    -e 's&\bAARCH64_OP_IMM\b&ARM64_OP_IMM&g' \
    -e 's&\bAARCH64_OP_MEM\b&ARM64_OP_MEM&g' \
    -e 's&\bAARCH64_OP_REG\b&ARM64_OP_REG&g' \
    -e 's&\bCS_ARCH_AARCH64\b&CS_ARCH_ARM64&g' \
    -i "$_pkgsrc"/server/TracyWorker.cpp \
    "$_pkgsrc"/profiler/src/profiler/TracyDisassembly.cpp \
    "$_pkgsrc"/profiler/src/profiler/TracySourceView.cpp
}

pkgver() {
  cd "$_pkgsrc"
  git describe --long --tags --abbrev=7 --exclude='*[a-zA-Z][a-zA-Z]*' \
    | sed -E 's/^[^0-9]*//;s/([^-]*-g)/r\1/;s/-/./g'
}

build() {
  export CFLAGS+=' -DNDEBUG'
  export CXXFLAGS+=' -DNDEBUG'

  local _cmake_base=(
    -G Ninja
    -DCMAKE_BUILD_TYPE=None
    -DCMAKE_INSTALL_PREFIX='/usr'
    -Wno-author
  )

  local _modules=(
    capture
    csvexport
    import
    profiler
    profiler_x11
    update
  )

  local i
  for i in "${_modules[@]}"; do
    printf "\n### Building %s...\n" "$i"
    local _cmake_options=(
      -B "build${i:+_$i}"
      -S "$_pkgsrc${i:+/$i}"
      ${_cmake_base[@]}

      -DNO_ISA_EXTENSIONS=ON # don't use march=native
      -DDOWNLOAD_CAPSTONE=OFF
      -DBUILD_TESTING=$CHECKFUNC
    )
    if [[ "$i" =~ x11 ]]; then
      _cmake_options+=(-DLEGACY=ON)
    fi

    cmake "${_cmake_options[@]}"
    cmake --build "build${i:+_$i}"
  done
}

package() {
  install -Dm755 build_capture/tracy-capture -t "$pkgdir/usr/bin/"
  install -Dm755 build_csvexport/tracy-csvexport -t "$pkgdir/usr/bin/"
  install -Dm755 build_import/tracy-import-chrome -t "$pkgdir/usr/bin/"
  install -Dm755 build_import/tracy-import-fuchsia -t "$pkgdir/usr/bin/"
  install -Dm755 build_profiler/tracy-profiler -t "$pkgdir/usr/bin/"
  install -Dm755 build_profiler_x11/tracy-profiler "$pkgdir/usr/bin/tracy-profiler-x11"
  install -Dm755 build_update/tracy-update -t "$pkgdir/usr/bin/"

  ln -sf tracy-profiler "$pkgdir/usr/bin/tracy"
  ln -sf tracy-profiler-x11 "$pkgdir/usr/bin/tracy-x11"

  mkdir -pm755 "$pkgdir"/usr/include/Tracy/client
  mkdir -pm755 "$pkgdir"/usr/include/Tracy/common
  mkdir -pm755 "$pkgdir"/usr/include/Tracy/tracy

  cp -p "$_pkgsrc"/public/client/*.{h,hpp} "$pkgdir"/usr/include/Tracy/client/
  cp -p "$_pkgsrc"/public/common/*.{h,hpp} "$pkgdir"/usr/include/Tracy/common/
  cp -p "$_pkgsrc"/public/tracy/*.{h,hpp} "$pkgdir"/usr/include/Tracy/tracy/

  install -Dm644 "$_pkgsrc"/icon/icon.svg "$pkgdir/usr/share/pixmaps/tracy.svg"
  install -Dm644 "$_pkgsrc"/extra/desktop/tracy.desktop -t "$pkgdir/usr/share/applications/"
  install -Dm644 "$_pkgsrc"/extra/desktop/application-tracy.xml -t "$pkgdir/usr/share/mime/packages/"

  install -Dm644 "$_pkgsrc"/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}
