# Default flags CC = gcc INCLUDES = -I../debug -D__MEMORY_ALLOCATION__ OFLAGS = -O4 -Os #OFLAGS = -O4 -ffast-math -finline-functions #OFLAGS = -O4 -finline-functions #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow #OFLAGS += -minline-all-stringops -fsingle-precision-constant #OFLAGS += -malign-double CFLAGS += -W -Wall -Wextra -g #CFLAGS += -std=c99 -D_XOPEN_SOURCE=500 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS) LDFLAGS += -g $(OPTIONS) ifeq ($(OS),Windows_NT) LDLIBS += -lws2_32 endif # Targets ALLEXE = ALLEXE += webserver #ALLEXE += skel SHELL = bash #MAKE = mingw32-make MAKEFLAGS += -s include $(wildcard .makefile) # Functions TITLE = echo -en "\033[0;1m$(strip $(1))\033[0;0m:\t" PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m" WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m" FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m" MKDIR = mkdir -p $(1) && chmod a+rx,go-w $(1) INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1) $(2) && chmod a+rX,go-w $(2) VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; } ## Generic rules all: depends $(MAKE) $(ALLEXE:%=%.exe) count: wc $(wildcard *.c *.h) $(MAKEFILE_LIST) clean: $(call TITLE, "Cleaning") touch clean rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_* gmon.out) $(call PASS, SUCCESS) depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE)) gcovs: $(MAKE) $(addprefix gcov_,$(ALLEXE)) gprofs: $(MAKE) $(addprefix gprof_,$(ALLEXE)) purge: clean $(call TITLE, "Purging") touch purge rm -f purge $(ALLEXE:%=%.exe) $(call PASS, SUCCESS) valgrinds: all $(MAKE) $(addprefix valgrind_,$(ALLEXE)) wipe: purge $(call TITLE, "Wiping") touch wipe rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.glog) $(call PASS, SUCCESS) tests: all $(MAKE) $(addprefix test_,$(ALLEXE)) ## Main rules include $(wildcard *.d) include $(wildcard *.ld) gcov_%: $(MAKE) purge OPTIONS="-coverage -O0" $(MAKE) $(MAKE) test_$(@:gcov_%=%) gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)` touch gcov rm -f gcov $(wildcard *.gcda *.gcno) $(MAKE) purge grep '#####' *.c.gcov || true gprof_%: $(MAKE) purge $(MAKE) depends OPTIONS="-pg" $(MAKE) ${@:gprof_%=%}.exe $(MAKE) ${@:gprof_%=%}.test IFS=$$'\n'; id=1; \ for test in `cat ${@:gprof_%=%}.test | sed 's,${@:gprof_%=%}.exe,./${@:gprof_%=%}.exe,g'`; do \ log=${@:gprof_%=%}.prof-$$id.glog; \ $(call TITLE, test: $$test); \ echo $$test > $$log; \ eval $$test >> $$log; \ [ $$? -eq 0 ] \ && echo -e "\033[1;32mSUCCESS\033[0;0m" \ || echo -e "\033[1;31mFAILED\033[0;0m"; \ [ -f gmon.out ] && { gprof ${@:gprof_%=%}.exe gmon.out >> $$log; rm gmon.out; }; \ let id++; \ done; $(MAKE) purge %.test: %.c $(call TITLE, "Building $@") # awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@ perl -- getcomments.pl -p='test:\s' -f='%' $< > $@ $(call PASS, SUCCESS) test_%: %.test %.exe IFS=$$'\n'; RC=0; \ for test in `cat $< | sed 's,${<:.test=.exe},$(VALGRIND) ./${<:.test=.exe},g'`; do \ echo "=== $$test ==="; \ eval $$test; \ [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \ || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \ done; \ test "$$RC" -ne 1 valgrind_%: %.exe VALGRIND="valgrind -v --leak-check=full --log-fd=3"; \ export VALGRIND; \ $(MAKE) $(@:valgrind_%=test_%) 3>$@.log %.d: %.c $(call TITLE, "Building $@") $(CC) $(INCLUDES) -MM $< -o $@~ echo ${<:.c=.o}: $(shell perl -- getcomments.pl -p='depend:\s' -f='%' $<) >> $@~ mv $@~ $@ $(call PASS, SUCCESS) %.ld: %.c $(call TITLE, "Building $@") echo ${<:.c=.exe}: $(shell perl -- getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@ $(call PASS, SUCCESS) %.o: %.c $(call TITLE, "Building $@") $(CC) $(CFLAGS) $(INCLUDES) $(shell perl -- getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@ $(call PASS, SUCCESS) %.exe: %.o %.d $(call TITLE, "Building $@") $(CC) $(LDFLAGS) $< $(shell perl -- getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $(LDLIBS) -o $@ $(call PASS, SUCCESS) ## Phony .PHONY: all clean count depends gcovs purge tests ## Precious .PRECIOUS: %.d %.o