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