CheckCCompilerFlag.cmake 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # - Check whether the C compiler supports a given flag.
  2. # CHECK_C_COMPILER_FLAG(<flag> <var>)
  3. # <flag> - the compiler flag
  4. # <var> - variable to store the result
  5. # This internally calls the check_c_source_compiles macro.
  6. # See help for CheckCSourceCompiles for a listing of variables
  7. # that can modify the build.
  8. #=============================================================================
  9. # Copyright 2006-2011 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(CheckCSourceCompiles)
  23. macro (CHECK_C_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(_CheckCCompilerFlag_LOCALE_VARS LC_ALL LC_MESSAGES LANG)
  28. foreach(v ${_CheckCCompilerFlag_LOCALE_VARS})
  29. set(_CheckCCompilerFlag_SAVED_${v} "$ENV{${v}}")
  30. set(ENV{${v}} C)
  31. endforeach()
  32. CHECK_C_SOURCE_COMPILES("int main(void) { 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 "WARNING: unknown flag:" # Open64
  46. )
  47. foreach(v ${_CheckCCompilerFlag_LOCALE_VARS})
  48. set(ENV{${v}} ${_CheckCCompilerFlag_SAVED_${v}})
  49. unset(_CheckCCompilerFlag_SAVED_${v})
  50. endforeach()
  51. unset(_CheckCCompilerFlag_LOCALE_VARS)
  52. set (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
  53. endmacro ()