AddCXXCompilerFlag.cmake 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. if(__add_cxx_compiler_flag)
  15. return()
  16. endif()
  17. set(__add_cxx_compiler_flag INCLUDED)
  18. include(CheckCXXCompilerFlag)
  19. function(add_cxx_compiler_flag FLAG)
  20. if(ARGV1)
  21. set(VARIANT ${ARGV1})
  22. string(TOLOWER ${VARIANT} VARIANT)
  23. set(VARIANT " ${VARIANT}")
  24. endif()
  25. string(TOUPPER "HAVE_${FLAG}" SANITIZED_FLAG)
  26. string(REGEX REPLACE "[^A-Za-z_0-9]" "_" SANITIZED_FLAG ${SANITIZED_FLAG})
  27. string(REGEX REPLACE "_+" "_" SANITIZED_FLAG ${SANITIZED_FLAG})
  28. check_cxx_compiler_flag(${FLAG} ${SANITIZED_FLAG})
  29. if(${SANITIZED_FLAG})
  30. string(REGEX REPLACE "[^A-Za-z_0-9]" "_" VARIANT "${VARIANT}")
  31. string(TOUPPER "${VARIANT}" VARIANT)
  32. set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
  33. endif()
  34. endfunction()