# BlueRune's pinned copy of Virtical's AUR recipe qemu-vmvga
# (https://git.joeypayne.com/jyapayne/virtical, packaging/aur/qemu-vmvga at 94dc219a819f4391003abda02710f976f98f7473),
# unchanged below this comment. release/publish.sh --extras builds it in a clean chroot and
# publishes it to BlueRune's repository. To move to a newer version, copy Virtical's
# packaging/aur/qemu-vmvga over this directory and update the commit above.
#
# Maintainer: Joey Yakimowich-Payne <jyapayne@pm.me>
#
# QEMU with qemus/qemu-vmvga's VMware SVGA II + SVGA3D adapter (`-device vmvga`) and the
# DXVK Native it renders Direct3D with, plus Virtical's patches, installed into
# /usr/lib/virtical/qemu-vmvga next to (never over) the system QEMU. The same recipe as
# packaging/qemu-vmvga/build.sh in Virtical's source: packaging/aur/release.sh copies the pins
# from there, and the patches come from the Virtical tag $_virtical.
pkgname=qemu-vmvga
pkgver=11.1.1
pkgrel=1
_virtical=0.1.0
_vmvga_commit=073c4dc64e49dd082c70f249dd883cba7ea93f43
_dxvk_version=2.7.1
_dxvk_commit=c3dd74be6baec53786d4e064a572185b70347a17
_patches=(
    '0001-vmvga-no-3d-without-kvm-vmware-backdoor.patch'
    '0002-ui-refresh-interval.patch'
    '0003-vmvga-report-d3d11-failure.patch'
    '0004-vmvga-advertise-video-blt.patch'
    '0005-vmvga-dx-format-caps-for-d3d11.patch'
    '0006-vmvga-default-vgpu-10.patch'
    '0007-vmport-wddm-2-off.patch'
    '0008-vmvga-reset-release-scanout.patch'
    '0009-vmport-tclo-resolution.patch'
    '0010-vmport-copy-paste.patch'
    '0011-vmport-vnc-tools-channel.patch'
    '0012-vnc-back-forward-buttons.patch'
    '0013-vmport-dnd-from-host.patch'
)
pkgdesc='QEMU with the VMware SVGA 3D adapter (qemu-vmvga) and DXVK Native, for Virtical'
arch=('x86_64')
url='https://github.com/qemus/qemu-vmvga'
license=('GPL-2.0-only' 'GPL-2.0-or-later' 'LGPL-2.1-or-later' 'MIT' 'Zlib')
# vulkan-icd-loader is loaded with dlopen() by DXVK, which links libstdc++ statically.
depends=(
    'alsa-lib' 'bzip2' 'curl' 'glib2' 'glibc' 'gnutls' 'keyutils' 'libaio' 'libcacard' 'libelf'
    'libepoxy' 'libgcc' 'libjpeg-turbo' 'libpipewire' 'libpng' 'libpulse' 'libsasl' 'libseccomp'
    'libslirp' 'libusb' 'liburing' 'lzo' 'mesa' 'ncurses' 'numactl' 'pam' 'pixman' 'snappy'
    'spice' 'systemd-libs' 'usbredir' 'vulkan-icd-loader' 'zlib' 'zstd'
)
makedepends=('git' 'glslang' 'libcap-ng' 'ninja' 'python' 'sdl2-compat' 'spice-protocol')
optdepends=('virtical: manages VMs on this adapter (Settings › Display › VMware SVGA 3D)')
install=qemu-vmvga.install
source=(
    "https://download.qemu.org/qemu-$pkgver.tar.xz"
    "qemu-vmvga::git+https://github.com/qemus/qemu-vmvga.git#commit=$_vmvga_commit"
    "dxvk::git+https://github.com/doitsujin/dxvk.git#commit=$_dxvk_commit"
    'mingw-directx-headers::git+https://github.com/Joshua-Ashton/mingw-directx-headers.git'
    'Vulkan-Headers::git+https://github.com/KhronosGroup/Vulkan-Headers.git'
    'SPIRV-Headers::git+https://github.com/KhronosGroup/SPIRV-Headers.git'
    'libdisplay-info::git+https://github.com/doitsujin/libdisplay-info.git'
)
source+=("${_patches[@]/#/https://git.joeypayne.com/jyapayne/virtical/raw/tag/v$_virtical/packaging/qemu-vmvga/patches/}")
sha256sums=(
    '079ffbff8a7111bbc89022107cbabf3bbfd614d5fc9d7cc675991196aca12482'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'
    'f1fee13f19d148985164e493bec9a60d7a42b1c42570cfd181c14379662a1a67'
    '0270cc8b22a1e4477788d661ff55cb667fb69094fd98af811ae4db974f81a85f'
    '0134096593d788eb9476c1f59bfd7ca8560a6da52613683b15e58117ea81ca91'
    '9ebc933feb5ac1c7a430f7403f13ce815981f8b2fa5643a671e37f20343829fb'
    'dcab1d06f69ef08494edc3ce7d8732cfb604a960f82650170833272bccc7965c'
    '00e6f2dcdb0ccb4dd7845ea943332d670657f37f02ba0e72d19e91775eb097a5'
    '782edf8c5c230e973008d37e4dabcae56056db8a6d938b6db6ae80d6032344dd'
    '40675cb5d57ccfbfb0604a665006bf86b3073c85b11270f0284464f701ab335b'
    'b7d470625002105111a7e5abe21e1630649bb79be47a7762e403085fceab4300'
    '006d4dfa734c3342641a5b13dee239fb13a034cc79b3c59b1d3fe1f47058eda9'
    '36c083fb9631ccc3e4670e33dc86e8ee5ed800957dff8d66fbbc163acbc90174'
    'a7dd20ad816bfe5cfd6140ce8b0c855063e0b43ee2b6fe3d687ec2414cae2b99'
    '42ee956a117527b1c85da7cf625eddc8c10b1d14ce2edf009d7d2802fd34a7c5'
)

_prefix=/usr/lib/virtical/qemu-vmvga

prepare() {
    # DXVK's submodules, at the commits its tag records, from the sources above.
    cd dxvk
    git submodule init
    git config submodule.include/native/directx.url "$srcdir/mingw-directx-headers"
    git config submodule.include/vulkan.url "$srcdir/Vulkan-Headers"
    git config submodule.include/spirv.url "$srcdir/SPIRV-Headers"
    git config submodule.subprojects/libdisplay-info.url "$srcdir/libdisplay-info"
    git -c protocol.file.allow=always submodule update

    # qemu-vmvga is an overlay of hw/ onto the QEMU release; then Virtical's patches.
    cd "$srcdir/qemu-$pkgver"
    cp -a ../qemu-vmvga/hw/. hw/
    local p
    for p in "${_patches[@]}"; do
        echo "applying $p"
        patch -Np1 -i "$srcdir/$p"
    done
}

build() {
    # QEMU's --version carries the same id as build.sh's build of these pins and patches.
    local patches_id
    patches_id=$(cd "$srcdir" && cat "${_patches[@]}" | sha256sum | cut -c1-7)

    # The flags are build.sh's. The device dlopen()s DXVK by soname; QEMU's RPATH (DT_RPATH,
    # which also covers what DXVK loads) points at the prefix's lib/, where DXVK goes.
    cd "qemu-$pkgver"
    ./configure \
        --prefix="$_prefix" \
        --target-list="$CARCH-softmmu" \
        --with-pkgversion="virtical-vmvga-${_vmvga_commit:0:7}-dxvk$_dxvk_version-p$patches_id" \
        --extra-ldflags="-Wl,-rpath,$_prefix/lib -Wl,--disable-new-dtags" \
        --enable-kvm \
        --enable-vnc --enable-vnc-jpeg --enable-png \
        --enable-spice \
        --enable-slirp \
        --enable-tpm \
        --enable-libusb --enable-usb-redir \
        --enable-seccomp --enable-cap-ng \
        --enable-linux-aio --enable-linux-io-uring \
        --enable-vhost-net --enable-vhost-user \
        --disable-modules \
        --disable-docs --disable-tools --disable-guest-agent \
        --disable-gtk --disable-sdl --disable-xen \
        --disable-rbd --disable-libiscsi --disable-libnfs --disable-brlapi \
        --disable-plugins --disable-debug-info
    make

    # DXVK Native, 64-bit with only the SDL2 window-system integration (the device stands in
    # for SDL2 with a headless shim), built with the Meson in QEMU's build venv. Its libraries
    # find each other next to themselves ($ORIGIN).
    cd "$srcdir"
    LDFLAGS="$LDFLAGS -Wl,-rpath,\$ORIGIN" "qemu-$pkgver/build/pyvenv/bin/meson" setup dxvk-build dxvk \
        --prefix="$_prefix" --libdir=lib --buildtype=release \
        -Dbuild_id=true \
        -Dnative_sdl2=enabled -Dnative_sdl3=disabled -Dnative_glfw=disabled \
        --force-fallback-for=libdisplay-info
    "qemu-$pkgver/build/pyvenv/bin/meson" compile -C dxvk-build
}

check() {
    "qemu-$pkgver/build/qemu-system-$CARCH" --version
    "qemu-$pkgver/build/qemu-system-$CARCH" -device help | grep -F '"vmvga"'
}

package() {
    "qemu-$pkgver/build/pyvenv/bin/meson" install -C dxvk-build --no-rebuild --destdir "$pkgdir"
    make -C "qemu-$pkgver" install DESTDIR="$pkgdir"
    # DXVK's headers and pkg-config files, the static libfdt, and QEMU's desktop entry and
    # icons: nothing builds against this prefix, and the emulator isn't an application of its own.
    rm -r "$pkgdir$_prefix"/{include,lib/pkgconfig,lib/libfdt.a,share/applications,share/icons}
    # QEMU installs every target's firmware; this one emulates the host's (x86_64) alone.
    (
        cd "$pkgdir$_prefix/share/qemu"
        rm -r dtb QEMU,*.bin ast27x0_bootrom.bin hppa-firmware*.img npcm*_bootrom.bin \
            openbios-* opensbi-* palcode-clipper pnv-pnor.bin qemu-nsis.bmp qemu_vga.ndrv \
            s390-ccw.img skiboot.lid slof.bin u-boot* vof*.bin
        find . firmware -maxdepth 1 -name '*edk2-*' ! -name '*edk2-i386*' ! -name "*edk2-$CARCH*" \
            ! -name edk2-licenses.txt -delete
    )

    install -Dm644 "qemu-$pkgver/COPYING" "$pkgdir/usr/share/licenses/$pkgname/COPYING.qemu"
    install -Dm644 "qemu-$pkgver/COPYING.LIB" "$pkgdir/usr/share/licenses/$pkgname/COPYING.LIB.qemu"
    install -Dm644 qemu-vmvga/license.md "$pkgdir/usr/share/licenses/$pkgname/LICENSE.qemu-vmvga"
    install -Dm644 dxvk/LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE.dxvk"
}
