Merge branch 'master' of https://secure.softndesign.org/git/compress
[compress.git] / makefile
index 15683ec6d21642a9051862afb8c42965fc65831f..7080f6859ae4edbb89359fdfe7ac62f18697645b 100644 (file)
--- a/makefile
+++ b/makefile
@@ -3,26 +3,28 @@
 CC = gcc
 
 INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
+OFLAGS  = -O4 -Os
 #OFLAGS  = -O4 -ffast-math -finline-functions
-OFLAGS  = -O4 -finline-functions
+#OFLAGS  = -O4 -finline-functions
 #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
-OFLAGS += -minline-all-stringops -fsingle-precision-constant
-OFLAGS += -malign-double
-CFLAGS += -Wall -Wextra -g
+#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)
-LDFLAGS += -g
+CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTION)
+LDFLAGS += -g $(OPTION)
 
 # Targets
 
 ALLEXE  =
 ALLEXE += compress
-ALLEXE += skel
+#ALLEXE += skel
 
 SHELL = bash
 
-MAKE = mingw32-make
+#MAKE = mingw32-make
 MAKEFLAGS += -s
+include $(wildcard .makefile)
 
 # Functions
 
@@ -42,29 +44,73 @@ VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, F
 all: depends
        $(MAKE) $(ALLEXE:%=%.exe)
 
-alltests: all
-       $(MAKE) $(addprefix test_,$(ALLEXE:%.exe=%))
-
-depends: $(patsubst %.c, %.d, $(wildcard *.c))
-
 count:
        wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
 
 clean:
        $(call TITLE, "Cleaning")
        touch clean
-       rm -f clean $(wildcard *.d *.log *.o *.test *~ .exec_*)
+       rm -f clean $(wildcard *.d *.ld *.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) $(shell [ -f .targets ] && { cat .targets | sort | uniq; echo .targets; })
+       rm -f purge $(ALLEXE:%=%.exe)
+       $(call PASS, SUCCESS)
+
+valgrinds:
+       $(MAKE) $(addprefix valgrind_,$(ALLEXE))
+
+wipe: purge
+       $(call TITLE, "Wiping")
+       touch wipe
+       rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.log)
        $(call PASS, SUCCESS)
 
+tests: all
+       $(MAKE) $(addprefix test_,$(ALLEXE))
+
 ## Main rules
 
 include $(wildcard *.d)
+include $(wildcard *.ld)
+
+gcov_%:
+       $(MAKE) purge
+       OPTION="-fprofile-arcs -ftest-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
+
+gprof_%:
+       $(MAKE) purge
+       $(MAKE) depends
+       OPTION="-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.log; \
+               $(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 $@")
@@ -80,12 +126,12 @@ test_%: %.test %.exe
          [ $$? -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 
+       test "$$RC" -ne 1
 
-valgrind_%: %
+valgrind_%: %.exe
        VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \
        export VALGRIND; \
-       $(MAKE) test_$<
+       $(MAKE) $(@:valgrind_%=test_%)
 
 %.d: %.c
        $(call TITLE, "Building $@")
@@ -94,6 +140,11 @@ valgrind_%: %
        mv $@~ $@
        $(call PASS, SUCCESS)
 
+%.ld: %.c
+       $(call TITLE, "Building $@")
+       echo ${<:.c=.exe}: $(shell ./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 ./getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@
@@ -102,14 +153,12 @@ valgrind_%: %
 
 %.exe: %.o %.d
        $(call TITLE, "Building $@")
-       $(CC) $(LDFLAGS) $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $< -o ${<:.o=}
-       echo ${<:.o=} >> .targets
-       #ln -sf ${<:.o=} $@
+       $(CC) $(LDFLAGS) $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $< -o $@
        $(call PASS, SUCCESS)
 
 ## Phony
 
-.PHONY: clean count purge
+.PHONY: all clean count depends gcovs purge tests
 
 ## Precious