cmake-compile-features.7.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. .. cmake-manual-description: CMake Compile Features Reference
  2. cmake-compile-features(7)
  3. *************************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. Project source code may depend on, or be conditional on, the availability
  9. of certain features of the compiler. There are three use-cases which arise:
  10. `Compile Feature Requirements`_, `Optional Compile Features`_
  11. and `Conditional Compilation Options`_.
  12. While features are typically specified in programming language standards,
  13. CMake provides a primary user interface based on granular handling of
  14. the features, not the language standard that introduced the feature.
  15. The :prop_gbl:`CMAKE_C_KNOWN_FEATURES`, :prop_gbl:`CMAKE_CUDA_KNOWN_FEATURES`,
  16. and :prop_gbl:`CMAKE_CXX_KNOWN_FEATURES` global properties contain all the
  17. features known to CMake, regardless of compiler support for the feature.
  18. The :variable:`CMAKE_C_COMPILE_FEATURES`, :variable:`CMAKE_CUDA_COMPILE_FEATURES`
  19. , and :variable:`CMAKE_CXX_COMPILE_FEATURES` variables contain all features
  20. CMake knows are known to the compiler, regardless of language standard
  21. or compile flags needed to use them.
  22. Features known to CMake are named mostly following the same convention
  23. as the Clang feature test macros. There are some exceptions, such as
  24. CMake using ``cxx_final`` and ``cxx_override`` instead of the single
  25. ``cxx_override_control`` used by Clang.
  26. Note that there are no separate compile features properties or variables for
  27. the ``OBJC`` or ``OBJCXX`` languages. These are based off ``C`` or ``C++``
  28. respectively, so the properties and variables for their corresponding base
  29. language should be used instead.
  30. Compile Feature Requirements
  31. ============================
  32. Compile feature requirements may be specified with the
  33. :command:`target_compile_features` command. For example, if a target must
  34. be compiled with compiler support for the
  35. :prop_gbl:`cxx_constexpr <CMAKE_CXX_KNOWN_FEATURES>` feature:
  36. .. code-block:: cmake
  37. add_library(mylib requires_constexpr.cpp)
  38. target_compile_features(mylib PRIVATE cxx_constexpr)
  39. In processing the requirement for the ``cxx_constexpr`` feature,
  40. :manual:`cmake(1)` will ensure that the in-use C++ compiler is capable
  41. of the feature, and will add any necessary flags such as ``-std=gnu++11``
  42. to the compile lines of C++ files in the ``mylib`` target. A
  43. ``FATAL_ERROR`` is issued if the compiler is not capable of the
  44. feature.
  45. The exact compile flags and language standard are deliberately not part
  46. of the user interface for this use-case. CMake will compute the
  47. appropriate compile flags to use by considering the features specified
  48. for each target.
  49. Such compile flags are added even if the compiler supports the
  50. particular feature without the flag. For example, the GNU compiler
  51. supports variadic templates (with a warning) even if ``-std=gnu++98`` is
  52. used. CMake adds the ``-std=gnu++11`` flag if ``cxx_variadic_templates``
  53. is specified as a requirement.
  54. In the above example, ``mylib`` requires ``cxx_constexpr`` when it
  55. is built itself, but consumers of ``mylib`` are not required to use a
  56. compiler which supports ``cxx_constexpr``. If the interface of
  57. ``mylib`` does require the ``cxx_constexpr`` feature (or any other
  58. known feature), that may be specified with the ``PUBLIC`` or
  59. ``INTERFACE`` signatures of :command:`target_compile_features`:
  60. .. code-block:: cmake
  61. add_library(mylib requires_constexpr.cpp)
  62. # cxx_constexpr is a usage-requirement
  63. target_compile_features(mylib PUBLIC cxx_constexpr)
  64. # main.cpp will be compiled with -std=gnu++11 on GNU for cxx_constexpr.
  65. add_executable(myexe main.cpp)
  66. target_link_libraries(myexe mylib)
  67. Feature requirements are evaluated transitively by consuming the link
  68. implementation. See :manual:`cmake-buildsystem(7)` for more on
  69. transitive behavior of build properties and usage requirements.
  70. .. _`Requiring Language Standards`:
  71. Requiring Language Standards
  72. ----------------------------
  73. In projects that use a large number of commonly available features from
  74. a particular language standard (e.g. C++ 11) one may specify a
  75. meta-feature (e.g. ``cxx_std_11``) that requires use of a compiler mode
  76. that is at minimum aware of that standard, but could be greater.
  77. This is simpler than specifying all the features individually, but does
  78. not guarantee the existence of any particular feature.
  79. Diagnosis of use of unsupported features will be delayed until compile time.
  80. For example, if C++ 11 features are used extensively in a project's
  81. header files, then clients must use a compiler mode that is no less
  82. than C++ 11. This can be requested with the code:
  83. .. code-block:: cmake
  84. target_compile_features(mylib PUBLIC cxx_std_11)
  85. In this example, CMake will ensure the compiler is invoked in a mode
  86. of at-least C++ 11 (or C++ 14, C++ 17, ...), adding flags such as
  87. ``-std=gnu++11`` if necessary. This applies to sources within ``mylib``
  88. as well as any dependents (that may include headers from ``mylib``).
  89. .. include:: ../prop_gbl/include/CMAKE_LANG_STD_FLAGS.rst
  90. Availability of Compiler Extensions
  91. -----------------------------------
  92. The :prop_tgt:`<LANG>_EXTENSIONS` target property defaults to the compiler's
  93. default (see :variable:`CMAKE_<LANG>_EXTENSIONS_DEFAULT`). Note that because
  94. most compilers enable extensions by default, this may expose portability bugs
  95. in user code or in the headers of third-party dependencies.
  96. :prop_tgt:`<LANG>_EXTENSIONS` used to default to ``ON``. See :policy:`CMP0128`.
  97. Optional Compile Features
  98. =========================
  99. Compile features may be preferred if available, without creating a hard
  100. requirement. This can be achieved by *not* specifying features with
  101. :command:`target_compile_features` and instead checking the compiler
  102. capabilities with preprocessor conditions in project code.
  103. In this use-case, the project may wish to establish a particular language
  104. standard if available from the compiler, and use preprocessor conditions
  105. to detect the features actually available. A language standard may be
  106. established by `Requiring Language Standards`_ using
  107. :command:`target_compile_features` with meta-features like ``cxx_std_11``,
  108. or by setting the :prop_tgt:`CXX_STANDARD` target property or
  109. :variable:`CMAKE_CXX_STANDARD` variable.
  110. See also policy :policy:`CMP0120` and legacy documentation on
  111. :ref:`Example Usage <WCDH Example Usage>` of the deprecated
  112. :module:`WriteCompilerDetectionHeader` module.
  113. Conditional Compilation Options
  114. ===============================
  115. Libraries may provide entirely different header files depending on
  116. requested compiler features.
  117. For example, a header at ``with_variadics/interface.h`` may contain:
  118. .. code-block:: c++
  119. template<int I, int... Is>
  120. struct Interface;
  121. template<int I>
  122. struct Interface<I>
  123. {
  124. static int accumulate()
  125. {
  126. return I;
  127. }
  128. };
  129. template<int I, int... Is>
  130. struct Interface
  131. {
  132. static int accumulate()
  133. {
  134. return I + Interface<Is...>::accumulate();
  135. }
  136. };
  137. while a header at ``no_variadics/interface.h`` may contain:
  138. .. code-block:: c++
  139. template<int I1, int I2 = 0, int I3 = 0, int I4 = 0>
  140. struct Interface
  141. {
  142. static int accumulate() { return I1 + I2 + I3 + I4; }
  143. };
  144. It may be possible to write an abstraction ``interface.h`` header
  145. containing something like:
  146. .. code-block:: c++
  147. #ifdef HAVE_CXX_VARIADIC_TEMPLATES
  148. #include "with_variadics/interface.h"
  149. #else
  150. #include "no_variadics/interface.h"
  151. #endif
  152. However this could be unmaintainable if there are many files to
  153. abstract. What is needed is to use alternative include directories
  154. depending on the compiler capabilities.
  155. CMake provides a ``COMPILE_FEATURES``
  156. :manual:`generator expression <cmake-generator-expressions(7)>` to implement
  157. such conditions. This may be used with the build-property commands such as
  158. :command:`target_include_directories` and :command:`target_link_libraries`
  159. to set the appropriate :manual:`buildsystem <cmake-buildsystem(7)>`
  160. properties:
  161. .. code-block:: cmake
  162. add_library(foo INTERFACE)
  163. set(with_variadics ${CMAKE_CURRENT_SOURCE_DIR}/with_variadics)
  164. set(no_variadics ${CMAKE_CURRENT_SOURCE_DIR}/no_variadics)
  165. target_include_directories(foo
  166. INTERFACE
  167. "$<$<COMPILE_FEATURES:cxx_variadic_templates>:${with_variadics}>"
  168. "$<$<NOT:$<COMPILE_FEATURES:cxx_variadic_templates>>:${no_variadics}>"
  169. )
  170. Consuming code then simply links to the ``foo`` target as usual and uses
  171. the feature-appropriate include directory
  172. .. code-block:: cmake
  173. add_executable(consumer_with consumer_with.cpp)
  174. target_link_libraries(consumer_with foo)
  175. set_property(TARGET consumer_with CXX_STANDARD 11)
  176. add_executable(consumer_no consumer_no.cpp)
  177. target_link_libraries(consumer_no foo)
  178. Supported Compilers
  179. ===================
  180. CMake is currently aware of the :prop_tgt:`C++ standards <CXX_STANDARD>`
  181. and :prop_gbl:`compile features <CMAKE_CXX_KNOWN_FEATURES>` available from
  182. the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  183. versions specified for each:
  184. * ``AppleClang``: Apple Clang for Xcode versions 4.4+.
  185. * ``Clang``: Clang compiler versions 2.9+.
  186. * ``GNU``: GNU compiler versions 4.4+.
  187. * ``MSVC``: Microsoft Visual Studio versions 2010+.
  188. * ``SunPro``: Oracle SolarisStudio versions 12.4+.
  189. * ``Intel``: Intel compiler versions 12.1+.
  190. CMake is currently aware of the :prop_tgt:`C standards <C_STANDARD>`
  191. and :prop_gbl:`compile features <CMAKE_C_KNOWN_FEATURES>` available from
  192. the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  193. versions specified for each:
  194. * all compilers and versions listed above for C++.
  195. * ``GNU``: GNU compiler versions 3.4+
  196. CMake is currently aware of the :prop_tgt:`C++ standards <CXX_STANDARD>` and
  197. their associated meta-features (e.g. ``cxx_std_11``) available from the
  198. following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  199. versions specified for each:
  200. * ``Cray``: Cray Compiler Environment version 8.1+.
  201. * ``Fujitsu``: Fujitsu HPC compiler 4.0+.
  202. * ``PGI``: PGI version 12.10+.
  203. * ``NVHPC``: NVIDIA HPC compilers version 11.0+.
  204. * ``TI``: Texas Instruments compiler.
  205. * ``TIClang``: Texas Instruments Clang-based compilers.
  206. * ``XL``: IBM XL version 10.1+.
  207. CMake is currently aware of the :prop_tgt:`C standards <C_STANDARD>` and
  208. their associated meta-features (e.g. ``c_std_99``) available from the
  209. following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  210. versions specified for each:
  211. * all compilers and versions listed above with only meta-features for C++.
  212. CMake is currently aware of the :prop_tgt:`CUDA standards <CUDA_STANDARD>` and
  213. their associated meta-features (e.g. ``cuda_std_11``) available from the
  214. following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  215. versions specified for each:
  216. * ``Clang``: Clang compiler 5.0+.
  217. * ``NVIDIA``: NVIDIA nvcc compiler 7.5+.
  218. .. _`Language Standard Flags`:
  219. Language Standard Flags
  220. =======================
  221. In order to satisfy requirements specified by the
  222. :command:`target_compile_features` command or the
  223. :variable:`CMAKE_<LANG>_STANDARD` variable, CMake may pass a
  224. language standard flag to the compiler, such as ``-std=c++11``.
  225. For :ref:`Visual Studio Generators`, CMake cannot precisely control
  226. the placement of the language standard flag on the compiler command line.
  227. For :ref:`Ninja Generators`, :ref:`Makefile Generators`, and
  228. :generator:`Xcode`, CMake places the language standard flag just after
  229. the language-wide flags from :variable:`CMAKE_<LANG>_FLAGS`
  230. and :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>`.
  231. .. versionchanged:: 3.26
  232. The language standard flag is placed before flags specified by other
  233. abstractions such as the :command:`target_compile_options` command.
  234. Prior to CMake 3.26, the language standard flag was placed after them.