SRC=nimOptim.cpp nimDerivs_atomic_dyn_ind.cpp nodeFun.cpp accessorClasses.cpp nimbleCppAD.cpp RcppUtils.cpp EigenTypedefs.cpp eigenUsingClasses.cpp RcppNimbleUtils.cpp Utils.cpp NamedObjects.cpp ModelClassUtils.cpp dists.cpp nimDists.cpp dllFinalizer.cpp nimbleGraph.cpp smartPtrs.cpp predefinedNimbleLists.cpp nimIntegrate.cpp nimDerivs_atomic_classes.cpp nimDerivs_atomic_matmult.cpp nimDerivs_atomic_matinverse.cpp nimDerivs_atomic_backsolve.cpp nimDerivs_atomic_forwardsolve.cpp nimDerivs_atomic_cholesky.cpp nimDerivs_atomic_solve_base.cpp nimDerivs_atomic_pow_int.cpp nimDerivs_atomic_zround.cpp nimDerivs_atomic_log_pow_int.cpp nimDerivs_atomic_probit.cpp
OBJECTS=$(SRC:%.cpp=%.o)

# nimbleGraph.cpp is only needed in SRC for --enable-dylib=true, but it's not a big deal to leave include it either way

ifndef R_SHARE_DIR
 R_SHARE_DIR=$(R_HOME)/share
endif


## By default, R's C++ compilation system generates debugging information,
## typically by a -g flag.  This makes the size of libnimble.a much much larger
## than it would be without debugging information
##
## The R compilation flags are defined in $(R_HOME)/etc$(R_ARCH)/Makeconf
## As a jerry-built way to disable this, we copy that Makeconf locally, modify to
## and include that.  Later in the script we erase it so it doesn't end up
## distributed in any tarball, since it will have contents relative to the
## *build* machine.  This is controlled by configure, generated by configure.ac
## by autoconf.

include ./Makevars

ifndef OS
 OS=$(shell uname)
endif

ifeq ($(OS), Darwin)
INSTALL_NAME=-install_name "$(R_PACKAGE_DIR)/CppCode/libnimble$(DYLIB_EXT)"
#Relative paths don't work on OSX. "../CppCode/libnimble$(DYLIB_EXT)"
endif

libnimble$(DYLIB_EXT): $(OBJECTS)
	@echo "linking libnimble$(DYLIB_EXT)"
	@echo "$(OS)   $(INSTALL_NAME)"
	$(CXX) $(SHLIB_CXXLDFLAGS) -o $@  $(OBJECTS) $(INSTALL_NAME)
	-rm *.o
# ifneq ($(OS), Windows)
# 	$(R_HOME)/bin$(R_ARCH)/Rscript --vanilla ./removeCustomizedMakeconf.R
# endif

#	cp libnimble$(DYLIB_EXT) ../../src

#%.o: %.cpp
#	$(R_HOME)/bin$(R_ARCH)/Rcmd COMPILE $<
# Was R CMD COMPILE

clean:
	-rm -f $(OBJECTS) libnimble.$(DYLIB_EXT) libnimble.dylib libnimble$(ARCH_SUFFIX).a
# DYLIB_EXT does not seem to be defined unless this is called within R CMD.  e.g. from ./cleanup, DYLIB_EXT is not defined

# libnimble32.a libnimble64.a
libnimble.a  libnimble_x64.a libnimble_i386.a: $(OBJECTS)
	echo "Making nimble $@"
	echo "$(OBJECTS)"
	$(AR) rcs $@ $(OBJECTS)
	-rm $(OBJECTS)
# ifneq ($(OS), Windows)
# 	$(R_HOME)/bin$(R_ARCH)/Rscript --vanilla ./removeCustomizedMakeconf.R
# endif
