CPPS=$(wildcard atoms/*.cpp offermon/*.cpp)
OBJS=$(CPPS:.cpp=.o)

CPP=c++

#STORAGE_CPPS=$(wildcard storage/*.cpp)
#STORAGE_OBJS=$(STORAGE_CPPS:.cpp=.o)

TESTS_CPPS=$(wildcard tests/*.cpp)
TESTS_SO=$(TESTS_CPPS:.cpp=.so)

FLAGS=-I../include -I/opt/local/include -fPIC -O2 -I/usr/include/postgresql -g3 -pg
LIBS=-lpcre -ldl -L../lib -lrelsyst -L/opt/local/lib -levent -lGeoIP -lpq -pg

all: Makefiledep.mk ../lib/librelsyst.so ../offermon
#all: Makefiledep.mk ../lib/librelsyst.so ../yagle
#all: Makefiledep.mk ../lib/librelsyst.so ../relsystdb ../storage
include Makefiledep.mk

clean:
	rm -f ../lib/librelsyst.so
	rm -f $(OBJS)

../lib:
	mkdir ../lib

../lib/librelsyst.so: ../lib $(OBJS)
	echo $(OBJS)
	$(CPP) -g -fPIC -shared -o ../lib/librelsyst.so $(OBJS)

sotest: sotest.o ../lib/librelsyst.so
	$(CPP) -fPIC sotest.o $(LIBS) -o sotest

../sqltest: sqltest.o ../lib/librelsyst.so
	$(CPP) -fPIC sqltest.o $(LIBS) -o ../sqltest

../relsystdb: relsystdb.o ../lib/librelsyst.so
	$(CPP) -fPIC relsystdb.o $(LIBS) -o ../relsystdb

../yagle: yagle/yagle.o ../lib/librelsyst.so
	$(CPP) -fPIC yagle/yagle.o $(LIBS) -o ../yagle

OM_OBJS= offermon/http-server.o offermon/memcache__cpa_network_engine.o offermon/memcache.o offermon/memcache__offer.o offermon/memcache__smartlink.o offermon/memcache__offer_info.o offermon/memcache__user_postbacks.o offermon/streams.o offermon/tracker.o offermon/tracker_static.o offermon/writer.o offermon/scan_struct.o offermon/memcache__banner.o offermon/memcache__incent_source.o offermon/memcache__default_pricing.o offermon/template_file.o
../offermon: $(OM_OBJS) ../lib/librelsyst.so
	$(CPP) -fPIC $(OM_OBJS) $(LIBS) -o ../offermon


../storage: storage.o ../lib/librelsyst.so
	$(CPP) -fPIC storage.o $(LIBS) -o ../storage

../storage_test: storage_test.o ../lib/librelsyst.so
	$(CPP) -fPIC storage_test.o $(LIBS) -o ../storage_test

dbfuncs/index_bplus_tree_any.cpp: dbfuncs/index_bplus_tree-tmpl.cxx dbfuncs/mkindex_bplus_tree.pl
	dbfuncs/mkindex_bplus_tree.pl
../include/dbfuncs/index_bplus_tree_any.h: ../include/dbfuncs/index_bplus_tree-tmpl.hxx dbfuncs/mkindex_bplus_tree.pl
	dbfuncs/mkindex_bplus_tree.pl
dbfuncs/index_bplus_tree_uint64.cpp: dbfuncs/index_bplus_tree-tmpl.cxx dbfuncs/mkindex_bplus_tree.pl
	dbfuncs/mkindex_bplus_tree.pl
../include/dbfuncs/index_bplus_tree_uint64.h: ../include/dbfuncs/index_bplus_tree-tmpl.hxx dbfuncs/mkindex_bplus_tree.pl
	dbfuncs/mkindex_bplus_tree.pl

bisoncpp:
	sh -c "cd sql;bisonc++ -b ../../include/sql/parser_base-auto.h -c ../../include/sql/parser-auto.h -i ../../include/sql/parser_impl-auto.h -p parse-auto.cpp sql.y"

bison:
	bison -v -o sql/sql-auto.cpp --defines=../include/sql/sql-auto.h -p sql sql/sql.ypp

%.o: %.cpp
	$(CPP) $(FLAGS) -c -o $@ $<

tests: $(TESTS_SO)

llvmtest:
	llvm-$(CPP) $(FLAGS)  -emit-llvm -c -o tests/test3.bc tests/test3.cpp

tests/%.so: tests/%.o
	$(CPP) -fPIC -shared -o $@ $< -L.

bplus: tests/bplus.o librelsyst.so
	$(CPP) -fPIC tests/bplus.o -odl -lrelsyst -L. -o bplus

depend:
	touch Makefiledep.mk
	makedepend -fMakefiledep.mk -I../include -Y `find . -name "*.cpp" | grep -v "/\." `
Makefiledep.mk:
	touch Makefiledep.mk
	makedepend -fMakefiledep.mk -I../include -Y `find . -name "*.cpp" | grep -v "/\." `
