diff --git a/.github/workflows/CodeCov.yml b/.github/workflows/CodeCov.yml new file mode 100644 index 000000000..1ad663f08 --- /dev/null +++ b/.github/workflows/CodeCov.yml @@ -0,0 +1,67 @@ +name: CodeCoverage Report + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Debug + +jobs: + build_and_generate_report: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + with: + submodules: true + token: ${{ secrets.TOKEN }} + fetch-depth: 0 + + - name: Update apt-get + run: sudo apt-get update + + # LCOV + - name: Install lcov + run: sudo apt-get install lcov + + # CODECOV + - name: Install codecov + shell: bash + run: sudo pip install codecov + + - name: Setup cache + uses: mikehardy/buildcache-action@v1 + if: matrix.config.os != 'windows-latest' + with: + cache_key: codecov + + - name: Configure CMake + env: + CC: gcc-10 + CXX: g++-10 + run: cmake -B ${{github.workspace}}/build -DAUTOTESTS=1 -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_C_COMPILER_LAUNCHER=buildcache -DCMAKE_CXX_COMPILER_LAUNCHER=buildcache -DCODE_COVERAGE=1 + + - name: Build + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --parallel 4 + + - name: Test + working-directory: ${{github.workspace}}/build + run: ctest -C ${{env.BUILD_TYPE}} + + # CODE COVERAGE + - name: Code coverage - Capture coverage info + working-directory: ${{runner.workspace}} + run: lcov --directory ${{github.workspace}} --capture --output-file coverage.info + - name: Code coverage - Filter out system, external, and unit test source files + working-directory: ${{runner.workspace}} + run: lcov --remove coverage.info '/usr/*' "${HOME}"'/.cache/*' '*/tests/*' '*/submodules/*' --output-file coverage.info + - name: Code coverage - Output coverage data for debugging + working-directory: ${{runner.workspace}} + run: lcov --list coverage.info + - name: Code coverage - Upload to CodeCov + working-directory: ${{runner.workspace}} + run: bash <(curl -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports" \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index b5d9a9af2..beb4c5e64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,16 +38,23 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(TEST_RESULTS_DIR ${CMAKE_BINARY_DIR}/test_results) set(BUILD_TESTS ON CACHE BOOL "Enable unit tests building" FORCE) -# if (AUTOTESTS AND MSVC) -# # PDB debug information is not supported by buildcache. -# # Store debug info in the object files. -# string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") -# string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") -# string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") -# string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") -# string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}") -# string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") -# endif() +# Code Coverage Configuration +add_library(coverage_config INTERFACE) + +option(CODE_COVERAGE "Enable coverage reporting" OFF) +if(CODE_COVERAGE) + # Add required flags (GCC & LLVM/Clang) + target_compile_options(coverage_config INTERFACE + -O0 # no optimization + -g # generate debug info + --coverage # sets all required flags + ) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13) + target_link_options(coverage_config INTERFACE --coverage) + else() + target_link_libraries(coverage_config INTERFACE --coverage) + endif() +endif(CODE_COVERAGE) # ================== TESTS ================== diff --git a/src/include/CMakeLists.txt b/src/include/CMakeLists.txt index 3d203ee28..6da6bfab1 100644 --- a/src/include/CMakeLists.txt +++ b/src/include/CMakeLists.txt @@ -26,6 +26,7 @@ file(GLOB_RECURSE FILES "*.h") add_library(rpp INTERFACE ${FILES}) target_sources(rpp INTERFACE ${FILES}) target_include_directories(rpp INTERFACE .) +target_link_libraries(rpp INTERFACE coverage_config) foreach(FILE ${FILES}) get_filename_component(PARENT_DIR "${FILE}" PATH) diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 7636854b4..1001f8297 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -37,7 +37,7 @@ add_executable(${TARGET} copy_count_tracker.h ) -target_link_libraries(${TARGET} PRIVATE rpp Catch2WithMain) +target_link_libraries(${TARGET} PRIVATE rpp Catch2WithMain coverage_config) set_target_properties(${TARGET} PROPERTIES FOLDER Tests)