Skip to content

Commit

Permalink
Merge branch 'coverage-refactor' into 'master'
Browse files Browse the repository at this point in the history
[cmake] Refactored coverage.

See merge request ogs/ogs!4776
  • Loading branch information
endJunction committed Oct 27, 2023
2 parents c6720e6 + 4d0e581 commit 8db8e6b
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 151 deletions.
4 changes: 2 additions & 2 deletions Applications/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ add_subdirectory(ApplicationsLib)
add_subdirectory(DataHolderLib)
add_subdirectory(FileIO)

if(OGS_BUILD_UTILS AND NOT _IS_SUBPROJECT)
if(OGS_BUILD_UTILS AND PROJECT_IS_TOP_LEVEL)
add_subdirectory(Utils)
elseif(OGS_BUILD_GUI)
add_subdirectory(Utils/OGSFileConverter)
endif() # OGS_BUILD_UTILS AND NOT _IS_SUBPROJECT
endif()

if(OGS_BUILD_GUI)
add_subdirectory(DataExplorer)
Expand Down
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ if(OGS_USE_CONAN AND OGS_USE_NETCDF)
endif()
include(DocumentationSetup)
include(test/Test)
if(OGS_COVERAGE AND NOT _IS_SUBPROJECT)
include(Coverage)
endif()
include(Coverage)
include(CppCheck)

# ---- More Options ----
Expand Down Expand Up @@ -230,7 +228,7 @@ if(OGS_BUILD_CLI OR OGS_BUILD_UTILS OR OGS_BUILD_TESTING)
add_subdirectory(ProcessLib)
add_subdirectory(Tests/xdmfdiff)
endif()
if(OGS_BUILD_TESTING AND NOT _IS_SUBPROJECT)
if(OGS_BUILD_TESTING AND PROJECT_IS_TOP_LEVEL)
add_subdirectory(Tests)
endif()

Expand Down
1 change: 0 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@
"debug"
],
"cacheVariables": {
"CODE_COVERAGE_VERBOSE": "ON",
"OGS_COVERAGE": "ON",
"OGS_CTEST_MAX_RUNTIME": "5",
"OGS_USE_MFRONT": "ON"
Expand Down
2 changes: 1 addition & 1 deletion ThirdParty/collection
Submodule collection updated from 5949ed to b24633
27 changes: 8 additions & 19 deletions scripts/ci/jobs/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,28 @@ code coverage:
stage: check
extends:
- .template-build-linux
tags: [shell, envinf]
tags: [shell, envinf23]
needs: [meta]
variables:
CMAKE_PRESET: coverage
TARGETS: "testrunner_coverage ctest_coverage"
# default python on arch is 3.10; there are no vtk wheels for it (VTUInterface)
TARGETS: "tests ctest"
CMAKE_ARGS: >-
-DOGS_USE_PIP=ON
-DBUILD_SHARED_LIBS=ON
after_script:
- cd ../build/coverage
- source .venv/bin/activate
- fastcov --branch-coverage -C testrunner_coverage.info ctest_coverage.info --lcov -o coverage.info
- fastcov --branch-coverage -C testrunner_coverage.info ctest_coverage.info -o coverage.json
- genhtml --branch-coverage --demangle-cpp -o coverage_report coverage.info
- wget https://raw.github.com/eriwen/lcov-to-cobertura-xml/master/lcov_cobertura/lcov_cobertura.py
- python lcov_cobertura.py coverage.info --base-dir $CI_PROJECT_DIR
- ./generate_coverage_vis_data.py
coverage: '/lines......: \d+\.\d+/'
- cmake --build --preset $CMAKE_PRESET -t process_coverage
coverage: '/lines: \d+\.\d+/'
artifacts:
when: always
paths:
- Coverage.html
- build/coverage/coverage_report
- build/coverage/coverage.json
- build/coverage/coverage.xml
- build/coverage/coverage_reports/*.json
- build/coverage/coverage/html
- build/coverage/coverage/coverage.xml
- build/coverage/logs/*.txt
expire_in: 1 week
reports:
coverage_report:
coverage_format: cobertura
path: build/coverage/coverage.xml
path: build/coverage/coverage/cobertura.xml
environment:
name: code coverage $CI_MERGE_REQUEST_IID
url: ${ARTIFACTS_PAGES_URL}/build/coverage/coverage_report/index.html
url: ${ARTIFACTS_PAGES_URL}/build/coverage/coverage/html/index.html
14 changes: 2 additions & 12 deletions scripts/cmake/CMakeSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ if(EXISTS ${_collection}/ufz/vtkdiff/CMakeLists.txt AND GUIX_BUILD)
include(${_collection}/Setup.cmake)
endif()

# Set additional CMake modules path
CPMAddPackage(
NAME cmake-modules
GITHUB_REPOSITORY bilke/cmake-modules
GIT_TAG d98828f54f6974717798e63195cfbf08fe2daad0
DOWNLOAD_ONLY YES
)
# To be replaced later. See
# Set additional CMake modules path To be replaced later. See
# https://gitlab.kitware.com/cmake/cmake/-/issues/22831
CPMAddPackage(
NAME findmkl_cmake
Expand All @@ -44,7 +37,6 @@ set(CMAKE_MODULE_PATH
"${PROJECT_SOURCE_DIR}/scripts/cmake/jedbrown"
"${PROJECT_SOURCE_DIR}/scripts/cmake/vector-of-bool"
"${findmkl_cmake_SOURCE_DIR}/cmake"
"${cmake-modules_SOURCE_DIR}"
)

list(
Expand All @@ -61,9 +53,7 @@ include(ProcessorCount)
ProcessorCount(NUM_PROCESSORS)
set(NUM_PROCESSORS ${NUM_PROCESSORS} CACHE STRING "Processor count")

# Check if this project is included in another
if(NOT PROJECT_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(_IS_SUBPROJECT ON CACHE INTERNAL "" FORCE)
if(NOT PROJECT_IS_TOP_LEVEL)
set(OGS_BUILD_CLI OFF CACHE BOOL "" FORCE)
endif()

Expand Down
144 changes: 38 additions & 106 deletions scripts/cmake/Coverage.cmake
Original file line number Diff line number Diff line change
@@ -1,116 +1,48 @@
find_program(FASTCOV_PATH NAMES fastcov fastcov.py)
if(NOT FASTCOV_PATH AND NOT OGS_USE_PIP)
message(
FATAL_ERROR "Code coverage requires either fastcov or OGS_USE_PIP=ON."
if(NOT (OGS_COVERAGE AND PROJECT_IS_TOP_LEVEL))
return()
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Og --coverage")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fprofile-abs-path")
endif()
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "--coverage")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "--coverage")
set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "--coverage")
else()
message(FATAL_ERROR "OGS_COVERAGE requires clang or gcc compiler!")
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
execute_process(
COMMAND xcrun --find gcov OUTPUT_VARIABLE GCOV_EXECUTABLE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
find_program(LLVM_COV_EXECUTABLE llvm-cov REQUIRED)
file(CREATE_LINK ${LLVM_COV_EXECUTABLE} ${CMAKE_BINARY_DIR}/gcov SYMBOLIC)
set(GCOV_EXECUTABLE "${LLVM_COV_EXECUTABLE} gcov")
else() # Assuming gcc for this example
find_program(GCOV_EXECUTABLE gcov REQUIRED)
endif()
configure_file(scripts/cmake/gcovr.cfg.in gcovr.cfg @ONLY)

# cmake-lint: disable=E1126

# https://github.com/linux-test-project/lcov/pull/125
if(APPLE)
set(GENHTML_PATH ${PROJECT_BINARY_DIR}/bin/genhtml)
file(
DOWNLOAD
https://raw.githubusercontent.com/linux-test-project/lcov/41d8655951d6898511f98be2a2dbcfbe662f0b17/bin/genhtml
${GENHTML_PATH}
)
file(
DOWNLOAD
https://raw.githubusercontent.com/linux-test-project/lcov/41d8655951d6898511f98be2a2dbcfbe662f0b17/bin/get_version.sh
${PROJECT_BINARY_DIR}/bin/get_version.sh
)
file(
CHMOD
${GENHTML_PATH}
${PROJECT_BINARY_DIR}/bin/get_version.sh
FILE_PERMISSIONS
OWNER_READ
OWNER_WRITE
OWNER_EXECUTE
)
endif()

include(CodeCoverage)
append_coverage_compiler_flags()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og")

# cmake-lint: disable=C0103
if(NOT FASTCOV_PATH)
list(APPEND OGS_PYTHON_PACKAGES "fastcov==1.14")
set(FASTCOV_PATH ${LOCAL_VIRTUALENV_BIN_DIR}/fastcov CACHE INTERNAL "")
find_program(GCOVR_EXECUTABLE NAMES gcovr)
if(NOT GCOVR_EXECUTABLE)
list(APPEND OGS_PYTHON_PACKAGES "gcovr==6.0")
set(GCOVR_EXECUTABLE ${LOCAL_VIRTUALENV_BIN_DIR}/gcovr CACHE PATH "" FORCE)
endif()

if(DEFINED ENV{CI})
set(COVERAGE_ADDITIONAL_ARGS SKIP_HTML)
endif()
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/coverage/html)

if(APPLE)
# System gcov does not work:
# https://github.com/RPGillespie6/fastcov/issues/36. Search for Homebrew
# installed gcov (included in gcc package).
find_program(
GCOV_PATH NAMES gcov-12 gcov-11 gcov-10 HINTS $ENV{HOMEBREW_PREFIX}/bin
REQUIRED
)
endif()

if(OGS_BUILD_TESTING)
setup_target_for_coverage_fastcov(
NAME
testrunner_coverage
BASE_DIRECTORY
${PROJECT_BINARY_DIR}
EXECUTABLE
$<TARGET_FILE:testrunner>
-l
warn
--gtest_filter=-GeoLib.SearchNearestPointsInDenseGrid
DEPENDENCIES
testrunner
FASTCOV_ARGS
--branch-coverage
--include
${PROJECT_SOURCE_DIR}
${COVERAGE_ADDITIONAL_ARGS}
EXCLUDE
Applications/CLI/
ProcessLib/
)
endif()

if(OGS_BUILD_CLI)
setup_target_for_coverage_fastcov(
NAME
ctest_coverage
BASE_DIRECTORY
${PROJECT_BINARY_DIR}
EXECUTABLE
ctest
DEPENDENCIES
all
FASTCOV_ARGS
--branch-coverage
--include
${PROJECT_SOURCE_DIR}
${COVERAGE_ADDITIONAL_ARGS}
EXCLUDE
Applications/CLI/
POST_CMD
perl
-i
-pe
s!${PROJECT_SOURCE_DIR}/!!g
ctest_coverage.json
NO_DEMANGLE
)
endif()
add_custom_target(
process_coverage
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Running gcovr to process coverage results"
COMMAND ${GCOVR_EXECUTABLE} --config gcovr.cfg .
)

if(UNIX)
add_custom_target(clean_coverage find . -name '*.gcda' -delete)
endif()

configure_file(
${PROJECT_SOURCE_DIR}/scripts/test/generate_coverage_vis_data.in.py
${PROJECT_BINARY_DIR}/generate_coverage_vis_data.py @ONLY
)
6 changes: 3 additions & 3 deletions scripts/cmake/PythonCreateVirtualEnv.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
message(STATUS "┌─ PythonCreateVirtualEnv.cmake")
list(APPEND CMAKE_MESSAGE_INDENT "│ ")

# Prefer unix location over frameworks (Apple-only)
set(Python_FIND_FRAMEWORK LAST)

# Prefer more recent Python version
set(Python_FIND_STRATEGY VERSION)

# Don't use venv
set(Python_FIND_VIRTUALENV STANDARD)

find_package(Python ${python_version} COMPONENTS Interpreter REQUIRED)

if(${Python_VERSION} VERSION_GREATER_EQUAL 3.9)
Expand Down
7 changes: 7 additions & 0 deletions scripts/cmake/gcovr.cfg.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = @CMAKE_SOURCE_DIR@
cobertura = @CMAKE_BINARY_DIR@/coverage/cobertura.xml
# html-nested shows wrong paths: build/coverage/[DIR]
html-details = @CMAKE_BINARY_DIR@/coverage/html/index.html
gcov-executable = @GCOV_EXECUTABLE@
gcov-parallel = yes
print-summary = yes
2 changes: 1 addition & 1 deletion scripts/cmake/packaging/Pack.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if(_IS_SUBPROJECT)
if(NOT PROJECT_IS_TOP_LEVEL)
include(CPack)
return()
endif()
Expand Down
4 changes: 2 additions & 2 deletions web/data/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
}
},
"cpm": {
"package_file_id": 1028,
"package_file_sha256": "815ef4ca37e05232526afe50e3eafe8a1d6366bce3ae3d509fac1214ab6b5f98"
"package_file_id": 1029,
"package_file_sha256": "baca0a575a0c7053e4c7a5bffb32bb30d8dce0ae97308858b3bf8a690063b303"
},
"ext": {
"cache_hash": "4ebd62e0ce484732dbf3cd9932f60b67762e156b"
Expand Down

0 comments on commit 8db8e6b

Please sign in to comment.