# Update these if libraries are installed in non-standard locations.
NTL_DIR =
GMP_DIR =

CC = g++
# Uncomment the first flags for testing, the second for benchmarking.
#CFLAGS = -Wall -pipe -DVERBOSE
CFLAGS = -Wall -pipe -O3 -DNDEBUG

COMP_HEADERS = misc.h
INCL_HEADERS = sparsepoly.h blackbox.h GSinterp.h newinterp.h
HEADERS = $(COMP_HEADERS) $(INCL_HEADERS)
TESTS = test_sparsepoly test_blackbox test_GSinterp test_newinterp
DOTESTS = $(patsubst %,do%,$(TESTS))
EXES = $(TESTS) benchmark
SOURCES = $(COMP_HEADERS:.h=.cc) $(EXES:=.cc)
OBJECTS = $(SOURCES:.cc=.o)
DEPENDENCIES = $(SOURCES:.cc=.d)

inclpre = $(if $(NTL_DIR),-I$(NTL_DIR)/include) \
$(if $(GMP_DIR),-I$(GMP_DIR)/include)
libpre = $(if $(NTL_DIR),-L$(NTL_DIR)/lib) $(if $(GMP_DIR),-L$(GMP_DIR)/lib)
libpost = -lntl -lgmp -lm

test: $(DOTESTS)

all: $(EXES)

# Dependencies should be added here
$(EXES): misc.o

# Include auto-dependencies
include $(DEPENDENCIES)

$(EXES): %: %.o
	$(CC) $(libpre) $^ $(libpost) -o $@ $(CFLAGS)

$(OBJECTS): %.o: %.cc
	$(CC) -c $(inclpre) $< -o $@ $(CFLAGS)

# Auto-generate dependencies. Copied from gnu make manual
$(DEPENDENCIES): %.d: %.cc
	@set -e; rm -f $@; \
	$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

.PHONY: clean test all $(DOTESTS)

$(DOTESTS): do%: %
	./$<

clean:
	-rm -f $(OBJECTS) $(EXES) $(DEPENDENCIES)
