Proper handling of dependencies. (Related to #26.)
[fw/pdclib] / Makefile
1 # $Id$
2
3 # This is where you chose which platform to compile for (see 'make links' / './platform')
4 PLATFORM := example
5
6 # This is a list of all non-source files that are part of the distribution.
7 AUXFILES := Makefile Readme.txt
8
9 # Directories belonging to the project
10 PROJDIRS := functions includes internals
11 # All source files of the project
12 SRCFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")
13 # All header files of the project
14 HDRFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.h")
15 # All .c files in functions/_PDCLIB that do not have a regression test driver
16 INTFILES := _Exit atomax digits open print scan remove rename seed stdinit strtox_main strtox_prelim filemode eol errno seek prepread prepwrite allocpages tmpfilename closeall
17 # All object files in the library
18 OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))
19 # All test drivers (.t)
20 TSTFILES := $(patsubst %.c,%_t,$(SRCFILES))
21 # All regression test drivers (.r)
22 REGFILES := $(filter-out $(patsubst %,functions/_PDCLIB/%_r,$(INTFILES)),$(patsubst %.c,%_r,$(SRCFILES)))
23 # All library dependency files (.d)
24 DEPFILES := $(patsubst %.c,%.d,$(SRCFILES))
25 # All test driver dependency files (_t.d)
26 TSTDEPFILES := $(patsubst %,%.d,$(TSTFILES))
27 # All regression test driver dependency files (_r.d)
28 REGDEPFILES := $(patsubst %,%.d,$(REGFILES))
29 # All files belonging to the source distribution
30 ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES)
31
32 # All files in platform/$(PLATFORM)/functions/_PDCLIB (for development only)
33 PATCHFILES1 := $(shell ls platform/$(PLATFORM)/functions/_PDCLIB/*.c)
34 # All files in platform/$(PLATFORM)/functions/stdlib (for development only)
35 PATCHFILES2 := $(shell ls platform/$(PLATFORM)/functions/stdlib/*.c)
36 # All files in platform/$(PLATFORM)/functions/stdio (for development only)
37 PATCHFILES3 := $(shell ls platform/$(PLATFORM)/functions/stdio/*.c)
38
39 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 -fno-builtin 
40 CFLAGS := -g -std=c99 -I./internals -I./testing $(WARNINGS) $(USERFLAGS)
41
42 .PHONY: all clean srcdist bindist test tests testdrivers regtests regtestdrivers todos fixmes find links unlink help
43
44 all: pdclib.a testdrivers regtestdrivers
45         @echo
46         @echo "========================"
47         @echo "Executing library tests:"
48         @echo "========================"
49         @echo
50         @$(MAKE) tests | grep -v "^ TST" | grep -v "^Failed"
51         @echo
52         @echo "==========================="
53         @echo "Executing regression tests:"
54         @echo "==========================="
55         @echo
56         @$(MAKE) regtests | grep -v "^ RTST" | grep -v "^Failed"
57
58 pdclib.a: $(OBJFILES)
59         @echo " AR      $@"
60         @ar rc pdclib.a $?
61         @echo
62
63 test: functions/$(FILE)
64         functions/$(FILE)
65
66 tests: testdrivers
67         -@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
68
69 testdrivers: $(TSTFILES)
70         @echo
71
72 regtests: regtestdrivers
73         -@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
74
75 regtestdrivers: $(REGFILES)
76         @echo
77
78 #-include $(DEPFILES) $(TSTDEPFILES) $(REGDEPFILES)
79
80 clean:
81         echo $(TSTFILES)
82         echo $(TSTDEPFILES)
83         @for file in $(OBJFILES) $(DEPFILES) $(TSTFILES) $(TSTDEPFILES) $(REGFILES) $(REGDEPFILES) pdclib.a pdclib.tgz scanf_testdata_*; do if [ -f $$file ]; then rm $$file; fi; done
84
85 srcdist:
86         @tar czf pdclib.tgz $(ALLFILES)
87
88 todos:
89         -@for file in $(ALLFILES); do grep -H TODO $$file; done; true
90
91 fixmes:
92         -@for file in $(ALLFILES); do grep -H FIXME $$file; done; true
93
94 find:
95         @find functions/ includes/ internals/ platform/ -name "*\.[ch]" -type f | xargs grep $$FIND
96
97 links:
98         @echo "Linking platform/$(PLATFORM)..."
99         @cd internals && ln -s ../platform/$(PLATFORM)/internals/_PDCLIB_config.h
100         @cd includes && ln -s ../platform/$(PLATFORM)/includes/float.h
101         @cd testing && ln -s ../platform/$(PLATFORM)/testing/printf_reference.txt
102         @cd functions/_PDCLIB && for file in $(PATCHFILES1); do basfile=`basename $$file`; if [ ! -f $$basfile ]; then ln -s `ls ../../$$file` .; fi; done
103         @cd functions/stdlib && for file in $(PATCHFILES2); do basfile=`basename $$file`; if [ ! -f $$basfile ]; then ln -s `ls ../../$$file` .; fi; done
104         @cd functions/stdio && for file in $(PATCHFILES3); do basfile=`basename $$file`; if [ ! -f $$basfile ]; then ln -s `ls ../../$$file` .; fi; done
105
106 unlink:
107         @echo "Unlinking platform files..."
108         @if [ -f internals/_PDCLIB_config.h ]; then rm internals/_PDCLIB_config.h; fi
109         @if [ -f includes/float.h ]; then rm includes/float.h; fi
110         @if [ -f testing/printf_reference.txt ]; then rm testing/printf_reference.txt; fi
111         @cd functions/_PDCLIB && for file in $(PATCHFILES1); do basfile=`basename $$file`; if [ -f $$basfile ]; then rm $$basfile; fi; done
112         @cd functions/stdlib && for file in $(PATCHFILES2); do basfile=`basename $$file`; if [ -f $$basfile ]; then rm $$basfile; fi; done
113         @cd functions/stdio && for file in $(PATCHFILES3); do basfile=`basename $$file`; if [ -f $$basfile ]; then rm $$basfile; fi; done
114
115 help:
116         @echo "Available make targets:"
117         @echo
118         @echo "all              - build pdclib.a"
119         @echo "clean            - remove all object files, dependency files and test drivers"
120         @echo "srcdist          - build pdclib.tgz (source tarball)"
121         @echo "test             - test a single testdriver (Usage: FILE=\"test.[rt]\" make test)"
122         @echo "tests            - build and run test drivers (link pdclib.a)"
123         @echo "  testdrivers    - build but do not run test drivers"
124         @echo "regtests         - build and run regression test drivers (link system clib)"
125         @echo "  regtestdrivers - build but do not run regression test drivers"
126         @echo "todos            - list all TODO comments in the sources"
127         @echo "fixmes           - list all FIXME comments in the sources"
128         @echo "find             - find a phrase in the sources (Usage: FIND=\"phrase\" make find)"
129         @echo "links            - link platform files (development only)"
130         @echo "unlink           - remove links to platform files (development only)"
131         @echo "%.o              - build an individual object file"
132         @echo "%.t              - build an individual test driver"
133         @echo "%.r              - build an individual regression test driver"
134         @echo "help             - print this list"
135         @echo
136         @echo "Any additional compiler flags you want to use can be passed as USERFLAGS"
137         @echo "(Usage: USERFLAGS=\"flags\" make [...])."
138
139 %.o: %.c Makefile
140         @echo " CC      $(patsubst functions/%,%,$@)"
141         @$(CC) $(CFLAGS) -MMD -MP -I./includes -c $< -o $@
142
143 %_t: %.c Makefile pdclib.a
144         @echo " CC      $(patsubst functions/%,%,$@)"
145         @$(CC) $(CFLAGS) -MMD -MP -DTEST -I./includes $< pdclib.a -o $@
146
147 %_r: %.c Makefile
148         @echo " CC      $(patsubst functions/%,%,$@)"
149         @$(CC) $(CFLAGS) -MMD -MP -DTEST -DREGTEST $< -o $@
150