178fdf2d0SEric Fiselierfind_program(CODE_COVERAGE_LCOV lcov)
278fdf2d0SEric Fiselierif (NOT CODE_COVERAGE_LCOV)
378fdf2d0SEric Fiselier  message(FATAL_ERROR "Cannot find lcov...")
478fdf2d0SEric Fiselierendif()
578fdf2d0SEric Fiselier
6*2aeac46eSEric Fiselierfind_program(CODE_COVERAGE_LLVM_COV llvm-cov)
7*2aeac46eSEric Fiselierif (NOT CODE_COVERAGE_LLVM_COV)
8*2aeac46eSEric Fiselier  message(FATAL_ERROR "Cannot find llvm-cov...")
9*2aeac46eSEric Fiselierendif()
10*2aeac46eSEric Fiselier
1178fdf2d0SEric Fiselierfind_program(CODE_COVERAGE_GENHTML genhtml)
1278fdf2d0SEric Fiselierif (NOT CODE_COVERAGE_GENHTML)
1378fdf2d0SEric Fiselier  message(FATAL_ERROR "Cannot find genhtml...")
1478fdf2d0SEric Fiselierendif()
1578fdf2d0SEric Fiselier
1678fdf2d0SEric Fiselierset(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 --coverage")
1778fdf2d0SEric Fiselier
1878fdf2d0SEric Fiselierfunction(setup_lcov_test_target_coverage target_name output_dir capture_dirs source_dirs)
19*2aeac46eSEric Fiselier  if (NOT DEFINED LIBCXX_BINARY_DIR)
20*2aeac46eSEric Fiselier    message(FATAL_ERROR "Variable must be set")
21*2aeac46eSEric Fiselier  endif()
22*2aeac46eSEric Fiselier
23*2aeac46eSEric Fiselier  set(GCOV_TOOL "${LIBCXX_BINARY_DIR}/llvm-cov-wrapper")
24*2aeac46eSEric Fiselier  file(GENERATE OUTPUT ${GCOV_TOOL}
25*2aeac46eSEric Fiselier    CONTENT "#!/usr/bin/env bash\n${CODE_COVERAGE_LLVM_COV} gcov \"$@\"\n")
26*2aeac46eSEric Fiselier
2778fdf2d0SEric Fiselier  file(MAKE_DIRECTORY ${output_dir})
2878fdf2d0SEric Fiselier
2978fdf2d0SEric Fiselier  set(CAPTURE_DIRS "")
3078fdf2d0SEric Fiselier  foreach(cdir ${capture_dirs})
3178fdf2d0SEric Fiselier    list(APPEND CAPTURE_DIRS "-d;${cdir}")
3278fdf2d0SEric Fiselier  endforeach()
3378fdf2d0SEric Fiselier
3478fdf2d0SEric Fiselier  set(EXTRACT_DIRS "")
3578fdf2d0SEric Fiselier  foreach(sdir ${source_dirs})
3678fdf2d0SEric Fiselier    list(APPEND EXTRACT_DIRS "'${sdir}/*'")
3778fdf2d0SEric Fiselier  endforeach()
3878fdf2d0SEric Fiselier
3978fdf2d0SEric Fiselier  message(STATUS "Capture Directories: ${CAPTURE_DIRS}")
4078fdf2d0SEric Fiselier  message(STATUS "Extract Directories: ${EXTRACT_DIRS}")
4178fdf2d0SEric Fiselier
4278fdf2d0SEric Fiselier  add_custom_target(generate-lib${target_name}-coverage
43*2aeac46eSEric Fiselier        COMMAND chmod +x ${GCOV_TOOL}
44*2aeac46eSEric Fiselier        COMMAND ${CODE_COVERAGE_LCOV} --gcov-tool ${GCOV_TOOL} --capture ${CAPTURE_DIRS} -o test_coverage.info
45*2aeac46eSEric Fiselier        COMMAND ${CODE_COVERAGE_LCOV} --gcov-tool ${GCOV_TOOL} --extract test_coverage.info ${EXTRACT_DIRS} -o test_coverage.info
4678fdf2d0SEric Fiselier        COMMAND ${CODE_COVERAGE_GENHTML} --demangle-cpp test_coverage.info -o test_coverage
4778fdf2d0SEric Fiselier        COMMAND ${CMAKE_COMMAND} -E remove test_coverage.info
4878fdf2d0SEric Fiselier        WORKING_DIRECTORY ${output_dir}
4978fdf2d0SEric Fiselier        COMMENT "Generating coverage results")
5078fdf2d0SEric Fiselierendfunction()
51