sdlcompilers.cmake 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_AddCommonCompilerFlags TARGET)
  22. option(SDL_WERROR "Enable -Werror" OFF)
  23. if(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QCC)
  24. if(MINGW)
  25. # See if GCC's -gdwarf-4 is supported
  26. # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101377 for why this is needed on Windows
  27. cmake_push_check_state()
  28. check_c_compiler_flag("-gdwarf-4" HAVE_GDWARF_4)
  29. if(HAVE_GDWARF_4)
  30. target_compile_options(${TARGET} PRIVATE "-gdwarf-4")
  31. endif()
  32. cmake_pop_check_state()
  33. endif()
  34. # Check for -Wall first, so later things can override pieces of it.
  35. # Note: clang-cl treats -Wall as -Weverything (which is very loud),
  36. # /W3 as -Wall, and /W4 as -Wall -Wextra. So: /W3 is enough.
  37. check_c_compiler_flag(-Wall HAVE_GCC_WALL)
  38. if(MSVC_CLANG)
  39. target_compile_options(${TARGET} PRIVATE "/W3")
  40. elseif(HAVE_GCC_WALL)
  41. target_compile_options(${TARGET} PRIVATE "-Wall")
  42. if(HAIKU)
  43. target_compile_options(${TARGET} PRIVATE "-Wno-multichar")
  44. endif()
  45. endif()
  46. check_c_compiler_flag(-Wundef HAVE_GCC_WUNDEF)
  47. if(HAVE_GCC_WUNDEF)
  48. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wundef>")
  49. endif()
  50. check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING)
  51. if(HAVE_GCC_NO_STRICT_ALIASING)
  52. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fno-strict-aliasing>")
  53. endif()
  54. check_c_compiler_flag(-Wdocumentation HAVE_GCC_WDOCUMENTATION)
  55. if(HAVE_GCC_WDOCUMENTATION)
  56. if(SDL_WERROR)
  57. check_c_compiler_flag(-Werror=documentation HAVE_GCC_WERROR_DOCUMENTATION)
  58. if(HAVE_GCC_WERROR_DOCUMENTATION)
  59. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Werror=documentation>")
  60. endif()
  61. endif()
  62. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wdocumentation>")
  63. endif()
  64. check_c_compiler_flag(-Wdocumentation-unknown-command HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
  65. if(HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND)
  66. if(SDL_WERROR)
  67. check_c_compiler_flag(-Werror=documentation-unknown-command HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
  68. if(HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND)
  69. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Werror=documentation-unknown-command>")
  70. endif()
  71. endif()
  72. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wdocumentation-unknown-command>")
  73. endif()
  74. check_c_compiler_flag(-fcomment-block-commands=threadsafety HAVE_GCC_COMMENT_BLOCK_COMMANDS)
  75. if(HAVE_GCC_COMMENT_BLOCK_COMMANDS)
  76. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fcomment-block-commands=threadsafety>")
  77. else()
  78. check_c_compiler_flag(/clang:-fcomment-block-commands=threadsafety HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
  79. if(HAVE_CLANG_COMMENT_BLOCK_COMMANDS)
  80. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:/clang:-fcomment-block-commands=threadsafety>")
  81. endif()
  82. endif()
  83. check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
  84. if(HAVE_GCC_WSHADOW)
  85. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wshadow>")
  86. endif()
  87. check_c_compiler_flag(-Wunused-local-typedefs HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
  88. if(HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS)
  89. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Wno-unused-local-typedefs>")
  90. endif()
  91. endif()
  92. if(SDL_WERROR)
  93. if(MSVC)
  94. check_c_compiler_flag(/WX HAVE_WX)
  95. if(HAVE_WX)
  96. target_compile_options(${TARGET} PRIVATE "/WX")
  97. endif()
  98. elseif(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QNX)
  99. check_c_compiler_flag(-Werror HAVE_WERROR)
  100. if(HAVE_WERROR)
  101. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-Werror>")
  102. endif()
  103. endif()
  104. endif()
  105. if(USE_CLANG)
  106. check_c_compiler_flag("-fcolor-diagnostics" COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
  107. if(COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS)
  108. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fcolor-diagnostics>")
  109. endif()
  110. else()
  111. check_c_compiler_flag("-fdiagnostics-color=always" COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
  112. if(COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS)
  113. target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fdiagnostics-color=always>")
  114. endif()
  115. endif()
  116. endfunction()