CheckCXXCompilerFlag.cmake 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # - Check whether the CXX compiler supports a given flag.
  2. # CHECK_CXX_COMPILER_FLAG(<flag> <var>)
  3. # <flag> - the compiler flag
  4. # <var> - variable to store the result
  5. # This internally calls the check_cxx_source_compiles macro and
  6. # sets CMAKE_REQUIRED_DEFINITIONS to <flag>.
  7. # See help for CheckCXXSourceCompiles for a listing of variables
  8. # that can otherwise modify the build.
  9. # The result only tells that the compiler does not give an error message when
  10. # it encounters the flag. If the flag has any effect or even a specific one is
  11. # beyond the scope of this module.
  12. #=============================================================================
  13. # Copyright 2006-2010 Kitware, Inc.
  14. # Copyright 2006 Alexander Neundorf <[email protected]>
  15. # Copyright 2011 Matthias Kretz <[email protected]>
  16. #
  17. # Distributed under the OSI-approved BSD License (the "License");
  18. # see accompanying file Copyright.txt for details.
  19. #
  20. # This software is distributed WITHOUT ANY WARRANTY; without even the
  21. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. # See the License for more information.
  23. #=============================================================================
  24. # (To distribute this file outside of CMake, substitute the full
  25. # License text for the above reference.)
  26. include(CheckCXXSourceCompiles)
  27. include(CMakeCheckCompilerFlagCommonPatterns)
  28. macro (CHECK_CXX_COMPILER_FLAG _FLAG _RESULT)
  29. set(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
  30. set(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}")
  31. # Normalize locale during test compilation.
  32. set(_CheckCXXCompilerFlag_LOCALE_VARS LC_ALL LC_MESSAGES LANG)
  33. foreach(v ${_CheckCXXCompilerFlag_LOCALE_VARS})
  34. set(_CheckCXXCompilerFlag_SAVED_${v} "$ENV{${v}}")
  35. set(ENV{${v}} C)
  36. endforeach()
  37. CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckCXXCompilerFlag_COMMON_PATTERNS)
  38. CHECK_CXX_SOURCE_COMPILES("int main() { return 0; }" ${_RESULT}
  39. # Some compilers do not fail with a bad flag
  40. FAIL_REGEX "command line option .* is valid for .* but not for C\\\\+\\\\+" # GNU
  41. ${_CheckCXXCompilerFlag_COMMON_PATTERNS}
  42. )
  43. foreach(v ${_CheckCXXCompilerFlag_LOCALE_VARS})
  44. set(ENV{${v}} ${_CheckCXXCompilerFlag_SAVED_${v}})
  45. unset(_CheckCXXCompilerFlag_SAVED_${v})
  46. endforeach()
  47. unset(_CheckCXXCompilerFlag_LOCALE_VARS)
  48. unset(_CheckCXXCompilerFlag_COMMON_PATTERNS)
  49. set (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
  50. endmacro ()