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