srcdir=@srcdir@
top_srcdir=@top_srcdir@

CC = @CC@
COV_CFLAGS = -ggdb3 -O0 -ftest-coverage -fprofile-arcs -pg
LDFLAGS =
CFLAGS ?= -Wall @CFLAGS@ -I. -I$(top_srcdir)/include
ALL_CFLAGS = $(CFLAGS) @DEFS@
LIBNAME = libnagios.a

all: $(LIBNAME)

SOCKETLIBS=@SOCKETLIBS@
SNPRINTF_O=@SNPRINTF_O@
TESTED_SRC_C := squeue.c kvvec.c iocache.c iobroker.c bitmap.c dkhash.c runcmd.c
TESTED_SRC_C += nsutils.c fanout.c
SRC_C := $(TESTED_SRC_C) prqueue.c worker.c skiplist.c nsock.c
SRC_C += nspath.c
SRC_O := $(patsubst %.c,%.o,$(SRC_C)) $(SNPRINTF_O)
TESTS := $(patsubst %.c,test-%,$(TESTED_SRC_C))

test: $(TESTS)
	@for t in $(TESTS); do echo $$t:; ./$$t || exit 1; echo; done

test-squeue: prqueue.o test-squeue.o t-utils.o
	$(CC) $(ALL_CFLAGS) $(LDFLAGS) $^ -o $@

test-iocache: t-utils.o test-iocache.o
	$(CC) $(ALL_CFLAGS) $(SOCKETLIBS) $^ -o $@

test-iobroker: t-utils.o test-iobroker.o
	$(CC) $(ALL_CFLAGS) $(SOCKETLIBS) $^ -o $@

%.o: $(srcdir)/%.c $(srcdir)/%.h Makefile $(srcdir)/lnag-utils.h
	$(CC) $(ALL_CFLAGS) -c $< -o $@

%.o: $(srcdir)/%.c %.h Makefile $(srcdir)/lnag-utils.h
	$(CC) $(ALL_CFLAGS) -c $< -o $@

test-%.o: $(srcdir)/test-%.c $(srcdir)/%.c $(srcdir)/%.h Makefile
	$(CC) $(ALL_CFLAGS) -c $< -o $@

test-%.o: $(srcdir)/test-%.c $(srcdir)/%.c %.h Makefile
	$(CC) $(ALL_CFLAGS) -c $< -o $@

test-%: t-utils.o test-%.o
	$(CC) $(ALL_CFLAGS) $^ -o $@

$(LIBNAME): $(SRC_O)
	$(AR) cr $@ $^

wproc: wproc.o $(LIBNAME)
	$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) $^ -o $@

coverage:
	@$(MAKE) -s --no-print-directory clean
	@$(MAKE) --no-print-directory cov-build
	@$(MAKE) --no-print-directory cov-report

cov-build:
	$(MAKE) CFLAGS='$(COV_CFLAGS)' LDFLAGS='$(COV_CFLAGS)' test

cov-report:
	@{ for f in $(TESTS) prqueue; do \
		gcov -b -p $$f.c >/dev/null; \
	done; }

	@rm -f *.h.gcov test-*.c.gcov
	@echo "--- untested functions:" | tee -a untested
	@grep '^function .* called 0' *.c.gcov \
	| sed -e 's/function \([^ ]*\) called 0.*/\1/' \
	| sed -e 's/\.c\.gcov:/\.c:/' | tee -a untested


distclean: clean
	rm -f Makefile iobroker.h

clean: clean-test clean-coverage
	rm -f core.* *.o *~ wproc *.a

clean-test: clean-coverage
	rm -f $(TESTS)

clean-coverage:
	rm -f untested *.gcov *.gcda *.gcno gmon.out

.PHONY: clean clean-test clean-coverage coverage

# stop make from removing intermediary files, as ours aren't really
# intermediary
.SECONDARY:
