#########################################################
#
# top level GNUmakefile to build Marlin with addtional 
# user packages containing processors
#
#  usage: all additional packages are expected to live 
#  in the packages subdirectory with the following structure:
#         ./packages/MyPackage/include
#                             ./src/
#                             ./src/GNUmakefile
#                             ./lib
#
#  where the GNUmakefile has a target 'lib; that creates
#  a library   ./packages/MyPackage/lib/libMyPackage.a  
#  see e.g.:  $MARLIN/examples/mymarlin
#  
#
# @author Frank Gaede, DESY
# @version $Id: GNUmakefile_desy,v 1.1 2005-08-04 16:38:57 aplin Exp $
#
#########################################################


#
# ---------------- additional user libs go here ------------------- 
#

USERINCLUDES  = -I $(MARLIN)/packages/MarlinUtil/include
USERINCLUDES += -Df2cFortran
USERINCLUDES += -I /afs/desy.de/group/it/ilcsoft/gsl/1.6/include

export USERINCLUDES


USERLIBS  = -lg2c
USERLIBS += -L /opt/products/cernlib/pro/lib/ -lmathlib -lkernlib
USERLIBS += -L /afs/desy.de/group/it/ilcsoft/gsl/1.6/lib -lgsl -lgslcblas

export USERLIBS



#USERINCLUDES += -I/opt/products/gsl/1.6/include
#USERINCLUDES += -I /afs/desy.de/user/g/gaede/marlin/devel/MarlinCommon/include
export USERINCLUDES

#USERLIBS += -L /opt/products/gsl/1.6/lib -lgsl -lgslcblas
#USERLIBS += -L  /afs/desy.de/user/g/gaede/marlin/devel/MarlinCommon/lib -lMarlinCommon
export USERLIBS

#
#-------------------------------------------------------------------
#


subdirs := $(patsubst packages/%/src/GNUmakefile,packages/%, $(wildcard packages/*/src/GNUmakefile) )


packages := $(patsubst packages/%,%,$(subdirs)) 

MARLINLIB := -Wl,-whole-archive $(foreach package,$(packages),  -L $(MARLIN)/packages/$(package)/lib -l$(package) ) -Wl,-no-whole-archive
export MARLINLIB

packagelibs := $(foreach package,$(packages), packages/$(package)/lib/lib$(package).a )
export packagelibs

.PHONY: lib clean bin doc $(subdirs) test



all: lib bin 


lib:
	$(MAKE) -C src lib
	@for i in $(subdirs); do \
	if [ -f "$$i/src/GNUmakefile" ] ; then \
	echo "Building library for $$i..."; \
	(cd $$i/src; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) lib); fi ; done

bin: lib ./bin/Marlin

./bin/Marlin: ./lib/libMarlin.a $(packagelibs)
	$(MAKE) -C src rebuild


doc:
	$(MAKE) -C src doc


clean:
	$(MAKE) -C src clean
	@for i in $(subdirs); do \
	if [ -f "$$i/src/GNUmakefile" ] ; then \
	echo "Clearing in $$i..."; \
	(cd $$i/src; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) clean); fi ; done


test:
	@echo $(subdirs)
	@echo $(packages)
	@echo $(packagelibs)
	@ls -l $(MARLIN)/bin/Marlin
	@for i in $(subdirs); do \
	echo "ls -l $$i" ; \
	ls -l $$i/lib ; done

