CheckCXXCompilerFlag.cmake 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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-2009 Kitware, Inc.
  10. # Copyright 2006 Alexander Neundorf <neundorf@kde.org>
  11. # Copyright 2011-2013 Matthias Kretz <kretz@kde.org>
  12. #
  13. # Redistribution and use in source and binary forms, with or without
  14. # modification, are permitted provided that the following conditions are
  15. # met:
  16. #
  17. # * Redistributions of source code must retain the above copyright notice,
  18. # this list of conditions and the following disclaimer.
  19. #
  20. # * Redistributions in binary form must reproduce the above copyright notice,
  21. # this list of conditions and the following disclaimer in the documentation
  22. # and/or other materials provided with the distribution.
  23. #
  24. # * The names of Kitware, Inc., the Insight Consortium, or the names of
  25. # any consortium members, or of any contributors, may not be used to
  26. # endorse or promote products derived from this software without
  27. # specific prior written permission.
  28. #
  29. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  30. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  33. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  37. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. #=============================================================================
  40. INCLUDE(CheckCXXSourceCompiles)
  41. MACRO (CHECK_CXX_COMPILER_FLAG _FLAG _RESULT)
  42. SET(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
  43. SET(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}")
  44. if(${ARGC} GREATER 2)
  45. SET(TEST_SOURCE "${ARGV2}")
  46. else()
  47. SET(TEST_SOURCE "int main() { return 0;}")
  48. endif()
  49. CHECK_CXX_SOURCE_COMPILES("${TEST_SOURCE}" ${_RESULT}
  50. # Some compilers do not fail with a bad flag
  51. FAIL_REGEX "error: bad value (.*) for .* switch" # GNU
  52. FAIL_REGEX "argument unused during compilation" # clang
  53. FAIL_REGEX "is valid for .* but not for C\\\\+\\\\+" # GNU
  54. FAIL_REGEX "unrecognized .*option" # GNU
  55. FAIL_REGEX "ignored for target" # GNU
  56. FAIL_REGEX "ignoring unknown option" # MSVC
  57. FAIL_REGEX "warning D9002" # MSVC
  58. FAIL_REGEX "[Uu]nknown option" # HP
  59. FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro
  60. FAIL_REGEX "command option .* is not recognized" # XL
  61. FAIL_REGEX "WARNING: unknown flag:" # Open64
  62. FAIL_REGEX "command line error" # ICC
  63. FAIL_REGEX "command line warning" # ICC
  64. FAIL_REGEX "#10236:" # ICC: File not found
  65. FAIL_REGEX " #10159: " # ICC
  66. FAIL_REGEX " #10353: " # ICC: option '-mfma' ignored, suggest using '-march=core-avx2'
  67. )
  68. SET (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
  69. ENDMACRO (CHECK_CXX_COMPILER_FLAG)