Actually use $(CPU) when building object files. Add clean target.
[fw/pdclib] / Makefile
1 # $Id$
2
3 DESTDIR=
4 prefix=/opt/sat
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; done
67
68 check: all testdrivers regtestdrivers
69         @echo
70         @echo "========================"
71         @echo "Executing library tests:"
72         @echo "========================"
73         @echo
74         @$(MAKE) tests | grep -v "^ TST" | grep -v "^Failed"
75         @echo
76         @echo "==========================="
77         @echo "Executing regression tests:"
78         @echo "==========================="
79         @echo
80         @$(MAKE) regtests | grep -v "^ RTST" | grep -v "^Failed"
81
82 $(PDCLIB): $(BUILD) $(OBJFILES)
83         @echo " AR      $@"
84         @ar rc $@ $(OBJFILES)
85         @echo
86
87 install-$(CPU): install-$(CPU)-lib install-hdr
88
89 install-lib-$(CPU): $(BUILD) $(PDCLIB)
90         install -D -c $(PDCLIB) $(DESTDIR)$(libdir)/$(PDCLIB)
91
92 install-hdr-$(CPU): $(BUILD) $(BUILDHDRFILES)
93         for file in $(BUILDHDRFILES); do \
94                 install -D -c $$file $(DESTDIR)$(includedir)/.; \
95         done
96
97 test: functions/$(FILE)
98         functions/$(FILE)
99
100 tests: testdrivers
101         -@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
102
103 testdrivers: $(TSTFILES)
104         @echo
105
106 regtests: regtestdrivers
107         -@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
108
109 regtestdrivers: $(REGFILES)
110         @echo
111
112 -include $(DEPFILES) $(TSTDEPFILES) $(REGDEPFILES)
113
114 clean-$(CPU): $(BUILD)
115         -@$(RM) $(wildcard $(OBJFILES) $(DEPFILES) $(TSTFILES) $(TSTDEPFILES) $(REGFILES) $(REGDEPFILES) $(PDCLIB) pdclib.tgz scanf_testdata_*)
116
117 srcdist:
118         @tar czf pdclib.tgz $(ALLFILES)
119
120 todos:
121         -@for file in $(ALLFILES:Makefile=); do grep -H TODO $$file; done; true
122
123 fixmes:
124         -@for file in $(ALLFILES:Makefile=); do grep -H FIXME $$file; done; true
125
126 find:
127         @find functions/ includes/ internals/ platform/ -name "*\.[ch]" -type f | xargs grep $$FIND
128
129 links: $(BUILD)
130         @echo "Linking platform/$(PLATFORM)..."
131         @for dir in $$(find functions includes internals -type d); do mkdir -p $(BUILD)/$$dir; done
132         @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
133         @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
134
135 unlink: $(BUILD)
136         @echo "Unlinking platform files..."
137         @for dir in $(PROJDIRS); do find $(BUILD)/$$dir -type l -exec rm {} +; done
138
139 help:
140         @echo "Available make targets:"
141         @echo
142         @echo "all              - build $(PDCLIB)"
143         @echo "clean            - remove all object files, dependency files and test drivers"
144         @echo "srcdist          - build pdclib.tgz (source tarball)"
145         @echo "test             - test a single testdriver (Usage: FILE=\"test.[rt]\" make test)"
146         @echo "tests            - build and run test drivers (link $(PDCLIB))"
147         @echo "  testdrivers    - build but do not run test drivers"
148         @echo "regtests         - build and run regression test drivers (link system clib)"
149         @echo "  regtestdrivers - build but do not run regression test drivers"
150         @echo "todos            - list all TODO comments in the sources"
151         @echo "fixmes           - list all FIXME comments in the sources"
152         @echo "find             - find a phrase in the sources (Usage: FIND=\"phrase\" make find)"
153         @echo "links            - link platform files (development only)"
154         @echo "unlink           - remove links to platform files (development only)"
155         @echo "%.o              - build an individual object file"
156         @echo "%.t              - build an individual test driver"
157         @echo "%.r              - build an individual regression test driver"
158         @echo "help             - print this list"
159         @echo
160         @echo "Any additional compiler flags you want to use can be passed as USERFLAGS"
161         @echo "(Usage: USERFLAGS=\"flags\" make [...])."
162
163 %.o: %.c Makefile
164         echo " CC       $(patsubst functions/%,%,$@)"
165         $(CC) $(CFLAGS) -MMD -MP -I./$(BUILD)/includes -c $< -o $@
166
167 %_t: %.c Makefile $(PDCLIB)
168         @echo " CC      $(patsubst functions/%,%,$@)"
169         @$(CC) $(CFLAGS) -MMD -MP -DTEST -I./$(BUILD)/includes $< $(PDCLIB) -o $@
170
171 %_r: %.c Makefile
172         @echo " CC      $(patsubst functions/%,%,$@)"
173         @$(CC) $(CFLAGS) -MMD -MP -DTEST -DREGTEST $< -o $@
174
175 $(BUILD):
176         mkdir -p $(BUILD)