new rule: wipe
[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
b449884c 14CFLAGS += $(OFLAGS) $(INCLUDES) $(GCOVER)
ec15bdbc
LM
15LDFLAGS += -g
16
17# Targets
18
19ALLEXE =
20ALLEXE += calc
efdfb543 21#ALLEXE += skel
ec15bdbc
LM
22
23SHELL = bash
24
0b489a77 25#MAKE = mingw32-make
ec15bdbc
LM
26MAKEFLAGS += -s
27
28# Functions
29
30TITLE = echo -en "\033[0;1m$(strip $(1))\033[0;0m:\t"
31PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m"
32WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m"
33FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m"
34
35MKDIR = mkdir -p $(1) && chmod a+rx,go-w $(1)
36
37INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1) $(2) && chmod a+rX,go-w $(2)
38
39VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; }
40
41## Generic rules
42
43all: depends
44 $(MAKE) $(ALLEXE:%=%.exe)
45
ec15bdbc
LM
46count:
47 wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
48
49clean:
50 $(call TITLE, "Cleaning")
51 touch clean
52 rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_*)
53 $(call PASS, SUCCESS)
54
b449884c
LM
55depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE))
56
57gcovs:
58 $(MAKE) $(addprefix gcov_,$(ALLEXE))
59
ec15bdbc
LM
60purge: clean
61 $(call TITLE, "Purging")
62 touch purge
55cfeafa
LM
63 rm -f purge $(ALLEXE:%=%.exe)
64 $(call PASS, SUCCESS)
65
66wipe: purge
67 $(call TITLE, "wiping")
68 touch wipe
69 rm -f wipe $(wildcard *.gcda *.gcno)
ec15bdbc
LM
70 $(call PASS, SUCCESS)
71
0c2d2e4e 72tests: all
b449884c 73 $(MAKE) $(addprefix test_,$(ALLEXE))
0c2d2e4e 74
ec15bdbc
LM
75## Main rules
76
77include $(wildcard *.d)
78include $(wildcard *.ld)
79
b449884c
LM
80gcov_%:
81 make purge
82 CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="-fprofile-arcs -ftest-coverage" make
83 make test_$(@:gcov_%=%)
84 gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)`
85 touch gcov
86 rm -f gcov $(wildcard *.gcda *.gcno)
87 make purge
88
ec15bdbc
LM
89%.test: %.c
90 $(call TITLE, "Building $@")
91# awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@
92 ./getcomments.pl -p='test:\s' -f='%' $< > $@
93 $(call PASS, SUCCESS)
94
95test_%: %.test %.exe
96 IFS=$$'\n'; RC=0; \
97 for test in `cat $< | sed 's,${<:.test=.exe},./${<:.test=.exe},g'`; do \
98 echo "=== $$test ==="; \
99 eval $(VALGRIND) $$test; \
100 [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
101 || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
102 done; \
103 test "$$RC" -ne 1
104
105valgrind_%: %
106 VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \
107 export VALGRIND; \
108 $(MAKE) test_$<
109
110%.d: %.c
111 $(call TITLE, "Building $@")
112 $(CC) $(INCLUDES) -MM $< -o $@~
113 echo ${<:.c=.o}: $(shell ./getcomments.pl -p='depend:\s' -f='%' $<) >> $@~
114 mv $@~ $@
115 $(call PASS, SUCCESS)
116
117%.ld: %.c
118 $(call TITLE, "Building $@")
1968fc94 119 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
120 $(call PASS, SUCCESS)
121
122%.o: %.c
123 $(call TITLE, "Building $@")
124 $(CC) $(CFLAGS) $(INCLUDES) $(shell ./getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@
125 $(call PASS, SUCCESS)
126
127
128%.exe: %.o %.d
129 $(call TITLE, "Building $@")
130 $(CC) $(LDFLAGS) $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $< -o $@
131 $(call PASS, SUCCESS)
132
133## Phony
134
b449884c 135.PHONY: all clean count depends gcovs purge tests
ec15bdbc
LM
136
137## Precious
138
139.PRECIOUS: %.d %.o