From 1a0d61b33c2365cf413bf584289058d0d9097377 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Wed, 17 Jul 2024 22:02:47 +0300 Subject: make tests a bit more sensible + At least by my standards. Some amount of scripting going on, but should still be fine. Effectively, source.mk contains information to build the test, check.mk checks the generated log from running the test. As a results, check.mk should output OK into reports/$TEST/OK if everything went well, and anything else otherwise. The user can then look at reports/$TEST/log to see what went wrong. I expect to add some more features, such as starting QEMU with gdb and running a single test a bit easier than right now (same limitation as in the top level makefile, i.e. you have to specify make -f scripts/makefile $TEST from within `tests` and you must have ran `make check` at that point so that `tests.mk` is fully generated. Not huge issues, but slightly annoying, somewhat unsure how to work around them properly but we'll see. --- tests/scripts/gen-prog | 34 ++++++++++++++++++++++++++++++++++ tests/scripts/gen-simple | 31 +++++++++++++++++++++++++++++++ tests/scripts/makefile | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100755 tests/scripts/gen-prog create mode 100755 tests/scripts/gen-simple create mode 100644 tests/scripts/makefile (limited to 'tests/scripts') diff --git a/tests/scripts/gen-prog b/tests/scripts/gen-prog new file mode 100755 index 0000000..e56ecc3 --- /dev/null +++ b/tests/scripts/gen-prog @@ -0,0 +1,34 @@ +#!/bin/sh + +NAME= +PROG= +while getopts "n:p:" opt; do + case "$opt" in + n) NAME="$OPTARG";; + p) PROG="$OPTARG";; + *) echo "unrecognised options -$OPTARG" >&2; exit 1; + esac +done + +shift $((OPTIND - 1)) + +# create all subdirectories +mkdir -p $(echo "${@}" | tr ' ' '\n' | sed "s|[^/]*$||;s|^|build/$NAME|" | uniq) + +for s in ${@} +do + obj="build/${NAME}/${s%.*}.o" + dep="${obj}.d" + + echo "${NAME}_OBJS += ${obj}" >> tests.mk + echo "${dep}:" >> tests.mk + echo "-include ${dep}" >> tests.mk + echo "${obj}: $NAME/${s}" >> tests.mk + echo " \$(COMPILE_TEST) -c $NAME/${s} -o ${obj}" >> tests.mk +done + +echo "build/${NAME}/$PROG: \$(${NAME}_OBJS) \$(COMMON)" >> tests.mk +echo " \$(COMPILE_TEST) \$(${NAME}_OBJS) \$(COMMON) \ + -o build/${NAME}/$PROG" >> tests.mk + +echo "include ${NAME}/check.mk" >> tests.mk diff --git a/tests/scripts/gen-simple b/tests/scripts/gen-simple new file mode 100755 index 0000000..ddec9a4 --- /dev/null +++ b/tests/scripts/gen-simple @@ -0,0 +1,31 @@ +#!/bin/sh + +NAME= +PROGS= +while getopts "n:p:" opt; do + case "$opt" in + n) NAME="$OPTARG";; + p) PROGS="$PROGS $OPTARG";; + *) echo "unrecognised option -$OPTARG" >&2; exit 1;; + esac +done + +if [ -z "$NAME" ]; then + echo "No name given for tests" >&2; exit 2; +fi + +if [ -z "$PROGS" ]; then + echo "No programs to add to initrd" >&2; exit 3; +fi + +echo "build/$NAME/initrd: build/$NAME/init \$(COMMON) \$(KMI)" >> tests.mk +echo " echo build/$NAME/init | \$(GEN_INITRD) build/$NAME/initrd" >> tests.mk +echo "reports/$NAME/log: build/$NAME/initrd" >> tests.mk +echo " mkdir -p reports/$NAME" >> tests.mk +echo " rm -f reports/$NAME/*" >> tests.mk +echo " timeout --foreground 30s \$(QEMU) \ + build/$NAME/initrd > reports/$NAME/log" >> tests.mk + +echo "TESTS += $NAME" >> tests.mk +echo ".PHONY: do-$NAME" >> tests.mk +echo "do-$NAME: reports/$NAME/log" >> tests.mk diff --git a/tests/scripts/makefile b/tests/scripts/makefile new file mode 100644 index 0000000..8ea802f --- /dev/null +++ b/tests/scripts/makefile @@ -0,0 +1,42 @@ +.PHONY: all +all: check + +ARCH ?= riscv64 +CROSS_COMPILE ?= $(ARCH)-unknown-elf- + +LLVM ?= 0 +COMPILER != [ "$(LLVM)" != "0" ] \ + && echo clang --target="$(CROSS_COMPILE)" \ + || echo $(CROSS_COMPILE)gcc + +OBFLAGS := -ffreestanding -nostdlib -std=c17 -g +INCLUDEFLAGS := -I ../include -I. +WARNFLAGS := -Wall -Wextra +COMPILE_TEST := $(COMPILER) $(WARNFLAGS) $(INCLUDEFLAGS) $(OBFLAGS) + +GEN_INITRD := cpio -H newc -o > +QEMU := qemu-system-$(ARCH) -machine virt -kernel ../kmi.bin \ + -serial stdio \ + -monitor none \ + -nographic \ + -no-reboot \ + -initrd + +KMI := ../kmi.bin +COMMON := build/printf.o + +build/printf.o: common/printf.c + $(COMPILE_TEST) -c common/printf.c -o build/printf.o + +include tests.mk + +.PHONY: check +check: $(TESTS) + @for d in reports/* ; do \ + if [ ! -f "$$d/OK" ]; then \ + echo "BROKEN: $$d" ; \ + elif [ "$$(tail -n1 $$d/OK)" != "OK" ]; then \ + echo "FAIL: $$d" ; \ + fi \ + done + @echo "Done." -- cgit v1.3