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