## -*- mode: makefile; tab-width: 8; -*-
##
## Simple Makefile for Windows
##
## Note that the libRInside library encodes the value of R_HOME found
## at compilation. So if you use the CRAN package of RInside, its value
## may not correspond to where you have R installed.  One quick fix is 
## export the appropriate value of R_HOME, eg ony my work machine
##           set R_HOME=C:\opt\R-current
## The other is to re-install RInside from source on your machine.
## Either one should allow you to actually run the binaries created
## with this Makefile


## This version is fairly directly derived from the Unix versions
## You may have to set R_HOME manually if this does not work
## It requires Rtools in the path -- as does all R package building
R_HOME := 		$(shell R RHOME | sed -e "s|\\\\|\/|g")

## You may have to set this to one of the two values below to enforce a  particular
## architecture in case the autodetection in the next line does not work correctly
R_ARCH :=		--arch $(shell echo 'cat(.Platform$$r_arch)' | R --vanilla --slave)
##R_ARCH :=		--arch i386
##R_ARCH :=		--arch x64

## You may need to set R_LIBS_USER if Rcpp or RInside are installed where R does not see them by default
#R_LIBS_USER :=		"C:/myRstuff/library"

sources := 		$(wildcard *.cpp)
programs := 		$(sources:.cpp=)

## include headers and libraries for R
RCPPFLAGS := 		$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config --cppflags)
RLDFLAGS := 		$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config --ldflags)
RBLAS := 		$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config BLAS_LIBS)
RLAPACK := 		$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config LAPACK_LIBS)


## include headers and libraries for Rcpp interface classes
RCPPINCL := 		$(shell echo 'Rcpp:::CxxFlags()' | $(R_HOME)/bin/R $(R_ARCH) --vanilla --slave)
RCPPLIBS := 		$(shell echo 'Rcpp:::LdFlags()'  | $(R_HOME)/bin/R $(R_ARCH) --vanilla --slave)


## include headers and libraries for RInside embedding classes
RINSIDEINCL := 		$(shell echo 'RInside:::CxxFlags()' | $(R_HOME)/bin/R $(R_ARCH) --vanilla --slave)
RINSIDELIBS := 		$(shell echo 'RInside:::LdFlags()'  | $(R_HOME)/bin/R $(R_ARCH) --vanilla --slave)


## compiler etc settings used in default make rules
CXX := 			$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config CXX)
CPPFLAGS := 		-Wall $(shell $(R_HOME)/bin/R $(R_ARCH) CMD config CPPFLAGS)
CXXFLAGS := 		$(RCPPFLAGS) $(RCPPINCL) $(RINSIDEINCL) $(shell $(R_HOME)/bin/R $(R_ARCH) CMD config CXXFLAGS)
LDFLAGS	= 		-s
LDLIBS := 		$(RLDFLAGS) $(RBLAS) $(RLAPACK) $(RINSIDELIBS) $(RCPPLIBS)
CC := 			$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config CXX)


all : 			$(programs)

clean:
			rm -vf $(programs)

checkR:
			echo "R is at $(R_HOME)"
