Commit | Line | Data |
---|---|---|
4e91efbc LM |
1 | # Default flags |
2 | ||
3 | CC = gcc | |
4 | ||
5 | INCLUDES = -I../debug -D__MEMORY_ALLOCATION__ | |
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 | |
13 | CFLAGS += -std=c99 -D_XOPEN_SOURCE=500 | |
14 | CFLAGS += $(OFLAGS) $(INCLUDES) $(GCOVER) | |
15 | LDFLAGS += -g | |
16 | ||
17 | # Targets | |
18 | ||
19 | ALLEXE = | |
20 | ALLEXE += indent | |
21 | ||
22 | SHELL = bash | |
23 | ||
24 | MAKE = mingw32-make | |
25 | MAKEFLAGS += -s | |
26 | ||
27 | # Functions | |
28 | ||
29 | TITLE = echo -en "\033[0;1m$(strip $(1))\033[0;0m:\t" | |
30 | PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m" | |
31 | WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m" | |
32 | FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m" | |
33 | ||
34 | MKDIR = mkdir -p $(1) && chmod a+rx,go-w $(1) | |
35 | ||
36 | INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1) $(2) && chmod a+rX,go-w $(2) | |
37 | ||
38 | VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; } | |
39 | ||
40 | ## Generic rules | |
41 | ||
42 | all: depends | |
43 | $(MAKE) $(ALLEXE:%=%.exe) | |
44 | ||
45 | count: | |
46 | wc $(wildcard *.c *.h) $(MAKEFILE_LIST) | |
47 | ||
48 | clean: | |
49 | $(call TITLE, "Cleaning") | |
50 | touch clean | |
51 | rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_*) | |
52 | $(call PASS, SUCCESS) | |
53 | ||
54 | depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE)) | |
55 | ||
56 | gcovs: | |
57 | $(MAKE) $(addprefix gcov_,$(ALLEXE)) | |
58 | ||
59 | purge: clean | |
60 | $(call TITLE, "Purging") | |
61 | touch purge | |
62 | rm -f purge $(ALLEXE:%=%.exe) | |
63 | $(call PASS, SUCCESS) | |
64 | ||
65 | valgrinds: | |
66 | $(MAKE) $(addprefix valgrind_,$(ALLEXE)) | |
67 | ||
68 | wipe: purge | |
69 | $(call TITLE, "Wiping") | |
70 | touch wipe | |
71 | rm -f wipe $(wildcard *.gcda *.gcno) | |
72 | $(call PASS, SUCCESS) | |
73 | ||
74 | tests: all | |
75 | $(MAKE) $(addprefix test_,$(ALLEXE)) | |
76 | ||
77 | ## Main rules | |
78 | ||
79 | include $(wildcard *.d) | |
80 | include $(wildcard *.ld) | |
81 | ||
82 | gcov_%: | |
83 | $(MAKE) purge | |
84 | CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="-fprofile-arcs -ftest-coverage" $(MAKE) | |
85 | $(MAKE) test_$(@:gcov_%=%) | |
86 | gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)` | |
87 | touch gcov | |
88 | rm -f gcov $(wildcard *.gcda *.gcno) | |
89 | $(MAKE) purge | |
90 | ||
91 | %.test: %.c | |
92 | $(call TITLE, "Building $@") | |
93 | # awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@ | |
94 | ./getcomments.pl -p='test:\s' -f='%' $< > $@ | |
95 | $(call PASS, SUCCESS) | |
96 | ||
97 | test_%: %.test %.exe | |
98 | IFS=$$'\n'; RC=0; \ | |
99 | for test in `cat $< | sed 's,${<:.test=.exe},./${<:.test=.exe},g'`; do \ | |
100 | echo "=== $$test ==="; \ | |
101 | eval $(VALGRIND) $$test; \ | |
102 | [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \ | |
103 | || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \ | |
104 | done; \ | |
105 | test "$$RC" -ne 1 | |
106 | ||
107 | valgrind_%: %.exe | |
108 | VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \ | |
109 | export VALGRIND; \ | |
110 | $(MAKE) $(@:valgrind_%=test_%) | |
111 | ||
112 | %.d: %.c | |
113 | $(call TITLE, "Building $@") | |
114 | $(CC) $(INCLUDES) -MM $< -o $@~ | |
115 | echo ${<:.c=.o}: $(shell ./getcomments.pl -p='depend:\s' -f='%' $<) >> $@~ | |
116 | mv $@~ $@ | |
117 | $(call PASS, SUCCESS) | |
118 | ||
119 | %.ld: %.c | |
120 | $(call TITLE, "Building $@") | |
121 | echo ${<:.c=.exe}: $(shell ./getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@ | |
122 | $(call PASS, SUCCESS) | |
123 | ||
124 | %.o: %.c | |
125 | $(call TITLE, "Building $@") | |
126 | $(CC) $(CFLAGS) $(INCLUDES) $(shell ./getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@ | |
127 | $(call PASS, SUCCESS) | |
128 | ||
129 | ||
130 | %.exe: %.o %.d | |
131 | $(call TITLE, "Building $@") | |
132 | $(CC) $(LDFLAGS) $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $< -o $@ | |
133 | $(call PASS, SUCCESS) | |
134 | ||
135 | ## Phony | |
136 | ||
137 | .PHONY: all clean count depends gcovs purge tests | |
138 | ||
139 | ## Precious | |
140 | ||
141 | .PRECIOUS: %.d %.o |