TARGET1 = testCompiler
TARGET2 = testScope
TARGET3 = TestStore

BSPLIB_DIR=../bspLib
CONVERTER_DIR=../dataConverters
UTILSPATH=../../shared/utils/c++

CPPFILES = src/CheckpointingLib.cpp \
           src/CheckpointTimer.cpp \
           src/CkpCompilerStack.cpp \
           src/CkpRestoredData.cpp \
           src/StackMemoryPositions.cpp \
           src/HeapController.cpp \
           src/CkpRepositoryStore.cpp \
           src/CkpStore.cpp \
           $(UTILSPATH)/Config.cpp \
           $(UTILSPATH)/StringTokenizer.cpp

CPPFILES_BSP = src/BspLibCkp.cpp

include Makefile.vars

##########################################################
# Compiler Flags                                         #
##########################################################

INCDIR = -I../../shared -Iinclude -I$(BSPLIB_DIR)/include -I$(CONVERTER_DIR)/include
CXX = g++
#ifeq ($(DEBUG), true)
DEBUGFLAGS =  -g3 -O0 -fno-inline
#endif
WARN_FLAGS = -Wall -ansi -pedantic
CPPFLAGS   = -pthread -fPIC #-O2
#LDFLAGS_LUA = -L$(LUALIBDIR) -Wl,-rpath,$(LUALIBDIR) -lm -llualib -llua -lpthread
LDFLAGS_BSP = -L$(BSPLIB_DIR)/lib -Wl,-rpath,$(BSPLIB_DIR)/lib -lbsp

##########################################################
# Generating modified paths for objects and dependencies #
##########################################################

DIRLIST_BSP      = $(dir $(CPPFILES_BSP))
DIRDEPSLIST_BSP  = $(addsuffix dependencies/, $(DIRLIST_BSP))
DIROBJSLIST_BSP  = $(addsuffix objects/, $(DIRLIST_BSP))
FILELIST_BSP     = $(notdir $(CPPFILES_BSP))
OBJFILESLIST_BSP = $(join $(DIROBJSLIST_BSP), $(FILELIST_BSP))
DEPFILESLIST_BSP = $(join $(DIRDEPSLIST_BSP), $(FILELIST_BSP))
DIRDEPS_BSP      = $(sort $(DIRDEPSLIST_BSP)) #Removes duplicate dirs
DIROBJS_BSP      = $(sort $(DIROBJSLIST_BSP)) #Removes duplicate dirs
OBJFILES_BSP     = $(OBJFILESLIST_BSP:.cpp=.o)
CPPDEPENDS_BSP   = $(DEPFILESLIST_BSP:.cpp=.d)

DIRLIST      = $(dir $(CPPFILES))
DIRDEPSLIST  = $(addsuffix dependencies/, $(DIRLIST))
DIROBJSLIST  = $(addsuffix objects/, $(DIRLIST))
FILELIST     = $(notdir $(CPPFILES))
OBJFILESLIST = $(join $(DIROBJSLIST), $(FILELIST))
DEPFILESLIST = $(join $(DIRDEPSLIST), $(FILELIST))
DIRDEPS      = $(sort $(DIRDEPSLIST)) #Removes duplicate dirs
DIROBJS      = $(sort $(DIROBJSLIST)) #Removes duplicate dirs
OBJFILES_CKP   = $(OBJFILESLIST:.cpp=.o)
CPPDEPENDS_CKP = $(DEPFILESLIST:.cpp=.d)

transfname = $(patsubst %.o, %.d, $(subst objects/,dependencies/,$(1) ) )

all: first libckp.so libckpbsp.so $(TARGET3) testCkpTmp testCkpGraph

-include $(CPPDEPENDS_CKP) $(CPPDEPENDS_BSP)

first:
	@mkdir -p lib $(DIRDEPS) $(DIROBJS)

../../shared/utils/c++/objects/%.o: ../../shared/utils/c++/%.cpp
	$(CXX) $(INCDIR) $(WARN_FLAGS) -c $(CPPFLAGS) $(DEBUGFLAGS)  -o $@ $<
	$(CXX) $(INCDIR) $(WARN_FLAGS) $(CPPFLAGS) -MM  $< > $(call transfname, $@)
	@mv -f $(call transfname, $@) $(call transfname, $@).tmp
	@sed -e 's|.*:|$@:|' < $(call transfname, $@).tmp > $(call transfname, $@)
	@sed -e 's/.*://' -e 's/\\$$//' < $(call transfname, $@).tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $(call transfname, $@)
	@rm -f $(call transfname, $@).tmp

src/objects/%.o: src/%.cpp
	$(CXX) $(INCDIR) $(WARN_FLAGS) -c $(CPPFLAGS) $(DEBUGFLAGS)  -o $@ $<
	$(CXX) $(INCDIR) $(WARN_FLAGS) $(CPPFLAGS) -MM  $< > $(call transfname, $@)
	@mv -f $(call transfname, $@) $(call transfname, $@).tmp
	@sed -e 's|.*:|$@:|' < $(call transfname, $@).tmp > $(call transfname, $@)
	@sed -e 's/.*://' -e 's/\\$$//' < $(call transfname, $@).tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $(call transfname, $@)
	@rm -f $(call transfname, $@).tmp

testCkpTmp: src/testCkpTmp.cpp
	$(CXX) $(INCDIR) $(WARN_FLAGS) $(CPPFLAGS) $(DEBUGFLAGS) -o $@ $+ -Llib -Wl,-rpath,lib -lckp

testCkpGraph: src/testCkpGraph.cpp
	$(CXX) $(INCDIR) $(WARN_FLAGS) $(CPPFLAGS) $(DEBUGFLAGS) -o $@ $+ -Llib -Wl,-rpath,lib -lckp

# Tests the CkpRepositoryStore
$(TARGET3): src/$(TARGET3).cpp libckp.so 
	$(CXX) $(INCDIR) $(WARN_FLAGS) $(CPPFLAGS) $(DEBUGFLAGS) -o $@ src/$(TARGET3).cpp -Llib -Wl,-rpath,lib -lckp

libckp.so: $(OBJFILES_CKP)
	$(CXX) -shared -fPIC -o lib/$@ $+ $(CONVERTER_DIR)/lib/libconverter.a

libckpbsp.so: $(OBJFILES_CKP) $(OBJFILES_BSP)
	$(CXX) -shared -fPIC -o lib/$@ $+ $(CONVERTER_DIR)/lib/libconverter.a $(LDFLAGS_BSP)

clean:
	rm -rf lib $(DIRDEPS) $(DIROBJS)

