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