Home | History | Annotate | Download | only in Modules
      1 find_program(CODE_COVERAGE_LCOV lcov)
      2 if (NOT CODE_COVERAGE_LCOV)
      3   message(FATAL_ERROR "Cannot find lcov...")
      4 endif()
      5 
      6 find_program(CODE_COVERAGE_GENHTML genhtml)
      7 if (NOT CODE_COVERAGE_GENHTML)
      8   message(FATAL_ERROR "Cannot find genhtml...")
      9 endif()
     10 
     11 set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 --coverage")
     12 
     13 function(setup_lcov_test_target_coverage target_name output_dir capture_dirs source_dirs)
     14   file(MAKE_DIRECTORY ${output_dir})
     15 
     16   set(CAPTURE_DIRS "")
     17   foreach(cdir ${capture_dirs})
     18     list(APPEND CAPTURE_DIRS "-d;${cdir}")
     19   endforeach()
     20 
     21   set(EXTRACT_DIRS "")
     22   foreach(sdir ${source_dirs})
     23     list(APPEND EXTRACT_DIRS "'${sdir}/*'")
     24   endforeach()
     25 
     26   message(STATUS "Capture Directories: ${CAPTURE_DIRS}")
     27   message(STATUS "Extract Directories: ${EXTRACT_DIRS}")
     28 
     29   add_custom_target(generate-lib${target_name}-coverage
     30         COMMAND ${CODE_COVERAGE_LCOV} --capture ${CAPTURE_DIRS} -o test_coverage.info
     31         COMMAND ${CODE_COVERAGE_LCOV} --extract test_coverage.info ${EXTRACT_DIRS} -o test_coverage.info
     32         COMMAND ${CODE_COVERAGE_GENHTML} --demangle-cpp test_coverage.info -o test_coverage
     33         COMMAND ${CMAKE_COMMAND} -E remove test_coverage.info
     34         WORKING_DIRECTORY ${output_dir}
     35         COMMENT "Generating coverage results")
     36 endfunction()
     37