Skip to content

Commit e3782e8

Browse files
spalickimgouicem
authored andcommitted
build: add check_cxx_compiler_flag for SDL flags
1 parent 2b3389f commit e3782e8

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

cmake/SDL.cmake

+24-13
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ include("cmake/utils.cmake")
2727
# The flags that can be used for the main and host compilers should be moved to
2828
# the macros to avoid code duplication and ensure consistency.
2929
macro(sdl_unix_common_ccxx_flags var)
30-
append(${var} "-fPIC -Wformat -Wformat-security")
30+
test_compiler_and_add_flag("-fPIC" ${var})
31+
test_compiler_and_add_flag("-Wformat" ${var})
32+
test_compiler_and_add_flag("-Wformat-security" ${var})
3133
endmacro()
3234

3335
macro(sdl_gnu_common_ccxx_flags var gnu_version)
3436
if(${gnu_version} VERSION_LESS 4.9)
35-
append(${var} "-fstack-protector-all")
37+
test_compiler_and_add_flag("-fstack-protector-all" ${var})
3638
else()
37-
append(${var} "-fstack-protector-strong")
39+
test_compiler_and_add_flag("-fstack-protector-strong" ${var})
3840
if(NOT (${gnu_version} VERSION_LESS 8.0) AND (DNNL_TARGET_ARCH STREQUAL "X64"))
39-
append(${var} "-fcf-protection=full")
41+
test_compiler_and_add_flag("-fcf-protection=full" ${var})
4042
endif()
4143
endif()
4244
endmacro()
@@ -48,7 +50,7 @@ endmacro()
4850
# this warning on, let's use it too. Applicable for the library sources
4951
# and interfaces only (tests currently rely on that fact heavily)
5052
macro(sdl_gnu_src_ccxx_flags var)
51-
append(${var} "-Wmissing-field-initializers")
53+
test_compiler_and_add_flag("-Wmissing-field-initializers" ${var})
5254
endmacro()
5355

5456
macro(sdl_gnu_example_ccxx_flags var)
@@ -62,7 +64,7 @@ set(ONEDNN_SDL_LINKER_FLAGS)
6264
if(UNIX)
6365
sdl_unix_common_ccxx_flags(ONEDNN_SDL_COMPILER_FLAGS)
6466
if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
65-
append(ONEDNN_SDL_COMPILER_FLAGS "-D_FORTIFY_SOURCE=2")
67+
test_compiler_and_add_flag("-D_FORTIFY_SOURCE=2" ONEDNN_SDL_COMPILER_FLAGS)
6668
endif()
6769
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
6870
sdl_gnu_common_ccxx_flags(ONEDNN_SDL_COMPILER_FLAGS CMAKE_CXX_COMPILER_VERSION)
@@ -72,10 +74,10 @@ if(UNIX)
7274
get_filename_component(CXX_CMD_NAME ${CMAKE_CXX_COMPILER} NAME)
7375
# Fujitsu CXX compiler does not support "-fstack-protector-all".
7476
if(NOT CXX_CMD_NAME STREQUAL "FCC")
75-
append(ONEDNN_SDL_COMPILER_FLAGS "-fstack-protector-all")
77+
test_compiler_and_add_flag("-fstack-protector-all" ONEDNN_SDL_COMPILER_FLAGS)
7678
endif()
7779
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
78-
append(ONEDNN_SDL_COMPILER_FLAGS "-fstack-protector")
80+
test_compiler_and_add_flag("-fstack-protector" ONEDNN_SDL_COMPILER_FLAGS)
7981
endif()
8082
if(APPLE)
8183
append(ONEDNN_SDL_LINKER_FLAGS "-Wl,-bind_at_load")
@@ -86,20 +88,29 @@ if(UNIX)
8688
endif()
8789
elseif(WIN32)
8890
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
89-
append(ONEDNN_SDL_COMPILER_FLAGS "/GS /Gy /guard:cf /DYNAMICBASE /sdl")
91+
test_compiler_and_add_flag("/GS" ONEDNN_SDL_COMPILER_FLAGS)
92+
test_compiler_and_add_flag("/Gy" ONEDNN_SDL_COMPILER_FLAGS)
93+
test_compiler_and_add_flag("/guard:cf" ONEDNN_SDL_COMPILER_FLAGS)
94+
test_compiler_and_add_flag("/DYNAMICBASE" ONEDNN_SDL_COMPILER_FLAGS)
95+
test_compiler_and_add_flag("/sdl" ONEDNN_SDL_COMPILER_FLAGS)
9096
append(ONEDNN_SDL_LINKER_FLAGS "/NXCOMPAT /LTCG")
9197
elseif(CMAKE_BASE_NAME STREQUAL "icx")
92-
append(ONEDNN_SDL_COMPILER_FLAGS "/GS /Gy /guard:cf /Wformat /Wformat-security")
98+
test_compiler_and_add_flag("/GS" ONEDNN_SDL_COMPILER_FLAGS)
99+
test_compiler_and_add_flag("/Gy" ONEDNN_SDL_COMPILER_FLAGS)
100+
test_compiler_and_add_flag("/guard:cf" ONEDNN_SDL_COMPILER_FLAGS)
101+
test_compiler_and_add_flag("/Wformat" ONEDNN_SDL_COMPILER_FLAGS)
102+
test_compiler_and_add_flag("/Wformat-security" ONEDNN_SDL_COMPILER_FLAGS)
93103
append(ONEDNN_SDL_LINKER_FLAGS "/link /NXCOMPAT")
94104
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
95-
append(ONEDNN_SDL_COMPILER_FLAGS "-Wformat -Wformat-security")
105+
test_compiler_and_add_flag("-Wformat" ONEDNN_SDL_COMPILER_FLAGS)
106+
test_compiler_and_add_flag("-Wformat-security" ONEDNN_SDL_COMPILER_FLAGS)
96107
if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
97-
append(ONEDNN_SDL_COMPILER_FLAGS "-D_FORTIFY_SOURCE=2")
108+
test_compiler_and_add_flag("-D_FORTIFY_SOURCE=2" ONEDNN_SDL_COMPILER_FLAGS)
98109
endif()
99110
get_filename_component(CXX_CMD_NAME ${CMAKE_CXX_COMPILER} NAME)
100111
# Fujitsu CXX compiler does not support "-fstack-protector-all".
101112
if(NOT CXX_CMD_NAME STREQUAL "FCC")
102-
append(ONEDNN_SDL_COMPILER_FLAGS "-fstack-protector-all")
113+
test_compiler_and_add_flag("-fstack-protector-all" ONEDNN_SDL_COMPILER_FLAGS)
103114
endif()
104115
append(ONEDNN_SDL_LINKER_FLAGS "-Xlinker /NXCOMPAT -Xlinker /LTCG")
105116
endif()

cmake/utils.cmake

+14
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,17 @@ function(find_libm var)
174174
find_library(${var} m REQUIRED)
175175
endif()
176176
endfunction()
177+
178+
include(CheckCXXCompilerFlag)
179+
macro(test_compiler_and_add_flag flag var)
180+
set(CMAKE_REQUIRED_QUIET 1)
181+
unset(COMPILER_SUPPORTS_FLAG CACHE)
182+
183+
if(NOT test_compiler_flag_nowarn)
184+
append(CMAKE_REQUIRED_FLAGS "${CMAKE_CCXX_NOWARN_FLAGS}")
185+
set(test_compiler_flag_nowarn true)
186+
endif()
187+
188+
check_cxx_compiler_flag("${flag}" COMPILER_SUPPORTS_FLAG)
189+
append_if(COMPILER_SUPPORTS_FLAG ${var} ${flag})
190+
endmacro()

0 commit comments

Comments
 (0)