add more test (3)
[cmore.git] / makefile
1 # Default flags
2
3 CC = gcc
4
5 #INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
6 INCLUDES =
7 OFLAGS = -O4 -Os
8 #OFLAGS = -O4 -ffast-math -finline-functions
9 #OFLAGS = -O4 -finline-functions
10 #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
11 #OFLAGS += -minline-all-stringops -fsingle-precision-constant
12 #OFLAGS += -malign-double
13 CFLAGS += -W -Wall -Wextra -g
14 #CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
15 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
16 LDFLAGS += -g $(OPTIONS)
17
18 LDOPT = linker
19 ifeq ($(OS),Windows_NT)
20 #LDLIBS += -lws2_32
21 LDOPT = winlnk
22 endif
23
24
25 # Targets
26
27 ALLEXE =
28 ALLEXE += cmore
29
30 SHELL = bash
31
32 #MAKE = mingw32-make
33 MAKEFLAGS += -s
34 include $(wildcard .makefile)
35
36 # Functions
37
38 TITLE = echo -en "\033[0;1m$(strip $(1))\033[0;0m:\t"
39 PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m"
40 WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m"
41 FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m"
42
43 MKDIR = mkdir -p $(1) && chmod a+rx,go-w $(1)
44
45 INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1) $(2) && chmod a+rX,go-w $(2)
46
47 VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; }
48
49 ## Generic rules
50
51 all: depends
52 $(MAKE) $(ALLEXE:%=%.exe)
53
54 count:
55 wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
56
57 clean:
58 $(call TITLE, "Cleaning")
59 touch clean
60 rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_* gmon.out)
61 $(call PASS, SUCCESS)
62
63 depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE))
64
65 gcovs:
66 $(MAKE) $(addprefix gcov_,$(ALLEXE))
67
68 gprofs:
69 $(MAKE) $(addprefix gprof_,$(ALLEXE))
70
71 purge: clean
72 $(call TITLE, "Purging")
73 touch purge
74 rm -f purge $(ALLEXE:%=%.exe)
75 $(call PASS, SUCCESS)
76
77 valgrinds: all
78 $(MAKE) $(addprefix valgrind_,$(ALLEXE))
79
80 wipe: purge
81 $(call TITLE, "Wiping")
82 touch wipe
83 rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.glog)
84 $(call PASS, SUCCESS)
85
86 tests: all
87 $(MAKE) $(addprefix test_,$(ALLEXE))
88
89 ## Main rules
90
91 include $(wildcard *.d)
92 include $(wildcard *.ld)
93
94 gcov_%:
95 $(MAKE) purge
96 OPTIONS="-coverage -O0" $(MAKE)
97 $(MAKE) test_$(@:gcov_%=%)
98 gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)`
99 touch gcov
100 rm -f gcov $(wildcard *.gcda *.gcno)
101 $(MAKE) purge
102 grep '#####' *.c.gcov || true
103
104 gprof_%:
105 $(MAKE) purge
106 $(MAKE) depends
107 OPTIONS="-pg" $(MAKE) ${@:gprof_%=%}.exe
108 $(MAKE) ${@:gprof_%=%}.test
109 IFS=$$'\n'; id=1; \
110 for test in `cat ${@:gprof_%=%}.test | sed 's,${@:gprof_%=%}.exe,./${@:gprof_%=%}.exe,g'`; do \
111 log=${@:gprof_%=%}.prof-$$id.glog; \
112 $(call TITLE, test: $$test); \
113 echo $$test > $$log; \
114 eval $$test >> $$log; \
115 [ $$? -eq 0 ] \
116 && echo -e "\033[1;32mSUCCESS\033[0;0m" \
117 || echo -e "\033[1;31mFAILED\033[0;0m"; \
118 [ -f gmon.out ] && { gprof ${@:gprof_%=%}.exe gmon.out >> $$log; rm gmon.out; }; \
119 let id++; \
120 done;
121 $(MAKE) purge
122
123 %.test: %.c
124 $(call TITLE, "Building $@")
125 # awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@
126 perl -- getcomments.pl -p='test:\s' -f='%' $< > $@
127 $(call PASS, SUCCESS)
128
129 test_%: %.test %.exe
130 IFS=$$'\n'; RC=0; \
131 for test in `cat $< | sed 's,${<:.test=.exe},$(VALGRIND) ./${<:.test=.exe},g'`; do \
132 echo "=== $$test ==="; \
133 eval $$test; \
134 [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
135 || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
136 done; \
137 test "$$RC" -ne 1
138
139 valgrind_%: %.exe
140 VALGRIND="valgrind -v --leak-check=full --log-fd=3"; \
141 export VALGRIND; \
142 $(MAKE) $(@:valgrind_%=test_%) 3>$@.log
143
144 %.d: %.c
145 $(call TITLE, "Building $@")
146 $(CC) $(INCLUDES) -MM $< -o $@~
147 echo ${<:.c=.o}: $(shell perl -- getcomments.pl -p='depend:\s' -f='%' $<) >> $@~
148 mv $@~ $@
149 $(call PASS, SUCCESS)
150
151 %.ld: %.c
152 $(call TITLE, "Building $@")
153 echo ${<:.c=.exe}: $(shell perl -- getcomments.pl -p='$(LDOPT):\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@
154 $(call PASS, SUCCESS)
155
156 %.o: %.c
157 $(call TITLE, "Building $@")
158 $(CC) $(CFLAGS) $(INCLUDES) $(shell perl -- getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@
159 $(call PASS, SUCCESS)
160
161
162 %.exe: %.o %.d
163 $(call TITLE, "Building $@")
164 $(CC) $(LDFLAGS) $< $(shell perl -- getcomments.pl -p='$(LDOPT):\s' -f='%' ${<:.o=.c}) $(LDLIBS) -o $@
165 $(call PASS, SUCCESS)
166
167 ## Phony
168
169 .PHONY: all clean count depends gcovs purge tests
170
171 ## Precious
172
173 .PRECIOUS: %.d %.o