CheckCompilerFlag.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckCompilerFlag
  5. ---------------------
  6. .. versionadded:: 3.19
  7. Check whether the compiler supports a given flag.
  8. .. command:: check_compiler_flag
  9. .. code-block:: cmake
  10. check_compiler_flag(<lang> <flag> <var>)
  11. Check that the ``<flag>`` is accepted by the compiler without a diagnostic.
  12. Stores the result in an internal cache entry named ``<var>``.
  13. This command temporarily sets the ``CMAKE_REQUIRED_DEFINITIONS`` variable
  14. and calls the ``check_source_compiles(<LANG>`` function from the
  15. :module:`CheckSourceCompiles` module. See documentation of that
  16. module for a listing of variables that can otherwise modify the build.
  17. A positive result from this check indicates only that the compiler did not
  18. issue a diagnostic message when given the flag. Whether the flag has any
  19. effect or even a specific one is beyond the scope of this module.
  20. .. note::
  21. Since the :command:`try_compile` command forwards flags from variables
  22. like :variable:`CMAKE_<LANG>_FLAGS <CMAKE_<LANG>_FLAGS>`, unknown flags
  23. in such variables may cause a false negative for this check.
  24. #]=======================================================================]
  25. include_guard(GLOBAL)
  26. include(CheckSourceCompiles)
  27. include(CMakeCheckCompilerFlagCommonPatterns)
  28. cmake_policy(PUSH)
  29. cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
  30. cmake_policy(SET CMP0057 NEW) # if() supports IN_LIST
  31. function(CHECK_COMPILER_FLAG _lang _flag _var)
  32. if(_lang STREQUAL C)
  33. set(_lang_src "int main(void) { return 0; }")
  34. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C")
  35. elseif(_lang STREQUAL CXX)
  36. set(_lang_src "int main() { return 0; }")
  37. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+")
  38. elseif(_lang STREQUAL Fortran)
  39. set(_lang_src " program test\n stop\n end program")
  40. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Fortran")
  41. elseif(_lang STREQUAL OBJC)
  42. set(_lang_src [=[
  43. #ifndef __OBJC__
  44. # error "Not an Objective-C compiler"
  45. #endif
  46. int main(void) { return 0; }]=])
  47. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C" # GNU
  48. FAIL_REGEX "argument unused during compilation: .*") # Clang
  49. elseif(_lang STREQUAL OBJCXX)
  50. set(_lang_src [=[
  51. #ifndef __OBJC__
  52. # error "Not an Objective-C++ compiler"
  53. #endif
  54. int main(void) { return 0; }]=])
  55. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C\\+\\+" # GNU
  56. FAIL_REGEX "argument unused during compilation: .*") # Clang
  57. else()
  58. message (SEND_ERROR "check_compiler_flag: ${_lang}: unknown language.")
  59. return()
  60. endif()
  61. get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  62. if (NOT _lang IN_LIST _supported_languages)
  63. message (SEND_ERROR "check_compiler_flag: ${_lang}: needs to be enabled before use.")
  64. return()
  65. endif()
  66. set(CMAKE_REQUIRED_DEFINITIONS ${_flag})
  67. # Normalize locale during test compilation.
  68. set(_locale_vars LC_ALL LC_MESSAGES LANG)
  69. foreach(v IN LISTS _locale_vars)
  70. set(_locale_vars_saved_${v} "$ENV{${v}}")
  71. set(ENV{${v}} C)
  72. endforeach()
  73. check_compiler_flag_common_patterns(_common_patterns)
  74. check_source_compiles(${_lang}
  75. "${_lang_src}"
  76. ${_var}
  77. ${_lang_fail_regex}
  78. ${_common_patterns}
  79. )
  80. foreach(v IN LISTS _locale_vars)
  81. set(ENV{${v}} ${_locale_vars_saved_${v}})
  82. endforeach()
  83. set(${_var} "${${_var}}" PARENT_SCOPE)
  84. endfunction ()
  85. cmake_policy(POP)