diff --git a/compile_flag.cmake b/compile_flag.cmake index f8b06c1..a2dd21b 100644 --- a/compile_flag.cmake +++ b/compile_flag.cmake @@ -1,5 +1,13 @@ -include(CheckCXXCompilerFlag) +include(CheckCXXSourceCompiles) +# Test if a compiler flag is supported. +# +# Note: We _cannot_ use check_cxx_compiler_flag because it will not add the +# flag to the linker parameters and some compiler flags _must_ be passed to +# the linker but the test it performs fails to do so. eg, checking for +# -fsanitize=address is impossible with this macro. +# +# Instead, we roll out own. Yay cmake... macro (append_compile_flag _flag) string (MAKE_C_IDENTIFIER ${_flag} _name) @@ -14,14 +22,16 @@ macro (append_compile_flag _flag) string (REGEX MATCH "^-Wno-(.+)$" compile_flag_inverse ${_flag}) if ("x" STREQUAL "x${compile_flag_inverse}") - check_cxx_compiler_flag (${_flag} compile_flag${_name}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}") else () string (REGEX REPLACE "-Wno-" "" _inverse_flag ${_flag}) - check_cxx_compiler_flag ("-W${_inverse_flag}" compile_flag${_name}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS} -W${_inverse_flag}") endif () + + check_cxx_source_compiles("int main(int,char**) { return 0; }" compile_flag_${_name}) endif () - if (compile_flag${_name}) + if (compile_flag_${_name}) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}") endif() endmacro(append_compile_flag)