############################ -*- Mode: Makefile -*- ###########################
## Makefile-rcb --- Makefile for R cross-building
##
## Author          : Jun Yan      <jyan@stat.uconn.edu>
##                   A.J. Rossini <blindglobe@gmail.com>
##
## Acknowledgments : We are grateful to 
## 		     Ben Bolker, 
## 		     Stéphane Cano,
## 		     Stéphane Dray,
## 		     Stephen Eglen, 
## 		     Paul Murrell, and
## 		     Brian Ripley 
##                   for helpful discussions.
##
## This file       : http://cran.r-project.org/doc/contrib/Makefile-rcb
## Documentation   : http://cran.r-project.org/doc/contrib/cross-build.pdf
##
## Last modified   : 2007-10-10
###############################################################################

SHELL = /bin/sh
WGET = wget --passive-ftp
BINS = \*.so \*.dll \*.RData \*.rda \*.gz \*.zip \*.rds \*.pdf \*.ps \*.raw \*.bin

#### define directories

## the current directory where all the following cross-building happens
RCB := $(shell pwd)
## the directory where the downloaded sources are stored
DOWNDIR = $(RCB)/downloads
## the directory where the cross-tools are installed
CROSSDIR = $(RCB)/cross-tools
## the directory where R sources are unpacked for cross-building
WINR = $(RCB)/WinR
## the dirECTOry where R for Linux will be compiled and installed
LINUXR = $(RCB)/LinuxR

## the directory where package sources are stored
PKGDIR = $(RCB)/pkgsrc
## the directory where the built packages for windows are to be stored
WINRLIBS = $(RCB)/WinRlibs

## CRAN address
CRAN = http://rh-mirror.linux.iastate.edu/CRAN/
## current versions
R = R-2.6.0
#R = R-devel
## snapshot R is only available at ftp://ftp.stat.math.ethz.ch/Software/R
ifeq ($(findstring devel, $(R)), devel)
RURL = ftp://ftp.stat.math.ethz.ch/Software/R
REXT = tar.gz
else
RURL = $(CRAN)src/base/R-2
#RURL = http://cran.r-project.org/src/base/R-2
REXT = tar.gz
endif


## cross-compilers
## MINGW_CROSS = mingw-cross5
MINGW_DIR = i586-mingw32
MINGW_BINPREF = i586-mingw32-

MINGW_RUNTIME = mingw-runtime-3.13

XTOOLS_URL = http://www.stats.ox.ac.uk/pub/Rtools/i586-cross-tools.tar.bz2
XTOOLS = i586-cross-tools.tar.bz2

## other components
R_TCL_URL = http://www.stats.ox.ac.uk/pub/Rtools/R_Tcl.zip 
ICONV_URL = http://www.stats.ox.ac.uk/pub/Rtools/iconv.dll
# JPEG_SRC = jpegsrc.v6b.tar.gz
# JPEG_URL = ftp://ftp.uu.net/graphics/jpeg/$(JPEG_SRC)
# PNG_SRC = libpng-1.2.8-config.tar.gz
# PNG_URL = http://download.sourceforge.net/libpng/$(PNG_SRC)



## R_EXE
LinuxFresh = NO
ifeq ($(strip $(LinuxFresh)),YES)
R_EXE = $(LINUXR)/R/bin/R
else
R_EXE = R
endif

default: 
	@echo "syntax:"
	@echo "make <target>"
	@echo "No <Target> Specified.  Should be one of:"
	@echo " "
	@echo "-------------------"
	@echo "down     : downloads files"
	@echo "xtools   : unpack cross compilers"
	@echo "prepsrc  : unpack and prepare source code"
	@echo "linuxR   : build a current version Linux R"
	@echo "LinuxFresh=YES mkrules  : patch source code for cross-compile"
	@echo "R        : cross-compile R"
	@echo " "
	@echo "(or to run all at once (trusting, aren't you?))"
	@echo "Assuming a current version of Linux R is on the path. "
	@echo " "
	@echo "CrossCompileBuild"
	@echo " "
	@echo "(To build packages or bundles, steps above have to be run"
	@echo " first to get a working build of R, which is required!)"
	@echo " "
	@echo "pkg-NAME_VER    : cross-build package NAME_VER"
	@echo "bundle-NAME_VER : cross-build packages in bundle NAME_VER"
	@echo " "
	@echo "(For example, put geepack_0.2-7.tar.gz and VR_7.1-10.tar.gz"
	@echo " in subdirectory pkgsrc, then do"
	@echo " "
	@echo " make pkg-geepack_0.2-7"
	@echo " make bundle-VR_7.1-10)"
	@echo "-------------------"
	@echo " "

buildR: clean prepsrc mkrules R

#### steps described in the document

down:
	mkdir -p $(DOWNDIR); \
	cd $(DOWNDIR); \
	$(WGET) $(RURL)/$(R).$(REXT) -O $(R).tgz; \
	$(WGET) $(XTOOLS_URL); \
	$(WGET) $(R_TCL_URL); \
	$(WGET) $(ICONV_URL); \


downR:
	mkdir -p $(DOWNDIR); \
	cd $(DOWNDIR); \
	$(WGET) $(RURL)/$(R).$(REXT) -O $(R).tgz; \

downRTcl:
	mkdir -p $(DOWNDIR); \
	cd $(DOWNDIR); \
	$(WGET) $(R_TCL_URL);\

downIconv:
	mkdir -p $(DOWNDIR); \
	cd $(DOWNDIR); \
	$(WGET) $(ICONV_URL); \

downXtools:
	mkdir -p $(DOWNDIR); \
	cd $(DOWNDIR); \
	$(WGET) $(XTOOLS); \

## added '--enable-R-shlib' to the configure script to make a R shared library
## needed for the SJava package for example
## Cano Stéphane" <Stephane.Cano@he-arc.ch>
linuxR:
	mkdir -p $(LINUXR); \
	cd $(LINUXR); \
	tar zxf $(DOWNDIR)/$(R).tgz; \
	cd $(LINUXR)/$(R); \
	./configure --enable-R-shlib; \
	make; \
	make prefix=$(LINUXR)/R install; \

xtools:
	mkdir -p $(RCB)/cross-tools; \
	cd $(CROSSDIR); \
	tar jxf $(DOWNDIR)/$(XTOOLS); \
#	cd $(CROSSDIR)/$(MINGW_DIR); \
#	tar zxf $(DOWNDIR)/$(MINGW_RUNTIME).tar.gz; \

prepsrc:
	mkdir -p $(WINR); \
	cd $(WINR); \
	rm -rf $(R); \
	tar zxf $(DOWNDIR)/$(R).tgz; \
	cd $(WINR)/$(R); \
	unzip $(DOWNDIR)/R_Tcl.zip; \
	cd $(WINR)/$(R)/src/gnuwin32/unicode; \
	cp $(DOWNDIR)/iconv.dll . ; \


mkrules:
	cd $(WINR)/$(R)/src/gnuwin32/; \
	cp MkRules MkRules.orig; \
	sed 's|^BUILD=GCC4|BUILD=CROSS|; s|^CROSS-GCC=gcc4|CROSS-GCC=gcc3|; s|^HEADER=.*$$|HEADER=$(CROSSDIR)/$(MINGW_DIR)/include|; s|^BINPREF=.*$$|BINPREF=$(MINGW_BINPREF)|; s|^R_EXE=.*$$|R_EXE=$(R_EXE)|; s|# R_EXE=R|R_EXE=$(R_EXE)|' MkRules.orig > MkRules

## Only gcc3 is supported in Rtools for CROSS-GCC at this moment


R:
	export PATH=$(CROSSDIR)/bin:$(CROSSDIR)/$(MINGW_DIR)/bin:$(PATH); \
	cd $(WINR)/$(R)/src/gnuwin32/; \
	make; \
	cd $(RCB)/WinR; \
	tar zcf Win-$(R).tgz $(R)

pkg-%:
	export PATH=$(CROSSDIR)/bin:$(CROSSDIR)/mingw32/bin:$(PATH); \
	export mypkg=`echo $* | cut -d'_' -f1,1`; \
	export mypkg_v=$(*); \
	cd $(PKGDIR); \
	rm -rf $$mypkg; \
	tar zxf $*.tar.gz; \
	echo -------$$mypkg------; \
	cd $(WINR)/$(R)/src/gnuwin32/; \
	make PKGDIR=$(PKGDIR) RLIB=$(WINRLIBS) STAMP=no pkg-$$mypkg; \
	mkdir -p $(WINRLIBS); \
	cd $(WINRLIBS); \
	rm -rf $$mypkg_v.zip; \
	zip -rl $$mypkg_v.zip $$mypkg -x $(BINS);\
	zip -r9 $$mypkg_v.zip $$mypkg -i $(BINS);\
	rm -rf $$mypkg_v

bundle-%:
	export PATH=$(CROSSDIR)/bin:$(CROSSDIR)/mingw32/bin:$(PATH); \
	cd $(WINR)/$(R)/src/library;\
	tar zxf $(PKGDIR)/$*.tar.gz; \
	export mybundle=`echo $* | cut -d'_' -f1,1`; \
	export mybundle_v=$(*); \
	echo -------$$mybundle------; \
	cd $(WINR)/$(R)/src/gnuwin32/; \
	$(R_EXE) CMD perl XINSTALL --unsafe --docs=normal ../library/$${mybundle} || exit 1; \
	mkdir -p $(WINRLIBS); \
	ls $(WINR)/$(R)/src/library/$$mybundle/; \
	echo $(PKGDIR)/$$mybundle/DESCRIPTION; \
	grep "^Contains:" $(WINR)/$(R)/src/library/$$mybundle/DESCRIPTION | sed -e 's/Contains: //'; \
	export incl=`grep "^Contains:" $(WINR)/$(R)/src/library/$$mybundle/DESCRIPTION | sed -e 's/Contains: //'`; \
	echo ------$$incl------; \
	cd $(WINR)/$(R)/library; \
	for pkg in $$incl; do \
	(rm -rf $$pkg.zip; \
	 zip -rl $(WINRLIBS)/$$pkg.zip $$pkg -x $(BINS); \
	 zip -r9 $(WINRLIBS)/$$pkg.zip $$pkg -i $(BINS); \
	 rm -rf $$pkg); \
	done; \
	rm -rf $(WINR)/$(R)/src/library/$${mybundle}; \


#### other targets that may be useful

pkgclean-%:
	cd $(PKGDIR); \
	rm -rf $*


recommended:
	export PATH=$(CROSSDIR)/bin:$(CROSSDIR)/mingw32/bin:$(PATH); \
	cd $(WINR)/$(R)/src/gnuwin32/; \
	make recommended

# dist-recommended:
# 	mkdir -p $(WINRLIBS); \
# 	cd $(WINR)/$(R)/src/gnuwin32/; \
# 	make dist-recommended; \
# 	mv Recommended.zip $(WINRLIBS)/

clean:
	rm -rf $(WINR)

#### not tested
# rinstaller:
# 	export PATH=$(CROSSDIR)/bin:$(CROSSDIR)/mingw32/bin:$(PATH); \
# 	cd $(WINR)/$(R)/src/gnuwin32/; \
# 	make rinstaller


CrossCompileBuild :
	make down
	make xtools 
	make prepsrc
	make mkrules
	make R

## build both LinuxR and WindowsR
CrossCompileBuildAll :
	make down
	make xtools 
	make prepsrc
	make linuxR
	make LinuxFresh=YES mkrules
	make R
