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