cleaning
[hexdump.git] / makefile
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) $(OPTIONS)
15 LDFLAGS += -g $(OPTIONS)
16
17 ifeq ($(OS),Windows_NT)
18 #LDLIBS += -lws2_32
19 endif
20
21 # Targets
22
23 ALLEXE =
24 ALLEXE += hexdump
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_* gmon.out)
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 gprofs:
65 $(MAKE) $(addprefix gprof_,$(ALLEXE))
66
67 purge: clean
68 $(call TITLE, "Purging")
69 touch purge
70 rm -f purge $(ALLEXE:%=%.exe)
71 $(call PASS, SUCCESS)
72
73 valgrinds: all
74 $(MAKE) $(addprefix valgrind_,$(ALLEXE))
75
76 wipe: purge
77 $(call TITLE, "Wiping")
78 touch wipe
79 rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.glog)
80 $(call PASS, SUCCESS)
81
82 tests: all
83 $(MAKE) $(addprefix test_,$(ALLEXE))
84
85 ## Main rules
86
87 include $(wildcard *.d)
88 include $(wildcard *.ld)
89
90 gcov_%:
91 $(MAKE) purge
92 OPTIONS="-coverage -O0" $(MAKE)
93 $(MAKE) test_$(@:gcov_%=%)
94 gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)`
95 touch gcov
96 rm -f gcov $(wildcard *.gcda *.gcno)
97 $(MAKE) purge
98 grep '#####' *.c.gcov || true
99
100 gprof_%:
101 $(MAKE) purge
102 $(MAKE) depends
103 OPTIONS="-pg" $(MAKE) ${@:gprof_%=%}.exe
104 $(MAKE) ${@:gprof_%=%}.test
105 IFS=$$'\n'; id=1; \
106 for test in `cat ${@:gprof_%=%}.test | sed 's,${@:gprof_%=%}.exe,./${@:gprof_%=%}.exe,g'`; do \
107 log=${@:gprof_%=%}.prof-$$id.glog; \
108 $(call TITLE, test: $$test); \
109 echo $$test > $$log; \
110 eval $$test >> $$log; \
111 [ $$? -eq 0 ] \
112 && echo -e "\033[1;32mSUCCESS\033[0;0m" \
113 || echo -e "\033[1;31mFAILED\033[0;0m"; \
114 [ -f gmon.out ] && { gprof ${@:gprof_%=%}.exe gmon.out >> $$log; rm gmon.out; }; \
115 let id++; \
116 done;
117 $(MAKE) purge
118
119 %.test: %.c
120 $(call TITLE, "Building $@")
121 # awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@
122 perl -- getcomments.pl -p='test:\s' -f='%' $< > $@
123 $(call PASS, SUCCESS)
124
125 test_%: %.test %.exe
126 IFS=$$'\n'; RC=0; \
127 for test in `cat $< | sed 's,${<:.test=.exe},$(VALGRIND) ./${<:.test=.exe},g'`; do \
128 echo "=== $$test ==="; \
129 eval $$test; \
130 [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
131 || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
132 done; \
133 test "$$RC" -ne 1
134
135 valgrind_%: %.exe
136 VALGRIND="valgrind -v --leak-check=full --log-fd=3"; \
137 export VALGRIND; \
138 $(MAKE) $(@:valgrind_%=test_%) 3>$@.log
139
140 %.d: %.c
141 $(call TITLE, "Building $@")
142 $(CC) $(INCLUDES) -MM $< -o $@~
143 echo ${<:.c=.o}: $(shell perl -- getcomments.pl -p='depend:\s' -f='%' $<) >> $@~
144 mv $@~ $@
145 $(call PASS, SUCCESS)
146
147 %.ld: %.c
148 $(call TITLE, "Building $@")
149 echo ${<:.c=.exe}: $(shell perl -- getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@
150 $(call PASS, SUCCESS)
151
152 %.o: %.c
153 $(call TITLE, "Building $@")
154 $(CC) $(CFLAGS) $(INCLUDES) $(shell perl -- getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@
155 $(call PASS, SUCCESS)
156
157
158 %.exe: %.o %.d
159 $(call TITLE, "Building $@")
160 $(CC) $(LDFLAGS) $< $(shell perl -- getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $(LDLIBS) -o $@
161 $(call PASS, SUCCESS)
162
163 ## Phony
164
165 .PHONY: all clean count depends gcovs purge tests
166
167 ## Precious
168
169 .PRECIOUS: %.d %.o