#! make -f
#----------------------------------------------------------------------------
#  Project:  NitroSDK - buildtools - compiler dependant settings for CodeWarrior
#  File:     modulerules.cctype.CW
#
#  Copyright 2003,2004 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.
#
# $Log: modulerules.cctype.CW,v $
# Revision 1.45  2004/11/05 06:42:09  yasu
# Add version info for CW NITRO V1.0
#
# Revision 1.44  2004/10/03 02:04:03  yasu
# Support component file list for compstatic tool
#
# Revision 1.43  2004/09/17 03:35:34  yasu
# Invalidate NITRO_COMPRESS for ARM7 build
#
# Revision 1.42  2004/09/09 11:41:32  yasu
# Support NITRO_DIGEST
#
# Revision 1.41  2004/09/08 00:06:26  yasu
# Set NITRO_STDOUT to '> /dev/null' if -s option defined
#
# Revision 1.40  2004/09/06 05:34:38  yasu
# Add -a option on compstatic
#
# Revision 1.39  2004/09/06 01:08:41  yasu
# Add -c option on compstatic
#
# Revision 1.38  2004/09/04 12:37:52  yasu
# Add OverlayTable for compstatic tool
#
# Revision 1.37	 2004/09/01 07:30:03  yasu
# Support NITRO_COMPRESS
#
# Revision 1.36  2004/08/10 14:18:48  yasu
# Add dependancies for makelcf
#
# Revision 1.35  2004/08/10 00:13:46  yasu
# Support CodeWarrior 0.6.1
#
# Revision 1.34  2004/07/26 08:11:08  yasu
# Rename ADDRESS_ARM7 -> ADDRESS_DTCM
#
# Revision 1.33  2004/07/26 04:59:02  yasu
# Add ADDRESS_ARM7 on makelcf
#
# Revision 1.32  2004/07/18 12:20:05  yasu
# Change ["] -> ['] of MAKELCF_DEFS for Mingw
#
# Revision 1.31  2004/07/18 06:10:46  yasu
# show makelcf command line on making *.autogen.lcf
#
# Revision 1.30  2004/07/13 12:34:19  yasu
# Check libcxx workaround on invoking linker
#
# Revision 1.29  2004/07/10 04:55:30  yasu
# support USE_MY_RULE_A, USE_MY_RULE_NEF
#
# Revision 1.28  2004/07/10 04:20:23  yasu
# Use response file to support overlay group
#
# Revision 1.27  2004/06/28 10:52:11  yasu
# Delete group setting
#
# Revision 1.26  2004/05/01 06:10:33  yada
# add build number rule for CW0.5
#
# Revision 1.25  2004/04/21 11:22:06  yasu
# Support codecheck as workaround for blxeq/ne problem
#
# Revision 1.24  2004/04/20 03:49:38  yasu
# add LDEPENDS_BIN/NEF for dependancies
#
# Revision 1.23  2004/04/14 10:14:53  yasu
# delete LIBRARIES_STATIC
#
# Revision 1.22  2004/04/03 08:43:35  yasu
# with MINGW, support detect CW versions
#
# Revision 1.21  2004/04/03 08:01:43  yasu
# auto-detect CW verinfo and switch strb parameters
#
# Revision 1.20  2004/04/01 12:54:38  yasu
# support LIBRARIES_STATIC for makerom
#
# Revision 1.19  2004/03/23 09:28:57  yasu
# add $(MAKEFILE) for dependancy list of %.nef
#
# Revision 1.18  2004/03/18 02:29:44  yasu
# change condition to switch rom packing method (makerom or elftobin)
#
# Revision 1.17  2004/02/23 05:01:38  yasu
# axf->nef
#
# Revision 1.16  2004/02/17 09:22:48  yasu
# support dependancies with MinGW
#
# Revision 1.15  2004/02/16 01:24:24  yasu
# support file dependancies
#
# Revision 1.14  2004/02/05 07:09:01  yasu
# change SDK prefix iris -> nitro
#
# Revision 1.13  2004/01/15 10:49:31  yasu
# support overlay
#
# Revision 1.12  2003/12/10 05:12:48  yasu
# Changed to use xxxx/src.c format for source files specification.
# Added Makefile.srcs
# Revised so that when IS_NITRO_DIR is defined, the IS Debugger library is linke#d.
#
# Revision 1.11  2003/12/02 04:53:06  yasu
# Revised header notation
#
# Revision 1.10  2003/12/02 03:45:11  yasu
# Revisions made for IS-NITRO-EMULATOR library
#
# Revision 1.9  2003/12/01 11:30:55  yasu
# Changed ROM create tool: objcopy -> elftobin
#
# Revision 1.8  2003/11/28 02:01:12  yasu
# Swapped CW for NITRO standard libraries
#
# Revision 1.7  2003/11/17 01:30:43  yasu
# Separated C++ and C definitions
#
# Revision 1.6  2003/11/11 03:06:31  yasu
# Made so that base.a position specification is done with lcf file. Therefore di# sbled previous revision. 
#
# Revision 1.5  2003/11/11 02:12:16  yasu
# Revision: Made so that link base.a before OBJS
#
# Revision 1.4  2003/11/06 10:38:39  yasu
# Revision: Library search path setting mistake
#
# Revision 1.3  2003/11/05 09:14:17  yasu
# Added library generation rules
#
# Revision 1.2  2003/11/05 07:12:36  yasu
# Provisional completion of compiler dependent rules.
#
# Revision 1.1  2003/10/29 06:47:28  yasu
# Support "make clean/clobber" only
#
# $NoKeywords: $
#----------------------------------------------------------------------------

AINCLUDE_DIRS		= $(LINCLUDES)	   $(GINCLUDES)
ALIBRARY_DIRS		= $(LLIBRARY_DIRS) $(GLIBRARY_DIRS)
ALIBRARIES		= $(LLIBRARIES)	   $(GLIBRARIES)

INCLUDE_DIRS		= $(subst \ -I,\ ,$(addprefix -I,$(AINCLUDE_DIRS)))
LIBRARY_DIRS		= $(subst \ -L,\ ,$(addprefix -L,$(ALIBRARY_DIRS)))
LIBRARIES		= $(subst \ -l,\ ,$(addprefix -l,$(ALIBRARIES)))

INCLUDES		= -gccinc $(INCLUDE_DIRS) -cwd proj

ifdef	MSYSTEM		# for MinGW32
QUOTE			='
else
QUOTE			="
endif

#----------------------------------------------------------------------------

.PRECIOUS:	$(OBJDIR)/%.o $(BINDIR)/%.nef $(LCFILE)

ifdef	NO_DEPENDFILES
#
#  Don't generate dependency files
#

# .c
$(OBJS_CC):%.o:
	$(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@

# .cpp
$(OBJS_CXX):%.o:
	$(CC) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

# .s
$(OBJS_AS):%.o:
	$(AS) $(ASFLAGS) $(INCLUDES) -c $< -o $@

else
#
# Generate dependency files
#
define MOVE_DEPEND
	@$(SED) -e $(QUOTE)/ARM_EABI_Support/d;s/\\\\\\([^ ]\\)/\\/\\1/g;$(QUOTE) $(*F).d > $(DEPENDDIR)/$(*F).d
	@$(RM) $(*F).d
endef

# .c
$(OBJS_CC):%.o:
	$(CC) $(CCFLAGS) $(INCLUDES) -c $< -MD -o $@
	$(MOVE_DEPEND)

# .cpp
$(OBJS_CXX):%.o:
	$(CC) $(CXXFLAGS) $(INCLUDES) -c $< -MD -o $@
	$(MOVE_DEPEND)

# .s
$(OBJS_AS):%.o:
	$(AS) $(ASFLAGS) $(INCLUDES) -c $< -MD -o $@
	$(MOVE_DEPEND)

endif

# .lcf .par
ifdef	LCFILE_SPEC

ADDRESS_DTCM	?= $(DEFAULT_ADDRESS_DTCM)

# exporting
MAKELCF_DEFS	= -DNITROSDK_ROOT='$(ROOT)'			\
		  -DTARGET_NAME='$(TARGET_NAME)'		\
		  -DOBJS_STATIC='$(OBJS_STATIC)'		\
		  -DOBJS_AUTOLOAD='$(OBJS_AUTOLOAD)'		\
		  -DOBJS_OVERLAY='$(OBJS_OVERLAY)'		\
		  -DGLIBS='$(GLIBRARIES)'			\
		  -DLLIBS='$(LLIBRARIES)'			\
		  -DCW_LIBS='$(CW_LIBS)'			\
		  -DOBJDIR='$(OBJDIR)'				\
		  -DLIBDIR='$(LIBDIR)'				\
		  -DBINDIR='$(BINDIR)'				\
		  -DPROC='$(CODEGEN_PROC)'			\
		  -DPLATFORM='$(NITRO_PLATFORM)'		\
		  -DCODEGEN='$(CODEGEN_ARCH)'			\
		  -DBUILD='$(NITRO_BUILD_DIR)'			\
		  -DADDRESS_DTCM='$(ADDRESS_DTCM)'

MAKELCF_OPTS	= $(MAKELCF_FLAGS) $(MAKELCF_DEFS)


$(LCFILE_AUTOGEN): $(LCFILE_SPEC) $(LCFILE_TEMPLATE) $(MAKEFILE) $(NITRO_BUILDTOOLSDIR)/commondefs
	$(MAKELCF) $(MAKELCF_OPTS)  $< $(LCFILE_TEMPLATE) $@

$(LDRES_AUTOGEN):  $(LCFILE_SPEC) $(LDRES_TEMPLATE)  $(MAKEFILE) $(NITRO_BUILDTOOLSDIR)/commondefs
	@$(MAKELCF) $(MAKELCF_OPTS)  $< $(LDRES_TEMPLATE)  $@

endif

# compress/digest
ifeq	($(CODEGEN_PROC),ARM9)
ifdef	NITRO_COMPRESS
COMPSTATIC_CMDS		= -9 -c
#COMPSTATIC_CMDS	= -7 -c
endif
ifdef	NITRO_DIGEST
COMPSTATIC_CMDS		+= -a
endif
endif

ifdef	COMPSTATIC_CMDS
COMPSUFFIX		?= $(DEFAULT_COMPSUFFIX)
ifneq	($(COMPSUFFIX),)
COMPSTATIC_FLAGS	+= -e$(COMPSUFFIX)
endif
endif


# libcxx workaround
ifdef	SDK_CW_WA_LIBCXX
CW_LIBCXX	= $(CW_LIBCXX_DIR)/$(CW_LIBCXX_ARM)
$(CW_LIBCXX):	$(ROOT)/build/buildsetup/libcxx/Makefile
	$(MAKE) -f $<
endif


# .nef
ifndef	USE_MY_RULE_NEF
ifneq	($(NITRO_MAKEROM),)
$(BINDIR)/%.nef: $(OBJS) $(LCFILE) $(MAKEFILE) $(LDEPENDS_NEF) $(LDRES_FILE) $(CW_LIBCXX)
	$(LD) $(LDFLAGS) $(LIBRARY_DIRS) @$(LDRES_FILE) $(LCFILE) -o $@
ifdef	COMPSTATIC_CMDS
	$(COMPSTATIC) $(COMPSTATIC_CMDS) $(COMPSTATIC_FLAGS) -f $(BINDIR)/component.files $(NITRO_STDOUT)
endif
else
$(BINDIR)/%.nef: $(OBJS) $(LCFILE) $(MAKEFILE) $(LDEPENDS_NEF) $(CW_LIBCXX)
	$(LD) $(LDFLAGS) $(LIBRARY_DIRS) $(OBJS) $(LIBRARIES) $(LCFILE) -o $@
endif
endif


# .a
ifndef	USE_MY_RULE_A
$(LIBDIR)/%.a: $(OBJS)
	$(AR) $(ARFLAGS) $(OBJS) -o $@
endif


#----------------------------------------------------------------------------
# Get SDK_CW_BUILD_NUMBER
#
# IRIS	V0.2 Hotfix 1
#	SDK_CW_BUILD_NUMBER_CC=0038
#	SDK_CW_BUILD_NUMBER_LD=0050
#
# NITRO V0.3
#	SDK_CW_BUILD_NUMBER_CC=0038
#	SDK_CW_BUILD_NUMBER_LD=0056
#
# NITRO V0.4.1
#	SDK_CW_BUILD_NUMBER_CC=0043
#	SDK_CW_BUILD_NUMBER_LD=0057
#
# NITRO V0.5
#	SDK_CW_BUILD_NUMBER_CC=0046
#	SDK_CW_BUILD_NUMBER_LD=0058
#
# NITRO V0.5 + HotFix1
#	SDK_CW_BUILD_NUMBER_CC=0049
#	SDK_CW_BUILD_NUMBER_LD=0061
#
# NITRO V0.6
#	SDK_CW_BUILD_NUMBER_CC=0050
#	SDK_CW_BUILD_NUMBER_LD=0062
#
# NITRO V0.6.1
#	SDK_CW_BUILD_NUMBER_CC=0050
#	SDK_CW_BUILD_NUMBER_LD=0063
#
# NITRO V0.7
#	SDK_CW_BUILD_NUMBER_CC=0050
#	SDK_CW_BUILD_NUMBER_LD=0063
#
# NITRO V1.0
#	SDK_CW_BUILD_NUMBER_CC=0056
#	SDK_CW_BUILD_NUMBER_LD=0067
#

ifeq	($(MAKECMDGOALS),do-build)

.PHONY: clean-verinfo

$(SDK_VERINFO_CW_CC): $(CC)
	@$(CC) -version | \
	 $(SED) -e $(QUOTE)/^Version /!d;s/.*(build \([0-9]*\)).*/SDK_CW_BUILD_NUMBER_CC=\1/i;$(QUOTE) >$(SDK_VERINFO_CW_CC)
	@$(TOUCH) -r $(CC) $(SDK_VERINFO_CW_CC)

$(SDK_VERINFO_CW_LD): $(LD)
	@$(LD) -version | \
	 $(SED) -e $(QUOTE)/^Version /!d;s/.*(build \([0-9]*\)).*/SDK_CW_BUILD_NUMBER_LD=\1/i;$(QUOTE) >$(SDK_VERINFO_CW_LD)
	@$(TOUCH) -r $(LD) $(SDK_VERINFO_CW_LD)

endif

clean-verinfo:
	@$(RM) $(NITRO_BUILDTOOLSDIR)/verinfo.*

#----------------------------------------------------------------------------
# Check codegen bug around blxeq/blxne
#

.PHONY: codecheck

codecheck:
	@$(ECHO) --- checking codegen ---
	@$(NITRO_TOOLSDIR)/bin/blxchecker

#----- End of modulerules.cctype.CW -----
