#!/bin/sh
# Verify MicroPython's minimal port (Cortex-M4) with cverif and print a scoreboard.
#
#   scripts/micropython.sh [WORKDIR]
#
# Fetches the pinned MicroPython release into WORKDIR (default: ./micropython-work, outside
# the cverif sources; nothing is fetched if the checkout is already there), builds
# ports/minimal with its own Makefile and flags, and runs cverif on build/firmware.elf with
# every source file the build compiled.  The report is WORKDIR/report.json.
#
# Environment:
#   CVERIF        the cverif binary (default: target/release/cverif, built if missing)
#   CROSS_COMPILE toolchain prefix (default: arm-none-eabi-)
#   WORK_LIMIT    per-function budget (default: cverif's own)
#   JOBS          parallel jobs (default: 8)
#   OPT           flags appended to every compiler command, e.g. OPT=-O2 (the port's own
#                 choice is -Os); each value gets a build directory of its own
#
# Needs: git, make, python3 and an arm-none-eabi GCC with newlib headers.
set -eu

TAG=v1.29.0
COMMIT=0fd6c573ea815774668bbb16b8e197c8822368b2
here=$(cd "$(dirname "$0")/.." && pwd)
work=${1:-$here/micropython-work}
cross=${CROSS_COMPILE:-arm-none-eabi-}
mkdir -p "$work"
work=$(cd "$work" && pwd)
upy=$work/micropython

if [ ! -d "$upy/.git" ]; then
    git clone --quiet --depth 1 --branch "$TAG" https://github.com/micropython/micropython.git "$upy"
fi
got=$(git -C "$upy" rev-parse HEAD)
if [ "$got" != "$COMMIT" ]; then
    echo "micropython.sh: $upy is at $got, expected $TAG ($COMMIT)" >&2
    exit 1
fi

cverif=${CVERIF:-$here/target/release/cverif}
if [ ! -x "$cverif" ]; then
    (cd "$here" && cargo build --release)
fi

port=$upy/ports/minimal
build=build
cc=${cross}gcc
if [ -n "${OPT:-}" ]; then
    # The port's Makefile ends its flags with -Os: a wrapper has the last word.
    build=build$(printf %s "$OPT" | tr -c 'A-Za-z0-9' _)
    cc=$work/cc$(printf %s "$OPT" | tr -c 'A-Za-z0-9' _)
    printf '#!/bin/sh\nexec %sgcc "$@" %s\n' "$cross" "$OPT" >"$cc"
    chmod +x "$cc"
fi
make -C "$port" CROSS=1 V=1 CROSS_COMPILE="$cross" CC="$cc" BUILD="$build" >"$work/$build.log" 2>&1

# The sources the build compiled, from its dependency files ("x.o: path/x.c ...").
find "$port/$build" -name '*.P' | sort | while read -r d; do
    src=$(tr ' \\' '\n\n' <"$d" | grep '\.c$' | head -n 1)
    [ -n "$src" ] && echo "$src"
done >"$work/sources.txt"

# The compiler's own header directories and the macros it defines by itself that are not a
# property of the target (cverif derives those from the binary).
incs=$("$cc" -E -Wp,-v -xc /dev/null 2>&1 >/dev/null |
    sed -n '/^#include <...>/,/^End of search/p' | grep '^ ' | sed 's/^ */-I/')
fortify=$("$cc" -Os -dM -E -xc /dev/null | sed -n 's/^#define _FORTIFY_SOURCE \(.*\)$/-D_FORTIFY_SOURCE=\1/p')

cd "$port"
status=0
# shellcheck disable=SC2046,SC2086
"$cverif" "$build/firmware.elf" $(cat "$work/sources.txt") \
    -I. -I../.. -I"$build" $incs -DNDEBUG -DMICROPY_ROM_TEXT_COMPRESSION=1 $fortify \
    --json "$work/report.json" --list-unsupported -j "${JOBS:-8}" \
    ${WORK_LIMIT:+--work-limit "$WORK_LIMIT"} >"$work/report.txt" 2>"$work/report.err" || status=$?
if [ "$status" -ge 2 ]; then
    cat "$work/report.err" >&2
    exit "$status"
fi
python3 -I "$here/scripts/scoreboard.py" "$work/report.json"
echo
echo "full report: $work/report.txt and $work/report.json"
exit "$status"
