# Makefile fragment for C and C++ modules
#
# $Id$
#
# This makefile provides the following phony targets:
#
#    all 
#    export  export-hdrs  export-libs
#    resources  resources_common  resources-None
#    rom  rom_link
#    standalone  install
#    debug  gpa_debug
#    prepro
#    clean
#
# It is intended that this be used instead of the ROMCModule and RAMCModule
# makefiles, but they remain for the time being for compatibility reasons.
#
#
# This fragment uses the following macros set by the master makefile.
#
#
# COMPONENT          (the name of the component)
# TARGET       (opt) (the leafname of the module - otherwise ${COMPONENT})
# ROM_TARGET   (opt) (partially linked module leafname - otherwise ${TARGET} in aof)
# LNK_TARGET   (opt) (fixed-position module leafname - otherwise ${TARGET} in linked)
# SA_TARGET    (opt) (standalone module leafname - otherwise ${TARGET} in rm)
# DBG_TARGET   (opt) (debug module leafname - otherwise ${TARGET}D in rm)
# AIF_TARGET   (opt) (GPA-intermediate leafname - otherwise ${TARGET} in aif)
# GPA_TARGET   (opt) (GPA debug leafname - otherwise ${TARGET} in gpa)
# INSTDIR      (opt) (the target directory - otherwise <Install$Dir>)
# C_EXP_HDR    (opt) (C header target directory - otherwise <cexport$dir>.Interface.h)
# EXP_HDR      (opt) (assembly header target directory - otherwise <export$dir>
# DIRS         (opt) (stamp object for directory creation - otherwise o._dirs)
# HDRS         (opt) (C header files to export, no h. prefix - otherwise ${TARGET})
# ASMHDRS      (opt) (assembly header files to export, no Hdr. prefix - otherwise none)
# ASMCHDRS     (opt) (C-from-assembly auto-generated header files to export, no Hdr. prefix - otherwise none)
# MERGEDMDIR   (opt) (directory for generating exported message file - otherwise o._Messages_)
# MERGEDMSGS   (opt) (leafname of generated messages file - otherwise ${MERGEDMDIR}.${TARGET})
# RES_AREA     (opt) (area name to use for embedded messages in standalone build - otherwise Resources)
# RES_OBJ      (opt) (object file for embedded messages in standalone build, no o.prefix - otherwise ${TARGET}Msgs)
# RES_PATH     (opt) (path in ResourceFS to messages file - otherise Resources.${TARGET}.Messages)
# CMHGFILE     (opt) (name of CMHG source file, no cmhg. prefix - otherwise ${TARGET}Hdr)
# CMHGAUTOHDR  (opt) (header file, if any, from HDRS or ASMCHDRS, to which to append the SWI defines from CMHG)
# CMHGFILE_SWIPREFIX (opt) (name of SWI prefix used in CMHG file - otherwise ${TARGET})
# OBJS         (opt) (object files, no o. prefixes - otherwise ${TARGET} ${CMHGFILE})
# ROM_OBJS     (opt) (ROM build objects - otherwise ${OBJS})
# SA_OBJS      (opt) (standalone build objects - otherwise ${OBJS})
# DBG_OBJS     (opt) (debug build object files, no prefixes - otherwise ${OBJS})
# LIBS         (opt) (extra libraries; CLib is always used)
# ROM_LIBS     (opt) (libraries to link for ROM targets - otherwise ${LIBS})
# SA_LIBS      (opt) (libraries to link for standalone targets - otherwise ${LIBS})
# DBG_LIBS     (opt) (extra debug libraries in addition to SA_LIBS and DEBUGLIBS)
# ROM_DEPEND   (opt) (any extra dependency to assert on ROM_TARGET)
# SA_DEPEND    (opt) (any extra dependency to assert on SA_TARGET and DBG_TARGET)
# CUSTOMEXP    (opt) (set to "custom" to override the export rules)
# CUSTOMRES    (opt) (set to "custom" to override the resources rules)
# CUSTOMROM    (opt) (set to "custom" to override the rom rules)
# CUSTOMSA     (opt) (set to "custom" to override the standalone rules)
# CUSTOMDBG    (opt) (set to "custom" to override the debug rules)
# CUSTOMGPA    (opt) (set to "custom" to override the GPA rules)
#
#
# It relies on the following from the build system:
#
#
# PHASE            (export phase discriminator)
# CMDHELP          (whether star command help/syntax strings are included)
# RESFSDIR         (location to install files destined for ResourceFS)
# FORCEROMLINK     (set to force a relink of the ROM target, used in BBE)
# LINKDIR          (installation directory for LNK_TARGET)
# ADDRESS          (base address for LNK_TARGET)
#
#
# It relies on the following generic tool macros from the StdTools makefile
#
#
#�C + CFLAGS       (C compiler; CDFLAGS also used in debug builds; -g implicit)
# CP + CPFLAGS     (copy, cp etc.)
# WIPE + WFLAGS    (recursive delete)
# RM               (non-recursive delete)
# AS + ASFLAGS     (assembler)
# LD + LDFLAGS     (linker; LDDFLAGS also used in debug builds; -d implicit)
# LDRAMFLAGS       (more linker flags)
# LDROMFLAGS       (more linker flags)
# LDLINKFLAGS      (flags for the rom_link phase)
# MODSQZ + MODSQZFLAGS (module binary compressor)
# CHMOD
# MKDIR            (cdir/mkdir -p)
# ECHO
# TOUCH            (create/touch)
# TOKENCHECK
# RESGEN
# TOGPA
#
#
# It relies on the following from the StdRules makefile
#
#
# .c.o  .c++.o  .cpp.o  .s.o
#
#
# It relies on the following from the ModStdRule makefile
#
#
# .cmhg.h  .cmhg.o
#
#
# It relies on the following from the DbgRules makefile
#
#
# CDFLAGS  C++DFLAGS  ASDFLAGS  LDDFLAGS
# .c.od  .c++.od  .cpp.od  .s.od
#
#

INSTALLDIR    = <Install$Dir>
EXPDIR        = <export$dir>
CEXPDIR       = <cexport$dir>

TARGET       ?= ${COMPONENT}
ROM_TARGET   ?= ${TARGET}
LNK_TARGET   ?= ${TARGET}
SA_TARGET    ?= ${TARGET}
DBG_TARGET   ?= ${TARGET}D
AIF_TARGET   ?= ${TARGET}
GPA_TARGET   ?= ${TARGET}
INSTDIR      ?= ${INSTALLDIR}
C_EXP_HDR    ?= ${CEXPDIR}.Interface.h
EXP_HDR      ?= ${EXPDIR}
DIRS         ?= o._dirs
HDRS         ?= ${TARGET}
ASMHDRS      ?= 
ASMCHDRS     ?= 
MERGEDMDIR   ?= o._Messages_
MERGEDMSGS   ?= ${MERGEDMDIR}.${TARGET}
RES_AREA     ?= Resources
RES_OBJ      ?= ${TARGET}Msgs
RES_PATH     ?= Resources.${TARGET}.Messages
CMHGFILE     ?= ${TARGET}Hdr
CMHGFILE_SWIPREFIX ?= ${TARGET}
OBJS         ?= ${TARGET}
ROM_OBJS     ?= ${OBJS}
SA_OBJS      ?= ${OBJS}
DBG_OBJS     ?= ${OBJS}
ROM_OBJS     += ${CMHGFILE}
SA_OBJS      += ${CMHGFILE} ${RES_OBJ}
DBG_OBJS     += ${CMHGFILE} ${RES_OBJ}
ROM_LIBS     ?= ${LIBS}
SA_LIBS      ?= ${LIBS}
DBG_LIBS     += ${DEBUGLIBS}

CFLAGS       := -zM -zps1 ${CFLAGS}
OBJS_         = $(addprefix o.,${OBJS})
ROM_OBJS_     = $(addprefix o.,${ROM_OBJS})
SA_OBJS_      = $(addprefix o.,${SA_OBJS})
DBG_OBJS_     = $(addprefix od.,${DBG_OBJS})
PP_OBJS_      = $(addprefix i.,$(filter-out ${CMHGFILE} ${RES_OBJ},${DBG_OBJS}))
EXPORTING_HDRS     = $(addprefix exphdr.,${HDRS})
EXPORTING_ASMHDRS  = $(addprefix expasm.,${ASMHDRS})
EXPORTING_ASMCHDRS = $(addprefix expasmc.,${ASMCHDRS})


# General rules

all: aof.${ROM_TARGET} rm.${SA_TARGET} rm.${DBG_TARGET}
        @${ECHO} ${COMPONENT}: all built

${DIRS} ::
        ${MKDIR} aif
        ${MKDIR} aof
        ${MKDIR} i
        ${MKDIR} linked
        ${MKDIR} gpa
        ${MKDIR} o
        ${MKDIR} od
        ${MKDIR} rm
        ${TOUCH} $@

clean ::
        @IfThere aif    Then ${ECHO} ${WIPE} aif ${WFLAGS}
        @IfThere aif    Then ${WIPE} aif ${WFLAGS}
        @IfThere aof    Then ${ECHO} ${WIPE} aof ${WFLAGS}
        @IfThere aof    Then ${WIPE} aof ${WFLAGS}
        @IfThere i      Then ${ECHO} ${WIPE} i ${WFLAGS}
        @IfThere i      Then ${WIPE} i ${WFLAGS}
        @IfThere linked Then ${ECHO} ${WIPE} linked ${WFLAGS}
        @IfThere linked Then ${WIPE} linked ${WFLAGS}
        @IfThere gpa    Then ${ECHO} ${WIPE} gpa ${WFLAGS}
        @IfThere gpa    Then ${WIPE} gpa ${WFLAGS}
        @IfThere o      Then ${ECHO} ${WIPE} o ${WFLAGS}
        @IfThere o      Then ${WIPE} o ${WFLAGS}
        @IfThere od     Then ${ECHO} ${WIPE} od ${WFLAGS}
        @IfThere od     Then ${WIPE} od ${WFLAGS}
        @IfThere rm     Then ${ECHO} ${WIPE} rm ${WFLAGS}
        @IfThere rm     Then ${WIPE} rm ${WFLAGS}
        @IfThere h.${CMHGFILE} Then ${RM} h.${CMHGFILE}
        @${ECHO} ${COMPONENT}: cleaned


# Export rules

export${CUSTOMEXP}: export_${PHASE}
        @${NOP}

export_: export_libs export_hdrs
        @${NOP}

create_exp_hdr_dirs:
        ${MKDIR} ${C_EXP_HDR}
        ${MKDIR} ${EXP_HDR}

o._h_${CMHGAUTOHDR}: h.${CMHGFILE} ${DIRS}
        Do ${AWK} -- "/.ifndef ${CMHGFILE_SWIPREFIX}/,/endif/" h.${CMHGFILE} > o._h_${CMHGAUTOHDR}

ifneq ($(findstring ${CMHGAUTOHDR},${HDRS}),)
exphdr.${CMHGAUTOHDR}: h.${CMHGAUTOHDR} o._h_${CMHGAUTOHDR}
        FAppend ${C_EXP_HDR}.${CMHGAUTOHDR} h.${CMHGAUTOHDR} o._h_${CMHGAUTOHDR}
endif

ifneq ($(findstring ${CMHGAUTOHDR},${ASMCHDRS}),)
expasmc.${CMHGAUTOHDR}: hdr.${CMHGAUTOHDR} o._h_${CMHGAUTOHDR}
        ${HDR2H} hdr.${CMHGAUTOHDR} ${C_EXP_HDR}.${CMHGAUTOHDR}
        FAppend ${C_EXP_HDR}.${CMHGAUTOHDR} ${C_EXP_HDR}.${CMHGAUTOHDR} o._h_${CMHGAUTOHDR}
endif

.SUFFIXES: .exphdr .expasm .expasmc .h .hdr
.h.exphdr:;    @${ECHO} ${CP} $< ${C_EXP_HDR}.$(subst h.,,$<) ${CPFLAGS} ; ${CP} $< ${C_EXP_HDR}.$(subst h.,,$<) ${CPFLAGS} 
.hdr.expasm:;  @${ECHO} ${CP} $< ${EXP_HDR}.$(subst hdr.,,$<) ${CPFLAGS} ; ${CP} $< ${EXP_HDR}.$(subst hdr.,,$<) ${CPFLAGS} 
.hdr.expasmc:; @${ECHO} ${HDR2H} $< ${C_EXP_HDR}.$(subst hdr.,,$<)       ; ${HDR2H} $< ${C_EXP_HDR}.$(subst hdr.,,$<)

export_hdrs${CUSTOMEXP}: ${EXPORTING_ASMCHDRS} ${EXPORTING_ASMHDRS} ${EXPORTING_HDRS} ${DIRS} create_exp_hdr_dirs
        @${ECHO} ${COMPONENT}: header export complete

export_libs${CUSTOMEXP}:
        @${ECHO} ${COMPONENT}: no exported libraries


# Resources rules

resources${CUSTOMRES}: resources-${CMDHELP}
        @${ECHO} ${COMPONENT}: resources copied to Messages module

resources_common:
        ${MKDIR} ${RESFSDIR}
        ${TOKENCHECK} LocalRes:Messages
        ${CP} LocalRes:Messages ${RESFSDIR}.Messages ${CPFLAGS}

resources-None: resources_common
        ${CP} LocalRes:Messages ${RESFSDIR}.Messages ${CPFLAGS}

resources-: resources_common
        IfThere LocalRes:CmdHelp Then ${TOKENCHECK} LocalRes:CmdHelp
        IfThere LocalRes:CmdHelp Then FAppend ${RESFSDIR}.Messages LocalRes:Messages LocalRes:CmdHelp

${MERGEDMSGS}: LocalRes:Messages
        ${MKDIR} ${MERGEDMDIR}
        ${TOKENCHECK} LocalRes:Messages
        IfThere LocalRes:CmdHelp Then ${TOKENCHECK} LocalRes:CmdHelp
        IfThere LocalRes:CmdHelp Then FAppend $@ LocalRes:Messages LocalRes:CmdHelp Else ${CP} LocalRes:Messages $@ ${CPFLAGS}

ifneq (${RES_OBJ},)
o.${RES_OBJ}: ${MERGEDMSGS} ${DIRS}
	${RESGEN} ${RES_AREA} $@ ${MERGEDMSGS} ${RES_PATH}

od.${RES_OBJ}: o.${RES_OBJ} ${DIRS}
        ${CP} o.${RES_OBJ} od.${RES_OBJ} ${CPFLAGS}
endif


# ROM rules

rom${CUSTOMROM}: aof.${ROM_TARGET}
        @${ECHO} ${COMPONENT}: rom module built

rom_link${CUSTOMROM}: aof.${ROM_TARGET} ${ABSSYM} ${FORCEROMLINK}
        ${LD} ${LDFLAGS} ${LDLINKFLAGS} -o linked.${LNK_TARGET} -rmf -base ${ADDRESS} aof.${ROM_TARGET} ${ABSSYM}
        ${CP} linked.${LNK_TARGET} ${LINKDIR}.${TARGET} ${CPFLAGS}
        @${ECHO} ${COMPONENT}: rom_link complete

aof.${ROM_TARGET}: ${ROM_OBJS_} ${ROM_LIBS} ${ROMCSTUBS} ${DIRS} ${ROM_DEPEND}
        ${LD} -o $@ -aof ${ROM_OBJS_} ${ROM_LIBS} ${ROMCSTUBS}

linked.${LNK_TARGET}: aof.${ROM_TARGET} ${ABSSYM} ${FORCEROMLINK}
        ${LD} ${LDFLAGS} ${LDLINKFLAGS} -o $@ -rmf -base ${ADDRESS} aof.${ROM_TARGET} ${ABSSYM}


# Standalone rules

install${CUSTOMSA}: rm.${SA_TARGET}
        ${MKDIR} ${INSTDIR}
        ${CP} rm.${SA_TARGET} ${INSTDIR}.${TARGET} ${CPFLAGS}
        @${ECHO} ${COMPONENT}: ram module installed

standalone${CUSTOMSA}: rm.${SA_TARGET}
        @${ECHO} ${COMPONENT}: ram module built

debug${CUSTOMDBG}: rm.${DBG_TARGET}
        @${ECHO} ${COMPONENT}: debug module built

gpa_debug${CUSTOMGPA}: gpa.${GPA_TARGET}
        @${ECHO} ${COMPONENT}: GPA debug built

prepro: ${PP_OBJS_} ${DIRS}
        @${ECHO} ${COMPONENT}: sources preprocessed

rm.${SA_TARGET}: ${SA_OBJS_} ${SA_LIBS} ${CLIB} ${DIRS} ${SA_DEPEND}
        ${LD} ${LDFLAGS} ${LDRAMFLAGS} -o $@ -rmf ${SA_OBJS_} ${SA_LIBS} ${CLIB}
        ${MODSQZ} ${MODSQZFLAGS} $@
        ${CHMOD} -R a+rx rm

rm.${DBG_TARGET}: ${DBG_OBJS_} ${DBG_LIBS} ${SA_LIBS} ${CLIB} ${DIRS} ${SA_DEPEND}
        ${LD} ${LDFLAGS} ${LDRAMFLAGS} -o $@ -rmf ${DBG_OBJS_} ${DBG_LIBS} ${SA_LIBS} ${CLIB}
        ${CHMOD} -R a+rx rm

gpa.${GPA_TARGET}: aif.${AIF_TARGET}
        ${TOGPA} -s aif.${AIF_TARGET} $@

aif.${AIF_TARGET}: rm.${DBG_TARGET}
        ${LD} -aif -bin -d -o aif.${AIF_TARGET} ${DBG_OBJS_} ${DBG_LIBS} ${SA_LIBS} ${CLIB}

# EOF