# Makefile fragment for C and C++ applications
#
# This makefile provides the following phony targets:
#
#    all  install  debug
#
#
# 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})
# DBG_TARGET   (opt) (debug application leafname - otherwise ${TARGET}-D)
# INSTDIR      (opt) (the target directory - otherwise ${INSTALLDIR}/${TARGET})
# DIRS         (opt) (stamp object for directory creation - otherwise _dirs)
# OBJS         (opt) (object files, no o. prefixes - otherwise ${TARGET})
# DBG_OBJS     (opt) (debug build object files, no prefixes - otherwise ${OBJS})
# LIBS         (opt) (extra libraries; CLib is always used)
# DBG_LIBS     (opt) (extra debug libraries - otherwsie ${LIBS};
#                     CLib and DEBUGLIBS always used)
# APP_OBJS     (opt) (object files for application version - otherwise derived
#                     from ${OBJS})
# APP_DBG_OBJS (opt) (object files for debug app version - otherwise derived
#                     from ${DBG_OBJS})
# CLEAN_DEPEND (opt) (phony target for additional clean actions)
# LINK_TYPE    (opt) (variant of linking command, eg C++ - defaults to C)
# INSTTYPE     (opt) (use "tool" or "app" to install executable vs application - defaults to "tool")
# INSTALLAPPFILES   (opt) (list of files to be installed in application directory - use InstRes specification rules)
# INSTALLAPP_DEPEND (opt) (list of dependencies to be satisfied before doing application install - ${TARGET} assumed if in INSTALLAPPFILES)
# 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)
# SOURCES_TO_LINK   (opt) (files which need be linked to by the link farm, in addition to contents of c and h directories)
#
#
# 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)
# SQZ + SQZFLAGS   (binary compressor)
# MKDIR            (cdir/mkdir -p)
# ECHO
# TOUCH            (create/touch)
#
#
# It relies on the following from the StdRules makefile
#
#
# .c.o  .c++.o  .cpp.o  .s.o
#
#
# It relies on the following from the DbgRules makefile
#
#
# CDFLAGS  C++DFLAGS  ASDFLAGS  LDDFLAGS
# .c.od  .c++.od  .cpp.od  .s.od
#
#

TARGET       ?= ${COMPONENT}
DBG_TARGET   ?= ${TARGET}-D
INSTDIR      ?= ${INSTALLDIR}/${TARGET}
DIRS         ?= _dirs
OBJS         ?= ${TARGET}
DBG_OBJS     ?= ${OBJS}
DBG_LIBS     ?= ${LIBS}
DBG_LIBS     += ${DEBUGLIBS}
ifeq (C++,${LINK_TYPE})
LIBS         += ${C++LIB}
DBG_LIBS     += ${C++LIB}
endif
LIBS         += ${CLIB}
DBG_LIBS     += ${CLIB}
APP_OBJS     ?= $(addsuffix .o,${OBJS})
APP_DBG_OBJS ?= $(addsuffix .od,${DBG_OBJS})

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

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

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

all install debug clean links: ${SYMLINK_DEPEND}
ifneq (clean,${MAKECMDGOALS})
	$(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} $$linkdestdir; \
			[ -d ${linksource} ] || [ -L $$linkdest ] || ln -s $$linkbackpath/${linksource} $$linkdest; \
		 ) \
	)
	@[ -L objs/Resources ] || ln -s ../Resources objs/Resources
endif
	@[ -d objs ] || ${MKDIR} objs
ifneq (links,${MAKECMDGOALS})
	@${MAKE} -C objs -f ../$(firstword ${MAKEFILE_LIST}) ${MAKECMDGOALS}
endif

else

# Makefile invoked from objs subdirectory

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

${DIRS}:
	${TOUCH} $@

clean :: ${CLEAN_DEPEND}
	@${ECHO} Cleaning...
	@cd .. && ${WIPE} objs ${WFLAGS}
	@${ECHO} ${COMPONENT}: cleaned

install: install_${INSTTYPE}

install_: install_tool

INSTALLAPP_DEPEND +=  $(addsuffix ${SUFFIX_ABSOLUTE},$(filter ${TARGET},${INSTALLAPPFILES}))

install_app${CUSTOMINSTALLAPP}: ${INSTALLAPP_DEPEND}
	${MKDIR} ${INSTAPP}
	${INSTRES} -I Resources.${USERIF}.${LOCALE},Resources.${USERIF}.UK,Resources.${LOCALE},Resources.UK,Resources ${INSTAPP} ${INSTALLAPPFILES}
ifneq (,$(filter Desc,${INSTALLAPPFILES}))
	${CPREPRO} -I. ${INSTAPP}/Desc > ${INSTAPP}/DescTmp
	${TIDYDESC} ${INSTAPP}/Desc ${INSTAPP}/DescTmp
	${RM} ${INSTAPP}/DescTmp
endif
	@${ECHO} ${COMPONENT}: application installation complete

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

debug: ${DBG_TARGET}${SUFFIX_DEBIMAGE}
	@${ECHO} ${COMPONENT}: debug application built

${TARGET}${SUFFIX_ABSOLUTE}: ${DIRS} ${APP_OBJS} ${LIBS}
	${LD} ${LDFLAGS} -o $@ ${APP_OBJS} ${LIBS}
	${STRIP} $@
	${SQZ} ${SQZFLAGS} $@

${DBG_TARGET}${SUFFIX_DEBIMAGE}: ${DIRS} ${APP_DBG_OBJS} ${DBG_LIBS}
	${LD} ${LDFLAGS} ${LDDFLAGS} -o $@ ${APP_DBG_OBJS} ${DBG_LIBS}

endif

# EOF