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