###############################################################################
# Makefile for ECommerce Library
#
# 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.
###############################################################################

default: all

GBA_SOURCE = ../../../../../

SDK_NAME = ecdk_current
include $(GBA_SOURCE)/sw/devroot/include/make/SDKdefs
include $(REVOLUTION_EXT_ROOT)/build/buildtools/commondefs

# don't want to include following file, but it defines HFILES and HFILES_I
# which are needed locally
include $(GBA_SOURCE)/sw/common/lib/ec/Make.common

###############################################################################
# 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 install_src headers build install

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

MODULENAME  = ecdk
LIB         = TRUE

SRCDIR      = $(GBA_SOURCE)/sw/common/lib/ec
# ends up being BUILD_ROOT, the core of MODULE_ROOT
REX_BUILDDIR = ../..

# various directories that hold the headers that are currently scattered
# H_DIR: private headers that are needed on top of RVL_SDK
# SHR_DIR: shared headers and source files are separate in the GBA tree, as they
#          are also used by the P2P_Services library
H_DIR = extra_headers
SHR_DIR = ../share

INCLUDES   += \
	      -Isrc -Iinclude -I$(H_DIR)\
	      -I$(H_DIR)/private -I$(H_DIR)/secure \
	      -I$(GBA_SOURCE)/sw/common/lib/ec\
	      -I$(GBA_SOURCE)/sw/common/lib/ec/include\
	      -I$(SHR_DIR)/include \
	      -I$(GBA_SOURCE)/sw/common/lib/p2p/share 

ifndef NHTTP_ROOT
    NHTTP_ROOT = $(REVOLUTION_EXT_ROOT)
endif

ifneq ($(NHTTP_ROOT),$(REVOLUTION_EXT_ROOT))
    INCLUDES += -I$(NHTTP_ROOT)/include/revolution
endif

INCLUDES   := -i- -ir $(MWDIR)/PowerPC_EABI_Support/MSL/MSL_C++ $(INCLUDES)

CCFLAGS += -opt space -DEC_NON_SHIM
CCFLAGS_WARNING = -W nopadding,nounused

ifdef EC_LOG_LEVEL
    CCFLAGS += -DEC_LOG_LEVEL=$(EC_LOG_LEVEL)
endif

ifdef EC_USE_EXCEPTIONS
    CCFLAGS += -DEC_USE_EXCEPTIONS=$(EC_USE_EXCEPTIONS)
endif

ifndef SDATA
    CCFLAGS += -sdata 0 -sdata2 0
endif


# always EC_SDK
CCFLAGS += -DEC_SDK=1 -DEC_CATALOG -DEC_NO_JS=1

# dunno what this is
ifdef EC_TEST
   CCFLAGS += -DEC_TEST
endif
    

###############################################################################
# Library building
# LIBNAME specifies the name of the library.  No suffix is required, as
# that will depend on whether this is a DEBUG build or not.
# The final name of the library will be $(LIBNAME)$(LIBSUFFIX)
#
# CLIBSRCS/CPPLIBSRCS specifies all C/C++ files that are built and linked into
# the library.
# ASMLIBSRCS are all assembly files that will be built and linked into the lib.
###############################################################################


###############################################################################
# C_FILES list files in common/lib/ec
# SHR_C_FILES list C files in bw/build/libraries/share, which need to be copied
#
# CPP_FILES list files in common/lib/ec, which need to be copied
# BW_CPP_FILES list files that are already in ./src
############################################################################### 

HFILES     += ecestypes.h

LIBNAME     = ec
C_FILES     = ec_md5c.c 
CPP_FILES   = ec_api.cpp  ec_asyncOp.cpp ec_misc.cpp \
              ec_getTitle.cpp  ec_purchaseTitle.cpp \
              ec_checkReg.cpp \
              ec_mem.cpp \
              ec_soap.cpp  ec_base64.cpp \
              ec_string.cpp  ec_md5.cpp \
              ec_content.cpp \
              ec_logbuf.cpp \
              ec_chkDevStat.cpp \
              ec_register.cpp \
              ec_csup.cpp

# EC_SDK is always true; following are EC_SDK specific
CPP_FILES  += ec_dk.cpp ec_connect.cpp ec_downloadContents.cpp \
              ec_deleteContents.cpp \
              ec_listTitleContents.cpp \
              ec_list.cpp ec_listTitles.cpp ec_listContentSets.cpp

# BW_Specific files
SHR_C_FILES   = shr_th_bw.c shr_time_bw.c
BW_CPP_FILES = \
              ec_file_bw.cpp \
              ec_shoplog_bw.cpp \
              ec_sysconfig_bw.cpp \
              ec_chkDownload_bw.cpp 

ASMLIBSRCS  =

ifdef TEST_HTTP
    CCFLAGS += -DEC_USE_TEST_HTTP
    CPP_FILES += ec_http_t.cpp  ec_http.cpp ssl_socket.cpp
else
    BW_CPP_FILES += ec_http_bw.cpp
endif  

ifdef EC_TEST
CPP_FILES  +=  ec_test.cpp
endif  

CPP_FILES += $(BW_CPP_FILES)

CPPLIBSRCS = $(CPP_FILES) 
CLIBSRCS   = $(C_FILES) $(SHR_C_FILES)
              
###############################################################################
# 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

setup:
	@if [ ! -d src ]; then\
		mkdir src;\
	fi
	@if [ ! -d include ]; then\
		mkdir include;\
	fi
	@mkdir -p $(DEP_ROOT)

headers:
	@mkdir -p $(REVOLUTION_EXT_ROOT)/include/revolution/ec
	@$(INSTALL) $(ECLIB_SRC_ROOT)/include/ec.h \
	    $(REVOLUTION_EXT_ROOT)/include/revolution/ec.h
	@$(INSTALL) $(ECLIB_SRC_ROOT)/include/ecestypes.h \
	    $(REVOLUTION_EXT_ROOT)/include/revolution/ec/ecestypes.h

# modulerules require all CPP files to be in ./src; need to move some over;
# also, the shared lib C files also should be moved over
ECLIB_SRC_ROOT=$(GBA_SOURCE)/sw/common/lib/ec
install_src:
	@for file in $(SHR_C_FILES); do \
	    $(INSTALL) $(SHR_DIR)/src/$$file src/; \
	done
	@for file in $(CPP_FILES); do \
	    $(INSTALL) $(ECLIB_SRC_ROOT)/$$file src/; \
	done

clean:
	@for file in $(SHR_C_FILES); do \
		rm -f src/$$file;\
	done
	@for file in $(CPP_FILES); do \
		rm -f src/$$file; \
	done
