#!/bin/bash
set -e
set -x

ROOT=`git rev-parse --show-toplevel`
NPROCS="$(nproc 2> /dev/null || sysctl -n hw.ncpu)"
HERE=`pwd`
CI="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
INSTALL=$HERE/install
BUILD=$HERE/build
RUN=$HERE/run

# build pk
rm -rf $BUILD/pk
mkdir $BUILD/pk
cd $BUILD/pk
git clone https://github.com/riscv-software-src/riscv-pk.git
riscv-pk/configure --host=riscv64-linux-gnu --prefix=$INSTALL
make -j$NPROCS
make install

# build tests
rm -rf $RUN
mkdir -p $RUN
cd $RUN
riscv64-linux-gnu-gcc -static -O2 -o hello $CI/hello.c
riscv64-linux-gnu-gcc -static -O2 -o dummy-slliuw $CI/dummy-slliuw.c
riscv64-linux-gnu-gcc -static -O2 -o customcsr $CI/customcsr.c
riscv64-linux-gnu-gcc -static -O2 -o atomics $CI/atomics.c
riscv64-linux-gnu-gcc -static -O2 -march=rv64gcv -o vector-sum $CI/vector-sum.c

# run snippy-based tests
wget https://github.com/syntacore/snippy/releases/download/snippy-2.1/snippy-x86_64-linux.tar.xz
tar xf snippy-x86_64-linux.tar.xz

# test that snippy runs
bin/llvm-snippy --version | grep "Snippy version: 2.1.0"
PATH="$PATH:$RUN/bin" "$ROOT"/ci-tests/run-snippy-tests.sh "$RUN" "$ROOT"/ci-tests/snippy-tests "$INSTALL"/bin/spike


# check that including sim.h in an external project works
g++ -std=c++2a -I$INSTALL/include -L$INSTALL/lib $CI/testlib.cc -lriscv -o test-libriscv
g++ -std=c++2a -I$INSTALL/include -L$INSTALL/lib $CI/test-customext.cc -lriscv -o test-customext
g++ -std=c++2a -I$INSTALL/include -L$INSTALL/lib $CI/custom-csr.cc -lriscv -o test-custom-csr

# check that all installed headers are functional
g++ -std=c++2a -I$INSTALL/include -L$INSTALL/lib $CI/testlib.cc -lriscv -o /dev/null -include $BUILD/spike/install-hdrs-list.h

# run tests
time $INSTALL/bin/spike --isa=rv64gc $BUILD/pk/pk hello | grep "Hello, world!  Pi is approximately 3.141588."
time $INSTALL/bin/spike --isa=rv64gcv $BUILD/pk/pk vector-sum | grep "The sum of the first 1000 positive integers is 500500."
$INSTALL/bin/spike --log-commits --isa=rv64gc $BUILD/pk/pk atomics 2> /dev/null | grep "First atomic counter is 1000, second is 100"
LD_LIBRARY_PATH=$INSTALL/lib ./test-libriscv $BUILD/pk/pk hello | grep "Hello, world!  Pi is approximately 3.141588."
LD_LIBRARY_PATH=$INSTALL/lib ./test-customext $BUILD/pk/pk dummy-slliuw | grep "Executed successfully"
LD_LIBRARY_PATH=$INSTALL/lib ./test-custom-csr $BUILD/pk/pk customcsr | grep "Executed successfully"

echo "DTB discovery tests"
g++ -std=c++2a -I$INSTALL/include -I$CI/dtb-discovery/devices-sample -L$INSTALL/lib  $CI/dtb-discovery/test-dtb-discovery.cc $CI/dtb-discovery/devices-sample/led-device.cc -lriscv -o test-dtb-discovery
#compile sample dts
DTS_DIR="$CI/dtb-discovery/dts-samples"
TMP_DTB_DIR="/tmp/dtb-tests"
mkdir -p "$TMP_DTB_DIR"
DTB_LIST=()
#For each dts execute the test
for dts in "$DTS_DIR"/*.dts; do
    name=$(basename "$dts" .dts)
    dtb="$TMP_DTB_DIR/$name.dtb"
    dtc -I dts -O dtb "$dts" -o "$dtb"
    LD_LIBRARY_PATH=$INSTALL/lib ./test-dtb-discovery  --dtb="$dtb"  $BUILD/pk/pk  hello 
done
