Build library in 'build' directory. Allows replacing generic code
[fw/pdclib] / Makefile
1 # $Id$
2
3 CC=arm-none-eabi-gcc
4
5 # This is where you chose which platform to compile for (see 'make links' / './platform')
6 PLATFORM := altos
7
8 # This is a list of all non-source files that are part of the distribution.
9 AUXFILES := Makefile Readme.txt
10
11 # Directories belonging to the project
12 PROJDIRS := functions includes internals
13 # All source files of the project
14 SRCFILES := $(shell find -L $(PROJDIRS) -type f -name "*.c")
15 BUILDFILES := $(shell find -L build -type f -name "*.c")
16 # All header files of the project
17 HDRFILES := $(shell find -L $(PROJDIRS) -type f -name "*.h")
18 # All .c files in functions/_PDCLIB that do not have a regression test driver
19 INTFILES := _Exit atomax digits open print scan remove rename seed stdinit strtox_main strtox_prelim filemode eol errno seek prepread prepwrite allocpages tmpfilename closeall
20 # All object files in the library
21 OBJFILES := $(patsubst %.c,%.o,$(BUILDFILES))
22 # All test drivers (.t)
23 TSTFILES := $(patsubst %.c,%_t,$(SRCFILES))
24 # All regression test drivers (.r)
25 REGFILES := $(filter-out $(patsubst %,functions/_PDCLIB/%_r,$(INTFILES)),$(patsubst %.c,%_r,$(SRCFILES)))
26 # All library dependency files (.d)
27 DEPFILES := $(patsubst %.c,%.d,$(SRCFILES))
28 # All test driver dependency files (_t.d)
29 TSTDEPFILES := $(patsubst %,%.d,$(TSTFILES))
30 # All regression test driver dependency files (_r.d)
31 REGDEPFILES := $(patsubst %,%.d,$(REGFILES))
32 # All files belonging to the source distribution
33 ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES)
34
35 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 
36 PLATFORM_FLAGS=-nostdinc -mlittle-endian -mthumb -mcpu=cortex-m3 -ffreestanding -nostdlib
37 CFLAGS := -fno-builtin -Os -g -std=c99 -I./build/internals -I./testing $(WARNINGS) $(USERFLAGS) $(PLATFORM_FLAGS)
38
39 .PHONY: all clean srcdist bindist test tests testdrivers regtests regtestdrivers todos fixmes find links unlink help
40
41 all: pdclib.a
42
43 check: all testdrivers regtestdrivers
44         @echo
45         @echo "========================"
46         @echo "Executing library tests:"
47         @echo "========================"
48         @echo
49         @$(MAKE) tests | grep -v "^ TST" | grep -v "^Failed"
50         @echo
51         @echo "==========================="
52         @echo "Executing regression tests:"
53         @echo "==========================="
54         @echo
55         @$(MAKE) regtests | grep -v "^ RTST" | grep -v "^Failed"
56
57 pdclib.a: $(OBJFILES)
58         @echo " AR      $@"
59         @ar rc pdclib.a $?
60         @echo
61
62 test: functions/$(FILE)
63         functions/$(FILE)
64
65 tests: testdrivers
66         -@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
67
68 testdrivers: $(TSTFILES)
69         @echo
70
71 regtests: regtestdrivers
72         -@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
73
74 regtestdrivers: $(REGFILES)
75         @echo
76
77 -include $(DEPFILES) $(TSTDEPFILES) $(REGDEPFILES)
78
79 clean:
80         -@$(RM) $(wildcard $(OBJFILES) $(DEPFILES) $(TSTFILES) $(TSTDEPFILES) $(REGFILES) $(REGDEPFILES) pdclib.a pdclib.tgz scanf_testdata_*)
81
82 srcdist:
83         @tar czf pdclib.tgz $(ALLFILES)
84
85 todos:
86         -@for file in $(ALLFILES:Makefile=); do grep -H TODO $$file; done; true
87
88 fixmes:
89         -@for file in $(ALLFILES:Makefile=); do grep -H FIXME $$file; done; true
90
91 find:
92         @find functions/ includes/ internals/ platform/ -name "*\.[ch]" -type f | xargs grep $$FIND
93
94 links:
95         @echo "Linking platform/$(PLATFORM)..."
96         @for dir in $$(find functions includes internals -type d); do mkdir -p build/$$dir; done
97         @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
98         @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
99
100 unlink:
101         @echo "Unlinking platform files..."
102         @for dir in $(PROJDIRS); do find build/$$dir -type l -exec rm {} +; done
103
104 help:
105         @echo "Available make targets:"
106         @echo
107         @echo "all              - build pdclib.a"
108         @echo "clean            - remove all object files, dependency files and test drivers"
109         @echo "srcdist          - build pdclib.tgz (source tarball)"
110         @echo "test             - test a single testdriver (Usage: FILE=\"test.[rt]\" make test)"
111         @echo "tests            - build and run test drivers (link pdclib.a)"
112         @echo "  testdrivers    - build but do not run test drivers"
113         @echo "regtests         - build and run regression test drivers (link system clib)"
114         @echo "  regtestdrivers - build but do not run regression test drivers"
115         @echo "todos            - list all TODO comments in the sources"
116         @echo "fixmes           - list all FIXME comments in the sources"
117         @echo "find             - find a phrase in the sources (Usage: FIND=\"phrase\" make find)"
118         @echo "links            - link platform files (development only)"
119         @echo "unlink           - remove links to platform files (development only)"
120         @echo "%.o              - build an individual object file"
121         @echo "%.t              - build an individual test driver"
122         @echo "%.r              - build an individual regression test driver"
123         @echo "help             - print this list"
124         @echo
125         @echo "Any additional compiler flags you want to use can be passed as USERFLAGS"
126         @echo "(Usage: USERFLAGS=\"flags\" make [...])."
127
128 %.o: %.c Makefile
129         echo " CC       $(patsubst functions/%,%,$@)"
130         $(CC) $(CFLAGS) -MMD -MP -I./build/includes -c $< -o $@
131
132 %_t: %.c Makefile pdclib.a
133         @echo " CC      $(patsubst functions/%,%,$@)"
134         @$(CC) $(CFLAGS) -MMD -MP -DTEST -I./build/includes $< pdclib.a -o $@
135
136 %_r: %.c Makefile
137         @echo " CC      $(patsubst functions/%,%,$@)"
138         @$(CC) $(CFLAGS) -MMD -MP -DTEST -DREGTEST $< -o $@
139