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