#!/bin/sh
set -e
export CFLAGS="-Wno-error=unused-but-set-variable -Wno-error=unused-function  -Wformat -Werror=format-y2k"
make_configure=0
opt_level=1
low_mem_mode=""
avx_support=""

help() {
    echo "Usage:"
    echo "  -c              configure"
    echo "  -O              [options]"
    echo "                  default: -O 1"
    echo "                  -O 0 : Disable all optimization, include basic"
    echo "                  -O 1 : Open stable optimization"
    echo "                  -O 2 : Open unstable optimization, include O1"
    echo "                  -O 3 : Open testing optimization, include O2"
    echo "  -l              low memory mode"
    echo "                  -l 0 : Open shadow file, close CONFIG_LATX_LARGE_CC"
    echo "                  -l 1 : l0 + close CONFIG_LATX_SPLIT_TB, CONFIG_LATX_TU, CONFIG_LATX_JRRA"
    echo "                  -l 2 : l1 + set 64MB code cache, close CONFIG_LATX_INSTS_PATTERN"
    echo "  -a              AVX Instruction Translation Support"
    echo "  -h              help"
}

parseArgs() {
    while getopts "cO:l:h,a" opt; do
        case ${opt} in
        c)
            make_configure=1
            ;;
        O)
            opt_level="$OPTARG"
            ;;
        l)
            low_mem_mode=""--low_mem_mode"${OPTARG}"
            ;;
        a)
            avx_support="--enable-latx-avx-opt"
            ;;
        h)
            help
            exit
            ;;
        # 若选项需要参数但未收到，则走冒号分支
        :)
            help
            exit
            ;;
        # 若遇到未指定的选项，会走问号分支
        ?)
            help
            exit
            ;;
        esac
    done
}

make_cmd() {

    cd $(dirname $0)/../
    if [ $make_configure -eq 1 ] ; then
        rm -rf build32
    fi
    mkdir -p build32
    cd build32

    if [ $make_configure -eq 1 ] ; then
        if [ "$opt_level" = "0" ] ; then
            ../configure --target-list=i386-linux-user --enable-latx \
                --enable-guest-base-zero --disable-debug-info --optimize-O0 \
                --disable-docs ${low_mem_mode} ${avx_support}
        elif [ "$opt_level" = "1" ] ; then
            ../configure --target-list=i386-linux-user --enable-latx \
                --enable-guest-base-zero --disable-debug-info --optimize-O1 \
                --disable-docs ${low_mem_mode} ${avx_support}
        elif [ "$opt_level" = "2" ] ; then
            ../configure --target-list=i386-linux-user --enable-latx \
                --enable-guest-base-zero --disable-debug-info --optimize-O2 \
                --disable-docs ${low_mem_mode} ${avx_support}
        elif [ "$opt_level" = "3" ] ; then
            ../configure --target-list=i386-linux-user --enable-latx \
                --enable-guest-base-zero --disable-debug-info --optimize-O3 \
                --disable-docs ${low_mem_mode} ${avx_support}
        else
            echo "invalid options"
        fi
    fi

    if [ ! -f "/usr/bin/ninja" ]; then
        make -j $(nproc)
    else
        ninja
    fi
}

parseArgs "$@"
make_cmd
