Skip to content

Commit 26ccd26

Browse files
Use compiler ID check for /guard:cf and /GL flags on MSVC (#374)
* Add more strict check for compiler flags Signed-off-by: Tikhomirova, Kseniya <[email protected]>
1 parent 2881074 commit 26ccd26

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

CMakeLists.txt

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,25 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
8484
#Define a path for custom commands to work around MSVC
8585
set(CUSTOM_COMMAND_BINARY_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
8686

87-
macro(mangle_name str output)
88-
string(STRIP "${str}" strippedStr)
89-
string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
90-
string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")
91-
string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")
92-
string(REPLACE "-" "_" strippedStr "${strippedStr}")
93-
string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
94-
string(REPLACE "+" "X" strippedStr "${strippedStr}")
95-
string(TOUPPER "${strippedStr}" ${output})
96-
endmacro()
97-
98-
include(CheckCXXCompilerFlag)
99-
function(add_cxx_flag_if_supported flag)
100-
mangle_name("${flag}" flagname)
101-
check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
102-
if (CXX_SUPPORTS_${flagname}_FLAG)
103-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
104-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
105-
endif()
106-
endfunction()
107-
10887
if(MSVC)
10988
#MSVC implicitly adds $<CONFIG> to the output path
11089
set(CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR}/$<CONFIG>)
111-
#enabling Control Flow Guard
112-
add_cxx_flag_if_supported(/guard:cf)
11390
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DYNAMICBASE")
11491
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DYNAMICBASE")
115-
add_cxx_flag_if_supported(/GL)
11692
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG}/DYNAMICBASE")
11793
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG}/guard:cf")
11894
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG}/LTCG /INCREMENTAL:NO")
119-
# enable Spectre Mitigation, not supported by clang-cl
120-
add_cxx_flag_if_supported(/Qspectre)
95+
# enable flags, that are not supported or unused by clang-cl
96+
if((NOT CMAKE_CXX_COMPILER_ID STREQUAL Clang) AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL IntelLLVM))
97+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qspectre")
98+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf")
99+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL")
100+
endif()
101+
if((NOT CMAKE_C_COMPILER_ID STREQUAL Clang) AND NOT (CMAKE_C_COMPILER_ID STREQUAL IntelLLVM))
102+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Qspectre")
103+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
104+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /GL")
105+
endif()
121106
endif()
122107

123108
#CXX compiler support
@@ -416,4 +401,4 @@ if(CPACK_GENERATOR MATCHES "DEB")
416401
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
417402
endif()
418403

419-
INCLUDE(CPack)
404+
INCLUDE(CPack)

0 commit comments

Comments
 (0)