common_compile_options.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Flags shared by Lagom (including Ladybird) and Serenity.
  2. set(CMAKE_CXX_STANDARD 23)
  3. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  4. set(CMAKE_CXX_EXTENSIONS OFF)
  5. set(CMAKE_COLOR_DIAGNOSTICS ON)
  6. macro(add_cxx_compile_options)
  7. set(args "")
  8. foreach(arg ${ARGN})
  9. string(APPEND args ${arg}$<SEMICOLON>)
  10. add_compile_options("SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc ${arg}>")
  11. endforeach()
  12. add_compile_options($<$<COMPILE_LANGUAGE:C,CXX,ASM>:${args}>)
  13. endmacro()
  14. macro(add_cxx_link_options)
  15. set(args "")
  16. foreach(arg ${ARGN})
  17. string(APPEND args ${arg}$<SEMICOLON>)
  18. endforeach()
  19. add_link_options($<$<LINK_LANGUAGE:C,CXX>:${args}>)
  20. endmacro()
  21. macro(add_swift_compile_options)
  22. set(args "")
  23. foreach(arg ${ARGN})
  24. string(APPEND args ${arg}$<SEMICOLON>)
  25. endforeach()
  26. add_compile_options($<$<COMPILE_LANGUAGE:Swift>:${args}>)
  27. endmacro()
  28. macro(add_swift_link_options)
  29. set(args "")
  30. foreach(arg ${ARGN})
  31. string(APPEND args ${arg}$<SEMICOLON>)
  32. endforeach()
  33. add_link_options($<$<LINK_LANGUAGE:Swift>:${args}>)
  34. endmacro()
  35. # FIXME: Rework these flags to remove the suspicious ones.
  36. if (WIN32)
  37. add_compile_options(-Wno-unknown-attributes) # [[no_unique_address]] is broken in MSVC ABI until next ABI break
  38. add_compile_options(-Wno-reinterpret-base-class)
  39. add_compile_options(-Wno-microsoft-unqualified-friend) # MSVC doesn't support unqualified friends
  40. add_compile_definitions(_CRT_SECURE_NO_WARNINGS) # _s replacements not desired (or implemented on any other platform other than VxWorks)
  41. add_compile_definitions(_CRT_NONSTDC_NO_WARNINGS) # POSIX names are just fine, thanks
  42. add_compile_definitions(_USE_MATH_DEFINES)
  43. add_compile_definitions(NOMINMAX)
  44. add_compile_definitions(WIN32_LEAN_AND_MEAN)
  45. add_compile_definitions(NAME_MAX=255)
  46. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  47. add_compile_options(-Wno-deprecated-declarations)
  48. endif()
  49. if (MSVC)
  50. add_cxx_compile_options(/W4)
  51. # disable exceptions
  52. add_cxx_compile_options(/EHsc)
  53. # disable floating-point expression contraction
  54. add_cxx_compile_options(/fp:precise)
  55. else()
  56. add_cxx_compile_options(-Wall -Wextra)
  57. add_cxx_compile_options(-fno-exceptions)
  58. add_cxx_compile_options(-ffp-contract=off)
  59. endif()
  60. add_cxx_compile_options(-Wcast-qual)
  61. add_cxx_compile_options(-Wformat=2)
  62. add_cxx_compile_options(-Wimplicit-fallthrough)
  63. add_cxx_compile_options(-Wmissing-declarations)
  64. add_cxx_compile_options(-Wsuggest-override)
  65. add_cxx_compile_options(-Wno-invalid-offsetof)
  66. add_cxx_compile_options(-Wno-unknown-warning-option)
  67. add_cxx_compile_options(-Wno-unused-command-line-argument)
  68. if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "18")
  69. add_cxx_compile_options(-Wpadded-bitfield)
  70. endif()
  71. if (NOT CMAKE_HOST_SYSTEM_NAME MATCHES SerenityOS)
  72. # FIXME: Something makes this go crazy and flag unused variables that aren't flagged as such when building with the toolchain.
  73. # Disable -Werror for now.
  74. add_cxx_compile_options(-Werror)
  75. endif()
  76. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
  77. # Clang's default constexpr-steps limit is 1048576(2^20), GCC doesn't have one
  78. add_cxx_compile_options(-fconstexpr-steps=16777216)
  79. add_cxx_compile_options(-Wmissing-prototypes)
  80. add_cxx_compile_options(-Wno-implicit-const-int-float-conversion)
  81. add_cxx_compile_options(-Wno-user-defined-literals)
  82. add_cxx_compile_options(-Wno-vla-cxx-extension)
  83. add_cxx_compile_options(-Wno-unqualified-std-cast-call)
  84. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  85. # Only ignore expansion-to-defined for g++, clang's implementation doesn't complain about function-like macros
  86. add_cxx_compile_options(-Wno-expansion-to-defined)
  87. add_cxx_compile_options(-Wno-literal-suffix)
  88. # FIXME: This warning seems useful but has too many false positives with GCC 13.
  89. add_cxx_compile_options(-Wno-dangling-reference)
  90. elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang$" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
  91. add_cxx_compile_options(-Wno-reserved-identifier)
  92. add_cxx_compile_options(-Wno-user-defined-literals)
  93. add_cxx_compile_options(-Wno-unqualified-std-cast-call)
  94. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  95. # TODO: this seems wrong, but we use this kind of code too much
  96. # add_cxx_compile_options(-Wno-unsafe-buffer-usage)
  97. endif()
  98. if (UNIX AND NOT APPLE AND NOT ENABLE_FUZZERS)
  99. add_cxx_compile_options(-fno-semantic-interposition)
  100. add_cxx_compile_options(-fvisibility-inlines-hidden)
  101. endif()
  102. if (NOT WIN32)
  103. add_cxx_compile_options(-fstack-protector-strong)
  104. add_cxx_link_options(-fstack-protector-strong)
  105. endif()
  106. if (NOT MSVC)
  107. add_cxx_compile_options(-fstrict-flex-arrays=2)
  108. endif()