extras.cmake 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. set(PROJECT_TARGETS_PREFIX ${PROJECT_NAME})
  2. message("*****************************************************")
  3. message("EXTRA TARGETS:")
  4. message("*****************************************************")
  5. add_library( ${PROJECT_TARGETS_PREFIX}_coverage INTERFACE)
  6. add_library( ${PROJECT_TARGETS_PREFIX}::coverage ALIAS ${PROJECT_TARGETS_PREFIX}_coverage)
  7. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
  8. target_compile_options(${PROJECT_TARGETS_PREFIX}_coverage
  9. INTERFACE
  10. --coverage -g -O0 -fprofile-arcs -ftest-coverage)
  11. target_link_libraries( ${PROJECT_TARGETS_PREFIX}_coverage
  12. INTERFACE --coverage -g -O0 -fprofile-arcs -ftest-coverage)
  13. add_custom_target(coverage
  14. COMMAND rm -rf coverage
  15. COMMAND mkdir -p coverage
  16. #COMMAND ${CMAKE_MAKE_PROGRAM} test
  17. #COMMAND gcovr . -r ${CMAKE_SOURCE_DIR} --html-details --html -o coverage/index.html -e ${CMAKE_SOURCE_DIR}/test/third_party;
  18. COMMAND gcovr . -r ${CMAKE_SOURCE_DIR} --xml -o coverage/report.xml -e ${CMAKE_SOURCE_DIR}/third_party;
  19. COMMAND gcovr . -r ${CMAKE_SOURCE_DIR} -o coverage/report.txt -e ${CMAKE_SOURCE_DIR}/third_party;
  20. COMMAND cat coverage/report.txt
  21. #COMMAND lcov --no-external --capture --directory ${CMAKE_BINARY_DIR} --output-file coverage2.info
  22. #COMMAND genhtml coverage.info --output-directory lcov-report
  23. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  24. )
  25. endif()
  26. #==========
  27. # from here:
  28. #
  29. # https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md
  30. function(set_project_warnings project_name)
  31. option(${PROJECT_NAME}_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE)
  32. set(MSVC_WARNINGS
  33. /W4 # Baseline reasonable warnings
  34. /w14242 # 'identfier': conversion from 'type1' to 'type1', possible loss
  35. # of data
  36. /w14254 # 'operator': conversion from 'type1:field_bits' to
  37. # 'type2:field_bits', possible loss of data
  38. /w14263 # 'function': member function does not override any base class
  39. # virtual member function
  40. /w14265 # 'classname': class has virtual functions, but destructor is not
  41. # virtual instances of this class may not be destructed correctly
  42. /w14287 # 'operator': unsigned/negative constant mismatch
  43. /we4289 # nonstandard extension used: 'variable': loop control variable
  44. # declared in the for-loop is used outside the for-loop scope
  45. /w14296 # 'operator': expression is always 'boolean_value'
  46. /w14311 # 'variable': pointer truncation from 'type1' to 'type2'
  47. /w14545 # expression before comma evaluates to a function which is missing
  48. # an argument list
  49. /w14546 # function call before comma missing argument list
  50. /w14547 # 'operator': operator before comma has no effect; expected
  51. # operator with side-effect
  52. /w14549 # 'operator': operator before comma has no effect; did you intend
  53. # 'operator'?
  54. /w14555 # expression has no effect; expected expression with side- effect
  55. # /w14619 # pragma warning: there is no warning number 'number'
  56. /w14640 # Enable warning on thread un-safe static member initialization
  57. /w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may
  58. # cause unexpected runtime behavior.
  59. /w14905 # wide string literal cast to 'LPSTR'
  60. /w14906 # string literal cast to 'LPWSTR'
  61. /w14928 # illegal copy-initialization; more than one user-defined
  62. # conversion has been implicitly applied
  63. # Disable the following errors
  64. /wd4101
  65. /wd4201
  66. )
  67. set(CLANG_WARNINGS
  68. -Wall
  69. -Wextra # reasonable and standard
  70. -Wshadow # warn the user if a variable declaration shadows one from a
  71. # parent context
  72. -Wnon-virtual-dtor # warn the user if a class with virtual functions has a
  73. # non-virtual destructor. This helps catch hard to
  74. # track down memory errors
  75. -Wold-style-cast # warn for c-style casts
  76. -Wcast-align # warn for potential performance problem casts
  77. -Wunused # warn on anything being unused
  78. -Woverloaded-virtual # warn if you overload (not override) a virtual
  79. # function
  80. -Wpedantic # warn if non-standard C++ is used
  81. -Wconversion # warn on type conversions that may lose data
  82. -Wsign-conversion # warn on sign conversions
  83. -Wnull-dereference # warn if a null dereference is detected
  84. -Wdouble-promotion # warn if float is implicit promoted to double
  85. -Wformat=2 # warn on security issues around functions that format output
  86. # (ie printf)
  87. -Wno-gnu-zero-variadic-macro-arguments
  88. )
  89. if (${PROJECT_NAME}_WARNINGS_AS_ERRORS)
  90. set(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror)
  91. set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX )
  92. endif()
  93. set(GCC_WARNINGS
  94. ${CLANG_WARNINGS}
  95. -Wmisleading-indentation # warn if identation implies blocks where blocks
  96. # do not exist
  97. -Wduplicated-cond # warn if if / else chain has duplicated conditions
  98. -Wduplicated-branches # warn if if / else branches have duplicated code
  99. -Wlogical-op # warn about logical operations being used where bitwise were
  100. # probably wanted
  101. # -Wuseless-cast # warn if you perform a cast to the same type
  102. -Wno-class-memaccess
  103. -Wno-stringop-overflow
  104. -Wsuggest-override
  105. -Wno-gnu-zero-variadic-macro-arguments
  106. )
  107. if(MSVC)
  108. set(_PROJECT_WARNINGS ${MSVC_WARNINGS})
  109. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  110. set(_PROJECT_WARNINGS ${CLANG_WARNINGS})
  111. else()
  112. set(_PROJECT_WARNINGS ${GCC_WARNINGS})
  113. endif()
  114. get_target_property(type ${project_name} TYPE)
  115. message("--->${type}")
  116. if (${type} STREQUAL "INTERFACE_LIBRARY")
  117. target_compile_options(${project_name} INTERFACE ${_PROJECT_WARNINGS})
  118. else()
  119. target_compile_options(${project_name} PRIVATE ${_PROJECT_WARNINGS})
  120. endif()
  121. endfunction()
  122. #==========
  123. add_library(${PROJECT_TARGETS_PREFIX}_warnings INTERFACE)
  124. add_library(${PROJECT_TARGETS_PREFIX}::warnings ALIAS ${PROJECT_TARGETS_PREFIX}_warnings)
  125. set_project_warnings(${PROJECT_TARGETS_PREFIX}_warnings)
  126. #target_compile_options(${PROJECT_TARGETS_PREFIX}_warnings INTERFACE -Wall -Wextra -Wpedantic)
  127. add_library(${PROJECT_TARGETS_PREFIX}_warnings_error INTERFACE)
  128. add_library(${PROJECT_TARGETS_PREFIX}::error ALIAS ${PROJECT_TARGETS_PREFIX}_warnings_error)
  129. target_compile_options(${PROJECT_TARGETS_PREFIX}_warnings_error INTERFACE -Werror)
  130. message("New Target: ${PROJECT_TARGETS_PREFIX}::coverage")
  131. message("New Target: ${PROJECT_TARGETS_PREFIX}::warnings")
  132. message("New Target: ${PROJECT_TARGETS_PREFIX}::error")
  133. message("*****************************************************")