# Please use GNU make for this makefile e.g.: gmake
# This is basically a wrapper which calls GNUmakefile in every sub-folder

ifneq ($(findstring .exe,$(SHELL))$(findstring .EXE,$(SHELL)),)
  ENV_NAME := WINDOWS
  PATH+=;C:\cygwin\bin
  SHELL := C:\cygwin\bin\bash.exe
else
  ENV_NAME := $(shell uname -s)
endif
$(info $(ENV_NAME))
ifneq ($(findstring CYGWIN,$(ENV_NAME)),)
  export SHELLOPTS := igncr
endif


ALLPROJECTS = example_apu example_apudisass example_virtualapu arm_corelink_dma_330_disass example_address_translation example_usraccess


.PHONY: all
ifneq ($(findstring CYGWIN,$(ENV_NAME))$(findstring WINDOWS,$(ENV_NAME))$(findstring MINGW,$(ENV_NAME)),)
all: 32 64
else
all: $(ALLPROJECTS)
endif

.PHONY: 32
32: $(addsuffix .32,$(ALLPROJECTS))

.PHONY: 64
64: $(addsuffix .64,$(ALLPROJECTS))

.PHONY: clean
clean:
	rm -rf $(addsuffix /debug,$(ALLPROJECTS))   $(addsuffix /debug64,$(ALLPROJECTS))
	rm -rf $(addsuffix /release,$(ALLPROJECTS)) $(addsuffix /release64,$(ALLPROJECTS))
	rm -rf $(addsuffix /build-*,$(ALLPROJECTS))
	rm -f  $(addsuffix /*.dll,$(ALLPROJECTS))   $(addsuffix /*.so.1,$(ALLPROJECTS))


define PROJECT_template

 .PHONY: $(1)
 $(1):
	@echo -e "\n#### Building \"$$@\""
	$(MAKE) -R $(MAKEOPTS) -C $(project)

 .PHONY: $(1).32
 $(1).32:
	@echo -e "\n#### Building \"$$@\""
	$(MAKE) -R $(MAKEOPTS) -C $(project) PLATFORM=x86

 .PHONY: $(1).64
 $(1).64:
	@echo -e "\n#### Building \"$$@\""
	$(MAKE) -R $(MAKEOPTS) -C $(project) PLATFORM=x64

 .PHONY: $(1).clean
 $(1).clean:
	@echo -e "\n#### Cleaning \"$$@\""
	$(MAKE) -R $(MAKEOPTS) -C $(project) clean

endef

$(foreach project,$(ALLPROJECTS),$(eval $(call PROJECT_template,$(project))))

