#-*- Makefile -*-
all:  libR makeMakedeps $(DLLNAME).dll

# set this in Makevars.win to supply your own resource file
RCNAME=${DLLNAME}_res
# set this (e.g. in SHLIB) to suppress adding version info to the DLL
RCOBJ=$(RCNAME).o

# Don't use SOURCES as this get passed on for libR.
CFSOURCES=$(wildcard -f *.c *.f)
CSOURCES=$(wildcard -f *.c)
CXXSOURCES=$(wildcard -f *.cc *.cpp *.C)
OBJSA=$(foreach i,$(CFSOURCES) $(CXXSOURCES),$(basename $i).o)

include $(RHOME)/src/gnuwin32/MkRules

ifneq ($(strip $(CXXSOURCES)),)
  DLL_LDMAIN=g++
endif

ifdef DEBUG
  DLLFLAGS=
  DEBUGFLAG=-g -Wall
else
  DLLFLAGS=-s
  DEBUGFLAG=-Wall
endif

CPPFLAGS=$(PKG_CPPFLAGS) -I$(RHOME)/include
CFLAGS=$(CPPFLAGS) $(DEBUGFLAG) -O2 $(PKG_CFLAGS)
CXXFLAGS=$(CPPFLAGS) $(DEBUGFLAG) -O2 $(PKG_CXXFLAGS)
FFLAGS=-O2 $(DEBUGFLAG) $(PKG_FFLAGS)
DLLLIBS=-L$(RHOME)/src/gnuwin32 $(PKG_LIBS) $(FLIBS) -lR 
RESFLAGS=--include-dir $(RHOME)/include
BLAS_LIBS=-lRblas
LAPACK_LIBS=-L$(RHOME)/lib -lRlapack
# for use by Makevars.win
R_HOME=$(RHOME)

AA=$(wildcard Makefile)
ifeq ($(AA),Makefile)
 include Makefile
else
 OBJS=$(filter-out $(OBJS-NO),$(OBJSA))
endif
AA=$(wildcard Makevars.win)
ifeq ($(AA),Makevars.win)
 include Makevars.win
else
-include Makevars
endif

libR:
	@$(MAKE) -s --no-print-directory -C $(RHOME)/src/gnuwin32 libR.a
	@$(MAKE) -s --no-print-directory -C $(RHOME)/src/gnuwin32 libRblas.a

$(DLLNAME)_res.rc:
	@PERL5LIB=$(RHOME)/share/perl perl $(RHOME)/src/gnuwin32/makeDllRes.pl $(DLLNAME) > $@

$(DLLNAME)_res.o: $(DLLNAME)_res.rc $(RHOME)/include/Rversion.h

$(DLLNAME).a: $(OBJS)
	@$(RM) -f $@
	$(AR) cr $@ $(OBJS)
	$(RANLIB) $@

$(DLLNAME).dll : $(DLLNAME).a $(RCOBJ)

# Dependencies
CXXSOURCES1=$(wildcard -f *.cc)
CXXSOURCES2=$(wildcard -f *.cpp)
CXXSOURCES3=$(wildcard -f *.C)
DEPS=$(CSOURCES:.c=.d) $(CXXSOURCES1:.cc=.d) $(CXXSOURCES2:.cpp=.d) $(CXXSOURCES3:.C=.d)

makeMakedeps: $(DEPS)
	@$(RM) Makedeps
ifneq ($(strip $(DEPS)),)
	@cat $(DEPS) >> Makedeps
endif

-include Makedeps
