sdlcompilers.cmake 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. macro(SDL_DetectCompiler)
  2. set(USE_CLANG FALSE)
  3. set(USE_GCC FALSE)
  4. set(USE_INTELCC FALSE)
  5. set(USE_QCC FALSE)
  6. if(CMAKE_C_COMPILER_ID MATCHES "Clang|IntelLLVM")
  7. set(USE_CLANG TRUE)
  8. # Visual Studio 2019 v16.2 added support for Clang/LLVM.
  9. # Check if a Visual Studio project is being generated with the Clang toolset.
  10. if(MSVC)
  11. set(MSVC_CLANG TRUE)
  12. endif()
  13. elseif(CMAKE_COMPILER_IS_GNUCC)
  14. set(USE_GCC TRUE)
  15. elseif(CMAKE_C_COMPILER_ID MATCHES "^Intel$")
  16. set(USE_INTELCC TRUE)
  17. elseif(CMAKE_C_COMPILER_ID MATCHES "QCC")
  18. set(USE_QCC TRUE)
  19. endif()
  20. endmacro()
  21. function(sdl_target_compile_option_all_languages TARGET OPTION)
  22. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:${OPTION}>")
  23. if(CMAKE_OBJC_COMPILER)
  24. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:OBJC>:${OPTION}>")
  25. endif()
  26. endfunction()
  27. function(SDL_AddCommonCompilerFlags TARGET)
  28. option(SDL_WERROR "Enable -Werror" OFF)
  29. if(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QCC)
  30. if(MINGW)
  31. # See if GCC's -gdwarf-4 is supported
  32. # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101377 for why this is needed on Windows
  33. cmake_push_check_state()
  34. check_c_compiler_flag("-gdwarf-4" HAVE_GDWARF_4)
  35. if(HAVE_GDWARF_4)
  36. target_compile_options(${TARGET} PRIVATE "-gdwarf-4")
  37. endif()
  38. cmake_pop_check_state()
  39. endif()
  40. # Check for -Wall first, so later things can override pieces of it.
  41. # Note: clang-cl treats -Wall as -Weverything (which is very loud),
  42. # /W3 as -Wall, and /W4 as -Wall -Wextra. So: /W3 is enough.
  43. check_c_compiler_flag(-Wall HAVE_GCC_WALL)
  44. if(MSVC_CLANG)
  45. target_compile_options(${TARGET} PRIVATE "/W3")
  46. elseif(HAVE_GCC_WALL)
  47. sdl_target_compile_option_all_languages(${TARGET} "-Wall")
  48. if(HAIKU)
  49. sdl_target_compile_option_all_languages(${TARGET} "-Wno-multichar")
  50. endif()
  51. endif()
  52. check_c_compiler_flag(-Wundef HAVE_GCC_WUNDEF)
  53. if(HAVE_GCC_WUNDEF)
  54. sdl_target_compile_option_all_languages(${TARGET} "-Wundef")
  55. endif()
  56. check_c_compiler_flag(-Wfloat-conversion HAVE_GCC_WFLOAT_CONVERSION)
  57. if(HAVE_GCC_WFLOAT_CONVERSION)
  58. sdl_target_compile_option_all_languages(${TARGET} "-Wfloat-conversion")
  59. endif()
  60. check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING)
  61. if(HAVE_GCC_NO_STRICT_ALIASING)
  62. sdl_target_compile_option_all_languages(${TARGET} "-fno-strict-aliasing")
  63. endif()
  64. check_c_compiler_flag(-Wdocumentation HAVE_GCC_WDOCUMENTATION)
  65. if(HAVE_GCC_WDOCUMENTATION)
  66. if(SDL_WERROR)
  67. check_c_compiler_flag(-Werror=documentation HAVE_GCC_WERROR_DOCUMENTATION)
  68. if(HAVE_GCC_WERROR_DOCUMENTATION)
  69. sdl_target_compile_option_all_languages(${TARGET} "-Werror=documentation")
  70. endif()
  71. endif()
  72. sdl_target_compile_option_all_languages(${TARGET} "-Wdocumentation")
  73. endif()
  74. check_c_compiler_flag(-Wdocumentation-unknown-command HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
  75. if(HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
  76. if(SDL_WERROR)
  77. check_c_compiler_flag(-Werror=documentation-unknown-command HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
  78. if(HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
  79. sdl_target_compile_option_all_languages(${TARGET} "-Werror=documentation-unknown-command")
  80. endif()
  81. endif()
  82. sdl_target_compile_option_all_languages(${TARGET} "-Wdocumentation-unknown-command")
  83. endif()
  84. check_c_compiler_flag(-fcomment-block-commands=threadsafety HAVE_GCC_COMMENT_BLOCK_COMMANDS)
  85. if(HAVE_GCC_COMMENT_BLOCK_COMMANDS)
  86. sdl_target_compile_option_all_languages(${TARGET} "-fcomment-block-commands=threadsafety")
  87. else()
  88. check_c_compiler_flag(/clang:-fcomment-block-commands=threadsafety HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
  89. if(HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
  90. sdl_target_compile_option_all_languages(${TARGET} "/clang:-fcomment-block-commands=threadsafety")
  91. endif()
  92. endif()
  93. check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
  94. if(HAVE_GCC_WSHADOW)
  95. sdl_target_compile_option_all_languages(${TARGET} "-Wshadow")
  96. endif()
  97. check_c_compiler_flag(-Wunused-local-typedefs HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
  98. if(HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
  99. sdl_target_compile_option_all_languages(${TARGET} "-Wno-unused-local-typedefs")
  100. endif()
  101. check_c_compiler_flag(-Wimplicit-fallthrough HAVE_GCC_WIMPLICIT_FALLTHROUGH)
  102. if(HAVE_GCC_WIMPLICIT_FALLTHROUGH)
  103. sdl_target_compile_option_all_languages(${TARGET} "-Wimplicit-fallthrough")
  104. endif()
  105. endif()
  106. if(SDL_WERROR)
  107. if(MSVC)
  108. check_c_compiler_flag(/WX HAVE_WX)
  109. if(HAVE_WX)
  110. target_compile_options(${TARGET} PRIVATE "/WX")
  111. endif()
  112. elseif(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QNX)
  113. check_c_compiler_flag(-Werror HAVE_WERROR)
  114. if(HAVE_WERROR)
  115. sdl_target_compile_option_all_languages(${TARGET} "-Werror")
  116. endif()
  117. endif()
  118. endif()
  119. if(USE_CLANG)
  120. check_c_compiler_flag("-fcolor-diagnostics" COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
  121. if(COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
  122. sdl_target_compile_option_all_languages(${TARGET} "-fcolor-diagnostics")
  123. endif()
  124. else()
  125. check_c_compiler_flag("-fdiagnostics-color=always" COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
  126. if(COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
  127. sdl_target_compile_option_all_languages(${TARGET} "-fdiagnostics-color=always")
  128. endif()
  129. endif()
  130. endfunction()