Home | History | Annotate | Download | only in cmake
      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 
     15 if(__add_cxx_compiler_flag)
     16   return()
     17 endif()
     18 set(__add_cxx_compiler_flag INCLUDED)
     19 
     20 include(CheckCXXCompilerFlag)
     21 
     22 function(mangle_compiler_flag FLAG OUTPUT)
     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(${OUTPUT} "${SANITIZED_FLAG}" PARENT_SCOPE)
     28 endfunction(mangle_compiler_flag)
     29 
     30 function(add_cxx_compiler_flag FLAG)
     31   mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
     32   set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
     33   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}")
     34   check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
     35   set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
     36   if(${MANGLED_FLAG})
     37     set(VARIANT ${ARGV1})
     38     if(ARGV1)
     39       string(TOUPPER "_${VARIANT}" VARIANT)
     40     endif()
     41     set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${BENCHMARK_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
     42   endif()
     43 endfunction()
     44 
     45 function(add_required_cxx_compiler_flag FLAG)
     46   mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
     47   set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
     48   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}")
     49   check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
     50   set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
     51   if(${MANGLED_FLAG})
     52     set(VARIANT ${ARGV1})
     53     if(ARGV1)
     54       string(TOUPPER "_${VARIANT}" VARIANT)
     55     endif()
     56     set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
     57     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
     58     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
     59     set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
     60     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}" PARENT_SCOPE)
     61   else()
     62     message(FATAL_ERROR "Required flag '${FLAG}' is not supported by the compiler")
     63   endif()
     64 endfunction()
     65 
     66 function(check_cxx_warning_flag FLAG)
     67   mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
     68   set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
     69   # Add -Werror to ensure the compiler generates an error if the warning flag
     70   # doesn't exist.
     71   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror ${FLAG}")
     72   check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
     73   set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
     74 endfunction()
     75