############# Names and dirs #############

EXENAME = ExternalModules$(EXT)
SONAME = libExamples$(EXT).so

BINDIR = bin
SRCDIR = src
BUILDDIR = build$(EXTLOW)

INCLUDEDIRS = include ../CMSDK/include
LIBRARIES = ../CMSDK/lib/arch/libCMSDK$(EXT).a

############# Include system stuff #############

include ../CMSDK/Makefile.sys

############# Files to compile #############

EXECPPFILES =  ExternalModules.cpp

LIBCPPFILES =  Examples.cpp PsyTest.cpp

############# Setup dirs #############

EXE = $(BINDESTDIR)/$(EXENAME)
SO = $(BINDESTDIR)/$(SONAME)

EXESRCFILES = $(patsubst %.cpp,$(SRCDIR)/%.cpp,$(EXECPPFILES))
EXEOBJFILES = $(patsubst %.cpp,$(BUILDDIR)/%.o,$(EXECPPFILES))

LIBSRCFILES = $(patsubst %.cpp,$(SRCDIR)/%.cpp,$(LIBCPPFILES))
LIBOBJFILES = $(patsubst %.cpp,$(BUILDDIR)/%.o,$(LIBCPPFILES))

INCLUDES = $(patsubst %,-I% ,$(INCLUDEDIRS))
LIBS = $(subst /arch/,/$(ARCH)/,$(LIBRARIES))

RESULT := $(shell if [ ! -d $(BINDIR) ]; then mkdir $(BINDIR); fi)
RESULT := $(shell if [ ! -d $(BINDESTDIR) ]; then mkdir $(BINDESTDIR); fi)
RESULT := $(shell if [ ! -d $(BUILDDIR) ]; then mkdir $(BUILDDIR); fi)

############# Overall commands #############

all : $(SO) $(EXE)
	@echo $(SO) and $(EXE) ready

debug: all

profile: all

ssl: all

ssldebug: all

############# Dependencies #############

$(BUILDDIR)/%.d: $(SRCDIR)/%.cpp
	@echo Making dependencies for $(<F)...
	@$(SHELL) -ec '$(CC) -MM $(INCLUDES) -D $(SYSTEM) $(STANDARD) $< \
		| sed '\''s/\($(*F)\)\.o[ :]*/\1.o $(@F) : /g'\'' > $@; \
		[ -s $@ ] || rm -f $@'

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),totalclean)
  -include $(OBJFILES:.o=.d)
endif
endif

############# Compiling #############

$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp
	@echo Compiling $(<F)
	@$(CC) $(DEBUG) $(INCLUDES) $(STANDARD) $(NOTLINK) $(OUTPUT_OPTION) $<

############# Linking #############

$(EXE): $(EXEOBJFILES) $(LIBS)
	@echo Linking $(EXE) $(SYSTEM)...
	@$(CC) $(DEBUG) $(STANDARD) $(EXEOBJFILES) $(LIBPATHS) -o $(EXE) $(LIBS) $(OTHERLIBS)

$(SO): $(LIBOBJFILES) $(LIBS)
	@echo Linking $(SO) $(SYSTEM)...
	@$(CC) $(DEBUG) $(STANDARD) $(LIBOPT) $(LIBOBJFILES) $(LIBPATHS) -o $(SO) $(LIBS) $(OTHERLIBS)

############# Cleaning #############

clean:
	@echo Removing .o files and executable
	@find $(BUILDDIR) -path '*/.svn' -prune -o -type f -name \*.[o,d] -exec rm -f {} \;
	@rm -rf $(BUILDDIR)
	@rm -f $(SO) $(EXE)
	@make cleandebug

cleandebug:
	@echo Removing debug .o files and executable
	@find $(BUILDDIR) -path '*/.svn' -prune -o -type f -name \*.[o,d] -exec rm -f {} \;
	@rm -rf $(BUILDDIR)
	@rm -f $(SO) $(EXE)
	
totalclean : clean
	#@echo Code size before is: `du -ks .`
	@echo Cleaning Visual Studio files...
	@rm -f *.lib
	@find . -type d -name Debug -exec rm -rf {} \; > /dev/null 2>&1
	@find . -type d -name Release -exec rm -rf {} \; > /dev/null 2>&1
	@find . -type f -name \*.ncb -exec rm -f {} \; > /dev/null 2>&1
	@find . -type f -name \*.plg -exec rm -f {} \; > /dev/null 2>&1
	@find . -type f -name \*.exe -exec rm -f {} \; > /dev/null 2>&1
	@find . -type f -name \*.ilk -exec rm -f {} \; > /dev/null 2>&1
	@find . -type f -name \*.vcl -exec rm -f {} \; > /dev/null 2>&1
	@echo Cleaning core dump files...
	@find . -type f -name core -exec rm -f {} \; > /dev/null 2>&1
	#@echo Code size after is: `du -ks .`

############# Additional commands #############

rebuild : clean all
