new rule for profiling
authorLaurent Mazet <mazet@softndesign.org>
Mon, 16 Jan 2023 23:02:16 +0000 (00:02 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 16 Jan 2023 23:02:16 +0000 (00:02 +0100)
makefile

index c4b250ed69c7c8fbb1af26e2d41eaaa4fdd0c9bd..7080f6859ae4edbb89359fdfe7ac62f18697645b 100644 (file)
--- a/makefile
+++ b/makefile
@@ -11,8 +11,8 @@ OFLAGS  = -O4 -Os
 #OFLAGS += -malign-double
 CFLAGS += -W -Wall -Wextra -g
 CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
-CFLAGS += $(OFLAGS) $(INCLUDES) $(GCOV)
-LDFLAGS += -g $(GCOV)
+CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTION)
+LDFLAGS += -g $(OPTION)
 
 # Targets
 
@@ -50,7 +50,7 @@ count:
 clean:
        $(call TITLE, "Cleaning")
        touch clean
-       rm -f clean $(wildcard *.d *.ld *.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))
@@ -58,6 +58,9 @@ 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
@@ -70,7 +73,7 @@ valgrinds:
 wipe: purge
        $(call TITLE, "Wiping")
        touch wipe
-       rm -f wipe $(wildcard *.gcda *.gcno *.gcov)
+       rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.log)
        $(call PASS, SUCCESS)
 
 tests: all
@@ -83,13 +86,32 @@ include $(wildcard *.ld)
 
 gcov_%:
        $(MAKE) purge
-       GCOV="-fprofile-arcs -ftest-coverage -O0" $(MAKE)
+       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 $@")
 #      awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@