Skip to content

Commit

Permalink
perf: refine coverage on windows (#40)
Browse files Browse the repository at this point in the history
Signed-off-by: msclock <msclock@qq.com>
  • Loading branch information
msclock authored Mar 9, 2024
1 parent 6806890 commit 6fc6d7b
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions cmake/test/Coverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ message(
ON - Enables code coverage with auto-selected coverage tools (lcov, gcovr, llvm-cov, opencppcoverage).
OFF - Disables code coverage.")

if(NOT CODE_COVERAGE)
return()
endif()

# Programs to generate coverage tools
find_program(LLVM_COV_PATH llvm-cov)
find_program(LLVM_PROFDATA_PATH llvm-profdata)
Expand Down Expand Up @@ -125,9 +121,7 @@ set_property(GLOBAL PROPERTY JOB_POOLS ccov_serial_pool=1)

# Common initialization and checks
if(CODE_COVERAGE AND NOT CODE_COVERAGE_INITIALIZED)
set(CODE_COVERAGE_INITIALIZED
ON
CACHE INTERNAL "Code coverage initialization flag")
set(CODE_COVERAGE_INITIALIZED ON)

# Enable ctest *Coverage, such as ctest -T
# Continuous/Experimental/Nightly[Coverage]
Expand All @@ -137,14 +131,14 @@ if(CODE_COVERAGE AND NOT CODE_COVERAGE_INITIALIZED)
"${LLVM_COV_PATH}"
CACHE STRING "LLVM coverage tool for gcov" FORCE)
set(COVERAGE_EXTRA_FLAGS
"gcov ${COVERAGE_EXTRA_FLAGS} -s ${CMAKE_BINARY_DIR}"
"gcov ${USER_COVERAGE_EXTRA_FLAGS} -s ${CMAKE_BINARY_DIR}"
CACHE STRING "Extra command line flags to pass to the coverage tool"
FORCE)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES
"GNU")
if(GCOV_PATH)
set(COVERAGE_EXTRA_FLAGS
"${COVERAGE_EXTRA_FLAGS} -s ${CMAKE_BINARY_DIR}"
"${USER_COVERAGE_EXTRA_FLAGS} -s ${CMAKE_BINARY_DIR}"
CACHE STRING "Extra command line flags to pass to the coverage tool"
FORCE)
endif(GCOV_PATH)
Expand Down Expand Up @@ -312,11 +306,11 @@ function(target_code_coverage TARGET_NAME)
# Add code coverage instrumentation to the target's linker command
if(CMAKE_C_COMPILER_ID MATCHES [[(Apple)?Clang]]
OR CMAKE_CXX_COMPILER_ID MATCHES [[(Apple)?Clang]])
target_compile_options(${TARGET_NAME} ${TARGET_VISIBILITY} --coverage
-fprofile-instr-generate -fcoverage-mapping)
target_link_options(
target_compile_options(
${TARGET_NAME} ${TARGET_VISIBILITY} -fprofile-instr-generate
-fcoverage-mapping --coverage)
target_link_options(${TARGET_NAME} ${TARGET_VISIBILITY}
-fprofile-instr-generate -fcoverage-mapping)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES
"GNU")
target_compile_options(
Expand Down Expand Up @@ -348,6 +342,10 @@ function(target_code_coverage TARGET_NAME)

add_dependencies(ccov-libs ccov-run-${arg_COVERAGE_TARGET_NAME})
endif()

if(MSVC)
add_dependencies(ccov-all-processing ${TARGET_NAME})
endif()
endif()

# For executables add targets to run and produce output
Expand Down Expand Up @@ -529,20 +527,24 @@ function(target_code_coverage TARGET_NAME)
ccov-report-${arg_COVERAGE_TARGET_NAME})
endif()
endif()
endif(NOT MSVC)

# ALL
if(arg_ALL)
if(NOT TARGET ccov-all-processing)
message(
FATAL_ERROR
"Calling target_code_coverage with 'ALL' must be after a call to 'add_code_coverage_all_targets'."
)
endif()
# ALL
if(arg_ALL)
if(NOT TARGET ccov-all-processing)
message(
FATAL_ERROR
"Calling target_code_coverage with 'ALL' must be after a call to 'add_code_coverage_all_targets'."
)
endif()

if(NOT MSVC)
add_dependencies(ccov-all-processing
ccov-run-${arg_COVERAGE_TARGET_NAME})
else()
add_dependencies(ccov-all-processing ${TARGET_NAME})
endif()
endif(NOT MSVC)
endif()
endif()
endif()
endfunction()
Expand Down Expand Up @@ -617,7 +619,7 @@ function(add_code_coverage_all_targets)
cobertura:${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage.xml
--working_dir ${CMAKE_SOURCE_DIR} ${_include_command}
${_exclude_command} --cover_children -- ${CMAKE_COMMAND} --build
${CMAKE_BINARY_DIR} --target ContinuousTest;
${CMAKE_BINARY_DIR} --target ExperimentalTest;
DEPENDS ccov-all-processing)

# Generates HTML output of all targets for perusal
Expand All @@ -630,7 +632,7 @@ function(add_code_coverage_all_targets)
html:${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage --working_dir
${CMAKE_SOURCE_DIR} ${_include_command} ${_exclude_command}
--cover_children -- ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}
--target ContinuousTest;;
--target ExperimentalTest;
DEPENDS ccov-all-capture)

return()
Expand All @@ -646,7 +648,7 @@ function(add_code_coverage_all_targets)
if(GCOV_PATH)
if(CMAKE_C_COMPILER_ID MATCHES [[(Apple)?Clang]]
OR CMAKE_CXX_COMPILER_ID MATCHES [[(Apple)?Clang]])
set(GCOV_OPTION "--gcov-executable=llvm-cov ${GCOV_PATH}")
set(GCOV_OPTION "--gcov-executable=${LLVM_COV_PATH} gcov")
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID
MATCHES "GNU")
set(GCOV_OPTION "--gcov-executable=${GCOV_PATH}")
Expand All @@ -672,8 +674,9 @@ function(add_code_coverage_all_targets)
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage.xml
COMMAND
${GCOVR_PATH} --print-summary --xml-pretty --root ${CMAKE_SOURCE_DIR}
--output ${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage.xml
${GCOV_OPTION} ${_include_command} ${_exclude_command}
--exclude-noncode-lines --output
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage.xml ${GCOV_OPTION}
${_include_command} ${_exclude_command}
DEPENDS ccov-all-processing)

# Generates HTML output of all targets for perusal
Expand All @@ -684,7 +687,8 @@ function(add_code_coverage_all_targets)
COMMAND ${CMAKE_COMMAND} -E make_directory
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage
COMMAND
${GCOVR_PATH} --html-details --root ${CMAKE_SOURCE_DIR} --output
${GCOVR_PATH} --html-details --root ${CMAKE_SOURCE_DIR}
--exclude-noncode-lines --output
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage/index.html ${GCOV_OPTION}
${_include_command} ${_exclude_command} --delete
DEPENDS ccov-all-capture)
Expand Down

0 comments on commit 6fc6d7b

Please # to comment.