From 4adb14f5ed0c5322ded3cd94e45485b8f12557fa Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 8 Nov 2016 12:12:27 +0100 Subject: [PATCH] add "cover" build target for code coverage statistics/analysis --- .gitignore | 1 + Makefile | 132 +++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 99 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index ea7759b..bfe6ef4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.swo *.trace blank +blank.cover blank.debug blank.profile blank.test diff --git a/Makefile b/Makefile index 5b1ea4b..55b93ba 100644 --- a/Makefile +++ b/Makefile @@ -16,18 +16,42 @@ CXXFLAGS += -Wall LDXXFLAGS ?= LDXXFLAGS += $(PKGLIBS) +# source +SOURCE_DIR := src +TEST_SRC_DIR := tst + +# build configurations +# cover: +# coverage reporting +# for use with gcov +# debug: +# unoptimized and maximally annotated +# for use with gdb +# profile: +# somewhat optimized and maximally annotated +# for use with valgrind +# release: +# optimized, without debugging instructions and minimally +# annotated (mainly for stack traces) +# for use with people +# tests: +# same flags as release, but with main replaced by cppunit +# test runner and tests (from tst dir) built in + +COVER_FLAGS = -g -O0 --coverage -I$(SOURCE_DIR) $(TESTFLAGS) DEBUG_FLAGS = -g3 -O0 PROFILE_FLAGS = -DNDEBUG -O1 -g3 -DBLANK_PROFILING RELEASE_FLAGS = -DNDEBUG -O2 -g1 -TEST_FLAGS = -g -O2 -I./src $(TESTFLAGS) +TEST_FLAGS = -g -O2 -I$(SOURCE_DIR) $(TESTFLAGS) -SOURCE_DIR := src -TEST_SRC_DIR := tst +# destination +COVER_DIR := build/cover DEBUG_DIR := build/debug PROFILE_DIR := build/profile RELEASE_DIR := build/release TEST_DIR := build/test -DIR := $(RELEASE_DIR) $(DEBUG_DIR) $(PROFILE_DIR) $(TEST_DIR) build + +DIR := $(RELEASE_DIR) $(COVER_DIR) $(DEBUG_DIR) $(PROFILE_DIR) $(TEST_DIR) build ASSET_DIR := assets ASSET_DEP := $(ASSET_DIR)/.git @@ -36,24 +60,33 @@ LIB_SRC := $(wildcard $(SOURCE_DIR)/*/*.cpp) BIN_SRC := $(wildcard $(SOURCE_DIR)/*.cpp) SRC := $(LIB_SRC) $(BIN_SRC) TEST_SRC := $(wildcard $(TEST_SRC_DIR)/*.cpp) $(wildcard $(TEST_SRC_DIR)/*/*.cpp) -RELEASE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(SRC)) -RELEASE_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(LIB_SRC)) + +COVER_OBJ := $(patsubst $(TEST_SRC_DIR)/%.cpp, $(COVER_DIR)/%.o, $(TEST_SRC)) $(patsubst $(SOURCE_DIR)/%.cpp, $(COVER_DIR)/src/%.o, $(LIB_SRC)) +COVER_DEP := $(COVER_OBJ:.o=.d) +COVER_BIN := blank.cover + DEBUG_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(DEBUG_DIR)/%.o, $(SRC)) DEBUG_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(DEBUG_DIR)/%.o, $(LIB_SRC)) +DEBUG_DEP := $(DEBUG_OBJ:.o=.d) +DEBUG_BIN := blank.debug + PROFILE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(PROFILE_DIR)/%.o, $(SRC)) PROFILE_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(PROFILE_DIR)/%.o, $(LIB_SRC)) -TEST_OBJ := $(patsubst $(TEST_SRC_DIR)/%.cpp, $(TEST_DIR)/%.o, $(TEST_SRC)) $(patsubst $(SOURCE_DIR)/%.cpp, $(TEST_DIR)/src/%.o, $(LIB_SRC)) -RELEASE_DEP := $(RELEASE_OBJ:.o=.d) -DEBUG_DEP := $(DEBUG_OBJ:.o=.d) PROFILE_DEP := $(PROFILE_OBJ:.o=.d) -TEST_DEP := $(TEST_OBJ:.o=.d) -RELEASE_BIN := blank -DEBUG_BIN := blank.debug PROFILE_BIN := blank.profile generate.profile + +RELEASE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(SRC)) +RELEASE_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(LIB_SRC)) +RELEASE_DEP := $(RELEASE_OBJ:.o=.d) +RELEASE_BIN := blank + +TEST_OBJ := $(patsubst $(TEST_SRC_DIR)/%.cpp, $(TEST_DIR)/%.o, $(TEST_SRC)) $(patsubst $(SOURCE_DIR)/%.cpp, $(TEST_DIR)/src/%.o, $(LIB_SRC)) +TEST_DEP := $(TEST_OBJ:.o=.d) TEST_BIN := blank.test -OBJ := $(RELEASE_OBJ) $(DEBUG_OBJ) $(PROFILE_OBJ) $(TEST_OBJ) -DEP := $(RELEASE_DEP) $(DEBUG_DEP) $(PROFILE_DEP) $(TEST_DEP) -BIN := $(RELEASE_BIN) $(DEBUG_BIN) $(PROFILE_BIN) $(TEST_BIN) + +OBJ := $(COVER_OBJ) $(DEBUG_OBJ) $(PROFILE_OBJ) $(RELEASE_OBJ) $(TEST_OBJ) +DEP := $(COVER_DEP) $(DEBUG_DEP) $(PROFILE_DEP) $(RELEASE_DEP) $(TEST_DEP) +BIN := $(COVER_BIN) $(DEBUG_BIN) $(PROFILE_BIN) $(RELEASE_BIN) $(TEST_BIN) release: $(RELEASE_BIN) @@ -75,6 +108,8 @@ info: all: $(BIN) +cover: $(COVER_BIN) + debug: $(DEBUG_BIN) profile: $(PROFILE_BIN) @@ -105,7 +140,16 @@ callgrind: $(ASSET_DEP) blank.profile ./blank.profile -n 256 -t 16 --no-keyboard --no-mouse -d --no-vsync --save-path saves/ test: blank.test - ./blank.test + @echo run: blank.test + @./blank.test + +coverage: blank.cover + @echo run: blank.cover + @./blank.cover + +codecov: coverage + @echo run: codecov.io + @bash -c 'bash <(curl -s https://codecov.io/bash) -Z' clean: rm -f $(OBJ) @@ -116,45 +160,59 @@ distclean: clean rm -f $(BIN) cachegrind.out.* callgrind.out.* rm -Rf build client-saves saves -.PHONY: all release debug profile tests run gdb cachegrind callgrind test clean distclean +.PHONY: all release cover debug profile tests run gdb cachegrind callgrind test coverage codecov clean distclean -include $(DEP) -$(RELEASE_BIN): %: $(RELEASE_DIR)/%.o $(RELEASE_LIB_OBJ) + +$(COVER_BIN): $(COVER_OBJ) @echo link: $@ - @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(RELEASE_FLAGS) $^ + @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(TESTLIBS) $(COVER_FLAGS) $^ + +$(COVER_DIR)/%.o: $(TEST_SRC_DIR)/%.cpp | $(COVER_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(COVER_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + +$(COVER_DIR)/src/%.o: $(SOURCE_DIR)/%.cpp | $(COVER_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(COVER_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + $(DEBUG_BIN): %.debug: $(DEBUG_DIR)/%.o $(DEBUG_LIB_OBJ) @echo link: $@ @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(DEBUG_FLAGS) $^ +$(DEBUG_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(DEBUG_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(DEBUG_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + + $(PROFILE_BIN): %.profile: $(PROFILE_DIR)/%.o $(PROFILE_LIB_OBJ) @echo link: $@ @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(PROFILE_FLAGS) $^ -$(TEST_BIN): $(TEST_OBJ) - @echo link: $@ - @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(TESTLIBS) $(TEST_FLAGS) $^ +$(PROFILE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(PROFILE_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(PROFILE_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< -$(ASSET_DEP): .git/$(shell git symbolic-ref HEAD 2>/dev/null || echo .git/HEAD) - @echo fetch: assets - @git submodule update --init >/dev/null - @touch $@ + +$(RELEASE_BIN): %: $(RELEASE_DIR)/%.o $(RELEASE_LIB_OBJ) + @echo link: $@ + @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(RELEASE_FLAGS) $^ $(RELEASE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(RELEASE_DIR) @mkdir -p "$(@D)" @echo compile: $@ @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(RELEASE_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< -$(DEBUG_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(DEBUG_DIR) - @mkdir -p "$(@D)" - @echo compile: $@ - @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(DEBUG_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< -$(PROFILE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(PROFILE_DIR) - @mkdir -p "$(@D)" - @echo compile: $@ - @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(PROFILE_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< +$(TEST_BIN): $(TEST_OBJ) + @echo link: $@ + @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(TESTLIBS) $(TEST_FLAGS) $^ $(TEST_DIR)/%.o: $(TEST_SRC_DIR)/%.cpp | $(TEST_DIR) @mkdir -p "$(@D)" @@ -166,5 +224,11 @@ $(TEST_DIR)/src/%.o: $(SOURCE_DIR)/%.cpp | $(TEST_DIR) @echo compile: $@ @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(TEST_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + +$(ASSET_DEP): .git/$(shell git symbolic-ref HEAD 2>/dev/null || echo .git/HEAD) + @echo fetch: assets + @git submodule update --init >/dev/null + @touch $@ + $(DIR): @mkdir -p "$@" -- 2.39.2