# $Id$

ifndef prefix
prefix=/opt/cortex
endif

libdir=$(prefix)/lib
bindir=$(prefix)/bin
includedir=$(prefix)/include

#CPU=cortex-m3
CPUS=cortex-m0 cortex-m3 cortex-m4
FIRSTCPU=cortex-m0
CPU=none
BUILD=build-$(CPU)

# CPU specific compiler flags
ifeq ($(CPU),cortex-m0)
CPU_FLAGS=
endif
ifeq ($(CPU),cortex-m3)
CPU_FLAGS=
endif
ifeq ($(CPU),cortex-m4)
CPU_FLAGS=-mfloat-abi=hard
endif

#CC=$(bindir)/arm-none-eabi-gcc
CC=/usr/bin/arm-none-eabi-gcc
AR=/usr/bin/arm-none-eabi-ar

# This is where you chose which platform to compile for (see 'make links' / './platform')
PLATFORM := altos

# This is a list of all non-source files that are part of the distribution.
AUXFILES := Makefile Readme.txt

# Directories belonging to the project
PROJDIRS := functions includes internals
# All source files of the project
SRCFILES := $(shell find -L $(PROJDIRS) -type f -name "*.c")
BUILDFILES := $(shell find -L $(BUILD) -type f -name "*.c")
# All header files of the project
HDRFILES := $(shell find -L $(PROJDIRS) -type f -name "*.h")
BUILDHDRFILES := $(shell find -L $(BUILD) -type f -name "*.h")
# All .c files in functions/_PDCLIB that do not have a regression test driver
INTFILES := _Exit atomax digits open print scan remove rename seed stdinit strtox_main strtox_prelim filemode eol errno seek prepread prepwrite allocpages tmpfilename closeall
# All object files in the library
OBJFILES := $(patsubst %.c,%.o,$(BUILDFILES))
# All test drivers (.t)
TSTFILES := $(patsubst %.c,%_t,$(SRCFILES))
# All regression test drivers (.r)
REGFILES := $(filter-out $(patsubst %,functions/_PDCLIB/%_r,$(INTFILES)),$(patsubst %.c,%_r,$(SRCFILES)))
# All library dependency files (.d)
DEPFILES := $(patsubst %.c,%.d,$(SRCFILES))
# All test driver dependency files (_t.d)
TSTDEPFILES := $(patsubst %,%.d,$(TSTFILES))
# All regression test driver dependency files (_r.d)
REGDEPFILES := $(patsubst %,%.d,$(REGFILES))
# All files belonging to the source distribution
ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES)

WARNINGS := -Wall -Wextra -pedantic -Wno-unused-parameter -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wuninitialized -Wstrict-prototypes 
PLATFORM_FLAGS=-mlittle-endian -mthumb -mcpu=$(CPU) -ffreestanding $(CPU_FLAGS)
CFLAGS := -Os -g -std=c99 -I./$(BUILD)/internals -I./testing $(WARNINGS) $(USERFLAGS) $(PLATFORM_FLAGS)

.PHONY: all clean srcdist bindist test tests testdrivers regtests regtestdrivers todos fixmes find links unlink help

PDCLIBNAME=libpdclib
PDCLIB=$(PDCLIBNAME)-$(CPU).a

all:
	+for i in $(CPUS); do make CPU=$$i links; make CPU=$$i $(PDCLIBNAME)-$$i.a; done

install: install-hdr
	@echo '######' install
	+for i in $(CPUS); do make CPU=$$i prefix=$(prefix) install-lib-$$i; done

install-hdr:
	@echo '######' install-hdr
	+make CPU=$(FIRSTCPU) prefix=$(prefix) install-hdr-$(FIRSTCPU)

clean:
	+for i in $(CPUS); do make CPU=$$i clean-$$i; rm -rf build-$$i; done

check: all testdrivers regtestdrivers
	@echo
	@echo "========================"
	@echo "Executing library tests:"
	@echo "========================"
	@echo
	@$(MAKE) tests | grep -v "^ TST" | grep -v "^Failed"
	@echo
	@echo "==========================="
	@echo "Executing regression tests:"
	@echo "==========================="
	@echo
	@$(MAKE) regtests | grep -v "^ RTST" | grep -v "^Failed"

$(PDCLIB): $(BUILD) $(OBJFILES)
	@echo " AR	$@"
	@$(AR) rc $@ $(OBJFILES)
	@echo

install-$(CPU): install-$(CPU)-lib install-hdr
	@echo '######' install-$(CPU)

install-lib-$(CPU): $(PDCLIB)
	@echo '######' install-lib-$(CPU)
	install -D -c $(PDCLIB) $(DESTDIR)$(libdir)/$(PDCLIB)

install-hdr-$(CPU): $(BUILD) $(BUILDHDRFILES)
	@echo '######' install-hdr-$(CPU)
	for file in $(BUILDHDRFILES); do \
		install -m 0444 -D -c $$file $(DESTDIR)$(includedir)/.; \
	done

test: functions/$(FILE)
	functions/$(FILE)

tests: testdrivers
	-@rc=0; count=0; failed=""; for file in $(TSTFILES); do echo " TST	$$file"; ./$$file; test=$$?; if [ $$test != 0 ]; then rc=`expr $$rc + $$test`; failed="$$failed $$file"; fi; count=`expr $$count + 1`; done; echo; echo "Tests executed (linking PDCLib): $$count  Tests failed: $$rc"; echo; for file in $$failed; do echo "Failed: $$file"; done; echo

testdrivers: $(TSTFILES)
	@echo

regtests: regtestdrivers
	-@rc=0; count=0; failed=""; for file in $(REGFILES); do echo " RTST	$$file"; ./$$file; test=$$?; if [ $$test != 0 ]; then rc=`expr $$rc + $$test`; failed="$$failed $$file"; fi; count=`expr $$count + 1`; done; echo; echo "Tests executed (linking system libc): $$count  Tests failed: $$rc"; echo; for file in $$failed; do echo "Failed: $$file"; done; echo

regtestdrivers: $(REGFILES)
	@echo

-include $(DEPFILES) $(TSTDEPFILES) $(REGDEPFILES)

clean-$(CPU): $(BUILD)
	-@$(RM) $(wildcard $(OBJFILES) $(DEPFILES) $(TSTFILES) $(TSTDEPFILES) $(REGFILES) $(REGDEPFILES) $(PDCLIB) pdclib.tgz scanf_testdata_*)

srcdist:
	@tar czf pdclib.tgz $(ALLFILES)

todos:
	-@for file in $(ALLFILES:Makefile=); do grep -H TODO $$file; done; true

fixmes:
	-@for file in $(ALLFILES:Makefile=); do grep -H FIXME $$file; done; true

find:
	@find functions/ includes/ internals/ platform/ -name "*\.[ch]" -type f | xargs grep $$FIND

links: $(BUILD)
	@echo "Linking platform/$(PLATFORM)..."
	@for dir in $$(find functions includes internals -type d); do mkdir -p $(BUILD)/$$dir; done
	@for file in $$(find platform/$(PLATFORM) -mindepth 2 -type f ! -path *.svn* -printf "%P\n"); do if [ ! -f $(BUILD)/$$file ]; then ln -s $$(dirname $$file | sed "s@[^/]*@..@g")/../platform/$(PLATFORM)/$$file $(BUILD)/$$file; fi; done
	@for super in $(PROJDIRS); do for file in $$(find $$super -type f ! -path *.svn* -printf "%P\n"); do if [ ! -f $(BUILD)/$$super/$$file ]; then ln -s $$(echo $$file | sed "s@[^/]*@..@g")/../$$super/$$file $(BUILD)/$$super/$$file; fi; done; done
	if [ -f platform/$(PLATFORM)/include-exclude ]; then for ex in `cat platform/$(PLATFORM)/include-exclude`; do echo exclude $$ex; rm -f $(BUILD)/includes/$$ex; done; fi; true

unlink: $(BUILD)
	@echo "Unlinking platform files..."
	@for dir in $(PROJDIRS); do find $(BUILD)/$$dir -type l -exec rm {} +; done

help:
	@echo "Available make targets:"
	@echo
	@echo "all              - build $(PDCLIB)"
	@echo "clean            - remove all object files, dependency files and test drivers"
	@echo "srcdist          - build pdclib.tgz (source tarball)"
	@echo "test             - test a single testdriver (Usage: FILE=\"test.[rt]\" make test)"
	@echo "tests            - build and run test drivers (link $(PDCLIB))"
	@echo "  testdrivers    - build but do not run test drivers"
	@echo "regtests         - build and run regression test drivers (link system clib)"
	@echo "  regtestdrivers - build but do not run regression test drivers"
	@echo "todos            - list all TODO comments in the sources"
	@echo "fixmes           - list all FIXME comments in the sources"
	@echo "find             - find a phrase in the sources (Usage: FIND=\"phrase\" make find)"
	@echo "links            - link platform files (development only)"
	@echo "unlink           - remove links to platform files (development only)"
	@echo "%.o              - build an individual object file"
	@echo "%.t              - build an individual test driver"
	@echo "%.r              - build an individual regression test driver"
	@echo "help             - print this list"
	@echo
	@echo "Any additional compiler flags you want to use can be passed as USERFLAGS"
	@echo "(Usage: USERFLAGS=\"flags\" make [...])."

%.o: %.c Makefile
	@echo " CC	$(patsubst functions/%,%,$@)"
	@$(CC) $(CFLAGS) -MMD -MP -I./$(BUILD)/includes -c $< -o $@

%_t: %.c Makefile $(PDCLIB)
	@echo " CC	$(patsubst functions/%,%,$@)"
	@$(CC) $(CFLAGS) -MMD -MP -DTEST -I./$(BUILD)/includes $< $(PDCLIB) -o $@

%_r: %.c Makefile
	@echo " CC	$(patsubst functions/%,%,$@)"
	@$(CC) $(CFLAGS) -MMD -MP -DTEST -DREGTEST $< -o $@

$(BUILD):
	mkdir -p $(BUILD)
