Simple Directmedia Layer
at main 6.2 kB view raw
1macro(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() 20endmacro() 21 22function(sdl_target_compile_option_all_languages TARGET OPTION) 23 target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:${OPTION}>") 24 if(CMAKE_OBJC_COMPILER) 25 target_compile_options(${TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:OBJC>:${OPTION}>") 26 endif() 27endfunction() 28 29function(SDL_AddCommonCompilerFlags TARGET) 30 option(SDL_WERROR "Enable -Werror" OFF) 31 32 get_property(TARGET_TYPE TARGET "${TARGET}" PROPERTY TYPE) 33 if(MSVC) 34 cmake_push_check_state() 35 check_c_compiler_flag("/W3" COMPILER_SUPPORTS_W3) 36 if(COMPILER_SUPPORTS_W3) 37 target_compile_options(${TARGET} PRIVATE "/W3") 38 endif() 39 cmake_pop_check_state() 40 endif() 41 42 if(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QCC) 43 if(MINGW) 44 # See if GCC's -gdwarf-4 is supported 45 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101377 for why this is needed on Windows 46 cmake_push_check_state() 47 check_c_compiler_flag("-gdwarf-4" HAVE_GDWARF_4) 48 if(HAVE_GDWARF_4) 49 target_compile_options(${TARGET} PRIVATE "$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:-gdwarf-4>") 50 endif() 51 cmake_pop_check_state() 52 endif() 53 54 # Check for -Wall first, so later things can override pieces of it. 55 # Note: clang-cl treats -Wall as -Weverything (which is very loud), 56 # /W3 as -Wall, and /W4 as -Wall -Wextra. So: /W3 is enough. 57 check_c_compiler_flag(-Wall HAVE_GCC_WALL) 58 if(MSVC_CLANG) 59 target_compile_options(${TARGET} PRIVATE "/W3") 60 elseif(HAVE_GCC_WALL) 61 sdl_target_compile_option_all_languages(${TARGET} "-Wall") 62 if(HAIKU) 63 sdl_target_compile_option_all_languages(${TARGET} "-Wno-multichar") 64 endif() 65 endif() 66 67 check_c_compiler_flag(-Wundef HAVE_GCC_WUNDEF) 68 if(HAVE_GCC_WUNDEF) 69 sdl_target_compile_option_all_languages(${TARGET} "-Wundef") 70 endif() 71 72 check_c_compiler_flag(-Wfloat-conversion HAVE_GCC_WFLOAT_CONVERSION) 73 if(HAVE_GCC_WFLOAT_CONVERSION) 74 sdl_target_compile_option_all_languages(${TARGET} "-Wfloat-conversion") 75 endif() 76 77 check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING) 78 if(HAVE_GCC_NO_STRICT_ALIASING) 79 sdl_target_compile_option_all_languages(${TARGET} "-fno-strict-aliasing") 80 endif() 81 82 check_c_compiler_flag(-Wdocumentation HAVE_GCC_WDOCUMENTATION) 83 if(HAVE_GCC_WDOCUMENTATION) 84 if(SDL_WERROR) 85 check_c_compiler_flag(-Werror=documentation HAVE_GCC_WERROR_DOCUMENTATION) 86 if(HAVE_GCC_WERROR_DOCUMENTATION) 87 sdl_target_compile_option_all_languages(${TARGET} "-Werror=documentation") 88 endif() 89 endif() 90 sdl_target_compile_option_all_languages(${TARGET} "-Wdocumentation") 91 endif() 92 93 check_c_compiler_flag(-Wdocumentation-unknown-command HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND) 94 if(HAVE_GCC_WDOCUMENTATION_UNKNOWN_COMMAND) 95 if(SDL_WERROR) 96 check_c_compiler_flag(-Werror=documentation-unknown-command HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND) 97 if(HAVE_GCC_WERROR_DOCUMENTATION_UNKNOWN_COMMAND) 98 sdl_target_compile_option_all_languages(${TARGET} "-Werror=documentation-unknown-command") 99 endif() 100 endif() 101 sdl_target_compile_option_all_languages(${TARGET} "-Wdocumentation-unknown-command") 102 endif() 103 104 check_c_compiler_flag(-fcomment-block-commands=threadsafety HAVE_GCC_COMMENT_BLOCK_COMMANDS) 105 if(HAVE_GCC_COMMENT_BLOCK_COMMANDS) 106 sdl_target_compile_option_all_languages(${TARGET} "-fcomment-block-commands=threadsafety") 107 else() 108 check_c_compiler_flag(/clang:-fcomment-block-commands=threadsafety HAVE_CLANG_COMMENT_BLOCK_COMMANDS) 109 if(HAVE_CLANG_COMMENT_BLOCK_COMMANDS) 110 sdl_target_compile_option_all_languages(${TARGET} "/clang:-fcomment-block-commands=threadsafety") 111 endif() 112 endif() 113 114 check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW) 115 if(HAVE_GCC_WSHADOW) 116 sdl_target_compile_option_all_languages(${TARGET} "-Wshadow") 117 endif() 118 119 check_c_compiler_flag(-Wunused-local-typedefs HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS) 120 if(HAVE_GCC_WUNUSED_LOCAL_TYPEDEFS) 121 sdl_target_compile_option_all_languages(${TARGET} "-Wno-unused-local-typedefs") 122 endif() 123 124 check_c_compiler_flag(-Wimplicit-fallthrough HAVE_GCC_WIMPLICIT_FALLTHROUGH) 125 if(HAVE_GCC_WIMPLICIT_FALLTHROUGH) 126 sdl_target_compile_option_all_languages(${TARGET} "-Wimplicit-fallthrough") 127 endif() 128 endif() 129 130 if(SDL_WERROR) 131 if(MSVC) 132 check_c_compiler_flag(/WX HAVE_WX) 133 if(HAVE_WX) 134 target_compile_options(${TARGET} PRIVATE "/WX") 135 endif() 136 elseif(USE_GCC OR USE_CLANG OR USE_INTELCC OR USE_QNX) 137 check_c_compiler_flag(-Werror HAVE_WERROR) 138 if(HAVE_WERROR) 139 sdl_target_compile_option_all_languages(${TARGET} "-Werror") 140 endif() 141 142 if(TARGET_TYPE STREQUAL "SHARED_LIBRARY") 143 check_linker_flag(C "-Wl,--no-undefined-version" LINKER_SUPPORTS_NO_UNDEFINED_VERSION) 144 if(LINKER_SUPPORTS_NO_UNDEFINED_VERSION) 145 target_link_options(${TARGET} PRIVATE "-Wl,--no-undefined-version") 146 endif() 147 endif() 148 endif() 149 endif() 150 151 if(USE_CLANG) 152 check_c_compiler_flag("-fcolor-diagnostics" COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS) 153 if(COMPILER_SUPPORTS_FCOLOR_DIAGNOSTICS) 154 sdl_target_compile_option_all_languages(${TARGET} "-fcolor-diagnostics") 155 endif() 156 else() 157 check_c_compiler_flag("-fdiagnostics-color=always" COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS) 158 if(COMPILER_SUPPORTS_FDIAGNOSTICS_COLOR_ALWAYS) 159 sdl_target_compile_option_all_languages(${TARGET} "-fdiagnostics-color=always") 160 endif() 161 endif() 162endfunction()