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