diff --git a/cmake/modules/AddCompilerFlags.cmake b/cmake/modules/AddCompilerFlags.cmake --- a/cmake/modules/AddCompilerFlags.cmake +++ b/cmake/modules/AddCompilerFlags.cmake @@ -1,11 +1,26 @@ # Allow to easily add flags for C and C++ include(CheckCXXCompilerFlag) include(CheckCCompilerFlag) +include(SanitizeHelper) + +function(check_compiler_flag RESULT LANGUAGE FLAG) + sanitize_variable(${FLAG} TEST_NAME) + string(PREPEND TEST_NAME "have_${LANGUAGE}_") + + if("${LANGUAGE}" STREQUAL "C") + CHECK_C_COMPILER_FLAG(${FLAG} ${TEST_NAME}) + elseif("${LANGUAGE}" STREQUAL "CXX") + CHECK_CXX_COMPILER_FLAG(${FLAG} ${TEST_NAME}) + else() + message(FATAL_ERROR "check_compiler_flag LANGUAGE should be C or CXX") + endif() + set(${RESULT} ${${TEST_NAME}} PARENT_SCOPE) +endfunction() function(add_c_compiler_flag) foreach(f ${ARGN}) - CHECK_C_COMPILER_FLAG(${f} FLAG_IS_SUPPORTED) - if(FLAG_IS_SUPPORTED) + check_compiler_flag(FLAG_IS_SUPPORTED C ${f}) + if(${FLAG_IS_SUPPORTED}) string(APPEND CMAKE_C_FLAGS " ${f}") endif() endforeach() @@ -14,8 +29,8 @@ function(add_cxx_compiler_flag) foreach(f ${ARGN}) - CHECK_CXX_COMPILER_FLAG(${f} FLAG_IS_SUPPORTED) - if(FLAG_IS_SUPPORTED) + check_compiler_flag(FLAG_IS_SUPPORTED CXX ${f}) + if(${FLAG_IS_SUPPORTED}) string(APPEND CMAKE_CXX_FLAGS " ${f}") endif() endforeach() diff --git a/cmake/modules/SanitizeHelper.cmake b/cmake/modules/SanitizeHelper.cmake new file mode 100644 --- /dev/null +++ b/cmake/modules/SanitizeHelper.cmake @@ -0,0 +1,7 @@ +# Sanitize a variable according to cmake rules +# https://cmake.org/cmake/help/v3.10/manual/cmake-language.7.html#variable-references +# The NUL and ';' characters cannot be escaped in this context (see CMP0053) +macro(sanitize_variable RAW_VAR SANITIZED_VAR) + string(REGEX REPLACE + "([^a-zA-Z0-9/_.+-])" "\\\\\\1" ${SANITIZED_VAR} "${RAW_VAR}") +endmacro() \ No newline at end of file