###############################################################################
# Top level makefile for build system
#
# Copyright 2006 Nintendo.  All rights reserved.
#
# These coded instructions, statements, and computer programs contain
# proprietary information of Nintendo of America Inc. and/or Nintendo
# Company Ltd., and are protected by Federal copyright law.  They may
# not be disclosed to third parties or copied or duplicated in any form,
# in whole or in part, without the prior written consent of Nintendo.
###############################################################################


# Valid targets
# "make" by itself builds and installs everything listed in LIBS, DEMOS and
# TESTS.  Default target is HW1 DEBUG
# "make clean" cleans everything listed in LIBS, DEMOS and
# TESTS.  Only MACOS DEBUG versions are cleaned
# "make NDEBUG=TRUE" builds nondebug versions.
# "make NDEBUG=TRUE clean" cleans only nondebug versions.

include ../../devroot/include/make/SDKdefs
include $(REVOLUTION_SDK_ROOT)/build/buildtools/commondefs

###############################################################################
# Dependencies
###############################################################################

# THE ORDER OF THE LIBRARIES LISTED IS ALSO THE LIBRARY BUILD ORDER

# XXX All libraries that are included in commondefs->REVOLUTION_LIBS must be listed
# XXX here under $(LIBS), or else executables will fail to link

LIBS     =  \
            p2p          \
            p2p_services \
            p2p_modules \
            p2p_testframework \
            ecdk \
	    ecshop

TESTS    =  \
            natTest \
            ntsClient \
            dwc_testCommon \
            dwc_matchAnybodyTest \
            voip \
	    ecdk-pre-release4/ectest1 \
	    ecdk-pre-release5/rmown   \
	    ecdk-pre-release5/ectest1 \
	    ecdk-pre-release5/ectest2 \

DEMOS    =  \
            voipNew \
            p2pPlaza

LIB_ROOT  = ./libraries/
TEST_ROOT = ./tests/
DEMO_ROOT = ./demos/

LIBDIRS     = $(addprefix $(LIB_ROOT)/,$(LIBS))
TESTDIRS    = $(addprefix $(TEST_ROOT)/,$(TESTS))
DEMODIRS    = $(addprefix $(DEMO_ROOT)/,$(DEMOS))

HOSTLIBDIRS  = $(addprefix $(LIB_ROOT)/,$(HOST_LIBS))
HOSTDEMODIRS = $(addprefix $(DEMO_ROOT)/,$(HOST_DEMOS))
ADPCMLIBDIRS = $(addprefix $(LIB_ROOT)/,$(ADPCMLIB))
SOUNDFILELIBDIRS = $(addprefix $(LIB_ROOT)/,$(SOUNDFILELIB))
HOSTTOOLDIRS = $(addprefix $(TOOL_ROOT)/,$(HOST_TOOLS))

# NOTE: the order of these directories determines build order.
# In this case, we want libs first, tests second, demos last
SUBDIRS     = $(addprefix $(LIB_ROOT)/,$(LIBS)) 	\
              $(addprefix $(TEST_ROOT)/,$(TESTS))	\
              $(addprefix $(DEMO_ROOT)/,$(DEMOS))	\
              $(addprefix $(LIB_ROOT)/,$(HOST_LIBS))	\
              $(addprefix $(DEMO_ROOT)/,$(HOST_DEMOS))	\
              $(addprefix $(LIB_ROOT)/,$(SOUNDFILELIB))	\
              $(addprefix $(LIB_ROOT)/,$(ADPCMLIB))


# The following changes the Makefile include path to include a
# definition of which architecture we're building for
ARCH_PATH	= $(MAKE_ROOT)/$(ARCH_TARGET)

all: setup headers subdirectories firmware

.PHONY: firmware
firmware:
	make -C firmware install

setup:
	@if [ ! -d $(INSTALL_ROOT) ] ; then \
		echo "Creating $(INSTALL_ROOT)" ; \
		rm -rf $(INSTALL_ROOT) ; \
		mkdir $(BASEINSTALL_ROOT) ; \
		mkdir $(BASEINSTALL_ROOT)/$(ARCH_TARGET) ; \
		mkdir $(INSTALL_ROOT)/lib ; \
		mkdir $(INSTALL_ROOT)/bin ; \
	else \
		echo "Install directory ready" ;\
	fi

# the following lines build up the flags for the recursive make commands
BUILDFLAGS	= X86=$(X86) PLATFORM=$(PLATFORM) DBREL="$(DBREL)" BUILDTOOLS_ROOT=$(MAKE_ROOT)

ifdef NDEBUG
BUILDFLAGS	+= NDEBUG=TRUE
else
BUILDFLAGS	+= DEBUG=TRUE
endif

ifdef BUILD
BUILDFLAGS	+= build
endif

ifdef INSTALL
BUILDFLAGS	+= install
endif

.PHONY: alllibs libraries demos tests hostlibs hostdemos adpcmlib

headers:
	@for d in $(SUBDIRS); do \
		echo "------------------------------------------------------------------" ; \
		echo "Importing $$d headers" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d headers; \
		fi ;\
	done ;

subdirectories:
	@for d in $(SUBDIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Building $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $(BUILDFLAGS) ; \
		fi ; \
	done ;

alllibs: libraries hostlibs

libraries:
	@for d in $(LIBDIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Building $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $(BUILDFLAGS) ; \
		fi ; \
	done ;

libraries.%:
	@echo "------------------------------------------------------------------" ; \
	echo "Building libraries/$*" ; \
	$(MAKE) -C $(LIB_ROOT)/$* $(BUILDFLAGS)

hostlibs:
	@for d in $(HOSTLIBDIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Building $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $(BUILDFLAGS) ; \
		fi ; \
	done ;

hostlibs.%:
	@echo "------------------------------------------------------------------" ; \
	echo "Building libraries/$*" ; \
	$(MAKE) -C $(LIB_ROOT)/$* $(BUILDFLAGS)

demos:
	@for d in $(DEMODIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Building $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $(BUILDFLAGS) ; \
		fi ; \
	done ;

demos.%:
	@echo "------------------------------------------------------------------" ; \
	echo "Building demos/$*" ; \
	$(MAKE) -C $(DEMO_ROOT)/$* $(BUILDFLAGS)

hostdemos:
	@for d in $(HOSTDEMODIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Building $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $(BUILDFLAGS) ; \
		fi ; \
	done ;

hostdemos.%:
	@echo "------------------------------------------------------------------" ; \
	echo "Building demos/$*" ; \
	$(MAKE) -C $(DEMO_ROOT)/$* $(BUILDFLAGS)

hosttools:
	@for d in $(HOSTTOOLDIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Building $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $(BUILDFLAGS) ; \
		fi ; \
	done ;

hosttools.%:
	@echo "------------------------------------------------------------------" ; \
	echo "Building demos/$*" ; \
	$(MAKE) -C $(TOOL_ROOT)/$* $(BUILDFLAGS)

tests:
	@for d in $(TESTDIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Building $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $(BUILDFLAGS) ; \
		fi ; \
	done ;

tests.%:
	@echo "------------------------------------------------------------------" ; \
	echo "Building tests/$*" ; \
	$(MAKE) -C $(TEST_ROOT)/$* $(BUILDFLAGS)

adpcmlib:
	@for d in $(ADPCMLIBDIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Building $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $(BUILDFLAGS) ; \
		fi ; \
	done ;

update:
	@if [ ! -f $(SS) ] ; then \
		echo "SourceSafe is not installed!  Please go to \\ntd-pdc\Vss\Dolphin Readme" ; \
		exit 1; \
	fi
	@echo "Changing Sourcesafe project to $(SSROOT), performing full GET."
	@$(SS) cp $(SSROOT) -I-Y -y$(SSUSER)
	@cd $(ROOT)
	@$(SS) get \*.\* -R -I-Y -y$(SSUSER) -r -gf

clean clobber superclobber:
	@echo ">>> Cleaning $(ARCH_TARGET) target" ;
	@for d in $(SUBDIRS) ; do \
		echo "------------------------------------------------------------------" ; \
		echo "Cleaning $$d" ; \
		if [ ! -d $$d ] ; then \
			echo "Module $$d is missing...skipping" ; \
		else \
			$(MAKE) -C $$d $@ $(BUILDFLAGS) ; \
		fi ; \
	done ;

