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