5b03e86e77438a3ffbef88e4892a8b01339774ba
[compress.git] / makefile
1 # Default flags
2
3 CC = gcc
4
5 INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
6 #OFLAGS = -O4 -ffast-math -finline-functions
7 OFLAGS = -O4 -finline-functions
8 #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
9 OFLAGS += -minline-all-stringops -fsingle-precision-constant
10 OFLAGS += -malign-double
11 CFLAGS += -Wall -Wextra -g
12 CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
13 CFLAGS += $(OFLAGS) $(INCLUDES)
14 LDFLAGS += -g
15
16 # Targets
17
18 ALLEXE =
19 ALLEXE += compress
20 ALLEXE += skel
21
22 SHELL = bash
23
24 MAKE = mingw32-make
25 MAKEFLAGS += -s
26
27 # Functions
28
29 TITLE = echo -en "\033[0;1m$(strip $(1))\033[0;0m:\t"
30 PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m"
31 WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m"
32 FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m"
33
34 MKDIR = mkdir -p $(1) && chmod a+rx,go-w $(1)
35
36 INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1) $(2) && chmod a+rX,go-w $(2)
37
38 VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; }
39
40 ## Generic rules
41
42 all: depends
43 $(MAKE) $(ALLEXE:%=%.exe)
44
45 alltests: all
46 $(MAKE) $(addprefix test_,$(ALLEXE:%.exe=%))
47
48 depends: $(patsubst %.c, %.d, $(wildcard *.c))
49
50 count:
51 wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
52
53 clean:
54 $(call TITLE, "Cleaning")
55 touch clean
56 rm -f clean $(wildcard *.d *.log *.o *.test *~ .exec_*)
57 $(call PASS, SUCCESS)
58
59 purge: clean
60 $(call TITLE, "Purging")
61 touch purge
62 rm -f purge $(ALLEXE) $(shell [ -f .targets ] && { cat .targets | sort | uniq; echo .targets; })
63 $(call PASS, SUCCESS)
64
65 ## Main rules
66
67 include $(wildcard *.d)
68
69 %.test: %.c
70 $(call TITLE, "Building $@")
71 # awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@
72 ./getcomments.pl -p='test:\s' -f='%' $< > $@
73 $(call PASS, SUCCESS)
74
75 test_%: %.test %.exe
76 PATH=$$PATH:.; \
77 IFS=$$'\n'; \
78 LOGFILE=${<:.test=.log}; \
79 rm -f $$LOGFILE; \
80 RC=0; \
81 for test in `cat $<`; do \
82 echo "=== $$test ===" | tee -a $$LOGFILE; \
83 ( eval $(VALGRIND) $$test ) 2>&1 | tee -a $$LOGFILE; \
84 [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
85 || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
86 done; \
87 test "$$RC" -ne 1
88
89 valgrind_%: %
90 VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \
91 export VALGRIND; \
92 $(MAKE) test_$<
93
94 %.d: %.c
95 $(call TITLE, "Building $@")
96 $(CC) $(INCLUDES) -MM $< -o $@~
97 echo ${<:.c=.o}: $(shell ./getcomments.pl -p='depend:\s' -f='%' $<) >> $@~
98 mv $@~ $@
99 $(call PASS, SUCCESS)
100
101 %.o: %.c
102 $(call TITLE, "Building $@")
103 $(CC) $(CFLAGS) $(INCLUDES) $(shell ./getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@
104 $(call PASS, SUCCESS)
105
106
107 %.exe: %.o %.d
108 $(call TITLE, "Building $@")
109 $(CC) $(LDFLAGS) $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $< -o ${<:.o=}
110 echo ${<:.o=} >> .targets
111 #ln -sf ${<:.o=} $@
112 $(call PASS, SUCCESS)
113
114 ## Phony
115
116 .PHONY: clean count purge
117
118 ## Precious
119
120 .PRECIOUS: %.d %.o