From 48deb72574416f431da3e805c52dbc76101e327e Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 7 Jul 2023 21:04:08 +0200 Subject: support code coverage report generation --- .github/workflows/ci.yml | 21 ++++++++++++++++++++- Makefile | 18 ++++++++++++++++++ src/CMakeLists.txt | 6 ++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 414e6fc..e207ccf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,8 +46,27 @@ jobs: - name: Run Valgrind tests run: make test/valgrind + coverage: + runs-on: ubuntu-latest + name: Coverage report + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Install dependencies + run: | + sudo apt-get update + sudo DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends gcovr libgit2-dev libsqlite3-dev python3-pytest valgrind + - name: Generate report + run: make coverage + - name: Upload report + uses: actions/upload-artifact@v3 + with: + name: coverage + path: ./build/coverage/ + if-no-files-found: error + publish: - needs: [lint, build] + needs: [lint, build, coverage] runs-on: ubuntu-latest name: 'Docker: publish' steps: diff --git a/Makefile b/Makefile index 8a4c755..7b5a8b9 100644 --- a/Makefile +++ b/Makefile @@ -4,18 +4,21 @@ src_dir := $(abspath .) build_dir := $(src_dir)/build cmake_dir := $(build_dir)/cmake install_dir := $(build_dir)/install +coverage_dir := $(build_dir)/coverage COMPILER ?= clang CONFIGURATION ?= Debug DEFAULT_HOST ?= 127.0.0.1 DEFAULT_PORT ?= 5556 INSTALL_PREFIX ?= $(install_dir) +COVERAGE ?= $(eval $(call noexpand,COMPILER)) $(eval $(call noexpand,CONFIGURATION)) $(eval $(call noexpand,DEFAULT_HOST)) $(eval $(call noexpand,DEFAULT_PORT)) $(eval $(call noexpand,INSTALL_PREFIX)) +$(eval $(call noexpand,COVERAGE)) .PHONY: all all: build @@ -37,6 +40,7 @@ build: -D 'CMAKE_INSTALL_PREFIX=$(call escape,$(INSTALL_PREFIX))' \ -D 'DEFAULT_HOST=$(call escape,$(DEFAULT_HOST))' \ -D 'DEFAULT_PORT=$(call escape,$(DEFAULT_PORT))' \ + -D 'COVERAGE=$(call escape,$(COVERAGE))' \ -S '$(call escape,$(src_dir))' \ -B '$(call escape,$(cmake_dir))' cmake --build '$(call escape,$(cmake_dir))' -- -j @@ -45,6 +49,20 @@ build: release: CONFIGURATION := Release release: build +.PHONY: coverage +coverage: COMPILER := gcc +coverage: CONFIGURATION := Debug +coverage: COVERAGE := 1 +coverage: clean build test + @echo ----------------------------------------------------------------- + @echo Generating code coverage report + @echo ----------------------------------------------------------------- + @mkdir -p -- '$(call escape,$(coverage_dir))' + gcovr --html-details '$(call escape,$(coverage_dir))/index.html' --print-summary +ifndef CI + xdg-open '$(call escape,$(coverage_dir))/index.html' &> /dev/null +endif + .PHONY: install install: build cmake --install '$(call escape,$(cmake_dir))' diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b9cddf..c26fe31 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,12 @@ add_compile_options(-std=c17 -Wpedantic -Wall -Wextra $<$>:- # any features macros defined at all, and prepare to be amazed! add_compile_definitions(_GNU_SOURCE) +option(COVERAGE "Enable line coverage analysis") +if(COVERAGE) + add_compile_options(--coverage -fprofile-update=atomic) + add_link_options(--coverage) +endif() + # Valgrind on Ubuntu Focal doesn't like DWARF version 5 debug info generated # by Clang 14. if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") -- cgit v1.2.3