#*******************************************************************************
# ALMA - Atacama Large Millimiter Array
# (c) European Southern Observatory, 2006 
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This library 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
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
# "@(#) $Id: Makefile,v 1.14 2011/05/13 12:52:57 mpasquat Exp $"
#
# Makefile of ........
#
# who       when      what
# --------  --------  ----------------------------------------------
# sturolla  2006-10-25  modified to add rtutil
# bjeram  2006-03-13  created
#
#*******************************************************************************
# This Makefile follows ACS Standards (see Makefile(5) for more).
#*******************************************************************************
# REMARKS
#    None
#------------------------------------------------------------------------


###############################################
# Macro definitions.                          #
###############################################
define makeIt
   ( ((/usr/bin/time -f "$1 COMPILATION TIME %E" make $(MAKE_FLAGS) -C $1 $2 2>&1) && ( echo "### ==> SUCCEDED" | tee -a  $4 >/dev/null )) || ( echo "### ==> FAILED $2 ! " | tee -a $3 $4 1>&2 )) | tee -a $3 $4 >/dev/null;
endef

define makeItAux
   (( make $(MAKE_FLAGS) -C $1 $2 2>&1 ) || ( echo "### ==> FAILED $2 ! " | tee -a $3 $4 1>&2 )) | tee -a $3 $4 >/dev/null;
endef
###############################################


SUBSYSTEM = "ACS-RTOS"

MODULES_RTOS  = lkm rtTest rtCommon rtlog rtAlarm rtutil

MODULES =  $(MODULES_RTOS)

KEEP_GOING = 1

#
# If option KEEP_GOING=on is present in the make command line gnu_make is NOT interrupted 
# when the first error is encountered
#

ifdef KEEP_GOING
   KEEP_GOING="on"
else
   KEEP_GOING="off"
endif

RETURN_CODE=return_code
TMP_RETURN_CODE=tmp_return_code

MAKE_FLAGS = "-k"
PLATFORM := $(shell uname)

SHELL=/bin/ksh
ECHO=echo

ifdef MAKE_VERBOSE
    AT = 
    OUTPUT =
else
    AT = @
    OUTPUT = > /dev/null
endif
#
os     = $(shell uname)
osrev  = $(shell uname -r)

ifeq ($(os),SunOS)
	realtime=YES
endif

#
# "Failed all" error management
#
define mng_failed_all
	if [[ -a  $(TMP_RETURN_CODE) ]]; then\
		$(ECHO) "### ==> FAILED all ! " | tee -a build.log | tee -a $(RETURN_CODE);\
		rm $(TMP_RETURN_CODE);\
		if [[ $(KEEP_GOING) = "off" ]]; then \
			if [[ -a $(RETURN_CODE) ]]; then \
				rm $(RETURN_CODE);\
			fi;\
			exit 2;\
		fi;\
	fi 
endef

#
# "Failed install" error management
#
define mng_failed_install
	if [[ -a  $(TMP_RETURN_CODE) ]]; then\
		$(ECHO) "### ==> FAILED install ! " | tee -a build.log | tee -a $(RETURN_CODE);\
		rm $(TMP_RETURN_CODE);\
		if [[ $(KEEP_GOING) = "off" ]]; then \
			if [[ -a $(RETURN_CODE) ]]; then \
				rm $(RETURN_CODE);\
			fi;\
			exit 2;\
		fi;\
	fi 
endef


#
# "Failed clean" error management
#
define mng_failed_clean
	if [[ -a  $(TMP_RETURN_CODE) ]]; then\
		$(ECHO) "### ==> FAILED clean ! " | tee -a build.log | tee -a $(RETURN_CODE);\
		rm $(TMP_RETURN_CODE);\
		if [[ $(KEEP_GOING) = "off" ]]; then \
			if [[ -a $(RETURN_CODE) ]]; then \
				rm $(RETURN_CODE);\
			fi;\
			exit 2;\
		fi;\
	fi 
endef

#
# This target just forward any make target to all modules
#
define canned
	@$(ECHO) "############ Executing '$@' on all $(SUBSYSTEM) modules #################"
	@for member in  $(foreach name, $(MODULES), $(name) ) ; do \
		    $(ECHO) "############ $${member}" ;\
		    if [ ! -d $${member} ]; then \
                         echo "### ==> $${member} MODULE NOT FOUND! FAILED! " | tee -a build.log;\
                    fi;\
		    if [ -f $${member}/src/Makefile ]; then \
			$(MAKE) $(MAKE_FLAGS) -C $${member}/src/ $@  >> build.log 2>&1 || break ;\
		    elif [ -f $${member}/ws/src/Makefile ]; then \
			$(MAKE) $(MAKE_FLAGS) -C $${member}/ws/src/ $@  >> build.log 2>&1  || break ;\
		    fi;\
		    if [ "$(realtime)" == "YES" ]; then \
			if [ -f $${member}/lcu/src/Makefile ]; then \
			$(MAKE) $(MAKE_FLAGS) -C $${member}/lcu/src/ $@  >> build.log 2>&1 || break ;\
			fi;\
		    fi;\
		done
endef

clean_log:
	@$(ECHO) "############ Clean Build Log File: build.log #################"
	@rm -f build.log
	@touch build.log

clean_test_log:
	@$(ECHO) "############ Clean Test Log File: test.log #################"
	@rm -f test.log
	@touch test.log

#
# This target builds and installs the complete Laser
# on a clean directory structure.
# Per each module it executes:
#    make clean all install
#
build:  clean_log update
	@$(ECHO) "... done"

## Building all modules and then clean the directory
build_clean:    clean_log update_clean
	@$(ECHO) "... done"

#
# This target re-builds and installs the complete Laser 
# on an existing directory structure.
# Per each module it executes:
#    make clean all man install clean
#
rebuild:        clean_log update
	@$(ECHO) "... done"

# Update of all core components
# This does also make man and does clenup at the end.
# We use a define for the procedure of a single module, so that
# we can reuse it also in the update_clean_test target further down.

# We use a define for the procedure of a single module, so that
# we can reuse it also in the update_clean_test target further down.

define update-one-module
		if [ ! -d $${member} ]; then \
			echo "######## ==> $${member} MODULE NOT FOUND! FAILED! " | tee -a build.log;\
			if [[ $(KEEP_GOING) = "off" ]]; then \
				exit 2;\
			fi;\
		fi;\
		if [ -f $${member}/src/Makefile ]; then \
			$(ECHO) "############ $${member} MAIN" | tee -a build.log || $(ECHO) "tee failed" ;\
			$(call makeItAux,$${member}/src,clean,build.log,$${member}/src/NORM-BUILD-OUTPUT) \
			$(call makeIt,$${member}/src,all,build.log,$${member}/src/NORM-BUILD-OUTPUT) \
			$(call makeItAux,$${member}/src,install,build.log,$${member}/src/NORM-BUILD-OUTPUT) \
			continue ;\
		elif [ -f $${member}/ws/src/Makefile ]; then \
			$(ECHO) "############ $${member} WS" | tee -a build.log;\
			$(call makeItAux,$${member}/ws/src,clean,build.log,$${member}/ws/src/NORM-BUILD-OUTPUT) \
			$(call makeIt,$${member}/ws/src,all,build.log,$${member}/ws/src/NORM-BUILD-OUTPUT) \
			$(call makeItAux,$${member}/ws/src,install,build.log,$${member}/ws/src/NORM-BUILD-OUTPUT) \
                fi; \
		if [  ! -d $${member} ]; then \
			if [ -f $${member}/lcu/src/Makefile ]; then \
				$(ECHO) "############ $${member} LCU" | tee -a build.log;\
				$(call makeItAux,$${member}/lcu/src,clean,build.log,$${member}/lcu/src/NORM-BUILD-OUTPUT) \
				$(call makeIt,$${member}/lcu/src,all,build.log,$${member}/lcu/src/NORM-BUILD-OUTPUT) \
				$(call makeItAux,$${member}/lcu/src,install,build.log,$${member}/lcu/src/NORM-BUILD-OUTPUT) \
			fi;\
		fi
endef

define update-clean-one-module
		if [ ! -d $${member} ]; then \
			echo "######## ==> $${member} MODULE NOT FOUND! FAILED! " | tee -a build.log;\
			if [[ $(KEEP_GOING) = "off" ]]; then \
				exit 2;\
			fi;\
		fi;\
		if [ -f $${member}/src/Makefile ]; then \
			$(ECHO) "############ $${member} MAIN" | tee -a build.log || $(ECHO) "tee failed" ;\
			$(MAKE) $(MAKE_FLAGS) -C $${member}/src/ clean >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
			$(mng_failed_clean);\
			$(MAKE) $(MAKE_FLAGS) -C $${member}/src/ all >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
			$(mng_failed_all);\
			$(MAKE) $(MAKE_FLAGS) -C $${member}/src/ install >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
			$(mng_failed_install);\
			$(MAKE) $(MAKE_FLAGS) -C $${member}/src/ clean >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
			$(mng_failed_clean);\
			continue ;\
		elif [ -f $${member}/ws/src/Makefile ]; then \
			$(ECHO) "############ $${member} WS" | tee -a build.log;\
			$(MAKE) $(MAKE_FLAGS) -C $${member}/ws/src/ clean >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
			$(mng_failed_clean);\
			$(MAKE) $(MAKE_FLAGS) -C $${member}/ws/src/ all >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
			$(mng_failed_all);\
			$(MAKE) $(MAKE_FLAGS) -C $${member}/ws/src/ install >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
			$(mng_failed_install);\
			$(MAKE) $(MAKE_FLAGS) -C $${member}/ws/src/ clean >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
			$(mng_failed_clean);\
                fi; \
		if [  ! -d $${member} ]; then \
			if [ -f $${member}/lcu/src/Makefile ]; then \
				$(ECHO) "############ $${member} LCU" | tee -a build.log;\
				$(MAKE) $(MAKE_FLAGS) -C $${member}/lcu/src/ clean >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
				$(mng_failed_clean);\
				$(MAKE) $(MAKE_FLAGS) -C $${member}/lcu/src/ all >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
				$(mng_failed_all);\
				$(MAKE) $(MAKE_FLAGS) -C $${member}/lcu/src/ install >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
				$(mng_failed_install);\
				$(MAKE) $(MAKE_FLAGS) -C $${member}/lcu/src/ clean >> build.log 2>& 1 || echo $$? >> $(TMP_RETURN_CODE) ;\
				$(mng_failed_clean);\
			fi;\
		fi
endef
#
#
# building all modules
#
update:	install_rtos_tarfile
	@$(ECHO) "############ build $(SUBSYSTEM) Software         #################"| tee -a build.log
	@# Deletion of temporary files used to store make return code
	@if [[ -a $(TMP_RETURN_CODE) ]]; then \
		rm $(TMP_RETURN_CODE);\
	fi
	@if [[ -a $(RETURN_CODE) ]]; then \
		rm $(RETURN_CODE);\
	fi
	@for member in  $(foreach name, $(MODULES), $(name) ) ; do \
	         $(update-one-module);\
	done;
	@$(ECHO) "... done"
#
update_clean:	install_rtos_tarfile
	@$(ECHO) "############ build $(SUBSYSTEM) Software  and clean       #################"| tee -a build.log
	@# Deletion of temporary files used to store make return code
	@if [[ -a $(TMP_RETURN_CODE) ]]; then \
		rm $(TMP_RETURN_CODE);\
	fi
	@if [[ -a $(RETURN_CODE) ]]; then \
		rm $(RETURN_CODE);\
	fi
	@for member in  $(foreach name, $(MODULES), $(name) ) ; do \
		$(update-clean-one-module);\
	done;\
	true;
	@$(ECHO) "############### DONE (Re-)build RTOS SOftware #################"| tee -a build.log


#
# building all modules, and then clean the temporary directory
#
#
# INCLUDE STANDARDS
# -----------------
MAKEDIRTMP := $(shell searchFile include/acsMakefile)
ifneq ($(MAKEDIRTMP),\#error\#)
   MAKEDIR := $(MAKEDIRTMP)/include
   include $(MAKEDIR)/acsMakefile
endif


#
# Test target
#
define test-one-module
		if [ -d $${member}/ws/test ]; then\
			$(ECHO) "############ $${member}/ws/test WS TEST ############" | tee -a test.log ;\
			$(MAKE) -k -C $${member}/ws/test/ $@ | tee -a test.log | egrep '(Nothing to|FAILED.|PASSED.|Error:)';\
			if [ -d $${member}/lcu/test ]; then\
				$(ECHO) "############ $${member}/lcu/test LCU TEST ############" | tee -a test.log;\
				$(MAKE) -k -C $${member}/lcu/test/ $@ | tee -a test.log | egrep '(Nothing to|FAILED.|PASSED.|Error:)';\
			fi;\
		elif [ -d $${member}/test ]; then\
			$(ECHO) "############ $${member}/test MAIN TEST ############" | tee -a test.log ;\
			$(MAKE) -k -C $${member}/test/ $@ | tee -a test.log | egrep '(Nothing to|FAILED.|PASSED.|Error:)';\
		else\
			$(ECHO) "### ==> $${member} TEST DIRECTORY STRUCTURE NOT FOUND! FAILED!" | tee -a test.log ;\
		fi
endef


.PHONY: test

Test = test
$(Test): clean_test_log
	@$(ECHO) "############ TEST $(SUBSYSTEM) Software #################"| tee -a test.log
	@for member in $(foreach name,$(MODULES),$(name)); do\
		$(test-one-module);\
	done
	@$(ECHO) "... done"

update_clean_test: clean_test_log 
	@$(ECHO) "############ UPDATE and TEST RTOS Software #################"| tee -a build.log
	@for member in $(foreach name,$(MODULES),$(name)); do\
		$(update-clean-one-module);\
		$(test-one-module);\
	done;\
	true;
	@$(SHELL)  $(MODULE_PREFIX)/acsBUILD/src/acsBUILDAfterBuildMod.sh >> build.log 2>& 1
	@$(ECHO) "############ DONE UPDATE and TEST Laser Software    #################"| tee -a build.log


install_rtos_tarfile: clean
ifndef NO_TAR_FILE
	@echo "############ installing rtos tar filefile      #################" 
	-@gtar -czf $(VLTTOP)/config/rtos.src.tar.gz --exclude="CVS" -C .. rtos   ; \
	chmod -R ug+rw $(VLTTOP)/config/rtos.src.tar.gz;
endif

#
# Standard canned targets
#
clean:	
	$(canned)
all:	
	$(canned)
install:
	$(canned)

man:
	$(canned)

buildClean: build clean

buildMan: build man

#
# ___oOo___
