# $Id: Makefile 995 2009-08-11 14:26:31Z mike $

ROOT_DIR := $(shell pwd)

include $(ROOT_DIR)/make/standard.mk

# As NUMBER_OF_PROCESSORS is a built-in variable on Windows, the following has to work only on Linux :-)
NUMBER_OF_PROCESSORS ?= $(shell grep ^processor /proc/cpuinfo | wc -l)

# Use the following number of CPUs for submakes
MAKECPUS ?= $(shell let "n=$(NUMBER_OF_PROCESSORS)*3/2"; echo $$n)

# our make command for submakes
MAKE := nice make --no-print-directory -j$(MAKECPUS)

# how long did it take
START_TIME := $(shell date +%s)
DURATION = $(shell let "d=$$(date +%s)-$(START_TIME)"; echo $$d seconds)

# default target 
default : all

all: header all_test_native all_test_ciao all_app all_libcmini footer
clean: clean_libciao clean_libcmini clean_test_native clean_test_ciao clean_app


header:
	@echo
	@echo "***********************************************************************"
	@echo "** CiAO master Makefile" 
	@echo "**   started at `date`"
	@echo "**   using $(MAKECPUS) parallel sub-makes [change this with MAKECPUS=<n>]"
	@echo "**" 
	@echo
	
footer:
	@echo
	@echo "**" 
	@echo "** CiAO make finished, took $(DURATION)" 
	@echo "***********************************************************************"
	@echo
   	

%_libciao:
	@if [ -d src ];then\
		echo "(MAKE)    $@";\
	 	cd src;\
	 	$(MAKE) $*;\
	 fi

%_libcmini:
	@if [ -d libcmini ];then\
		echo "(MAKE)    $@";\
		cd libcmini;\
		$(MAKE) $*;\
	fi

%_test_native:
	@if [ -d test/native ]; then echo "(MAKE)    $@"; fi
	@$(foreach MKDIR, $(dir $(shell find test/native/ -name "Makefile" 2>/dev/null) ), \
		pushd $(MKDIR) >/dev/null ;\
		echo "(MAKE)    $(MKDIR)";\
		$(MAKE) $* ;\
		popd >/dev/null ;\
	)

clean_test_ciao:
	@if [ -d test/ciao ]; then echo "(MAKE)    $@"; fi
	@$(foreach MKDIR, $(dir $(shell find test/ciao/ -name "Makefile" 2>/dev/null) ), \
		pushd $(MKDIR) >/dev/null ;\
		echo "(MAKE)    $(MKDIR)";\
		$(MAKE) clean ;\
		popd >/dev/null ;\
	)  


%_test_ciao:
	@if [ -d test/ciao ]; then echo "(MAKE)    $@"; fi
	@$(foreach MKDIR, $(dir $(shell find test/ciao/ -name "Makefile" 2>/dev/null) ), \
		pushd $(MKDIR) >/dev/null ;\
		echo "(MAKE)    $(MKDIR)";\
		$(MAKE) $*;\
		popd >/dev/null ;\
	)  


%_app:
	@if [ -d test/native ]; then echo "(MAKE)    $@"; fi
	@$(foreach MKDIR, $(dir $(shell find app/ -name "Makefile" 2>/dev/null) ), \
		pushd $(MKDIR) >/dev/null ;\
		echo "(MAKE)    $(MKDIR)";\
		$(MAKE) $* ;\
		popd >/dev/null ;\
	)  

editmode: 
	@echo Replacing copied files by hardlinks
	@xargs -a copied_files -l -t ln -f
	@echo 
	@echo "***********************************************************************"
	@echo "** WARINING: Editing generated files now changes originals as well!  **"
	@echo "***********************************************************************"
	@echo 
	
	

doc: all_doxygen_libciao

.PHONY: all clean doc editmode header footer


	
