#!/bin/sh -e

TARGETS='i386-softmmu i386-linux-user riscv32-softmmu riscv32-linux-user riscv64-softmmu riscv64-linux-user x86_64-softmmu x86_64-linux-user'


for patch in *.patch; do
    patch -p1 < "$patch"
done

# This is required for linux-headers 5.10
# If you encounter build failures using a later kernel,
# comment out this line:
sed -i 's/#include <sys\/sysinfo.h>//' linux-user/syscall.c

# Remove bash dependency
sed -i 's/env bash/env sh/g' Makefile

./configure \
    --prefix=/usr                      \
    --localstatedir=/var               \
    --sysconfdir=/etc                  \
    --without-default-features         \
    --disable-user                     \
    --disable-debug-info               \
    --disable-gtk                      \
    --disable-plugins                  \
    --disable-guest-agent              \
    --disable-gnutls                   \
    --disable-nettle                   \
    --disable-gcrypt                   \
    --disable-curl                     \
    --disable-fuse                     \
    --disable-tpm                      \
    --enable-sdl                       \
    --enable-curses                    \
    --enable-kvm                       \
    --enable-pie                       \
    --enable-linux-user                \
    --enable-system                    \
    --enable-sanitizers                \
    --enable-slirp                     \
    --enable-tools                     \
    --enable-avx2                      \
    --enable-multiprocess              \
    --enable-membarrier                \
    --enable-coroutine-pool            \
    --target-list="$TARGETS"


# # Build qemu-system* binaries dynamically linked
# ./configure \
    #     --prefix=/usr \
    #     --localstatedir=/var \
    #     --sysconfdir=/etc \
    #     --disable-debug-info \
    #     --disable-gtk \
    #     --disable-docs \
    #     --enable-sdl \
    #     --enable-kvm \
    #     --enable-pie \
    #     --enable-curses \
    #     --disable-user \
    #     --disable-linux-user \
    #     --enable-system

# make
# make DESTDIR="$1" PREFIX=/usr install
# make clean

# # Build qemu Linux user binaries statically linked.
# ./configure \
    #     --prefix=/usr \
    #     --localstatedir=/var \
    #     --sysconfdir=/etc \
    #     --disable-debug-info \
    #     --disable-gtk \
    #     --disable-docs \
    #     --disable-user \
    #     --enable-linux-user \
    #     --disable-system \
    #     --static

make
make DESTDIR="$1" PREFIX=/usr install
