1# - Adds a compiler flag if it is supported by the compiler 2# 3# This function checks that the supplied compiler flag is supported and then 4# adds it to the corresponding compiler flags 5# 6# add_cxx_compiler_flag(<FLAG> [<VARIANT>]) 7# 8# - Example 9# 10# include(AddCXXCompilerFlag) 11# add_cxx_compiler_flag(-Wall) 12# add_cxx_compiler_flag(-no-strict-aliasing RELEASE) 13# Requires CMake 2.6+ 14 15if(__add_cxx_compiler_flag) 16 return() 17endif() 18set(__add_cxx_compiler_flag INCLUDED) 19 20include(CheckCXXCompilerFlag) 21 22function(add_cxx_compiler_flag FLAG) 23 string(TOUPPER "HAVE_CXX_FLAG_${FLAG}" SANITIZED_FLAG) 24 string(REPLACE "+" "X" SANITIZED_FLAG ${SANITIZED_FLAG}) 25 string(REGEX REPLACE "[^A-Za-z_0-9]" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) 26 string(REGEX REPLACE "_+" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) 27 set(CMAKE_REQUIRED_FLAGS "${FLAG}") 28 check_cxx_compiler_flag("" ${SANITIZED_FLAG}) 29 if(${SANITIZED_FLAG}) 30 set(VARIANT ${ARGV1}) 31 if(ARGV1) 32 string(TOUPPER "_${VARIANT}" VARIANT) 33 endif() 34 set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE) 35 endif() 36endfunction() 37 38