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