#-*- Makefile -*-
include ../../gnuwin32/MkRules

CFLAGS = -O3 -Wall -pedantic $(EOPTS)
ifdef DEBUG
 CFLAGS += $(G_FLAG)
 DLLFLAGS =
else
 DLLFLAGS = -s
endif

.PHONY: cpy test

all: shared

static:
	$(MAKE) -f Makefile.win cpy
	$(MAKE) -f Makefile.win makeMakedeps
	$(MAKE) -f Makefile.win libRmath.a

shared: static
	$(MAKE) -f Makefile.win Rmath.dll

implib: libRmath.dll.a

cpy: stamp-src

stamp-src: $(SOURCES_NMATH)
	$(CP) -p ../*.c .
	touch $@

ifdef USE_IMPLIBS
test: test-static.exe
else
test: test-static.exe test.exe
endif


## see R-admin for alternatives
test.exe: test.c
	$(CC) -I../../include -DRMATH_DLL -o $@ $< -L. Rmath.dll
	./$@
test-static.exe: test.c
	$(CC) -I../../include -o $@ $< -L. libRmath.a
	./$@

check: distclean
	@rm -f check.log
	@if test -n "${TEST_MC_CORES}"; then \
	  echo "using ${TEST_MC_CORES} cores" > check.log; \
	  $(MAKE) -f Makefile.win -j ${TEST_MC_CORES} all >> check.log 2>&1; \
	  $(MAKE) -f Makefile.win test >> check.log 2>&1; \
	else \
	  $(MAKE) -f Makefile.win all test > check.log 2>&1; \
	fi
	@$(ECHO) "check output can be found in src/nmath/standalone/check.log"

clean:
	@$(RM) $(SOURCES_NMATH) *.o *.d *.def Makedeps stamp-src
distclean: clean
	@$(RM) *.a Rmath.dll test*.exe check.log

DEFS = -DHAVE_CONFIG_H -DMATHLIB_STANDALONE
CPPFLAGS = -I.. -I../../include $(DEFS)

SOURCES_NMATH = \
	mlutils.c \
	d1mach.c i1mach.c \
	fmax2.c fmin2.c fprec.c fround.c ftrunc.c \
	sign.c fsign.c \
	imax2.c imin2.c \
	chebyshev.c log1p.c lgammacor.c gammalims.c stirlerr.c bd0.c \
	gamma.c lgamma.c gamma_cody.c beta.c lbeta.c polygamma.c cospi.c \
	bessel_i.c bessel_j.c bessel_k.c bessel_y.c \
	choose.c \
	snorm.c sexp.c \
	dgamma.c pgamma.c qgamma.c rgamma.c \
	dbeta.c pbeta.c qbeta.c rbeta.c \
	dunif.c punif.c qunif.c runif.c \
	dnorm.c pnorm.c qnorm.c rnorm.c \
	dlnorm.c plnorm.c qlnorm.c rlnorm.c \
	df.c pf.c qf.c rf.c dnf.c \
	dt.c pt.c qt.c rt.c dnt.c \
	dchisq.c pchisq.c qchisq.c rchisq.c rnchisq.c \
	dbinom.c pbinom.c qbinom.c rbinom.c \
	rmultinom.c \
	dcauchy.c pcauchy.c qcauchy.c rcauchy.c \
	dexp.c pexp.c qexp.c rexp.c \
	dgeom.c pgeom.c qgeom.c rgeom.c \
	dhyper.c phyper.c qhyper.c rhyper.c \
	dnbinom.c pnbinom.c qnbinom.c qnbinom_mu.c rnbinom.c \
	dpois.c ppois.c qpois.c rpois.c \
	dweibull.c pweibull.c qweibull.c rweibull.c \
	dlogis.c plogis.c qlogis.c rlogis.c \
	dnchisq.c pnchisq.c qnchisq.c \
	dnbeta.c pnbeta.c qnbeta.c \
	pnf.c pnt.c qnf.c qnt.c \
	ptukey.c qtukey.c \
	toms708.c wilcox.c \
	signrank.c
DEPENDS = $(SOURCES_NMATH:.c=.d)

libRmath.a: sunif.o $(SOURCES_NMATH:.c=.o)
Rmath.def: sunif.o $(SOURCES_NMATH:.c=.o)
	$(ECHO) EXPORTS > $@
	$(NM) $^ | $(SED) -n $(SYMPAT) >> $@
Rmath.dll: libRmath.a Rmath.def
	$(DLL) $(DLLFLAGS) --shared -o $@ Rmath.def $^
libRmath.dll.a: Rmath.def

# Dependencies
makeMakedeps: $(DEPENDS)
	@$(RM) Makedeps
	@cat $(DEPENDS) >> Makedeps

-include Makedeps
