clean some tests
[calc.git] / makefile
CommitLineData
ec15bdbc
LM
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
8a33b741
LM
14CFLAGS += $(OFLAGS) $(INCLUDES) $(GCOV)
15LDFLAGS += -g $(GCOV)
ec15bdbc
LM
16
17# Targets
18
19ALLEXE =
20ALLEXE += calc
efdfb543 21#ALLEXE += skel
ec15bdbc
LM
22
23SHELL = bash
24
0b489a77 25#MAKE = mingw32-make
ec15bdbc 26MAKEFLAGS += -s
8a33b741 27include $(wildcard .makefile)
ec15bdbc
LM
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
ec15bdbc
LM
47count:
48 wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
49
50clean:
51 $(call TITLE, "Cleaning")
52 touch clean
53 rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_*)
54 $(call PASS, SUCCESS)
55
b449884c
LM
56depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE))
57
58gcovs:
59 $(MAKE) $(addprefix gcov_,$(ALLEXE))
60
ec15bdbc
LM
61purge: clean
62 $(call TITLE, "Purging")
63 touch purge
55cfeafa
LM
64 rm -f purge $(ALLEXE:%=%.exe)
65 $(call PASS, SUCCESS)
66
031d7bba
LM
67valgrinds:
68 $(MAKE) $(addprefix valgrind_,$(ALLEXE))
69
55cfeafa 70wipe: purge
24857300 71 $(call TITLE, "Wiping")
55cfeafa 72 touch wipe
8a33b741 73 rm -f wipe $(wildcard *.gcda *.gcno *.gcov)
ec15bdbc
LM
74 $(call PASS, SUCCESS)
75
0c2d2e4e 76tests: all
b449884c 77 $(MAKE) $(addprefix test_,$(ALLEXE))
0c2d2e4e 78
ec15bdbc
LM
79## Main rules
80
81include $(wildcard *.d)
82include $(wildcard *.ld)
83
b449884c 84gcov_%:
24857300 85 $(MAKE) purge
8a33b741 86 GCOV="-fprofile-arcs -ftest-coverage -O0" $(MAKE)
24857300 87 $(MAKE) test_$(@:gcov_%=%)
b449884c
LM
88 gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)`
89 touch gcov
90 rm -f gcov $(wildcard *.gcda *.gcno)
24857300 91 $(MAKE) purge
b449884c 92
ec15bdbc
LM
93%.test: %.c
94 $(call TITLE, "Building $@")
95# awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@
96 ./getcomments.pl -p='test:\s' -f='%' $< > $@
97 $(call PASS, SUCCESS)
98
99test_%: %.test %.exe
100 IFS=$$'\n'; RC=0; \
c56b57d5 101 for test in `cat $< | sed 's,${<:.test=.exe},$(VALGRIND) ./${<:.test=.exe},g'`; do \
ec15bdbc 102 echo "=== $$test ==="; \
c56b57d5 103 eval $$test; \
ec15bdbc
LM
104 [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
105 || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
106 done; \
8a33b741 107 test "$$RC" -ne 1
ec15bdbc 108
031d7bba 109valgrind_%: %.exe
c56b57d5 110 VALGRIND="valgrind -v --leak-check=full --log-fd=3"; \
ec15bdbc 111 export VALGRIND; \
c56b57d5 112 $(MAKE) $(@:valgrind_%=test_%) 3>$@.log
ec15bdbc
LM
113
114%.d: %.c
115 $(call TITLE, "Building $@")
116 $(CC) $(INCLUDES) -MM $< -o $@~
117 echo ${<:.c=.o}: $(shell ./getcomments.pl -p='depend:\s' -f='%' $<) >> $@~
118 mv $@~ $@
119 $(call PASS, SUCCESS)
120
121%.ld: %.c
122 $(call TITLE, "Building $@")
1968fc94 123 echo ${<:.c=.exe}: $(shell ./getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@
ec15bdbc
LM
124 $(call PASS, SUCCESS)
125
126%.o: %.c
127 $(call TITLE, "Building $@")
128 $(CC) $(CFLAGS) $(INCLUDES) $(shell ./getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@
129 $(call PASS, SUCCESS)
130
131
132%.exe: %.o %.d
133 $(call TITLE, "Building $@")
680bf70a 134 $(CC) $(LDFLAGS) $< $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) -o $@
ec15bdbc
LM
135 $(call PASS, SUCCESS)
136
137## Phony
138
b449884c 139.PHONY: all clean count depends gcovs purge tests
ec15bdbc
LM
140
141## Precious
142
143.PRECIOUS: %.d %.o