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