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