#!/usr/bin/sh
set -eu

BUILD="$(mktemp --directory --tmpdir=.)"
trap 'rm -rf "$BUILD"' EXIT

meson format --recursive --inplace || true
if ! git diff --exit-code
then
  echo Your changes must meet the upstream meson style guide
  echo
  echo https://mesonbuild.com/Style-guide.html
  echo https://mesonbuild.com/Commands.html#format
  exit 1
fi

# Make sure if the change touches the public headers, it also updates the
# changelog.
if ! git show --format="" --name-only HEAD -- CHANGELOG.md include/libpldm |
  awk -f scripts/changelog.awk
then
  echo You must document your changes under include/libpldm in CHANGELOG.md
  exit 1
fi

# Ensure the test suite passes in the default configuration. Note
# that we don't specify -Dabi=... - the default is equivalent to
# -Dabi=deprecated,stable,testing.
CC=gcc CXX=g++ CFLAGS=-fanalyzer meson setup -Dabi-compliance-check=false "$BUILD"
meson compile -C "$BUILD"
meson test -C "$BUILD"

# Ensure the test suite passes in release mode. libpldm specifies
# -Db_ndebug=if-release by default, so building with --buildtype=release passes
# -DNDEBUG to the compiler for the library implementation. This build
# configuration will catch any unexpected changes in the library implementation
# and incorrect test case implementations.
meson configure --buildtype=release "$BUILD"
meson compile -C "$BUILD"
meson test -C "$BUILD" --timeout-multiplier 10 --wrapper 'valgrind --error-exitcode=1'

# Ensure the test suite links when testing symbols are removed from the ABI
meson configure --buildtype=debug "$BUILD"
meson configure -Dabi=deprecated,stable "$BUILD"
meson compile -C "$BUILD"
meson test -C "$BUILD"