Skip to content

Commit

Permalink
build: add check_cxx_compiler_flag for SDL flags
Browse files Browse the repository at this point in the history
  • Loading branch information
spalicki authored and mgouicem committed Dec 4, 2024
1 parent 2b3389f commit e3782e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
37 changes: 24 additions & 13 deletions cmake/SDL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit e3782e8

Please sign in to comment.