CC   = gcc -O3
HDRS = server.h
SRCS = unittests.c
OBJS = lockManager.o treeController.o treeTransaction.o
PROG = contest

.SUFFIXES: .dylib .so

all: $(PROG)

lib.so: wdlm.o $(OBJS)
	g++ -shared -o $@ $< $(OBJS)

.c.so:
	$(CC) -fPIC -shared $*.c -o $*.so

$(PROG): $(SRCS) $(HDRS) lib.so
	$(CC) $(SRCS) ./lib.so -pthread -o $(PROG)

harness:
	$(MAKE) -C tests/
#	(for j in tests/*.c; do make tests/`basename $$j .c`.so; done)
	python harness.py

#dynamic library targets for macos testing
lib.dylib: $(SRCS) $(HDRS) $(OBJS)
	g++ -fPIC -dynamiclib $(OBJS) -lpthread wdlm.cpp -o lib.dylib

.c.dylib:
	$(CC) -dynamiclib $*.c -o $*.dylib

dummy:
	$(CC) -fPIC -shared ./dummyimpl.c -o lib.so

dummymacos:
	$(CC) -fPIC -dynamiclib ./dummyimpl.c -o lib.dylib

macos:  $(SRCS) $(HDRS) lib.dylib
	$(CC) $(SRCS) lib.dylib -o $(PROG)

macharness:
	$(MAKE) -C tests/ macos
#	(for j in tests/*.c; do make tests/`basename $$j .c`.dylib; done)
	python harness.py

test: $(PROG)
	rm -rf ENV
	./$(PROG)

clean:
	rm -rf *.o *.so *.dylib tests/*.dylib tests/*.so tests/speed_test $(PROG) ENV error.log
	rm -rf ftest-debug ftest-final mttest-debug mttest-final

ifeq ($(DEBUG),1)
SUFFIX=-debug
CPPARGS=-fPIC -ansi -Wall -Wextra -Wno-long-long -pedantic -g
else
SUFFIX=-final
CPPARGS=-fPIC -ansi -Wall -Wextra -Wno-long-long -pedantic -O3 -DNDEBUG
endif

ftest: ftest$(SUFFIX)
	./ftest$(SUFFIX)

mttest: mttest$(SUFFIX)
	./mttest$(SUFFIX)

%.o: %.cpp *.h *.hpp
	g++ $(CPPARGS) -c $<

ftest$(SUFFIX): ftest.o $(OBJS)
	g++ $< $(OBJS) -lpthread -o $@

mttest$(SUFFIX): mttest.o $(OBJS)
	g++ $< $(OBJS) -lpthread -o $@



