CheckCompilerFlag.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckCompilerFlag
  5. ---------------------
  6. .. versionadded:: 3.19
  7. This module provides a command to check whether the compiler supports a given
  8. flag.
  9. Load this module in a CMake project with:
  10. .. code-block:: cmake
  11. include(CheckCompilerFlag)
  12. Commands
  13. ^^^^^^^^
  14. This module provides the following command:
  15. .. command:: check_compiler_flag
  16. Checks once whether the compiler supports a given flag:
  17. .. code-block:: cmake
  18. check_compiler_flag(<lang> <flag> <variable>)
  19. This command checks once that the ``<flag>`` is accepted by the ``<lang>``
  20. compiler without producing a diagnostic message. The result of the check
  21. is stored in the internal cache variable specified by ``<variable>``.
  22. The arguments are:
  23. ``<lang>``
  24. The language of the compiler used for the check. Supported languages
  25. are: ``C``, ``CXX``, ``CUDA``, ``Fortran``, ``HIP``, ``ISPC``, ``OBJC``,
  26. and ``OBJCXX``, and ``Swift``.
  27. .. versionadded:: 3.21
  28. Support for ``HIP`` language.
  29. .. versionadded:: 3.26
  30. Support for ``Swift`` language.
  31. ``<flag>``
  32. Compiler flag(s) to check. Multiple flags can be specified in one
  33. argument as a string using a :ref:`semicolon-separated list
  34. <CMake Language Lists>`.
  35. ``<variable>``
  36. Variable name of an internal cache variable to store the result of the
  37. check, with boolean true for success and boolean false for failure.
  38. A successful result only indicates that the compiler did not report an
  39. error when given the flag. Whether the flag has any effect, or the
  40. intended one, is outside the scope of this module.
  41. .. note::
  42. Since the underlying :command:`try_compile` command also uses flags from
  43. variables like :variable:`CMAKE_<LANG>_FLAGS`, unknown or unsupported
  44. flags in those variables may result in a false negative for this check.
  45. .. rubric:: Variables Affecting the Check
  46. The following variables may be set before calling this command to modify
  47. the way the check is run:
  48. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  49. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  50. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  51. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  52. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  53. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  54. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  55. .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst
  56. Examples
  57. ^^^^^^^^
  58. The following example demonstrates how to use this module to check support
  59. for the C compiler flag ``-fno-optimize-strlen``, which disables
  60. optimizations related to the ``strlen()`` C function in GCC and Clang
  61. compilers. The result of the check is stored in the internal cache
  62. variable ``HAVE_FNO_OPTIMIZE_STRLEN``, and the flag is conditionally enabled
  63. using the :command:`target_compile_options` command. The
  64. :genex:`$<COMPILE_LANGUAGE:...> <COMPILE_LANGUAGE:languages>` generator
  65. expression ensures that the flag is added only to ``C`` source files.
  66. .. code-block:: cmake
  67. include(CheckCompilerFlag)
  68. check_compiler_flag(C -fno-optimize-strlen HAVE_FNO_OPTIMIZE_STRLEN)
  69. if(HAVE_FNO_OPTIMIZE_STRLEN)
  70. target_compile_options(
  71. example
  72. PRIVATE $<$<COMPILE_LANGUAGE:C>:-fno-optimize-strlen>
  73. )
  74. endif()
  75. See Also
  76. ^^^^^^^^
  77. * The :module:`CheckLinkerFlag` module to check whether a linker flag is
  78. supported by the compiler.
  79. #]=======================================================================]
  80. include_guard(GLOBAL)
  81. include(Internal/CheckCompilerFlag)
  82. function(CHECK_COMPILER_FLAG _lang _flag _var)
  83. cmake_check_compiler_flag(${_lang} "${_flag}" ${_var})
  84. endfunction()