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