CheckCompilerFlag.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 CUDA)
  39. set(_lang_src "__host__ int main() { return 0; }")
  40. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+" # Host GNU
  41. FAIL_REGEX "argument unused during compilation: .*") # Clang
  42. elseif(_lang STREQUAL Fortran)
  43. set(_lang_src " program test\n stop\n end program")
  44. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Fortran")
  45. elseif(_lang STREQUAL OBJC)
  46. set(_lang_src [=[
  47. #ifndef __OBJC__
  48. # error "Not an Objective-C compiler"
  49. #endif
  50. int main(void) { return 0; }]=])
  51. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C" # GNU
  52. FAIL_REGEX "argument unused during compilation: .*") # Clang
  53. elseif(_lang STREQUAL OBJCXX)
  54. set(_lang_src [=[
  55. #ifndef __OBJC__
  56. # error "Not an Objective-C++ compiler"
  57. #endif
  58. int main(void) { return 0; }]=])
  59. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C\\+\\+" # GNU
  60. FAIL_REGEX "argument unused during compilation: .*") # Clang
  61. elseif(_lang STREQUAL ISPC)
  62. set(_lang_src "float func(uniform int32, float a) { return a / 2.25; }")
  63. else()
  64. message (SEND_ERROR "check_compiler_flag: ${_lang}: unknown language.")
  65. return()
  66. endif()
  67. get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  68. if (NOT _lang IN_LIST _supported_languages)
  69. message (SEND_ERROR "check_compiler_flag: ${_lang}: needs to be enabled before use.")
  70. return()
  71. endif()
  72. set(CMAKE_REQUIRED_DEFINITIONS ${_flag})
  73. # Normalize locale during test compilation.
  74. set(_locale_vars LC_ALL LC_MESSAGES LANG)
  75. foreach(v IN LISTS _locale_vars)
  76. set(_locale_vars_saved_${v} "$ENV{${v}}")
  77. set(ENV{${v}} C)
  78. endforeach()
  79. check_compiler_flag_common_patterns(_common_patterns)
  80. check_source_compiles(${_lang}
  81. "${_lang_src}"
  82. ${_var}
  83. ${_lang_fail_regex}
  84. ${_common_patterns}
  85. )
  86. foreach(v IN LISTS _locale_vars)
  87. set(ENV{${v}} ${_locale_vars_saved_${v}})
  88. endforeach()
  89. set(${_var} "${${_var}}" PARENT_SCOPE)
  90. endfunction ()
  91. cmake_policy(POP)