# Makefile fragment for assembler modules, previously built using AAsm but now using objasm
# Such modules are characterised by the use of position-independent code
# and multiple source files (if applicable) linked at the assembly stage
# (using LNK and GET directives) rather than at the link stage.

INCLUDED_AASMMODULE = YES

#
# This makefile provides the following phony targets:
#
#    all     rom           install_rom   resources
#    export  export_libs   export_hdrs   standalone         install
#    clean   debug         gpa_debug
#
#
# 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})
# RESFSDIR        (opt) (actual directory to export resources to - otherwise ${RESDIR}/${TARGET})
# ROM_MODULE      (opt) (output filename - otherwise ${TARGET})
# ROM_OBJECT      (opt) (object file for romming - otherwise ${TARGET}.o)
# ROM_LIBS        (opt) (libraries to link again ROM_OBJECT to generate ROM_MODULE)
# ROM_DEPEND      (opt) (any extra dependency to assert on ROM_MODULE)
# DBG_MODULE      (opt) (output filename for debug - otherwise ${TARGET}D
# DBG_OBJECT      (opt) (object file for debug - otherwise ${TARGET}D.o
# DBG_LIBS        (opt) (libraries to link against DBG_OBJECT to generate DBG_MODULE - otherwise ${ROM_LIBS})
# SA_MODULE       (opt) (output filename for softload - otherwise ${TARGET}SA
# SA_OBJECT       (opt) (object file for softload - otherwise ${TARGET}SA.o
# SA_LIBS         (opt) (libraries to link against SA_OBJECT to generate SA_MODULE - otherwise ${ROM_LIBS})
# SA_DEPEND       (opt) (any extra dependency to assert on SA_MODULE and DBG_MODULE)
# DIRS            (opt) (magic target to ensure object directories are present - otherwise _dirs)
# HEADER[1-3]     (opt) (leafname of file in hdr to copy to ${EXPORTDIR} on export_hdrs)
# ASMCHEADER[1-3] (opt) (C-from-assembly auto-generated header files to export, no Hdr. prefix - otherwise none)
# CHEADER[1-3]    (opt) (C header files to export - otherwise none)
# EXPORTS         (opt) (list of C and C-from-assembly headers to export)
# C_EXP_HDR       (opt) (C header target directory - otherwise ${CEXPORTDIR}/Interface)
# EXP_HDR         (opt) (directory for exported assembler interface headers)
# CUSTOMEXP       (opt) (set to "custom" to override the export rules)
# CUSTOMRES       (opt) (set to "custom" to override the resources rules, or "no" for no resources)
# 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)
# SOURCES_TO_LINK (opt) (files which need be linked to by the link farm, in addition to contents of hdr and s directories)
#
#
# It relies on the following from the build system:
#
#
# INSTDIR          (installation directory for ROM_MODULE or disc install)
# INSTALLDIR       (if INSTDIR was not specified by the build system)
# RESDIR           (installation directory for resources - cf. RESFSDIR)
# EXPORTDIR        (export directory for assembler interface headers)
# CEXPORTDIR       (export directory for C interface headers)
# HDRDIR           (top-level export directory for assembler headers)
# CMDHELP          (optional, set to None to exclude syntax errors from Messages)
# PHASE            (export phase discriminator)
#
#
# It relies on the following generic tool macros from the Tools makefile
#
# CP + CPFLAGS     (copy, cp etc.)
# AS + ASFLAGS     (assembler)
# MKDIR            (cdir/mkdir -p)
# RM + WIPE        (deletions)
# ECHO
# LD               (linker)
#
#

TARGET     ?= ${COMPONENT}
INSTDIR    ?= ${INSTALLDIR}
ROM_MODULE ?= ${TARGET}
ROM_SOURCE ?= ${TARGET}.s
ROM_OBJECT ?= ${TARGET}.o
DIRS       ?= _dirs
EXP_HDR    ?= ${EXPORTDIR}
RESFSDIR   ?= ${RESDIR}/${TARGET}
RESAPPDIR  ?= ${RESDIR}/../Apps/${APP}
C_EXP_HDR  ?= ${CEXPORTDIR}/Interface

SA_MODULE  ?= ${TARGET}SA
SA_OBJECT  ?= ${TARGET}SA.o
MERGEDMDIR ?= _Messages_
MERGEDMSGS ?= ${MERGEDMDIR}/${TARGET}
ifeq (${CMDHELP},None)
SA_FLAGS   ?= -PD "standalone SETL {TRUE}" -PD "MergedMsgs SETS \"${MERGEDMSGS}\"" -PD "international_help SETL {FALSE}"
else
SA_FLAGS   ?= -PD "standalone SETL {TRUE}" -PD "MergedMsgs SETS \"${MERGEDMSGS}\""
endif
SA_LIBS    ?= ${ROM_LIBS}

DBG_MODULE ?= ${TARGET}D
DBG_OBJECT ?= ${TARGET}D.o
DBG_LIBS   ?= ${ROM_LIBS}

#GPA_FILE   ?= ${TARGET}.gpa
#GPA_AIF    ?= ${TARGET}.aif

TOKENS     ?= ${HDRDIR}/Global/Tokens

TOKENSOURCE ?= TokHelpSrc.s

SOURCES_TO_SYMLINK += $(wildcard h/*) $(wildcard hdr/*) $(wildcard s/*) Resources VersionNum VersionASM

ifneq (objs,$(notdir ${CURDIR}))

# Makefile invoked from same directory
# Create link farm, then execute the makefile from within it

ifeq (clean,${MAKECMDGOALS})
# With a double-colon rule which can have additional actions assigned from the
# master makefile, we'd normally need the master makefile to include the
# ${CURDIR} check to ensure that it's performed on the same invocation as us.
# However, there's no real benefit to performing clean from within the objs
# directory, and it adds an ordering problem between the different double-colon
# rules (the one that deletes the objs directory has to be last otherwise the
# cwd is invalid for the others) so to simplify things, we only ever do cleans
# from the same directory as the Makefile.
clean::
	@echo Cleaning...
	@rm -rf objs
	@echo ${COMPONENT}: cleaned
else
all debug export export_hdrs export_libs gpa_debug install install_rom links resources rom standalone: ${SYMLINK_DEPEND}
	$(foreach linksource,${SOURCES_TO_SYMLINK}, \
		$(shell \
			linkdest=`echo ${linksource} | sed -e 's,\([^/]*\)/\([^/]*\)$$,\2.\1,' -e 's,^,objs/,'`; \
			linkdestdir=`echo $$linkdest | sed -e 's,/[^/]*$$,,'`; \
			linkbackpath=`echo $$linkdestdir | sed -e 's,[^/]*,..,g'`; \
			[ -d ${linksource} ] || [ -L $$linkdest ] || mkdir -p $$linkdestdir; \
			[ -d ${linksource} ] || [ -L $$linkdest ] || ln -s $$linkbackpath/${linksource} $$linkdest; \
		 ) \
	)
	@[ -L objs/Resources ] || ln -s ../Resources objs/Resources
	@mkdir -p objs
ifneq (links,${MAKECMDGOALS})
	@${MAKE} -C objs -f ../$(firstword ${MAKEFILE_LIST}) ${MAKECMDGOALS}
endif
endif

else

# Makefile invoked from objs subdirectory

ifeq ("${INCLUDED_STDTOOLS}","")
include StdTools
endif

all: rom debug standalone

${DIRS} ::
	${TOUCH} $@

#
# ROM build rules
#
rom${CUSTOMROM}: ${ROM_MODULE}
	@${ECHO} ${COMPONENT}: rom module built

install_rom${CUSTOMROM}: ${ROM_MODULE}
	${CP} ${ROM_MODULE} ${INSTDIR}/${TARGET} ${CPFLAGS}
	#${CP} ${ROM_MODULE}_gpa ${INSTDIR}/${TARGET}_gpa ${CPFLAGS}
	@${ECHO} ${COMPONENT}: rom module installed

${ROM_MODULE}: ${ROM_DEPEND} ${DIRS} ${ROM_LIBS} ${ROM_OBJECT}
	${LD} -rmf -o $@ ${ROM_OBJECT} ${ROM_LIBS}
	#${LD} -aif -bin -d -Entry 0 -o ${ROM_MODULE}_aif ${ROM_OBJECT} ${ROM_LIBS}
	#${TOGPA} -s ${ROM_MODULE}_aif ${ROM_MODULE}_gpa

${ROM_OBJECT}: ${DIRS} ${TOKHELPSRC} ${ROM_SOURCE}
	${AS} ${ASFLAGS} -depend $(subst .o,.d,$@) -g -o $@ ${ROM_SOURCE}

#
# Standalone builds
#
standalone${CUSTOMSA}: ${SA_MODULE}
	@${ECHO} ${COMPONENT}: standalone module built

${SA_MODULE}: ${SA_DEPEND} ${DIRS} ${SA_LIBS} ${SA_OBJECT}
	${LD} -rmf -o $@ ${SA_OBJECT} ${SA_LIBS}
	${MODSQZ} -f ${MODSQZFLAGS} $@

${SA_OBJECT}: ${DIRS} ${TOKHELPSRC} ${MERGEDMSGS} ${ROM_SOURCE}
	${AS} ${ASFLAGS} -depend $(subst .o,.d,$@) -o $@ ${ROM_SOURCE} ${SA_FLAGS}

${MERGEDMSGS}${PRIVATEMSGS}:
	${MKDIR} ${MERGEDMDIR}
	${TOUCH} ${MERGEDMDIR}.Messages
	${TOUCH} ${MERGEDMDIR}.CmdHelp
	${INSTRES} -I Resources.${USERIF}.${LOCALE},Resources.${USERIF}.UK,Resources.${LOCALE},Resources.UK,Resources ${MERGEDMDIR} [Messages] [CmdHelp]
	${CAT} ${MERGEDMDIR}.Messages ${MERGEDMDIR}.CmdHelp > $@
	${RM} ${MERGEDMDIR}.Messages ${MERGEDMDIR}.CmdHelp

install${CUSTOMSA}: ${SA_MODULE}
	${MKDIR} ${INSTDIR}
	${CP} ${SA_MODULE} ${INSTDIR}/${TARGET} ${CPFLAGS}
	@${ECHO} ${COMPONENT}: standalone module installed

debug${CUSTOMDBG}: ${DBG_MODULE}
	@${ECHO} ${COMPONENT}: standalone debug module built

${DBG_MODULE}: ${DBG_OBJECT} ${DBG_LIBS} ${DIRS} ${SA_DEPEND}
	${LD} -rmf -o $@ ${DBG_OBJECT} ${DBG_LIBS}

${DBG_OBJECT}: ${ROM_SOURCE} ${MERGEDMSGS} ${TOKHELPSRC} ${DIRS}
	${AS} ${ASFLAGS} ${ASDFLAGS} -g -pd "debugging SETL {TRUE}" -depend $(subst .o,.d,$@) -o $@ ${ROM_SOURCE} ${SA_FLAGS}

#gpa_debug${CUSTOMGPA}: ${GPA_FILE}
#	@${ECHO} ${COMPONENT}: GPA debug listing generated
#
#${GPA_FILE}: ${GPA_AIF}
#	${TOGPA} -s ${GPA_AIF} ${GPA_FILE}
#
#${GPA_AIF}: ${DBG_OBJECT}
#	${LD} -aif -bin -d -o ${GPA_AIF} ${DBG_OBJECT}

#
# Export phases
#

export_hdrs: ${EXPORTS}
	@if [ -e ${HEADER1}.hdr ]; then ${ECHO} ${CP} ${HEADER1}.hdr ${EXP_HDR}/${HEADER1} ${CPFLAGS}; fi
	@if [ -e ${HEADER1}.hdr ]; then ${CP} ${HEADER1}.hdr ${EXP_HDR}/${HEADER1} ${CPFLAGS}; fi
	@if [ -e ${HEADER2}.hdr ]; then ${ECHO} ${CP} ${HEADER2}.hdr ${EXP_HDR}/${HEADER2} ${CPFLAGS}; fi
	@if [ -e ${HEADER2}.hdr ]; then ${CP} ${HEADER2}.hdr ${EXP_HDR}/${HEADER2} ${CPFLAGS}; fi
	@if [ -e ${HEADER3}.hdr ]; then ${ECHO} ${CP} ${HEADER3}.hdr ${EXP_HDR}/${HEADER3} ${CPFLAGS}; fi
	@if [ -e ${HEADER3}.hdr ]; then ${CP} ${HEADER3}.hdr ${EXP_HDR}/${HEADER3} ${CPFLAGS}; fi
	@${ECHO} ${COMPONENT}: header export complete

export_libs:
	@${ECHO} ${COMPONENT}: library export complete

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

export_:: export_hdrs export_libs
	${NOP}

# Need these to stop complaints about the rules below
ASMCHEADER1 ?= _!_x_!_
ASMCHEADER2 ?= _!_x_!_
ASMCHEADER3 ?= _!_x_!_
CHEADER1    ?= _!_x_!_
CHEADER2    ?= _!_x_!_
CHEADER3    ?= _!_x_!_

${C_EXP_HDR}/${ASMCHEADER1} :: ${ASMCHEADER1}.hdr
	${HDR2H} ${ASMCHEADER1}.hdr $@

${C_EXP_HDR}/${ASMCHEADER2} :: ${ASMCHEADER2}.hdr
	${HDR2H} ${ASMCHEADER2}.hdr $@

${C_EXP_HDR}.${ASMCHEADER3} :: ${ASMCHEADER3}.hdr
	${HDR2H} ${ASMCHEADER3}.hdr $@

${C_EXP_HDR}.${CHEADER1}.h :: ${CHEADER1}.h
	${CP} ${CHEADER1}.h $@ ${CPFLAGS}

${C_EXP_HDR}.${CHEADER2}.h :: ${CHEADER2}.h
	${CP} ${CHEADER2}.h $@ ${CPFLAGS}

${C_EXP_HDR}.${CHEADER3}.h :: ${CHEADER3}.h
	${CP} ${CHEADER3}.h $@ ${CPFLAGS}

#
# Resources rules
#
resources${CUSTOMRES}:: ${RESOURCEEXTRA} resources-${CMDHELP} 
	@${ECHO} ${COMPONENT}: resources copied to Messages module

ifeq (${CUSTOMRES},no)
resources:
	@${ECHO} ${COMPONENT}: no resources to export
endif        

resources-None:
	${MKDIR} ${RESFSDIR}
	${MKDIR} ${MERGEDMDIR}
	${INSTRES} -I Resources.${USERIF}.${LOCALE},Resources.${USERIF}.UK,Resources.${LOCALE},Resources.UK,Resources ${RESFSDIR} Messages
	${RM} ${MERGEDMDIR}.Messages

resources-: ${MERGEDMSGS}
	${MKDIR} ${RESFSDIR}
	${CP} ${MERGEDMSGS} ${RESFSDIR}/Messages
        
resources_res::
	${INSTRES} -I Resources.${USERIF}.${LOCALE},Resources.${USERIF}.UK,Resources.${LOCALE},Resources.UK,Resources ${RESFSDIR} Res
        
resources_sprites::
	${INSTRES} -I Resources.${USERIF}.${LOCALE},Resources.${USERIF}.UK,Resources.${LOCALE},Resources.UK,Resources ${RESFSDIR} Sprites [Sprites11] [Sprites22]

resources_templates::
	${INSTRES} -I Resources.${USERIF}.${LOCALE},Resources.${USERIF}.UK,Resources.${LOCALE},Resources.UK,Resources ${RESFSDIR} Templates

#
# Build the help tokens
#
${TOKENSOURCE}: ${TOKENS} ${HELPSRC}
	${TOKENISE} ${TOKENS} ${HELPSRC} $@

include $(wildcard *.d)

endif

# EOF