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