#
# Copyright � 2011 Daniel Taliun, Christian Fuchsberger and Cristian Pattaro. All rights reserved.
#
# This file is part of GWAtoolbox.
#
# GWAtoolbox 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.
#
# GWAtoolbox 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 GWAtoolbox.  If not, see <http://www.gnu.org/licenses/>.
#

PKG_LIBS = $(GWALIBS)

PKG_CFLAGS =
PKG_CPPFLAGS = 
PKG_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

GWADIRS = 	auxiliary \
			exception \
			bitarray \
			zlib \
			reader \
			writer \
			descriptor \
			gwafile \
			gwasformat/libs/columns \
			gwasformat/libs/formatter \
			gwasqc/libs/expressions \
			gwasqc/libs/metas \
			gwasqc/libs/plots \
			gwasqc/libs/analyzer \
			intervaltree \
			annotation \
			harmonization \
			independization

GWALIBS = 	auxiliary/*.o \
			exception/*.o \
			bitarray/*.o \
			zlib/*.o \
			reader/*.o \
			writer/*.o \
			descriptor/*.o \
			gwafile/*.o \
			gwasformat/libs/columns/*.o \
			gwasformat/libs/formatter/*.o \
			gwasqc/libs/expressions/*.o \
			gwasqc/libs/metas/*.o \
			gwasqc/libs/plots/*.o \
			gwasqc/libs/analyzer/*.o \
			intervaltree/*.o \
			annotation/*.o \
			harmonization/*.o \
			independization/*.o
			
.PHONY: all gwalibs
     
all: $(SHLIB)

$(SHLIB): gwalibs

gwalibs:	subclean
			@for gwadir in $(GWADIRS); do \
				(cd $${gwadir} && PKG_CFLAGS="$(PKG_CFLAGS)" PKG_CPPFLAGS="$(PKG_CPPFLAGS)" PKG_CXXFLAGS="$(PKG_CXXFLAGS)" R_MAKECONF="$(R_MAKECONF)" $(MAKE) gwalib) || exit 1; \
			done
			
subclean:
	@-rm -f $(SHLIB)
	@for gwadir in $(GWADIRS); do \
		(cd $${gwadir} && R_MAKECONF="$(R_MAKECONF)" $(MAKE) clean) || exit 1; \
	done

clean: subclean
	@-rm -f *.o
