CompileOptions.cmake 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. if(NOT DEFINED COVERAGE)
  2. set(COVERAGE OFF)
  3. endif()
  4. if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
  5. add_compile_options(
  6. -pedantic
  7. -Wall
  8. -Wcast-align
  9. -Wcast-qual
  10. -Wconversion
  11. -Wctor-dtor-privacy
  12. -Wdisabled-optimization
  13. -Werror
  14. -Wextra
  15. -Wformat=2
  16. -Winit-self
  17. -Wmissing-include-dirs
  18. -Wnon-virtual-dtor
  19. -Wold-style-cast
  20. -Woverloaded-virtual
  21. -Wparentheses
  22. -Wredundant-decls
  23. -Wshadow
  24. -Wsign-conversion
  25. -Wsign-promo
  26. -Wstrict-aliasing
  27. -Wundef
  28. )
  29. if(${COVERAGE})
  30. set(CMAKE_CXX_FLAGS "-fprofile-arcs -ftest-coverage")
  31. endif()
  32. endif()
  33. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  34. if((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9) AND(NOT ${COVERAGE}))
  35. add_compile_options(-g -Og)
  36. else() # GCC 4.8
  37. add_compile_options(
  38. -g
  39. -O0 # GCC 4.8 doesn't support -Og
  40. -Wno-shadow # allow the same name for a function parameter and a member functions
  41. -Wp,-w # Disable preprocessing warnings (see below)
  42. )
  43. # GCC 4.8 doesn't support __has_include, so we need to help him
  44. add_definitions(
  45. -DARDUINOJSON_ENABLE_STD_STRING=1
  46. -DARDUINOJSON_ENABLE_STD_STREAM=1
  47. )
  48. endif()
  49. add_compile_options(
  50. -Wstrict-null-sentinel
  51. -Wno-vla # Allow VLA in tests
  52. )
  53. add_definitions(-DHAS_VARIABLE_LENGTH_ARRAY)
  54. if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.5)
  55. add_compile_options(-Wlogical-op) # the flag exists in 4.4 but is buggy
  56. endif()
  57. if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.6)
  58. add_compile_options(-Wnoexcept)
  59. endif()
  60. endif()
  61. if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  62. add_compile_options(
  63. -Wc++11-compat
  64. -Wdeprecated-register
  65. -Wno-vla-extension # Allow VLA in tests
  66. )
  67. add_definitions(
  68. -DHAS_VARIABLE_LENGTH_ARRAY
  69. -DSUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR
  70. )
  71. endif()
  72. if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  73. add_compile_options(-stdlib=libc++)
  74. link_libraries(c++ m)
  75. if((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.0) AND(NOT ${COVERAGE}))
  76. add_compile_options(-g -Og)
  77. else()
  78. add_compile_options(-g -O0)
  79. endif()
  80. endif()
  81. if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
  82. if((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 9.0) AND(NOT ${COVERAGE}))
  83. add_compile_options(-g -Og)
  84. else()
  85. add_compile_options(-g -O0)
  86. endif()
  87. endif()
  88. if(MSVC)
  89. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  90. add_compile_options(
  91. /W4 # Set warning level
  92. /WX # Treats all compiler warnings as errors.
  93. /Zc:__cplusplus # Enable updated __cplusplus macro
  94. )
  95. endif()
  96. if(MINGW)
  97. # Static link on MinGW to avoid linking with the wrong DLLs when multiple
  98. # versions are installed.
  99. add_link_options(-static)
  100. endif()