#
# Copyright  2013 Daniel Taliun, Johann Gamper and Cristian Pattaro. All rights reserved.
#
# This file is part of LDExplorer.
#
# LDExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LDExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LDExplorer.  If not, see <http://www.gnu.org/licenses/>.
#

PKG_LIBS = $(APPLIBS) $(SHLIB_OPENMP_CFLAGS) $(SHLIB_OPENMP_CPPFLAGS) $(SHLIB_OPENMP_CXXFLAGS)

PKG_CFLAGS = $(SHLIB_OPENMP_CFLAGS)
PKG_CPPFLAGS = $(SHLIB_OPENMP_CPPFLAGS)
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)

R_MAKECONF = $(R_HOME)/etc${R_ARCH}/Makeconf

ifneq ($(strip $(wildcard ${HOME}/.R/Makevars-${R_PLATFORM})),)
	R_MAKECONF += ${HOME}/.R/Makevars-${R_PLATFORM}
else
ifneq ($(strip $(wildcard ${HOME}/.R/Makevars.win)),)
	R_MAKECONF += ${HOME}/.R/Makevars.win
else
ifneq ($(strip $(wildcard ${HOME}/.R/Makevars.win64)),)
	R_MAKECONF += ${HOME}/.R/Makevars.win64
else
ifneq ($(strip $(wildcard ${HOME}/.R/Makevars)),)
	R_MAKECONF += ${HOME}/.R/Makevars 
endif
endif
endif	
endif

APPDIRS = 	auxiliary \
			exception \
			zlib \
			reader \
			writer \
			db \
			algorithms

APPLIBS = 	auxiliary/*.o \
			exception/*.o \
			zlib/*.o \
			reader/*.o \
			writer/*.o \
			db/*.o \
			algorithms/*.o
			
.PHONY: all applibs
     
all: $(SHLIB)

$(SHLIB): applibs

applibs:	subclean
			@for appdir in $(APPDIRS); do \
				(cd $${appdir} && PKG_CFLAGS="$(PKG_CFLAGS)" PKG_CPPFLAGS="$(PKG_CPPFLAGS)" PKG_CXXFLAGS="$(PKG_CXXFLAGS)" R_MAKECONF="$(R_MAKECONF)" $(MAKE) applib) || exit 1; \
			done
			
subclean:
	@-rm -f $(SHLIB)
	@for appdir in $(APPDIRS); do \
		(cd $${appdir} && R_MAKECONF="$(R_MAKECONF)" $(MAKE) clean) || exit 1; \
	done

clean: subclean
	@-rm -f *.o
