Skip to content

Commit 2e212f7

Browse files
Check if compile flags are supported: /GL, /QSpectre, /guard:cf (#368)
* Check if compile flags are supported: /GL, /QSpectre, /guard:cf Signed-off-by: Tikhomirova, Kseniya <[email protected]>
1 parent a818001 commit 2e212f7

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

CMakeLists.txt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,41 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
8282

8383
#Define a path for custom commands to work around MSVC
8484
set(CUSTOM_COMMAND_BINARY_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
85+
86+
macro(mangle_name str output)
87+
string(STRIP "${str}" strippedStr)
88+
string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
89+
string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")
90+
string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")
91+
string(REPLACE "-" "_" strippedStr "${strippedStr}")
92+
string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
93+
string(REPLACE "+" "X" strippedStr "${strippedStr}")
94+
string(TOUPPER "${strippedStr}" ${output})
95+
endmacro()
96+
97+
include(CheckCXXCompilerFlag)
98+
function(add_cxx_flag_if_supported flag)
99+
mangle_name("${flag}" flagname)
100+
check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
101+
if (CXX_SUPPORTS_${flagname}_FLAG)
102+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
103+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
104+
endif()
105+
endfunction()
106+
85107
if(MSVC)
86108
#MSVC implicitly adds $<CONFIG> to the output path
87109
set(CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR}/$<CONFIG>)
88110
#enabling Control Flow Guard
89-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf")
90-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
111+
add_cxx_flag_if_supported(/guard:cf)
91112
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DYNAMICBASE")
92113
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DYNAMICBASE")
93-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL")
94-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /GL")
114+
add_cxx_flag_if_supported(/GL)
95115
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG}/DYNAMICBASE")
96116
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG}/guard:cf")
97117
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG}/LTCG /INCREMENTAL:NO")
98118
# enable Spectre Mitigation, not supported by clang-cl
99-
if((NOT CMAKE_CXX_COMPILER_ID STREQUAL Clang) AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL IntelLLVM))
100-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qspectre")
101-
endif()
102-
if((NOT CMAKE_C_COMPILER_ID STREQUAL Clang) AND NOT (CMAKE_C_COMPILER_ID STREQUAL IntelLLVM))
103-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Qspectre")
104-
endif()
119+
add_cxx_flag_if_supported(/Qspectre)
105120
endif()
106121

107122
#CXX compiler support

0 commit comments

Comments
 (0)