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

.PHONY : all makeMakedeps

# 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 *.f90 *.f95)
CSOURCES=$(wildcard -f *.c)
CXXSOURCES=$(wildcard -f *.cc *.cpp *.C)
FSOURCES=$(wildcard -f *.f)
FCSOURCES=$(wildcard -f *.f90 *.f95)
OBJSA=$(foreach i,$(CSOURCES) $(FSOURCES) $(CXXSOURCES) $(FCSOURCES),$(basename $i).o)

include $(RHOME)/src/gnuwin32/MkRules

ifneq ($(strip $(FCSOURCES)),)
# don't assume F95 can be cross-compiled in the same way
  DLL="$(F95)"
else

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

endif

ifdef DEBUG
  DLLFLAGS=
  DEBUGFLAG=$(G_FLAG) -Wall
else
  DLLFLAGS=-s
  DEBUGFLAG=-Wall
endif

COPTFLAG=-O2
CXXOPTFLAG=-O2
FOPTFLAG=-O2

CPPFLAGS=$(PKG_CPPFLAGS) -I$(RHOME)/include $(CLINK_CPPFLAGS)
CFLAGS=$(CPPFLAGS) $(DEBUGFLAG) $(COPTFLAG) -std=gnu99 $(PKG_CFLAGS)
CXXFLAGS=$(CPPFLAGS) $(DEBUGFLAG) $(CXXOPTFLAG) $(PKG_CXXFLAGS)
FFLAGS=$(FOPTFLAG) $(DEBUGFLAG) $(PKG_FFLAGS)
DLLLIBS=-L$(RHOME)/bin $(PKG_LIBS) $(EXTRA_LIBS) -lR
RESFLAGS=--include-dir $(RHOME)/include
# 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

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

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

ifeq ($(wildcard $(DLLNAME)-win.def),$(DLLNAME)-win.def)
$(DLLNAME).dll : $(DLLNAME)-win.def $(OBJS) $(RCOBJ)
	$(DLL) -shared $(DLLFLAGS) $($*-DLLFLAGS) -o $@ $^ $($*-DLLLIBS) $(DLLLIBS)
else
$(DLLNAME).dll : $(OBJS) $(RCOBJ)
endif

# 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
