Atlas - sdlcompilers.cmake
Home / ext / SDL / cmake Lines: 1 | Size: 7995 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1macro(SDL_DetectCompiler) 2 set(USE_CLANG FALSE) 3 set(USE_GCC FALSE) 4 set(USE_INTELCC FALSE) 5 set(USE_QCC FALSE) 6 set(USE_TCC FALSE) 7 if(CMAKE_C_COMPILER_ID MATCHES "Clang|IntelLLVM") 8 set(USE_CLANG TRUE) 9 # Visual Studio 2019 v16.2 added support for Clang/LLVM. 10 # Check if a Visual Studio project is being generated with the Clang toolset. 11 if(MSVC) 12 set(MSVC_CLANG TRUE) 13 endif() 14 elseif(CMAKE_COMPILER_IS_GNUCC) 15 set(USE_GCC TRUE) 16 elseif(CMAKE_C_COMPILER_ID MATCHES "^Intel$") 17 set(USE_INTELCC TRUE) 18 elseif(CMAKE_C_COMPILER_ID MATCHES "QCC") 19 set(USE_QCC TRUE) 20 elseif(CMAKE_C_COMPILER_ID MATCHES "TinyCC") 21 set(USE_TCC TRUE) 22 endif() 23endmacro() 24 25function(sdl_target_compile_option_all_languages TARGET OPTION) 26 target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:${OPTION}>") 27 if(CMAKE_OBJC_COMPILER) 28 target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:OBJC>:${OPTION}>") 29 endif() 30endfunction() 31 32function(SDL_AddCommonCompilerFlags TARGET) 33 option(SDL_WERROR "Enable -Werror" OFF) 34 35 get_property(TARGET_TYPE TARGET "${TARGET}" PROPERTY TYPE) 36 if(MSVC) 37 cmake_push_check_state() 38 check_c_compiler_flag("/W3" COMPILER_SUPPORTS_W3) 39 if(COMPILER_SUPPORTS_W3) 40 target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:/W3>") 41 endif() 42 cmake_pop_check_state() 43 endif() 44 45 if(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QCC OR USE_TCC) 46 if(MINGW) 47 # See if GCC's -gdwarf-4 is supported 48 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101377 for why this is needed on Windows 49 cmake_push_check_state() 50 check_c_compiler_flag("-gdwarf-4" HAVE_GDWARF_4) 51 if(HAVE_GDWARF_4) 52 target_compile_options(${TARGET} PRIVATE "$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:-gdwarf-4>") 53 endif() 54 cmake_pop_check_state() 55 endif() 56 57 # Check for -Wall first, so later things can override pieces of it. 58 # Note: clang-cl treats -Wall as -Weverything (which is very loud), 59 # /W3 as -Wall, and /W4 as -Wall -Wextra. So: /W3 is enough. 60 check_c_compiler_flag(-Wall HAVE_GCC_WALL) 61 if(MSVC_CLANG) 62 target_compile_options(${TARGET} PRIVATE "/W3") 63 elseif(HAVE_GCC_WALL) 64 sdl_target_compile_option_all_languages(${TARGET} "-Wall") 65 if(HAIKU) 66 sdl_target_compile_option_all_languages(${TARGET} "-Wno-multichar") 67 endif() 68 endif() 69 70 check_c_compiler_flag(-Wundef HAVE_GCC_WUNDEF) 71 if(HAVE_GCC_WUNDEF) 72 sdl_target_compile_option_all_languages(${TARGET} "-Wundef") 73 endif() 74 75 check_c_compiler_flag(-Wfloat-conversion HAVE_GCC_WFLOAT_CONVERSION) 76 if(HAVE_GCC_WFLOAT_CONVERSION) 77 sdl_target_compile_option_all_languages(${TARGET} "-Wfloat-conversion") 78 endif() 79 80 check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING) 81 if(HAVE_GCC_NO_STRICT_ALIASING) 82 sdl_target_compile_option_all_languages(${TARGET} "-fno-strict-aliasing") 83 endif() 84 85 check_c_compiler_flag(-Wdocumentation HAVE_GCC_WDOCUMENTATION) 86 if(HAVE_GCC_WDOCUMENTATION) 87 if(SDL_WERROR) 88 check_c_compiler_flag(-Werror=documentation HAVE_GCC_WERROR_DOCUMENTATION) 89 if(HAVE_GCC_WERROR_DOCUMENTATION) 90 sdl_target_compile_option_all_languages(${TARGET} "-Werror=documentation") 91 endif() 92 endif() 93 sdl_target_compile_option_all_languages(${TARGET} "-Wdocumentation") 94 endif() 95 96 check_c_compiler_flag(-Wdocumentation-unknown-command HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND) 97 if(HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND) 98 if(SDL_WERROR) 99 check_c_compiler_flag(-Werror=documentation-unknown-command HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND) 100 if(HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND) 101 sdl_target_compile_option_all_languages(${TARGET} "-Werror=documentation-unknown-command") 102 endif() 103 endif() 104 sdl_target_compile_option_all_languages(${TARGET} "-Wdocumentation-unknown-command") 105 endif() 106 107 check_c_compiler_flag(-fcomment-block-commands=threadsafety HAVE_GCC_COMMENT_BLOCK_COMMANDS) 108 if(HAVE_GCC_COMMENT_BLOCK_COMMANDS) 109 sdl_target_compile_option_all_languages(${TARGET} "-fcomment-block-commands=threadsafety") 110 else() 111 check_c_compiler_flag(/clang:-fcomment-block-commands=threadsafety HAVE_CLANG_COMMENT_BLOCK_COMMANDS) 112 if(HAVE_CLANG_COMMENT_BLOCK_COMMANDS) 113 sdl_target_compile_option_all_languages(${TARGET} "/clang:-fcomment-block-commands=threadsafety") 114 endif() 115 endif() 116 117 check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW) 118 if(HAVE_GCC_WSHADOW) 119 sdl_target_compile_option_all_languages(${TARGET} "-Wshadow") 120 endif() 121 122 check_c_compiler_flag(-Wunused-local-typedefs HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS) 123 if(HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS) 124 sdl_target_compile_option_all_languages(${TARGET} "-Wno-unused-local-typedefs") 125 endif() 126 127 check_c_compiler_flag(-Wimplicit-fallthrough HAVE_GCC_WIMPLICIT_FALLTHROUGH) 128 if(HAVE_GCC_WIMPLICIT_FALLTHROUGH) 129 sdl_target_compile_option_all_languages(${TARGET} "-Wimplicit-fallthrough") 130 endif() 131 endif() 132 133 if(SDL_WERROR) 134 if(MSVC) 135 check_c_compiler_flag(/WX HAVE_WX) 136 if(HAVE_WX) 137 target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:/WX>") 138 endif() 139 elseif(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QNX) 140 check_c_compiler_flag(-Werror HAVE_WERROR) 141 if(HAVE_WERROR) 142 sdl_target_compile_option_all_languages(${TARGET} "-Werror") 143 endif() 144 145 if(TARGET_TYPE STREQUAL "SHARED_LIBRARY") 146 check_linker_flag(C "-Wl,--no-undefined-version" LINKER_SUPPORTS_NO_UNDEFINED_VERSION) 147 if(LINKER_SUPPORTS_NO_UNDEFINED_VERSION) 148 target_link_options(${TARGET} PRIVATE "-Wl,--no-undefined-version") 149 endif() 150 endif() 151 endif() 152 endif() 153 154 if(USE_CLANG) 155 check_c_compiler_flag("-fcolor-diagnostics" COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS) 156 if(COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS) 157 sdl_target_compile_option_all_languages(${TARGET} "-fcolor-diagnostics") 158 endif() 159 else() 160 check_c_compiler_flag("-fdiagnostics-color=always" COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS) 161 if(COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS) 162 sdl_target_compile_option_all_languages(${TARGET} "-fdiagnostics-color=always") 163 endif() 164 endif() 165 166 if(USE_TCC) 167 sdl_target_compile_option_all_languages(${TARGET} "-DSTBI_NO_SIMD") 168 endif() 169endfunction() 170 171function(check_x86_source_compiles BODY VAR) 172 if(ARGN) 173 message(FATAL_ERROR "Unknown arguments: ${ARGN}") 174 endif() 175 if(APPLE_MULTIARCH AND (SDL_CPU_X86 OR SDL_CPU_X64)) 176 set(test_conditional 1) 177 else() 178 set(test_conditional 0) 179 endif() 180 check_c_source_compiles(" 181 #if ${test_conditional} 182 # if defined(__i386__) || defined(__x86_64__) 183 # define test_enabled 1 184 # else 185 # define test_enabled 0 /* feign success in Apple multi-arch configs */ 186 # endif 187 #else /* test normally */ 188 # define test_enabled 1 189 #endif 190 #if test_enabled 191 ${BODY} 192 #else 193 int main(int argc, char *argv[]) { 194 (void)argc; 195 (void)argv; 196 return 0; 197 } 198 #endif" ${VAR}) 199endfunction() 200 201function(check_arm_source_compiles BODY VAR) 202 if(ARGN) 203 message(FATAL_ERROR "Unknown arguments: ${ARGN}") 204 endif() 205 if(APPLE_MULTIARCH AND (SDL_CPU_ARM32 OR SDL_CPU_ARM64)) 206 set(test_conditional 1) 207 else() 208 set(test_conditional 0) 209 endif() 210 check_c_source_compiles(" 211 #if ${test_conditional} 212 # if defined(__arm__) || defined(__aarch64__) 213 # define test_enabled 1 214 # else 215 # define test_enabled 0 /* feign success in Apple multi-arch configs */ 216 # endif 217 #else /* test normally */ 218 # define test_enabled 1 219 #endif 220 #if test_enabled 221 ${BODY} 222 #else 223 int main(int argc, char *argv[]) { 224 (void)argc; 225 (void)argv; 226 return 0; 227 } 228 #endif" ${VAR}) 229endfunction() 230[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.