CC       = gcc
HDRS     = *.h
SRCS     = unittests.c

# Under many Linux installations, BDB is installed by default in /usr
# In this case, the following defines should be correct
# BASE = /usr
# BDBINC = -I$(BASE)/include/db4/
#
# Under MacOS with fink, you should use
# BASE = /sw
# BDBINC = -I$(BASE)/include/db4/
#
# Use the following if you installed BDB from Oracle in /usr/local/BerkeleyDB.4.7
BASE	 = /usr/local/BerkeleyDB.4.7
BDBINC = -I$(BASE)/include/

BDBLIBS = -ldb
BDBLIBSMACOS = -L $(BASE)/lib/libdb.a

PROG	 = contest

all: $(PROG)

#default BDB based library -- copy in your own lib.so to test it with
# our test cases
lib.so:	$(SRCS) $(HDRS) wdlm.c
	$(CC) -shared wdlm.c -o lib.so

#$(CC) -shared $(BDBINC) $(BDBLIBS) ./bdbimpl.c -o lib.so

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

#dynamic library targets for macos testing
lib.dylib: $(SRCS) $(HDRS)
	$(CC) -dynamiclib $(BDBINC) $(BDBLIBSMACOS) bdbimpl.c -o lib.dylib

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

#generates html and latex documentation files in ../docs
doc:
	doxygen Doxyfile

#runs the unit tests with the Berkeley DB implementation
test: $(PROG)
	rm -rf ENV
	./$(PROG)

clean:
	rm -rf *.o *.so *.dylib $(PROG) ENV error.log

debug: node32.h node64.h test.c
	rm -f test
	$(CC) -Wall -Wextra -Werror -pedantic -std=c99 -g -o test test.c
	time ./test

final: node32.h node64.h test.c
	rm -f test
	$(CC) -Wall -Wextra -Werror -pedantic -std=c99 -O3 -DNDEBUG -o test test.c
	time ./test

node32.h: node.h
	cp $< $@
	sed -i 's/NODE_CAPACITY/2046/g' $@
	sed -i 's/KEY_TYPE_ID/SHORT/g' $@
	sed -i 's/KEY_TYPE/int32_t/g' $@
	sed -i 's/KEY_NAME/shortkey/g' $@
	sed -i 's/node_/node32_/g' $@
	sed -i 's/Node/Node32/g' $@

node64.h: node.h
	cp $< $@
	sed -i 's/NODE_CAPACITY/1364/g' $@
	sed -i 's/KEY_TYPE_ID/INT/g' $@
	sed -i 's/KEY_TYPE/int64_t/g' $@
	sed -i 's/KEY_NAME/intkey/g' $@
	sed -i 's/node_/node64_/g' $@
	sed -i 's/Node/Node64/g' $@


