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