.PHONY: .FORCE rebuild-all check-all accept-all
.SUFFIXES:

ALL := $(wildcard *.in)

DISABLED=FailRange.in

IN=$(filter-out $(DISABLED), $(ALL))

default: check

# Rebuild all .out files
BUILD_TARGETS := $(IN:.in=.out)
build: ${BUILD_TARGETS}

# Check that .out files conform to .out.expect references
CHECK_TARGETS := $(BUILD_TARGETS:=.check)
check: ${CHECK_TARGETS}

# Show diffs of non-matching files
DIFF_TARGETS := $(BUILD_TARGETS:=.diff)
diff: ${DIFF_TARGETS}

# Update .expect.out files from .out files
ACCEPT_TARGETS := $(BUILD_TARGETS:=.expected)
accept: ${ACCEPT_TARGETS}

# Remove all .out files
clean:
	rm -f *.out

touch:
	touch ${IN}

.FORCE:

%.in: .FORCE

FSTAR_HOME = ../../..
include $(FSTAR_HOME)/ulib/gmake/z3.mk

ifdef Z3
SMT=--smt $(Z3)
endif

FSTAR:=${FSTAR_HOME}/bin/fstar.exe $(SMT) --ide --warn_error -282


# Feed .in to F* and record output as .out.  Output is passed through cleanup.py
# to ensure that the output is deterministic by pretty-printing JSON messages
# (otherwise the order of fields in JSON dictionaries might vary across runs)
# We turn off the .checked file cache so we don't get any races in CI
%.out: %.in $(FSTAR_HOME)/bin/fstar.exe
	$(eval FST := $(firstword $(subst ., ,$<)))
	$(FSTAR) --cache_off "$(realpath ${FST}.fst)" < "$<" | python cleanup.py "$@"

# Clean up and accept an existing ‘.out’ file (possibly generated by
# ‘fstar-write-transcript’, or possibly by ‘make build’) and save it as
# ‘.out.expected’.
%.out.expected: %.out
	python cleanup.py "$@" < "$<"

# Verify that a given output matches the corresponding input
%.out.check: %.out
	diff -u "$<.expected" "$<"
