# Maintainer: Eric Engestrom <aur@engestrom.ch>

pkgname=fex-emu
pkgver=2608
pkgrel=1
pkgdesc='Fast usermode x86 and x86-64 emulator for Arm64'
url=https://fex-emu.com
arch=(aarch64)
license=(MIT)
makedepends=(git cmake ninja clang lld llvm python-setuptools)
depends=(qt6-declarative qt6-base squashfs-tools squashfuse
         erofs-utils libxxhash.so)
source=("git+https://github.com/FEX-Emu/FEX#tag=FEX-$pkgver"
        "git+https://github.com/catchorg/Catch2"
        "git+https://github.com/KhronosGroup/Vulkan-Headers"
        "git+https://github.com/Sonicadvance1/cpp-optparse"
        "git+https://github.com/FEX-Emu/drm-headers"
        "git+https://github.com/FEX-Emu/fex-gcc-target-tests-bins"
        "git+https://github.com/FEX-Emu/fex-gvisor-tests-bins"
        "git+https://github.com/FEX-Emu/fex-posixtest-bins"
        "git+https://github.com/fmtlib/fmt"
        "git+https://github.com/Sonicadvance1/imgui"
        "git+https://github.com/FEX-Emu/jemalloc"
        "git+https://github.com/FEX-Emu/robin-map"
        "git+https://github.com/FEX-Emu/vixl"
        "git+https://github.com/herumi/xbyak"
        "git+https://github.com/Cyan4973/xxhash"
        )
sha256sums=('9418fadbc9922ebda194d0e5d4958d63a55c90632d52115fee47c9825f156efd'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP')

prepare() {
  cd FEX
  git submodule init
  for f in \
    Catch2 \
    Vulkan-Headers \
    drm-headers \
    fex-gcc-target-tests-bins \
    fex-gvisor-tests-bins \
    fex-posixtest-bins \
    fmt \
    imgui \
    jemalloc \
    robin-map \
    vixl \
    xbyak \
    xxhash \
    ;
  do
    git config submodule."External/$f".url "$srcdir/$f"
  done

  git config submodule."External/jemalloc_glibc".url "$srcdir/jemalloc"

  for f in \
    cpp-optparse \
    ;
  do
    git config submodule."Source/Common/$f".url "$srcdir/$f"
  done

  git -c protocol.file.allow=always submodule update

  find External/jemalloc* -iname '*.h' -exec \
    sed -i -e 's/#define *LG_PAGE *12/#define LG_PAGE 14/g' {} \;
}

build() {
  CC=clang \
  CXX=clang++ \
  cmake \
    -S FEX -B build -G Ninja \
    -DENABLE_OFFLINE_TELEMETRY=Off \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=Release \
    -DFMT_INSTALL=Off \
    -DBUILD_TESTING=Off \
    -DUSE_LINKER=lld \
    -DENABLE_LTO=On \
    -DTUNE_CPU=apple-m1
  ninja -C build
}

package() {
  DESTDIR="$pkgdir" ninja -C build install

  install -Dm644 "$srcdir"/FEX/LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}
