# Makefile fragment for Basic applications

INCLUDED_BASICAPP = YES

#
# This makefile provides the following phony targets:
#
#    all  install  resources  rom  install_rom
#
#
# This fragment uses the following macros set by the master makefile.
#
#
# COMPONENT                 (the name of the component)
# TARGET              (opt) (the leafname of the application - otherwise ${COMPONENT})
# ROM_MODULE          (opt) (module filename - otherwise ${COMPONENT})
# INSTAPP             (opt) (the application target directory - otherwise ${INSTDIR}/!${COMPONENT})
# INSTDIR             (opt) (the target directory - otherwise ${INSTALLDIR}/${TARGET})
# RESFSDIR            (opt) (actual directory to export non-application-dir resources to - otherwise ${RESDIR}/${COMPONENT})
# RESFSAPPDIR         (opt) (where to register application-dir resources to within ResourceFS, using RISC OS style directory separators - otherwise Apps.!${COMPONENT})
# DIRS                (opt) (stamp object for directory creation - otherwise _dirs)
# SRCS                (opt) (source files to be concatenated, no .bas suffixes - otherwise ${TARGET})
# INSTTYPE            (opt) (use "tool" or "app" to install executable vs application - defaults to "tool")
# INSTAPP_FILES       (opt) (list of files to be installed in application directory - use InstRes specification rules)
# INSTAPP_DEPENDS     (opt) (list of dependencies to be satisfied before doing application install - ${TARGET} assumed if in INSTAPP_FILES)
# INSTAPP_VERSION     (opt) (list of Messages/!Run/Desc files to insert app version from VersionNum - include in INSTAPP_FILES as well)
# RES_FILES           (opt) (list of files to be installed in ${RESFSDIR} when building a ROM - use InstRes specification rules. Where these conflict with the files in INSTAPP_FILES, prefer a subdirectory named 'ROM')
# RES_DEPENDS         (opt) (list of dependencies to be satisfied before doing resources export - ${TARGET} assumed if in RES_FILES)
# RESAPP_FILES        (opt) (list of files to be installed in ${RESFSAPPDIR} when building a ROM - use InstRes specification rules. Where these conflict with the files in INSTAPP_FILES, prefer a subdirectory named 'ROM')
# CUSTOMINSTALLAPP    (opt) (set to "custom" to override the install rule for resource files)
# CUSTOMINSTALLTOOL   (opt) (set to "custom" to override the install rule for target binary)
# CUSTOMROM           (opt) (set to "custom" to override the rom rules)
# SOURCES_TO_SYMLINK  (opt) (files which need be linked to by the link farm, in addition to contents of bas directory)
#
#

TARGET       ?= ${COMPONENT}
ROM_MODULE   ?= ${COMPONENT}${SUFFIX_MODULE}
INSTDIR      ?= ${INSTALLDIR}/${TARGET}
INSTAPP      ?= ${INSTDIR}/!${COMPONENT}
RESFSDIR     ?= ${RESDIR}/${COMPONENT}
RESFSAPPDIR  ?= Apps.!${COMPONENT}
MERGEDRDIR   ?= _ResData_
RES_AREA     ?= Resources
DIRS         ?= _dirs
SRCS         ?= ${TARGET}

SRCS_         = $(addsuffix .bas,${SRCS})

SOURCES_TO_SYMLINK += $(wildcard bas/*) VersionNum

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

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 install resources rom install_rom links: ${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

all: ${TARGET}${SUFFIX_BASIC}
	@${ECHO} ${COMPONENT}: application built

# GNU make seems to treat any double-colon rule with no dependencies as
# always out-of-date, therefore always rebuilds it and anything which in turn
# depends on the target of the double-colon rule. So use a single-colon rule
# instead. If any cross builds need to create extra directories on a
# per-component basis, we'll cross that bridge when we get to it.
${DIRS}:
	${TOUCH} $@

install: install_${INSTTYPE}

install_: install_tool

INSTAPP_DEPENDS += $(addsuffix ${SUFFIX_BASIC},$(filter ${TARGET},${INSTAPP_FILES}))
RES_DEPENDS     += $(addsuffix ${SUFFIX_BASIC},$(filter ${TARGET},${RES_FILES}))

install_app${CUSTOMINSTALLAPP}: ${INSTAPP_DEPENDS}
	${MKDIR} ${INSTAPP}
	${INSTRES} -I Resources.${USERIF}.${LOCALE},Resources.${USERIF}.UK,Resources.${LOCALE},Resources.UK,Resources ${INSTAPP} ${INSTAPP_FILES}
ifneq (,$(filter Messages,${INSTAPP_VERSION}))
	TMP=`mktemp`; ${INSERTVERSION} ${INSTAPP}/Messages > $$TMP; mv $$TMP ${INSTAPP}/Messages
endif
ifneq (,$(filter Desc,${INSTAPP_VERSION}))
	TMP=`mktemp`; ${INSERTVERSION} descmode=1 ${INSTAPP}/Desc ${INSTAPP}/Desc > $$TMP; mv $$TMP ${INSTAPP}/Desc
endif
ifneq (,$(filter !Run,${INSTAPP_VERSION}))
	TMP=`mktemp`; ${INSERTVERSION} obeymode=1 ${INSTAPP}/!Run${SUFFIX_OBEY} > $$TMP; mv $$TMP ${INSTAPP}/!Run${SUFFIX_OBEY}
endif
	@${ECHO} ${COMPONENT}: application installation complete

install_tool${CUSTOMINSTALLTOOL}: ${TARGET}${SUFFIX_BASIC}
	${MKDIR} ${INSTDIR}
	${CP} ${TARGET}${SUFFIX_BASIC} ${INSTDIR}/${TARGET}${SUFFIX_BASIC} ${CPFLAGS}
	@${ECHO} ${COMPONENT}: tool installation complete

resources: ${RES_DEPENDS}
	${MKDIR} ${RESFSDIR}
ifneq (${RES_FILES},)
	${INSTRES} -I Resources.${LOCALE}.ROM,Resources.UK.ROM,Resources.ROM,Resources.{LOCALE},Resources.UK,Resources ${RESFSDIR} ${RES_FILES}
endif
ifneq (,$(filter Messages,${INSTAPP_VERSION}))
	${INSERTVERSION} ${RESFSDIR}/Messages > ${RESFSDIR}/_Awk_
	${CP} ${RESFSDIR}/_Awk_ ${RESFSDIR}/Messages ${CPFLAGS}
	${RM} ${RESFSDIR}/_Awk_
	for path in ${LOCALE}/ROM UK/ROM ROM ${LOCALE} UK ""; do if [ -f Resources/$$path/Messages ]; then touch -r Resources/$$path/Messages ${RESFSDIR}/Messages; break; fi; done
endif
	@${ECHO} ${COMPONENT}: resources copied to Messages module

rom${CUSTOMROM}: ${ROM_MODULE}
	@${ECHO} ${COMPONENT}: rom module built

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

${TARGET}${SUFFIX_BASIC}: ${DIRS} ${SRCS_}
ifneq (${TARGET},${SRCS})
	${FAPPEND} ${TARGET}.bas ${SRCS_}
endif
	${BASCRUNCH} -1 ${TARGET}.bas ${TARGET}.crunched
	${SQUISH} ${SQUISHFLAGS} -from ${TARGET}.crunched -to $@
	@${ECHO} ${TARGET}${SUFFIX_BASIC}: ${SRCS_} > ${TARGET}.d

${ROM_MODULE}:
	${MKDIR} ${MERGEDRDIR}
	${INSTRES} -I Resources.${LOCALE}.ROM,Resources.UK.ROM,Resources.ROM,Resources.${LOCALE},Resources.UK,Resources ${MERGEDRDIR} ${RESAPP_FILES}
	${INSTVIARG} ${MERGEDRDIR} ${RESFSAPPDIR} _ModGen_
	${MODGEN} -date "$$(awk '/Module_Date / { $$1=""; $$2=""; gsub(/"/, ""); print }' VersionNum)" $@ !${COMPONENT} !${COMPONENT} $$(sed -n -e 's/^.*(\(.*\)).*$$/\1/;1p' VersionNum) -via _ModGen_

include $(wildcard *.d)

endif

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

export_ export_hdrs export_libs:
	@${NOP}

# EOF