#This is the top level Makefile.
#It goes thru all the subdirs calling make 
#expects that all makefiles in sub-dirs will have the following targets:
#all
#install
#distclean
#clean
#c++
#java
#The last 2 do not need to do anything particularly, but should not cause an error

#Make sure the sub-makefiles can find things
SCT_DAQ_ROOT := $(if $(SCT_DAQ_ROOT),$(SCT_DAQ_ROOT),$(CURDIR)/..)
export SCT_DAQ_ROOT

#default target
default: all

optimize: OPTIMIZE := O3
optimize:
ifeq ($(filter-out optimize,$(MAKECMDGOALS)),)
	$(MAKE) all
else 
	$(MAKE) $(filter-out optimize,$(MAKECMDGOALS))
endif

export OPTIMIZE

include Makefile.include

#Add any new subdirs here - if there are any pre-requisites, please add them below
ALLSUBDIRS := config SCT SCTData SCTTestAPI daq-1 SctService FittingService TestGUI \
           AnalysisService SctApi SctApiIPC sctConf sctConfIPC CalibrationController \
           SummaryWriter sctGUI sctddc-00-00-02 GuiComponents SctDataDisplay MiniUtils ArchivingService

#Now make sure that only contains list of ones that are actually here!
SUBDIRSEXIST = $(addsuffix .checkExist, $(ALLSUBDIRS) SystemTests)
SUBDIRS := $(foreach dir, $(ALLSUBDIRS), $(notdir $(wildcard ../$(dir))))

#Install dirs - the directories things are installed into.  Do not include $(InstallDir) in this list
#otherwise there may be problems with distclean
INSTALLDIRS := $(IncludeDir) $(ConfigDir) $(LibDir) $(EtcDir) $(BinDir) $(DocDir) $(ShareBinDir) \
               $(ShareLibDir) $(ScriptDir)

all: checkNonExist ext install $(SUBDIRS)

checkNonExist: $(SUBDIRSEXIST)

$(SUBDIRSEXIST):
	$(if $(wildcard ../$(basename $@)), ,@echo "*** Directory " $(basename $@) " does not exist so not built! (please check it out from CVS) ***")

#other targets
install: $(INSTALLDIRS) $(SUBDIRS)

ifeq ($(filter-out c++,$(MAKECMDGOALS)),)
c++: all
else
c++:
endif

ifeq ($(filter-out java,$(MAKECMDGOALS)),)
java: all
else
java:
endif

ifeq ($(filter-out clean,$(MAKECMDGOALS)),)
clean: $(SUBDIRS)
	-rm -r -f *.out
else
clean:
endif

#Make sure we don't delete the Config Directory
ifeq ($(filter-out distclean,$(MAKECMDGOALS)),)
distclean: $(SUBDIRS)
	-rm -r -f $(filter-out %$(ConfigDir),$(INSTALLDIRS))
else
distclean:
endif

#Documentation stuff
docs: $(DocDir)
	doxygen Doxyfile_c++
	doxygen Doxyfile_java
	doxygen Doxyfile_idl
	$(MAKE) -C ../docs

# Dirs to check for CVS (skipping RodDaq)
# Add in SystemTests which isn't normally built
CVSSUBDIRS = ${ALLSUBDIRS} SystemTests build docs

# Don't do update on everything as this includes _All_ of RodDaq
# update -l restricts it to the top level directory
update:
	cd ..; cvs update -Pd ${CVSSUBDIRS}; cvs update -l
checkCVS:
	cd ..; cvs -n -q update -Pd ${CVSSUBDIRS}; cvs -n -q update -l

#External stuff
ext: $(INSTALLDIRS)
include Makefile.ext

#Phony targets:
.PHONY: all install clean c++ java $(SUBDIRS) debug distclean docs optimize default $(SUBDIRSEXIST)\
        checkNonExist dist sourceDist binDist

#.IGNORE:

#Make a sub-dir - we are in the build dir - and pass on the target
$(SUBDIRS):
	$(MAKE) -C ../$@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))

#Make sure the install dirs exist
$(INSTALLDIRS):
	mkdir -p $@

$(InstallDir)/%: %
	cp $< $@

#Pre-requisites:
FittingService: SCTData SctService
SCTData: SCT daq-1 sctConfIPC
SCTTestAPI: SCTData
TestGUI: GuiComponents
GuiComponents: SCT SCTData SCTTestAPI CalibrationController AnalysisService ArchivingService
AnalysisService: SCTData SCT SummaryWriter CalibrationController FittingService
SummaryWriter : SCTData SCT
SCT: daq-1
archiving: SCT SCTData
SctService: SCT
SctApi: sctConf SCTData sctddc-00-00-02    # Need ScanResultWriter
SctApiIPC: SctApi sctConfIPC
sctConfIPC: SCT sctConf
sctConf: SCT
CalibrationController: SctApi SctApiIPC SCTData
SCTTestAPI: CalibrationController
sctGUI: GuiComponents TestGUI
SctDataDisplay: SCTData
ArchivingService: SCTData CalibrationController
