Coverage.cmake 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. if(QT_PROTOBUF_MAKE_COVERAGE AND UNIX)
  2. message(STATUS "Enable gcov")
  3. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
  4. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage")
  5. add_custom_target(coverage)
  6. endif()
  7. function(add_coverage_target)
  8. set(options)
  9. set(oneValueArgs TARGET)
  10. set(multiValueArgs)
  11. cmake_parse_arguments(add_coverage_target "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  12. set(GCOV_OUTPUT_DIR ${QT_PROTOBUF_BINARY_DIR}/gcov/${add_coverage_target_TARGET})
  13. set(OBJECTS_DIR ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${add_coverage_target_TARGET}.dir)
  14. set(COVERAGE_TARGET ${add_coverage_target_TARGET}_coverage)
  15. if(QT_PROTOBUF_MAKE_COVERAGE AND UNIX)
  16. find_program(GCOV_EXECUTABLE "gcov")
  17. if(GCOV_EXECUTABLE STREQUAL GCOV_EXECUTABLE-NOTFOUND)
  18. message(FATAL_ERROR "Coverage enabled for target ${add_coverage_target_TARGET}, but gcov is not installed")
  19. endif()
  20. file(MAKE_DIRECTORY ${GCOV_OUTPUT_DIR})
  21. add_custom_target(${COVERAGE_TARGET} DEPENDS ${add_coverage_target_TARGET})
  22. add_custom_command(TARGET ${COVERAGE_TARGET} COMMAND ${GCOV_EXECUTABLE} ${OBJECTS_DIR}/*.o -b -c
  23. WORKING_DIRECTORY ${GCOV_OUTPUT_DIR})
  24. find_program(GCOVR_EXECUTABLE "gcovr")
  25. if(NOT GCOVR_EXECUTABLE STREQUAL GCOVR_EXECUTABLE-NOTFOUND)
  26. set(GCOVR_OUTPUT_DIR ${GCOV_OUTPUT_DIR}/html)
  27. file(MAKE_DIRECTORY ${GCOVR_OUTPUT_DIR})
  28. add_custom_command(TARGET ${COVERAGE_TARGET} COMMAND ${GCOVR_EXECUTABLE} -g -k -r
  29. ${CMAKE_SOURCE_DIR} --html --html-details -o ${GCOVR_OUTPUT_DIR}/${add_coverage_target_TARGET}.html ${OBJECTS_DIR} .
  30. WORKING_DIRECTORY ${GCOV_OUTPUT_DIR})
  31. endif()
  32. add_dependencies(coverage ${COVERAGE_TARGET})
  33. endif()
  34. endfunction(add_coverage_target)