#!!!!!!!!!!!!!!!!!!!!!!
# NOTE: DEPRECATED: If you are using this, please migrate to build/demos/common/demos_common.mk
# Not-yet-migrated: sw/bw/build/tests/voip/makefileNew.mk
#!!!!!!!!!!!!!!!!!!!!!!


###############################################################################
# Common makefile for BCC RVL demos.
# Currently configured for RevoEx + P2P.
###############################################################################
# Based on the template makefile with the copyright statement below:
###############################################################################
# Template makefile for new modules
#
# Copyright (C) 2005-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.
###############################################################################
###############################################################################

# ensures that 'make' does 'make all' instead of first target
default: all

REVOLUTION_EXT_INSTALL_ROOT := $(REVOLUTION_P2P_ROOT)

# This path contains backslashes, so mwcceppc needs it to be enclosed in quotes.
P2P_INC_ROOT = "$(REVOLUTION_EXT_INSTALL_ROOT)\include"

REX_TITLEID ?= 34
REX_TITLEFW ?= noFW

TICKET ?= $(REVOLUTION_EXT_ROOT)/X86/bin/rvl.no.$(REX_TITLEID).bin

VERSION_NUM ?= 0

WAD_OPTION  = -I "$(VERSION_NUM)" -gc "0x$(TITLE_ID)" -m "$(BANNER)" -t "$(TICKET)"

###############################################################################
# commondefs must be included near the top so that all common variables
# will be defined before their use.
###############################################################################

include $(REVOLUTION_EXT_ROOT)/build/buildtools/commondefs

###############################################################################
# The following defines force the commondef defines to be directory independent
###############################################################################

MODULE_ROOT = .
DEMO = TRUE
DEMO_ROOT = ..
SAMPLE =

LINCLUDES  = -Iinclude
LINCLUDES += $(addprefix -I,$(MODULE_PRIVATE_INCLUDES)) 

GINCLUDES += $(addprefix -I,$(MODULE_SYSTEM_INCLUDES))
GINCLUDES  += -I$(P2P_INC_ROOT)

###############################################################################
# Definitions associated with Licensing and Publishing
###############################################################################

HEX_TITLE_ID    = 00010001$(TITLE_ID)

SCRIPT_DIR         = $(GBA_SOURCE)/sw/tools/build/cygwin

PUBLISH            = $(SCRIPT_DIR)/publish.sh
APPROVE            = $(SCRIPT_DIR)/approve-title.sh


# PUBLISH_ROOT is the location of the wad & properties file you wish you use
# It's 'bin/RVL' by default, but you may pass in via command line a different
# directory
PUBLISH_ROOT ?= $(FULLBIN_ROOT)

include $(GBA_SOURCE)/sw/bw/etc/properties.template.mk

###############################################################################
# Binary building
# BINNAMES lists all binaries that will be linked.  Note that no suffix is
# required, as that will depend on whether this is a DEBUG build or not.
# The final name of the binaries will be $(BINNAME)$(BINSUFFIX)
#
# CSRCS lists all C files that should be built
# ASMSRCS lists all assembly files that should be built.
# The makefile determines which objects are linked into which binaries
# based on the dependencies you fill in at the bottom of this file
###############################################################################

TEST_BIN = $(MODULENAME)

BINNAMES += $(TEST_BIN)

CSRCS   += $(MODULE_C_SOURCES)
CPPSRCS += $(MODULE_CPP_SOURCES)
ASMSRCS += $(MODULE_ASM_SOURCES)

MODULE_OBJS = $(MODULE_C_SOURCES:.c=.o) $(MODULE_CPP_SOURCES:.cpp=.o)

CCFLAGS += -dialect c99

# The compiler message style will default to 'gcc', but it can be overridden.
# 'mpw' is the only style that appears to be reasonably parseable without 
# look-ahead to later lines of output
COMPILER_MSG_STYLE ?= gcc
CCFLAGS += -msgstyle $(COMPILER_MSG_STYLE)

CCFLAGS_WARNING = -W padding,nounused

###############################################################################
# All modules have "setup" and "build" as targets.  System libraries
# and demo programs also have an "install" target that copies the compiled
# binaries to the binary tree (/revolution/$(ARCH_TARGET)).
###############################################################################
all:    setup build

###############################################################################
# Nothing to do for headers; this is here to suppress the error message.
###############################################################################
headers:

###############################################################################
# Include LCF file
###############################################################################
ifdef EPPC
LCF_FILE =  $(INC_ROOT)/revolution/eppc.$(ARCH_TARGET).lcf
endif

###############################################################################
# modulerules contains the rules that will use the above variables
# and dependencies below to construct the library and binaries specified.
###############################################################################
include $(REVOLUTION_EXT_ROOT)/build/buildtools/modulerules

REX_DIR     ?= $(subst $(space),\$(space),$(subst \,/,$(REVOLUTION_EXT_ROOT)))
REX_INCDIR  := $(REX_DIR)/include
REX_LIBNAMES = ncd so nwc24 net
REX_LIBTYPE  = $(ARCH_TARGET)/lib
REX_LIBDIR   = $(REX_DIR)/$(REX_LIBTYPE)
REX_LIBFILES = $(addsuffix $(LIBSUFFIX),$(REX_LIBNAMES)) 
REX_LIBS     = $(addprefix $(REX_LIBDIR)/,$(REX_LIBFILES))
GINCLUDES   += -I$(REX_INCDIR)
CCFLAGS     += -DUSE_REVOEX
# Allow includes to be relative to the location of the referencing file
INCLUDES    += -gccinc

setup: 
	@if [ -z '$(REVOLUTION_P2P_ROOT)' ]; then\
		echo "You must establish environment variable REVOLUTION_P2P_ROOT before proceeding."; \
		exit 1; \
	fi
	@if [ ! -w '$(REVOLUTION_P2P_ROOT)' -o ! -d '$(REVOLUTION_P2P_ROOT)' ]; then\
		mkdir -p '$(REVOLUTION_P2P_ROOT)'; \
	fi
# $(SETUP_ROOTS) does not create directory $(DEP_ROOT), without which build will fail
	@mkdir -p $(DEP_ROOT)
# make the properties file from template
	@$(REMAKE) properties

publish: 
	@if [ ! -s $(PUBLISH_ROOT)/$(TEST_BIN)$(WADSUFFIX) ]; then\
		echo "$(PUBLISH_ROOT)/$(TEST_BIN)$(WADSUFFIX) not found"; \
		exit 1; \
	fi
	@if [ ! -s $(PUBLISH_ROOT)/$(PROPERTIES_FILE) ]; then\
		echo "$(PUBLISH_ROOT)/$(PROPERTIES_FILE) not found"; \
		exit 1; \
	fi
	$(PUBLISH) $(PUBLISH_ROOT)/$(TEST_BIN)$(WADSUFFIX) $(PUBLISH_ROOT)/$(PROPERTIES_FILE) $(PRODUCTNAME)
	$(APPROVE) $(HEX_TITLE_ID) $(PRODUCTNAME) $(PRODUCTNAME)

###############################################################################
# Dependencies for the binaries listed in BINNAMES should come here
# They are your typical makefile rule, with extra variables to ensure
# that the names and paths match up.
# $(FULLBIN_ROOT) is the location of the local bin directory
# $(BINSUFFIX) depends on whether this is a debug build or not
# $(LOCALLIB_ROOT) points at the local lib directory
# $(REVOLUTION_LIBS) includes all the Revolution libraries.
###############################################################################

USB_MIC_ROOT = ../../../external_libs/usbmic-0.85/

P2P_LIBS = $(REVOLUTION_P2P_ROOT)/RVL/lib/p2p$(LIBSUFFIX) \
           $(REVOLUTION_P2P_ROOT)/RVL/lib/p2p_services$(LIBSUFFIX) \
           $(REVOLUTION_P2P_ROOT)/RVL/lib/p2p_servicesmodules$(LIBSUFFIX) \
           $(USB_MIC_ROOT)/lib/$(ARCH_TARGET)/usbmic$(LIBSUFFIX) 

$(FULLBIN_ROOT)/$(TEST_BIN)$(BINSUFFIX): \
				$(MODULE_OBJS) \
				$(MODULE_ADDITIONAL_LIBS) \
				$(REVOLUTION_LIBS) \
				$(P2P_LIBS) \
				$(REX_LIBS)
