1find_program(CODE_COVERAGE_LCOV lcov) 2if (NOT CODE_COVERAGE_LCOV) 3 message(FATAL_ERROR "Cannot find lcov...") 4endif() 5 6find_program(CODE_COVERAGE_LLVM_COV llvm-cov) 7if (NOT CODE_COVERAGE_LLVM_COV) 8 message(FATAL_ERROR "Cannot find llvm-cov...") 9endif() 10 11find_program(CODE_COVERAGE_GENHTML genhtml) 12if (NOT CODE_COVERAGE_GENHTML) 13 message(FATAL_ERROR "Cannot find genhtml...") 14endif() 15 16set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 --coverage") 17 18function(setup_lcov_test_target_coverage target_name output_dir capture_dirs source_dirs) 19 if (NOT DEFINED LIBCXX_BINARY_DIR) 20 message(FATAL_ERROR "Variable must be set") 21 endif() 22 23 set(GCOV_TOOL "${LIBCXX_BINARY_DIR}/llvm-cov-wrapper") 24 file(GENERATE OUTPUT ${GCOV_TOOL} 25 CONTENT "#!/usr/bin/env bash\n${CODE_COVERAGE_LLVM_COV} gcov \"$@\"\n") 26 27 file(MAKE_DIRECTORY ${output_dir}) 28 29 set(CAPTURE_DIRS "") 30 foreach(cdir ${capture_dirs}) 31 list(APPEND CAPTURE_DIRS "-d;${cdir}") 32 endforeach() 33 34 set(EXTRACT_DIRS "") 35 foreach(sdir ${source_dirs}) 36 list(APPEND EXTRACT_DIRS "'${sdir}/*'") 37 endforeach() 38 39 message(STATUS "Capture Directories: ${CAPTURE_DIRS}") 40 message(STATUS "Extract Directories: ${EXTRACT_DIRS}") 41 42 add_custom_target(generate-lib${target_name}-coverage 43 COMMAND chmod +x ${GCOV_TOOL} 44 COMMAND ${CODE_COVERAGE_LCOV} --gcov-tool ${GCOV_TOOL} --capture ${CAPTURE_DIRS} -o test_coverage.info 45 COMMAND ${CODE_COVERAGE_LCOV} --gcov-tool ${GCOV_TOOL} --extract test_coverage.info ${EXTRACT_DIRS} -o test_coverage.info 46 COMMAND ${CODE_COVERAGE_GENHTML} --demangle-cpp test_coverage.info -o test_coverage 47 COMMAND ${CMAKE_COMMAND} -E remove test_coverage.info 48 WORKING_DIRECTORY ${output_dir} 49 COMMENT "Generating coverage results") 50endfunction() 51