summaryrefslogtreecommitdiffstats
path: root/cmake/Coverage.cmake
blob: 31f8341219de119199daa561e582027b82b46ffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
if(enable_coverage)
  FIND_PROGRAM( LCOV_PATH lcov )
  FIND_PROGRAM( GENHTML_PATH genhtml )
  set(COVERAGE_COMPILER_FLAGS -g --coverage -O0
      CACHE INTERNAL "")
  set(COVERAGE_LINKER_FLAGS --coverage
      CACHE INTERNAL "")
  add_custom_target(coverage-clean
	COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --directory . --zerocounters
	WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  )
  add_custom_target(coverage
	COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --directory . --capture --output-file cov.info
	COMMAND ${LCOV_PATH} --rc lcov_branch_coverage=1 --remove cov.info '*/c++/*' '*/_ctype.h' '*/generated_src/*' --output-file cov.info.cleaned
	COMMAND ${CMAKE_COMMAND} -Dsearch=${CMAKE_BINARY_DIR}
	                         -Dreplace=${CMAKE_SOURCE_DIR}
				 -Dsrc=cov.info.cleaned
				 -Ddst=cov.info.final
				 -P ${CMAKE_SOURCE_DIR}/cmake/SearchReplace.cmake
	COMMAND ${GENHTML_PATH} --rc genhtml_branch_coverage=1
	                        --function-coverage --branch-coverage
	                        --title "Doxygen Coverage Report" --num-spaces 2
				--legend --prefix ${CMAKE_SOURCE_DIR} --demangle-cpp
				--output-directory cov_output cov.info.final
        COMMAND ${CMAKE_COMMAND} -E remove cov.info cov.info.cleaned cov.info.final
	WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  )
  add_custom_command(TARGET coverage POST_BUILD
	COMMAND ;
	COMMENT "Open ./cov_output/index.html in your browser to view the coverage report"
  )
endif()

function(set_project_coverage project_name)
  if(enable_coverage)
    target_compile_options(${project_name} PRIVATE ${COVERAGE_COMPILER_FLAGS})
  endif()
endfunction()