CheckCXXCompilerFlag.cmake 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. See help
  6. # for CheckCXXSourceCompiles for a listing of variables that can
  7. # modify the build.
  8. #=============================================================================
  9. # Copyright 2006-2010 Kitware, Inc.
  10. # Copyright 2006 Alexander Neundorf <[email protected]>
  11. # Copyright 2011 Matthias Kretz <[email protected]>
  12. #
  13. # Distributed under the OSI-approved BSD License (the "License");
  14. # see accompanying file Copyright.txt for details.
  15. #
  16. # This software is distributed WITHOUT ANY WARRANTY; without even the
  17. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. # See the License for more information.
  19. #=============================================================================
  20. # (To distribute this file outside of CMake, substitute the full
  21. # License text for the above reference.)
  22. include(CheckCXXSourceCompiles)
  23. macro (CHECK_CXX_COMPILER_FLAG _FLAG _RESULT)
  24. set(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
  25. set(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}")
  26. # Normalize locale during test compilation.
  27. set(_CheckCXXCompilerFlag_LOCALE_VARS LC_ALL LC_MESSAGES LANG)
  28. foreach(v ${_CheckCXXCompilerFlag_LOCALE_VARS})
  29. set(_CheckCXXCompilerFlag_SAVED_${v} "$ENV{${v}}")
  30. set(ENV{${v}} C)
  31. endforeach()
  32. CHECK_CXX_SOURCE_COMPILES("int main() { return 0;}" ${_RESULT}
  33. # Some compilers do not fail with a bad flag
  34. FAIL_REGEX "command line option .* is valid for .* but not for C\\\\+\\\\+" # GNU
  35. FAIL_REGEX "unrecognized .*option" # GNU
  36. FAIL_REGEX "unknown .*option" # Clang
  37. FAIL_REGEX "ignoring unknown option" # MSVC
  38. FAIL_REGEX "warning D9002" # MSVC, any lang
  39. FAIL_REGEX "option.*not supported" # Intel
  40. FAIL_REGEX "invalid argument .*option" # Intel
  41. FAIL_REGEX "ignoring option .*argument required" # Intel
  42. FAIL_REGEX "[Uu]nknown option" # HP
  43. FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro
  44. FAIL_REGEX "command option .* is not recognized" # XL
  45. FAIL_REGEX "not supported in this configuration; ignored" # AIX
  46. FAIL_REGEX "File with unknown suffix passed to linker" # PGI
  47. FAIL_REGEX "WARNING: unknown flag:" # Open64
  48. )
  49. foreach(v ${_CheckCXXCompilerFlag_LOCALE_VARS})
  50. set(ENV{${v}} ${_CheckCXXCompilerFlag_SAVED_${v}})
  51. unset(_CheckCXXCompilerFlag_SAVED_${v})
  52. endforeach()
  53. unset(_CheckCXXCompilerFlag_LOCALE_VARS)
  54. set (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
  55. endmacro ()