#####################################################################
#A scalable and high-performance platform for R.
#Copyright (C) [2013] Hewlett-Packard Development Company, L.P.

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or (at
#your option) any later version.

#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#General Public License for more details.  You should have received a
#copy of the GNU General Public License along with this program; if
#not, write to the Free Software Foundation, Inc., 59 Temple Place,
#Suite 330, Boston, MA 02111-1307 USA
#####################################################################

NCORES=$(shell getconf _NPROCESSORS_ONLN)


.PHONY: all third_party messaging common worker executor
all: third_party messaging common worker executor

#third party already passes -jNCORES to each component
third_party:
	(mkdir -p third_party/install/bin third_party/install/lib third_party/install/include; cd third_party; make)

messaging: third_party
	(cd messaging/; make -j$(NCORES) )

common: third_party messaging
	(cd common; make -j$(NCORES) ) 

worker: third_party messaging common
	(cd worker; make -j$(NCORES) )

executor: third_party messaging common
	(cd executor; make -j$(NCORES) )


clean:
	(cd messaging; make clean )
	(cd common; make clean )
	(cd worker; make clean )
	(cd executor; make clean )
	rm *.o *.so

clean-third-party:
	(cd third_party; make clean )

## Use the R_HOME indirection to support installations of multiple R version
R_CXXFLAGS = $(shell R CMD config --cppflags)  $(shell $(R_HOME)/bin/Rscript -e "Rcpp:::CxxFlags()") $(shell $(R_HOME)/bin/Rscript -e "RInside:::CxxFlags()")
#R_LDFLAGS = $(shell R CMD config --ldflags)  $(shell $(R_HOME)/bin/Rscript -e "RInside:::LdFlags()") $(shell $(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()")
# link to RInside statically
R_LDFLAGS = -L$(shell $(R_HOME)/bin/Rscript -e "cat(system.file(package='RInside'),'/lib',sep='')") -Wl,-static -lRInside  -Wl,-Bdynamic -Wl,-rpath,$(shell $(R_HOME)/bin/Rscript -e "cat(system.file(package='RInside'),'/lib',sep='')")

PKG_LIBS = $(R_LDFLAGS)


PKG_CPPFLAGS = -Ithird_party/boost_1_50_0 -Ithird_party/install/include -Imessaging/gen-cpp -Icommon/ -DSTRICT_R_HEADERS -std=c++0x -fPIC -g -O2 -fopenmp -finline-limit=10000 -DNDEBUG $(R_CXXFLAGS)
# To enable zipkin-based tracing add the following to PKG_LIBS and PKG_CPPFLAGS
#   BLKIN_INCLUDE = -Ithird_party/install/include/blkin -DPERF_TRACE
#   BLKIN_LINKER_FLAGS = -lzipkin-c -lblkin-front -lzipkin-cpp

PKG_LIBS += -Lthird_party/install/lib -lpthread -lboost_thread -lboost_system -lboost_log -lboost_log_setup -lboost_chrono -lboost_filesystem -lboost_date_time -latomicio -lR-common -lR-proto -lzmq -luuid -lprotobuf

%.o: %.cpp $(wildcard *.h) $(wildcard common/*.h)
	g++ -c $< $(PKG_CPPFLAGS) -o $@
