diff --git a/cmake/SDL.cmake b/cmake/SDL.cmake index cae0c82097a..0cfa5afc2e8 100644 --- a/cmake/SDL.cmake +++ b/cmake/SDL.cmake @@ -27,16 +27,18 @@ include("cmake/utils.cmake") # The flags that can be used for the main and host compilers should be moved to # the macros to avoid code duplication and ensure consistency. macro(sdl_unix_common_ccxx_flags var) - append(${var} "-fPIC -Wformat -Wformat-security") + test_compiler_and_add_flag("-fPIC" ${var}) + test_compiler_and_add_flag("-Wformat" ${var}) + test_compiler_and_add_flag("-Wformat-security" ${var}) endmacro() macro(sdl_gnu_common_ccxx_flags var gnu_version) if(${gnu_version} VERSION_LESS 4.9) - append(${var} "-fstack-protector-all") + test_compiler_and_add_flag("-fstack-protector-all" ${var}) else() - append(${var} "-fstack-protector-strong") + test_compiler_and_add_flag("-fstack-protector-strong" ${var}) if(NOT (${gnu_version} VERSION_LESS 8.0) AND (DNNL_TARGET_ARCH STREQUAL "X64")) - append(${var} "-fcf-protection=full") + test_compiler_and_add_flag("-fcf-protection=full" ${var}) endif() endif() endmacro() @@ -48,7 +50,7 @@ endmacro() # this warning on, let's use it too. Applicable for the library sources # and interfaces only (tests currently rely on that fact heavily) macro(sdl_gnu_src_ccxx_flags var) - append(${var} "-Wmissing-field-initializers") + test_compiler_and_add_flag("-Wmissing-field-initializers" ${var}) endmacro() macro(sdl_gnu_example_ccxx_flags var) @@ -62,7 +64,7 @@ set(ONEDNN_SDL_LINKER_FLAGS) if(UNIX) sdl_unix_common_ccxx_flags(ONEDNN_SDL_COMPILER_FLAGS) if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL "RELEASE") - append(ONEDNN_SDL_COMPILER_FLAGS "-D_FORTIFY_SOURCE=2") + test_compiler_and_add_flag("-D_FORTIFY_SOURCE=2" ONEDNN_SDL_COMPILER_FLAGS) endif() if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") sdl_gnu_common_ccxx_flags(ONEDNN_SDL_COMPILER_FLAGS CMAKE_CXX_COMPILER_VERSION) @@ -72,10 +74,10 @@ if(UNIX) get_filename_component(CXX_CMD_NAME ${CMAKE_CXX_COMPILER} NAME) # Fujitsu CXX compiler does not support "-fstack-protector-all". if(NOT CXX_CMD_NAME STREQUAL "FCC") - append(ONEDNN_SDL_COMPILER_FLAGS "-fstack-protector-all") + test_compiler_and_add_flag("-fstack-protector-all" ONEDNN_SDL_COMPILER_FLAGS) endif() elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") - append(ONEDNN_SDL_COMPILER_FLAGS "-fstack-protector") + test_compiler_and_add_flag("-fstack-protector" ONEDNN_SDL_COMPILER_FLAGS) endif() if(APPLE) append(ONEDNN_SDL_LINKER_FLAGS "-Wl,-bind_at_load") @@ -86,20 +88,29 @@ if(UNIX) endif() elseif(WIN32) if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - append(ONEDNN_SDL_COMPILER_FLAGS "/GS /Gy /guard:cf /DYNAMICBASE /sdl") + test_compiler_and_add_flag("/GS" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("/Gy" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("/guard:cf" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("/DYNAMICBASE" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("/sdl" ONEDNN_SDL_COMPILER_FLAGS) append(ONEDNN_SDL_LINKER_FLAGS "/NXCOMPAT /LTCG") elseif(CMAKE_BASE_NAME STREQUAL "icx") - append(ONEDNN_SDL_COMPILER_FLAGS "/GS /Gy /guard:cf /Wformat /Wformat-security") + test_compiler_and_add_flag("/GS" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("/Gy" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("/guard:cf" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("/Wformat" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("/Wformat-security" ONEDNN_SDL_COMPILER_FLAGS) append(ONEDNN_SDL_LINKER_FLAGS "/link /NXCOMPAT") elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - append(ONEDNN_SDL_COMPILER_FLAGS "-Wformat -Wformat-security") + test_compiler_and_add_flag("-Wformat" ONEDNN_SDL_COMPILER_FLAGS) + test_compiler_and_add_flag("-Wformat-security" ONEDNN_SDL_COMPILER_FLAGS) if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL "RELEASE") - append(ONEDNN_SDL_COMPILER_FLAGS "-D_FORTIFY_SOURCE=2") + test_compiler_and_add_flag("-D_FORTIFY_SOURCE=2" ONEDNN_SDL_COMPILER_FLAGS) endif() get_filename_component(CXX_CMD_NAME ${CMAKE_CXX_COMPILER} NAME) # Fujitsu CXX compiler does not support "-fstack-protector-all". if(NOT CXX_CMD_NAME STREQUAL "FCC") - append(ONEDNN_SDL_COMPILER_FLAGS "-fstack-protector-all") + test_compiler_and_add_flag("-fstack-protector-all" ONEDNN_SDL_COMPILER_FLAGS) endif() append(ONEDNN_SDL_LINKER_FLAGS "-Xlinker /NXCOMPAT -Xlinker /LTCG") endif() diff --git a/cmake/utils.cmake b/cmake/utils.cmake index fdd6b2c95dc..c9cb780903d 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -174,3 +174,17 @@ function(find_libm var) find_library(${var} m REQUIRED) endif() endfunction() + +include(CheckCXXCompilerFlag) +macro(test_compiler_and_add_flag flag var) + set(CMAKE_REQUIRED_QUIET 1) + unset(COMPILER_SUPPORTS_FLAG CACHE) + + if(NOT test_compiler_flag_nowarn) + append(CMAKE_REQUIRED_FLAGS "${CMAKE_CCXX_NOWARN_FLAGS}") + set(test_compiler_flag_nowarn true) + endif() + + check_cxx_compiler_flag("${flag}" COMPILER_SUPPORTS_FLAG) + append_if(COMPILER_SUPPORTS_FLAG ${var} ${flag}) +endmacro()