@@ -82,26 +82,41 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
82
82
83
83
#Define a path for custom commands to work around MSVC
84
84
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
+
85
107
if (MSVC )
86
108
#MSVC implicitly adds $<CONFIG> to the output path
87
109
set (CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR} /$<CONFIG> )
88
110
#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 )
91
112
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DYNAMICBASE" )
92
113
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 )
95
115
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG} /DYNAMICBASE" )
96
116
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG} /guard:cf" )
97
117
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_CXX_LINKER_WRAPPER_FLAG} /LTCG /INCREMENTAL:NO" )
98
118
# 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 )
105
120
endif ()
106
121
107
122
#CXX compiler support
0 commit comments