#!/bin/bash
#
# configure_qemu.sh — Configure QEMU for a stripped, minimal x86_64 build
# ---------------------------------------------------------------------------
# Must be run from inside the QEMU source tree (it invokes ./configure).
# Output goes to a local build/ subdir so the source tree stays clean.
#
# Rationale: hiperiso needs only x86_64 system emulation with KVM and the
# simpletrace backend. Every optional feature is disabled to shrink the
# resulting qemu-system-x86_64 binary that ships inside the initramfs.
#
set -euo pipefail

# Allow callers to pass extra flags, e.g. EXTRA_QEMU_CONFIGURE="--enable-werror"
EXTRA_CONFIGURE="${EXTRA_QEMU_CONFIGURE:-}"

echo "=== Configuring QEMU (stripped x86_64-softmmu, KVM + simpletrace) ==="

# shellcheck disable=SC2086  # intentional: let EXTRA_CONFIGURE word-split into separate configure arguments
./configure \
    --target-list=x86_64-softmmu \
    --enable-kvm \
    --enable-trace-backends=simple \
    \
    --disable-tools \
    --disable-gtk \
    --disable-sdl \
    --enable-vnc \
    --disable-curses \
    --disable-brlapi \
    --disable-virtfs \
    --disable-install-blobs \
    --disable-guest-agent \
    --disable-docs \
    --disable-libusb \
    --disable-usb-redir \
    --disable-opengl \
    --disable-spice \
    --disable-smartcard \
    --disable-libnfs \
    --disable-libiscsi \
    --disable-rbd \
    --disable-glusterfs \
    --disable-zstd \
    --disable-lzo \
    --disable-bzip2 \
    --disable-snappy \
    --disable-lzfse \
    --disable-capstone \
    --disable-slirp \
    --disable-werror \
    $EXTRA_CONFIGURE

echo "=== QEMU configured. Now run: make -j\$(nproc) qemu-system-x86_64 ==="
