CatchMiscFunctions.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright Catch2 Authors
  2. # Distributed under the Boost Software License, Version 1.0.
  3. # (See accompanying file LICENSE_1_0.txt or copy at
  4. # https://www.boost.org/LICENSE_1_0.txt)
  5. # SPDX-License-Identifier: BSL-1.0
  6. include(CheckCXXCompilerFlag)
  7. function(add_cxx_flag_if_supported_to_targets flagname targets)
  8. string(MAKE_C_IDENTIFIER ${flagname} flag_identifier )
  9. check_cxx_compiler_flag("${flagname}" HAVE_FLAG_${flag_identifier})
  10. if (HAVE_FLAG_${flag_identifier})
  11. foreach(target ${targets})
  12. target_compile_options(${target} PUBLIC ${flagname})
  13. endforeach()
  14. endif()
  15. endfunction()
  16. # Assumes that it is only called for development builds, where warnings
  17. # and Werror is desired, so it also enables Werror.
  18. function(add_warnings_to_targets targets)
  19. LIST(LENGTH targets TARGETS_LEN)
  20. # For now we just assume 2 possibilities: msvc and msvc-like compilers,
  21. # and other.
  22. if (MSVC)
  23. foreach(target ${targets})
  24. # Force MSVC to consider everything as encoded in utf-8
  25. target_compile_options( ${target} PRIVATE /utf-8 )
  26. # Enable Werror equivalent
  27. if (CATCH_ENABLE_WERROR)
  28. target_compile_options( ${target} PRIVATE /WX )
  29. endif()
  30. # MSVC is currently handled specially
  31. if ( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
  32. STRING(REGEX REPLACE "/W[0-9]" "/W4" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # override default warning level
  33. target_compile_options( ${target} PRIVATE /w44265 /w44061 /w44062 /w45038 )
  34. endif()
  35. endforeach()
  36. endif()
  37. if (NOT MSVC)
  38. set(CHECKED_WARNING_FLAGS
  39. "-Wabsolute-value"
  40. "-Wall"
  41. "-Wc++20-compat"
  42. "-Wcall-to-pure-virtual-from-ctor-dtor"
  43. "-Wcast-align"
  44. "-Wcatch-value"
  45. "-Wdangling"
  46. "-Wdeprecated"
  47. "-Wdeprecated-register"
  48. "-Wexceptions"
  49. "-Wexit-time-destructors"
  50. "-Wextra"
  51. "-Wextra-semi"
  52. "-Wfloat-equal"
  53. "-Wglobal-constructors"
  54. "-Winit-self"
  55. "-Wmisleading-indentation"
  56. "-Wmismatched-new-delete"
  57. "-Wmismatched-return-types"
  58. "-Wmismatched-tags"
  59. "-Wmissing-braces"
  60. "-Wmissing-declarations"
  61. "-Wmissing-noreturn"
  62. "-Wmissing-prototypes"
  63. "-Wmissing-variable-declarations"
  64. "-Wnull-dereference"
  65. "-Wold-style-cast"
  66. "-Woverloaded-virtual"
  67. "-Wparentheses"
  68. "-Wpedantic"
  69. "-Wreorder"
  70. "-Wreturn-std-move"
  71. "-Wshadow"
  72. "-Wstrict-aliasing"
  73. "-Wsuggest-destructor-override"
  74. "-Wsuggest-override"
  75. "-Wundef"
  76. "-Wuninitialized"
  77. "-Wunneeded-internal-declaration"
  78. "-Wunreachable-code"
  79. "-Wunused"
  80. "-Wunused-function"
  81. "-Wunused-parameter"
  82. "-Wvla"
  83. "-Wweak-vtables"
  84. # This is a useful warning, but our tests sometimes rely on
  85. # functions being present, but not picked (e.g. various checks
  86. # for stringification implementation ordering).
  87. # Ergo, we should use it every now and then, but we cannot
  88. # enable it by default.
  89. # "-Wunused-member-function"
  90. )
  91. foreach(warning ${CHECKED_WARNING_FLAGS})
  92. add_cxx_flag_if_supported_to_targets(${warning} "${targets}")
  93. endforeach()
  94. if (CATCH_ENABLE_WERROR)
  95. foreach(target ${targets})
  96. # Enable Werror equivalent
  97. target_compile_options( ${target} PRIVATE -Werror )
  98. endforeach()
  99. endif()
  100. endif()
  101. endfunction()
  102. # Adds flags required for reproducible build to the target
  103. # Currently only supports GCC and Clang
  104. function(add_build_reproducibility_settings target)
  105. # Make the build reproducible on versions of g++ and clang that supports -ffile-prefix-map
  106. if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
  107. add_cxx_flag_if_supported_to_targets("-ffile-prefix-map=${CATCH_DIR}/=" "${target}")
  108. endif()
  109. endfunction()