QtProtobufCoverage.cmake 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. if(QT_PROTOBUF_MAKE_COVERAGE AND UNIX)
  2. message(STATUS "Enable gcov")
  3. add_custom_target(coverage)
  4. add_custom_target(_qt_protobuf_internal_coverage_tests)
  5. add_custom_command(TARGET _qt_protobuf_internal_coverage_tests
  6. COMMAND ${CMAKE_CTEST_COMMAND}
  7. WORKING_DIRECTORY ${QT_PROTOBUF_BINARY_DIR}
  8. )
  9. endif()
  10. function(qt_protobuf_internal_add_coverage_target)
  11. if(NOT QT_PROTOBUF_MAKE_COVERAGE OR NOT UNIX)
  12. return()
  13. endif()
  14. set(options)
  15. set(oneValueArgs "TARGET")
  16. set(multiValueArgs)
  17. cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  18. set(GCOV_OUTPUT_DIR ${QT_PROTOBUF_BINARY_DIR}/gcov/${arg_TARGET})
  19. set(OBJECTS_DIR ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${arg_TARGET}.dir)
  20. set(COVERAGE_TARGET ${arg_TARGET}_coverage)
  21. if(NOT TARGET ${arg_TARGET})
  22. message(FATAL_ERROR "Unable to add a coverage target, '${arg_TARGET}' is not a traget")
  23. endif()
  24. find_program(GCOV_EXECUTABLE "gcov")
  25. if(NOT GCOV_EXECUTABLE)
  26. message(FATAL_ERROR "Coverage is enabled for target ${arg_TARGET}, \
  27. but gcov is not installed")
  28. endif()
  29. target_compile_options(${arg_TARGET} PUBLIC "-fprofile-arcs" "-ftest-coverage")
  30. target_link_options(${arg_TARGET} PUBLIC "--coverage")
  31. add_dependencies(_qt_protobuf_internal_coverage_tests ${arg_TARGET})
  32. add_custom_target(${COVERAGE_TARGET} DEPENDS _qt_protobuf_internal_coverage_tests)
  33. file(MAKE_DIRECTORY "${GCOV_OUTPUT_DIR}")
  34. add_custom_command(TARGET ${COVERAGE_TARGET}
  35. COMMAND "${GCOV_EXECUTABLE}" "${OBJECTS_DIR}/*.o" -b -c
  36. WORKING_DIRECTORY ${GCOV_OUTPUT_DIR}
  37. )
  38. find_program(GCOVR_EXECUTABLE "gcovr")
  39. if(GCOVR_EXECUTABLE)
  40. set(GCOVR_OUTPUT_DIR "${GCOV_OUTPUT_DIR}/html")
  41. file(MAKE_DIRECTORY "${GCOVR_OUTPUT_DIR}")
  42. add_custom_command(TARGET ${COVERAGE_TARGET}
  43. COMMAND "${GCOVR_EXECUTABLE}" -g -k -r "${CMAKE_SOURCE_DIR}" --html --html-details -o
  44. "${GCOVR_OUTPUT_DIR}/${arg_TARGET}.html" "${OBJECTS_DIR}" .
  45. WORKING_DIRECTORY ${GCOV_OUTPUT_DIR}
  46. )
  47. endif()
  48. add_dependencies(coverage ${COVERAGE_TARGET})
  49. endfunction()