CFLAGS := -DFAILLOOP_INCLUDE=$(FAILLOOP_INCLUDE)
CFLAGS += -std=c99
CFLAGS += -Wall -Wextra -Wpedantic
CFLAGS += -O3 -DNDEBUG

CFLAGS_EXTRA := 
CFLAGS_EXTRA += -Wno-misleading-indentation # comment line if not supported
CFLAGS_EXTRA += -flto=auto                  # comment line if not supported
CFLAGS_EXTRA += -flarge-source-files        # comment line if not supported

all: build build/failtest

build/failtest: \
  build/failloop.o \
  build/failloop_args.o \
  build/failtest.o \
  # keep this empty line
	cc $(CFLAGS) build/fail*.o -o $@

build/failloop.o: src/failloop.c src/failloop.h
	cc -c $(CFLAGS) $(CFLAGS_EXTRA) src/failloop.c -o $@

build/failloop_args.o: \
  src/failloop_args.c \
  src/failloop_args.h \
  src/failloop.h \
  # keep this empty line
	cc -c $(CFLAGS) src/failloop_args.c -o $@

build/failtest.o: \
  src/failtest.c \
  src/failloop_args.h \
  src/failloop.h \
  # keep this empty line
	cc -c $(CFLAGS) src/failtest.c -o $@

build:
	mkdir $@

test: $(wildcard tests/*.sh)
	@ for s in $(patsubst %.sh,%,$(notdir $?)); do \
          echo -n $$s ; echo -n ' ' ; \
          sh tests/$$s.sh > build/test.out.txt 2> build/test.tmp.txt ; \
          grep -e "^error: line " build/test.tmp.txt > build/test.err.txt ; \
          if [ -z "`diff -q build/test.out.txt tests/$$s.out.txt`" ] && \
             [ -z "`diff -q build/test.err.txt tests/$$s.err.txt`" ] ; then \
            echo ok ; \
          else \
            echo failed ; \
          fi ; \
	done

clean:
	rm -f *~ src/*~ pre/*~ tests/*~ tests/*.tmp.txt
	rm -f build/*.o build/test.*.txt build/test.std.pre

.PHONY: all test clean
