Debian Codegraph @

cmake/sdlcompilers.cmake

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