Coverage.cmake 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # from here:
  2. #
  3. # https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md
  4. #
  5. option(${PROJECT_NAME}_ENABLE_COVERAGE "Enable Code Coverage. Only enabled on non-Release mode" TRUE)
  6. function(set_coverage_flags project_name)
  7. if(NOT CMAKE_BUILD_TYPE MATCHES Release)
  8. if( ${PROJECT_NAME}_ENABLE_COVERAGE )
  9. if(CMAKE_COMPILER_IS_GNUCC)
  10. get_target_property(type ${project_name} TYPE)
  11. if (${type} STREQUAL "INTERFACE_LIBRARY")
  12. target_compile_options(${project_name} INTERFACE --coverage -g -O0 -fprofile-arcs -ftest-coverage)
  13. target_link_libraries( ${project_name} INTERFACE --coverage -g -O0 -fprofile-arcs -ftest-coverage)
  14. else()
  15. target_compile_options(${project_name} PRIVATE --coverage -g -O0 -fprofile-arcs -ftest-coverage)
  16. target_link_libraries( ${project_name} PRIVATE --coverage -g -O0 -fprofile-arcs -ftest-coverage)
  17. endif()
  18. message("Coverage Flags added to target : ${project_name}: --coverage -g -O0 -fprofile-arcs -ftest-coverage")
  19. endif()
  20. endif()
  21. endif()
  22. endfunction()
  23. message("=========================================")
  24. message("COVERAGE")
  25. message("=========================================")
  26. if(NOT CMAKE_BUILD_TYPE MATCHES Release)
  27. message("- CMAKE_BUILD_TYPE is NOT Release. Enabling Coverage")
  28. if( ${PROJECT_NAME}_ENABLE_COVERAGE )
  29. message("- ${PROJECT_NAME}_ENABLE_COVERAGE is set.")
  30. if(CMAKE_COMPILER_IS_GNUCC)
  31. message("- Compiler is GCC")
  32. message("- WILL BUILD WITH COVERAGE.")
  33. endif()
  34. endif()
  35. endif()
  36. add_custom_target(coverage
  37. COMMAND rm -rf coverage
  38. COMMAND mkdir -p coverage
  39. COMMAND gcovr . -e "build/*" -r ${CMAKE_SOURCE_DIR} --html-details --html -o coverage/index.html -e ${CMAKE_SOURCE_DIR}/build;
  40. COMMAND gcovr . -e "build/*" -r ${CMAKE_SOURCE_DIR} --xml -o coverage/report.xml -e ${CMAKE_SOURCE_DIR}/build;
  41. COMMAND gcovr . -e "build/*" -r ${CMAKE_SOURCE_DIR} -o coverage/report.txt -e ${CMAKE_SOURCE_DIR}/build;
  42. COMMAND cat coverage/report.txt
  43. WORKING_DIRECTORY ${CMAKE_BINARY_DIR} # Need separate command for this line
  44. )
  45. message("- adding new target: coverage")
  46. message("=========================================")