diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2b7f22bad6..20130f8156 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: matrix: os: [ubuntu-24.04, ubuntu-24.04-arm] compiler: [ [clang++-19, clang-19, "clang-19 libclang-rt-19-dev"] ] - build: [ Debug, Release, DebugLibdeps ] + build: [ Debug, Release, DebugLibdeps, DebugCov ] include: - build: Debug cmake_build_type: Debug @@ -27,11 +27,17 @@ jobs: - build: DebugLibdeps cmake_build_type: Debug flags: -DPHASAR_DEBUG_LIBDEPS=ON -DBUILD_SHARED_LIBS=ON + - build: DebugCov + cmake_build_type: Debug + flags: -DCODE_COVERAGE=ON + extra_dependencies: llvm-19 # For coverage exclude: - os: ubuntu-24.04-arm build: Debug - os: ubuntu-24.04-arm build: DebugLibdeps + - os: ubuntu-24.04-arm + build: DebugCov continue-on-error: false steps: @@ -44,7 +50,7 @@ jobs: - name: Install Phasar Dependencies shell: bash run: | - ./utils/InstallAptDependencies.sh --noninteractive tzdata ${{ matrix.compiler[2] }} + ./utils/InstallAptDependencies.sh --noninteractive tzdata ${{ matrix.compiler[2] }} ${{ matrix.extra_dependencies }} - name: Building Phasar in ${{ matrix.build }} with ${{ matrix.compiler[0] }} env: @@ -77,3 +83,18 @@ jobs: cd ./examples/how-to cmake -S . -B build -Dphasar_ROOT="$PHASAR_ROOT_DIR/INSTALL" cmake --build ./build --target run_sample_programs + + - name: Check test coverage and generate HTML report + if: matrix.build == 'DebugCov' + shell: bash + run: | + cmake --build ./build --target ccov-all + + - name: Upload coverage HTML report artifact + if: matrix.build == 'DebugCov' + uses: actions/upload-artifact@v4 + with: + name: CoverageReport + path: ./build/ccov/all-merged/ + if-no-files-found: error + retention-days: 7 diff --git a/CMakeLists.txt b/CMakeLists.txt index 945e85aec6..51706a8293 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -268,11 +268,43 @@ find_package(Threads) set(CMAKE_CXX_CLANG_TIDY "") # Nlohmann JSON - include(add_nlohmann_json) add_nlohmann_json() add_json_schema_validator() +# Coverage +if (CODE_COVERAGE) + set(CODE_COVERAGE_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/code-coverage.cmake") + file(DOWNLOAD "https://raw.githubusercontent.com/StableCoder/cmake-scripts/refs/tags/25.08/code-coverage.cmake" "${CODE_COVERAGE_SCRIPT}" + EXPECTED_HASH SHA256=cad4dd2bdf944f77627687e0298debc8341bfdeffaf949937f319c7c4ff3fdaf + TLS_VERIFY ON + STATUS CODE_COVERAGE_SCRIPT_DOWNLOAD_STATUS + ) + message(STATUS "Download code-coverage.cmake: ${CODE_COVERAGE_SCRIPT_DOWNLOAD_STATUS}") + + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + string(REGEX MATCH "^[0-9]+" COMPILER_VERSION_MAJOR "${CMAKE_CXX_COMPILER_VERSION}") + message(STATUS "Clang major version: ${COMPILER_VERSION_MAJOR}") + set(LLVM_TOOL_VERSION_SUFFIX "-${COMPILER_VERSION_MAJOR}") + endif() + + # pre-populate these cmake variables as we require specific versions: + find_program(LLVM_COV_PATH "llvm-cov${LLVM_TOOL_VERSION_SUFFIX}" REQUIRED) + find_program(LLVM_PROFDATA_PATH "llvm-profdata${LLVM_TOOL_VERSION_SUFFIX}" REQUIRED) + message(STATUS "Found llvm-cov: ${LLVM_COV_PATH}") + message(STATUS "Found llvm-profdata: ${LLVM_PROFDATA_PATH}") + + include("${CODE_COVERAGE_SCRIPT}") + + find_program(cxxfilt "c++filt" QUIET) + if (cxxfilt) + set(DEMANGLER_OPTIONS LLVM_COV_HTML_OPTIONS "-Xdemangler=${cxxfilt}") + endif() + + set(COV_FILEPATH_FILTER "(usr/*|external/|unittests/)") + add_code_coverage_all_targets(EXCLUDE ${COV_FILEPATH_FILTER} ${DEMANGLER_OPTIONS}) +endif() + # Googletest if (NOT PHASAR_IN_TREE) if(PHASAR_BUILD_UNITTESTS AND NOT TARGET gtest) diff --git a/bootstrap.sh b/bootstrap.sh index 1972f88eb6..6ae97de5da 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -97,6 +97,7 @@ fi tmp_dir=$(mktemp -d "llvm-build.XXXXXXXX" --tmpdir) ./utils/install-llvm.sh "${NUM_THREADS}" "${tmp_dir}" "${LLVM_INSTALL_DIR}" ${LLVM_RELEASE} rm -rf "${tmp_dir}" + echo "dependencies successfully installed" # *Always* set the LLVM root to ensure the Phasar script uses the proper toolchain diff --git a/cmake/phasar_macros.cmake b/cmake/phasar_macros.cmake index 2bd952852c..b8632e1114 100644 --- a/cmake/phasar_macros.cmake +++ b/cmake/phasar_macros.cmake @@ -35,6 +35,10 @@ function(add_phasar_unittest test_name) ) set_tests_properties("${test}" PROPERTIES LABELS "all") set(CTEST_OUTPUT_ON_FAILURE ON) + + if (CODE_COVERAGE) + target_code_coverage(${test} AUTO ALL) + endif() endfunction() function(validate_binary_version result item) @@ -351,6 +355,10 @@ function(add_phasar_library name) endif() set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) + + if (CODE_COVERAGE) + target_code_coverage(${name} AUTO ALL) + endif() endfunction(add_phasar_library) macro(subdirlist result curdir)