| 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) $(GCOV) |
| 15 | LDFLAGS += -g $(GCOV) |
| 16 | |
| 17 | # Targets |
| 18 | |
| 19 | ALLEXE = |
| 20 | ALLEXE += calc |
| 21 | #ALLEXE += skel |
| 22 | |
| 23 | SHELL = bash |
| 24 | |
| 25 | #MAKE = mingw32-make |
| 26 | MAKEFLAGS += -s |
| 27 | include $(wildcard .makefile) |
| 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 | count: |
| 48 | wc $(wildcard *.c *.h) $(MAKEFILE_LIST) |
| 49 | |
| 50 | clean: |
| 51 | $(call TITLE, "Cleaning") |
| 52 | touch clean |
| 53 | rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_*) |
| 54 | $(call PASS, SUCCESS) |
| 55 | |
| 56 | depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE)) |
| 57 | |
| 58 | gcovs: |
| 59 | $(MAKE) $(addprefix gcov_,$(ALLEXE)) |
| 60 | |
| 61 | purge: clean |
| 62 | $(call TITLE, "Purging") |
| 63 | touch purge |
| 64 | rm -f purge $(ALLEXE:%=%.exe) |
| 65 | $(call PASS, SUCCESS) |
| 66 | |
| 67 | valgrinds: |
| 68 | $(MAKE) $(addprefix valgrind_,$(ALLEXE)) |
| 69 | |
| 70 | wipe: purge |
| 71 | $(call TITLE, "Wiping") |
| 72 | touch wipe |
| 73 | rm -f wipe $(wildcard *.gcda *.gcno *.gcov) |
| 74 | $(call PASS, SUCCESS) |
| 75 | |
| 76 | tests: all |
| 77 | $(MAKE) $(addprefix test_,$(ALLEXE)) |
| 78 | |
| 79 | ## Main rules |
| 80 | |
| 81 | include $(wildcard *.d) |
| 82 | include $(wildcard *.ld) |
| 83 | |
| 84 | gcov_%: |
| 85 | $(MAKE) purge |
| 86 | GCOV="-fprofile-arcs -ftest-coverage -O0" $(MAKE) |
| 87 | $(MAKE) test_$(@:gcov_%=%) |
| 88 | gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)` |
| 89 | touch gcov |
| 90 | rm -f gcov $(wildcard *.gcda *.gcno) |
| 91 | $(MAKE) purge |
| 92 | |
| 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 | |
| 99 | test_%: %.test %.exe |
| 100 | IFS=$$'\n'; RC=0; \ |
| 101 | for test in `cat $< | sed 's,${<:.test=.exe},./${<:.test=.exe},g'`; do \ |
| 102 | echo "=== $$test ==="; \ |
| 103 | eval $(VALGRIND) $$test; \ |
| 104 | [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \ |
| 105 | || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \ |
| 106 | done; \ |
| 107 | test "$$RC" -ne 1 |
| 108 | |
| 109 | valgrind_%: %.exe |
| 110 | VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \ |
| 111 | export VALGRIND; \ |
| 112 | $(MAKE) $(@:valgrind_%=test_%) |
| 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 $@") |
| 123 | echo ${<:.c=.exe}: $(shell ./getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@ |
| 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 $@") |
| 134 | $(CC) $(LDFLAGS) $< $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) -o $@ |
| 135 | $(call PASS, SUCCESS) |
| 136 | |
| 137 | ## Phony |
| 138 | |
| 139 | .PHONY: all clean count depends gcovs purge tests |
| 140 | |
| 141 | ## Precious |
| 142 | |
| 143 | .PRECIOUS: %.d %.o |