#---*- Makefile -*-------------------------------------------------------
#$Author: saulius $
#$Date: 2019-03-07 15:33:31 +0000 (Thu, 07 Mar 2019) $
#$Revision: 184 $
#$URL: svn+ssh://saulius-grazulis.lt/home/saulius/svn-repositories/makefiles/fpc/Makelocal-fpc $
#------------------------------------------------------------------------

UNIT_DIR = units

PFILES  = ${wildcard *.pas}
UFILES  = ${wildcard ${UNIT_DIR}/*.pas}
PROGS   = ${PFILES:%.pas=%}
UNITS   = ${UFILES:${UNIT_DIR}/%.pas=%.ppu}
OBJECTS = ${PFILES:%.pas=%.o} ${UFILES:${UNIT_DIR}/%.pas=%.o}
DEPEND  = ${PFILES:%.pas=.%.d}

.PHONY: all clean cleanAll distclean test depend

PC = fpc

all: ${PROGS}

depend: ${DEPEND}
	head .*.d

ifneq ("${DEPEND}","")
include ${DEPEND}
endif

%: %.pas
	${PC} ${PFLAGS} $< -o$@

%.ppu: ${UNIT_DIR}/%.pas
	${PC} ${PFLAGS} -FU. $< -o$@

.%.d: %.pas
	grep -i '^ *uses' $< \
	| sed -e 's/;$$//' \
	| awk '{print "$*: "tolower($$2)".ppu"}' > $@

test: ${PROGS}
	set -x; for i in $^; do ./$$i ); done

clean:
	rm -f ${DEPEND}
	rm -f ${OBJECTS}

distclean cleanAll: clean
	rm -f ${PROGS}
	rm -f ${UNITS}
