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