Commit | Line | Data |
---|---|---|
8512671a 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) $(GCOV) | |
15 | LDFLAGS += -g $(GCOV) | |
184be781 LM |
16 | ifeq ($(OS),Windows_NT) |
17 | LDLIBS += -lws2_32 | |
18 | endif | |
8512671a LM |
19 | |
20 | # Targets | |
21 | ||
22 | ALLEXE = | |
23 | ALLEXE += webserver | |
24 | #ALLEXE += skel | |
25 | ||
26 | SHELL = bash | |
27 | ||
28 | #MAKE = mingw32-make | |
29 | MAKEFLAGS += -s | |
30 | include $(wildcard .makefile) | |
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 | |
56 | rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_*) | |
57 | $(call PASS, SUCCESS) | |
58 | ||
59 | depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE)) | |
60 | ||
61 | gcovs: | |
62 | $(MAKE) $(addprefix gcov_,$(ALLEXE)) | |
63 | ||
64 | purge: clean | |
65 | $(call TITLE, "Purging") | |
66 | touch purge | |
67 | rm -f purge $(ALLEXE:%=%.exe) | |
68 | $(call PASS, SUCCESS) | |
69 | ||
6b52fb90 | 70 | valgrinds: all |
8512671a LM |
71 | $(MAKE) $(addprefix valgrind_,$(ALLEXE)) |
72 | ||
73 | wipe: purge | |
74 | $(call TITLE, "Wiping") | |
75 | touch wipe | |
76 | rm -f wipe $(wildcard *.gcda *.gcno *.gcov) | |
77 | $(call PASS, SUCCESS) | |
78 | ||
79 | tests: all | |
80 | $(MAKE) $(addprefix test_,$(ALLEXE)) | |
81 | ||
82 | ## Main rules | |
83 | ||
84 | include $(wildcard *.d) | |
85 | include $(wildcard *.ld) | |
86 | ||
87 | gcov_%: | |
88 | $(MAKE) purge | |
89 | GCOV="-coverage -O0" $(MAKE) | |
90 | $(MAKE) test_$(@:gcov_%=%) | |
91 | gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)` | |
92 | touch gcov | |
93 | rm -f gcov $(wildcard *.gcda *.gcno) | |
94 | $(MAKE) purge | |
95 | grep '#####' *.c.gcov || true | |
96 | ||
97 | %.test: %.c | |
98 | $(call TITLE, "Building $@") | |
99 | # awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@ | |
100 | perl -- getcomments.pl -p='test:\s' -f='%' $< > $@ | |
101 | $(call PASS, SUCCESS) | |
102 | ||
103 | test_%: %.test %.exe | |
104 | IFS=$$'\n'; RC=0; \ | |
105 | for test in `cat $< | sed 's,${<:.test=.exe},$(VALGRIND) ./${<:.test=.exe},g'`; do \ | |
106 | echo "=== $$test ==="; \ | |
107 | eval $$test; \ | |
108 | [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \ | |
109 | || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \ | |
110 | done; \ | |
111 | test "$$RC" -ne 1 | |
112 | ||
113 | valgrind_%: %.exe | |
114 | VALGRIND="valgrind -v --leak-check=full --log-fd=3"; \ | |
115 | export VALGRIND; \ | |
116 | $(MAKE) $(@:valgrind_%=test_%) 3>$@.log | |
117 | ||
118 | %.d: %.c | |
119 | $(call TITLE, "Building $@") | |
120 | $(CC) $(INCLUDES) -MM $< -o $@~ | |
121 | echo ${<:.c=.o}: $(shell perl -- getcomments.pl -p='depend:\s' -f='%' $<) >> $@~ | |
122 | mv $@~ $@ | |
123 | $(call PASS, SUCCESS) | |
124 | ||
125 | %.ld: %.c | |
126 | $(call TITLE, "Building $@") | |
127 | echo ${<:.c=.exe}: $(shell perl -- getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@ | |
128 | $(call PASS, SUCCESS) | |
129 | ||
130 | %.o: %.c | |
131 | $(call TITLE, "Building $@") | |
132 | $(CC) $(CFLAGS) $(INCLUDES) $(shell perl -- getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@ | |
133 | $(call PASS, SUCCESS) | |
134 | ||
135 | ||
136 | %.exe: %.o %.d | |
137 | $(call TITLE, "Building $@") | |
184be781 | 138 | $(CC) $(LDFLAGS) $< $(shell perl -- getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $(LDLIBS) -o $@ |
8512671a LM |
139 | $(call PASS, SUCCESS) |
140 | ||
141 | ## Phony | |
142 | ||
143 | .PHONY: all clean count depends gcovs purge tests | |
144 | ||
145 | ## Precious | |
146 | ||
147 | .PRECIOUS: %.d %.o |