Coverage.cmake 2.0 KB

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