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 | |
8a33b741 LM |
14 | CFLAGS += $(OFLAGS) $(INCLUDES) $(GCOV) |
15 | LDFLAGS += -g $(GCOV) | |
ec15bdbc LM |
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 | 26 | MAKEFLAGS += -s |
8a33b741 | 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 | ||
b449884c 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 | |
55cfeafa LM |
64 | rm -f purge $(ALLEXE:%=%.exe) |
65 | $(call PASS, SUCCESS) | |
66 | ||
031d7bba LM |
67 | valgrinds: |
68 | $(MAKE) $(addprefix valgrind_,$(ALLEXE)) | |
69 | ||
55cfeafa | 70 | wipe: purge |
24857300 | 71 | $(call TITLE, "Wiping") |
55cfeafa | 72 | touch wipe |
8a33b741 | 73 | rm -f wipe $(wildcard *.gcda *.gcno *.gcov) |
ec15bdbc LM |
74 | $(call PASS, SUCCESS) |
75 | ||
0c2d2e4e | 76 | tests: all |
b449884c | 77 | $(MAKE) $(addprefix test_,$(ALLEXE)) |
0c2d2e4e | 78 | |
ec15bdbc LM |
79 | ## Main rules |
80 | ||
81 | include $(wildcard *.d) | |
82 | include $(wildcard *.ld) | |
83 | ||
b449884c | 84 | gcov_%: |
24857300 | 85 | $(MAKE) purge |
8a33b741 | 86 | GCOV="-fprofile-arcs -ftest-coverage -O0" $(MAKE) |
24857300 | 87 | $(MAKE) test_$(@:gcov_%=%) |
b449884c LM |
88 | gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)` |
89 | touch gcov | |
90 | rm -f gcov $(wildcard *.gcda *.gcno) | |
24857300 | 91 | $(MAKE) purge |
b449884c | 92 | |
ec15bdbc LM |
93 | %.test: %.c |
94 | $(call TITLE, "Building $@") | |
95 | # awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@ | |
96 | ./getcomments.pl -p='test:\s' -f='%' $< > $@ | |
97 | $(call PASS, SUCCESS) | |
98 | ||
99 | test_%: %.test %.exe | |
100 | IFS=$$'\n'; RC=0; \ | |
101 | for test in `cat $< | sed 's,${<:.test=.exe},./${<:.test=.exe},g'`; do \ | |
102 | echo "=== $$test ==="; \ | |
103 | eval $(VALGRIND) $$test; \ | |
104 | [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \ | |
105 | || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \ | |
106 | done; \ | |
8a33b741 | 107 | test "$$RC" -ne 1 |
ec15bdbc | 108 | |
031d7bba | 109 | valgrind_%: %.exe |
ec15bdbc LM |
110 | VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \ |
111 | export VALGRIND; \ | |
031d7bba | 112 | $(MAKE) $(@:valgrind_%=test_%) |
ec15bdbc LM |
113 | |
114 | %.d: %.c | |
115 | $(call TITLE, "Building $@") | |
116 | $(CC) $(INCLUDES) -MM $< -o $@~ | |
117 | echo ${<:.c=.o}: $(shell ./getcomments.pl -p='depend:\s' -f='%' $<) >> $@~ | |
118 | mv $@~ $@ | |
119 | $(call PASS, SUCCESS) | |
120 | ||
121 | %.ld: %.c | |
122 | $(call TITLE, "Building $@") | |
1968fc94 | 123 | 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 |
124 | $(call PASS, SUCCESS) |
125 | ||
126 | %.o: %.c | |
127 | $(call TITLE, "Building $@") | |
128 | $(CC) $(CFLAGS) $(INCLUDES) $(shell ./getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@ | |
129 | $(call PASS, SUCCESS) | |
130 | ||
131 | ||
132 | %.exe: %.o %.d | |
133 | $(call TITLE, "Building $@") | |
680bf70a | 134 | $(CC) $(LDFLAGS) $< $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) -o $@ |
ec15bdbc LM |
135 | $(call PASS, SUCCESS) |
136 | ||
137 | ## Phony | |
138 | ||
b449884c | 139 | .PHONY: all clean count depends gcovs purge tests |
ec15bdbc LM |
140 | |
141 | ## Precious | |
142 | ||
143 | .PRECIOUS: %.d %.o |