cmake-compile-features.7.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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` and
  16. :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` and
  19. :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. The 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. Compile Feature Requirements
  27. ============================
  28. Compile feature requirements may be specified with the
  29. :command:`target_compile_features` command. For example, if a target must
  30. be compiled with compiler support for the
  31. :prop_gbl:`cxx_constexpr <CMAKE_CXX_KNOWN_FEATURES>` feature:
  32. .. code-block:: cmake
  33. add_library(mylib requires_constexpr.cpp)
  34. target_compile_features(mylib PRIVATE cxx_constexpr)
  35. In processing the requirement for the ``cxx_constexpr`` feature,
  36. :manual:`cmake(1)` will ensure that the in-use C++ compiler is capable
  37. of the feature, and will add any necessary flags such as ``-std=gnu++11``
  38. to the compile lines of C++ files in the ``mylib`` target. A
  39. ``FATAL_ERROR`` is issued if the compiler is not capable of the
  40. feature.
  41. The exact compile flags and language standard are deliberately not part
  42. of the user interface for this use-case. CMake will compute the
  43. appropriate compile flags to use by considering the features specified
  44. for each target.
  45. Such compile flags are added even if the compiler supports the
  46. particular feature without the flag. For example, the GNU compiler
  47. supports variadic templates (with a warning) even if ``-std=gnu++98`` is
  48. used. CMake adds the ``-std=gnu++11`` flag if ``cxx_variadic_templates``
  49. is specified as a requirement.
  50. In the above example, ``mylib`` requires ``cxx_constexpr`` when it
  51. is built itself, but consumers of ``mylib`` are not required to use a
  52. compiler which supports ``cxx_constexpr``. If the interface of
  53. ``mylib`` does require the ``cxx_constexpr`` feature (or any other
  54. known feature), that may be specified with the ``PUBLIC`` or
  55. ``INTERFACE`` signatures of :command:`target_compile_features`:
  56. .. code-block:: cmake
  57. add_library(mylib requires_constexpr.cpp)
  58. # cxx_constexpr is a usage-requirement
  59. target_compile_features(mylib PUBLIC cxx_constexpr)
  60. # main.cpp will be compiled with -std=gnu++11 on GNU for cxx_constexpr.
  61. add_executable(myexe main.cpp)
  62. target_link_libraries(myexe mylib)
  63. Feature requirements are evaluated transitively by consuming the link
  64. implementation. See :manual:`cmake-buildsystem(7)` for more on
  65. transitive behavior of build properties and usage requirements.
  66. Requiring Language Standards
  67. ----------------------------
  68. In projects that use a large number of commonly available features from
  69. a particular language standard (e.g. C++ 11) one may specify a
  70. meta-feature (e.g. ``cxx_std_11``) that requires use of a compiler mode
  71. aware of that standard. This is simpler than specifying all the
  72. features individually, but does not guarantee the existence of any
  73. particular feature. Diagnosis of use of unsupported features will be
  74. delayed until compile time.
  75. For example, if C++ 11 features are used extensively in a project's
  76. header files, then clients must use a compiler mode aware of C++ 11
  77. or above. This can be requested with the code:
  78. .. code-block:: cmake
  79. target_compile_features(mylib PUBLIC cxx_std_11)
  80. In this example, CMake will ensure the compiler is invoked in a mode
  81. that is aware of C++ 11 (or above), adding flags such as
  82. ``-std=gnu++11`` if necessary. This applies to sources within ``mylib``
  83. as well as any dependents (that may include headers from ``mylib``).
  84. Availability of Compiler Extensions
  85. -----------------------------------
  86. Because the :prop_tgt:`CXX_EXTENSIONS` target property is ``ON`` by default,
  87. CMake uses extended variants of language dialects by default, such as
  88. ``-std=gnu++11`` instead of ``-std=c++11``. That target property may be
  89. set to ``OFF`` to use the non-extended variant of the dialect flag. Note
  90. that because most compilers enable extensions by default, this could
  91. expose cross-platform bugs in user code or in the headers of third-party
  92. dependencies.
  93. Optional Compile Features
  94. =========================
  95. Compile features may be preferred if available, without creating a hard
  96. requirement. For example, a library may provides alternative
  97. implementations depending on whether the ``cxx_variadic_templates``
  98. feature is available:
  99. .. code-block:: c++
  100. #if Foo_COMPILER_CXX_VARIADIC_TEMPLATES
  101. template<int I, int... Is>
  102. struct Interface;
  103. template<int I>
  104. struct Interface<I>
  105. {
  106. static int accumulate()
  107. {
  108. return I;
  109. }
  110. };
  111. template<int I, int... Is>
  112. struct Interface
  113. {
  114. static int accumulate()
  115. {
  116. return I + Interface<Is...>::accumulate();
  117. }
  118. };
  119. #else
  120. template<int I1, int I2 = 0, int I3 = 0, int I4 = 0>
  121. struct Interface
  122. {
  123. static int accumulate() { return I1 + I2 + I3 + I4; }
  124. };
  125. #endif
  126. Such an interface depends on using the correct preprocessor defines for the
  127. compiler features. CMake can generate a header file containing such
  128. defines using the :module:`WriteCompilerDetectionHeader` module. The
  129. module contains the ``write_compiler_detection_header`` function which
  130. accepts parameters to control the content of the generated header file:
  131. .. code-block:: cmake
  132. write_compiler_detection_header(
  133. FILE "${CMAKE_CURRENT_BINARY_DIR}/foo_compiler_detection.h"
  134. PREFIX Foo
  135. COMPILERS GNU
  136. FEATURES
  137. cxx_variadic_templates
  138. )
  139. Such a header file may be used internally in the source code of a project,
  140. and it may be installed and used in the interface of library code.
  141. For each feature listed in ``FEATURES``, a preprocessor definition
  142. is created in the header file, and defined to either ``1`` or ``0``.
  143. Additionally, some features call for additional defines, such as the
  144. ``cxx_final`` and ``cxx_override`` features. Rather than being used in
  145. ``#ifdef`` code, the ``final`` keyword is abstracted by a symbol
  146. which is defined to either ``final``, a compiler-specific equivalent, or
  147. to empty. That way, C++ code can be written to unconditionally use the
  148. symbol, and compiler support determines what it is expanded to:
  149. .. code-block:: c++
  150. struct Interface {
  151. virtual void Execute() = 0;
  152. };
  153. struct Concrete Foo_FINAL {
  154. void Execute() Foo_OVERRIDE;
  155. };
  156. In this case, ``Foo_FINAL`` will expand to ``final`` if the
  157. compiler supports the keyword, or to empty otherwise.
  158. In this use-case, the CMake code will wish to enable a particular language
  159. standard if available from the compiler. The :prop_tgt:`CXX_STANDARD`
  160. target property variable may be set to the desired language standard
  161. for a particular target, and the :variable:`CMAKE_CXX_STANDARD` may be
  162. set to influence all following targets:
  163. .. code-block:: cmake
  164. write_compiler_detection_header(
  165. FILE "${CMAKE_CURRENT_BINARY_DIR}/foo_compiler_detection.h"
  166. PREFIX Foo
  167. COMPILERS GNU
  168. FEATURES
  169. cxx_final cxx_override
  170. )
  171. # Includes foo_compiler_detection.h and uses the Foo_FINAL symbol
  172. # which will expand to 'final' if the compiler supports the requested
  173. # CXX_STANDARD.
  174. add_library(foo foo.cpp)
  175. set_property(TARGET foo PROPERTY CXX_STANDARD 11)
  176. # Includes foo_compiler_detection.h and uses the Foo_FINAL symbol
  177. # which will expand to 'final' if the compiler supports the feature,
  178. # even though CXX_STANDARD is not set explicitly. The requirement of
  179. # cxx_constexpr causes CMake to set CXX_STANDARD internally, which
  180. # affects the compile flags.
  181. add_library(foo_impl foo_impl.cpp)
  182. target_compile_features(foo_impl PRIVATE cxx_constexpr)
  183. The ``write_compiler_detection_header`` function also creates compatibility
  184. code for other features which have standard equivalents. For example, the
  185. ``cxx_static_assert`` feature is emulated with a template and abstracted
  186. via the ``<PREFIX>_STATIC_ASSERT`` and ``<PREFIX>_STATIC_ASSERT_MSG``
  187. function-macros.
  188. Conditional Compilation Options
  189. ===============================
  190. Libraries may provide entirely different header files depending on
  191. requested compiler features.
  192. For example, a header at ``with_variadics/interface.h`` may contain:
  193. .. code-block:: c++
  194. template<int I, int... Is>
  195. struct Interface;
  196. template<int I>
  197. struct Interface<I>
  198. {
  199. static int accumulate()
  200. {
  201. return I;
  202. }
  203. };
  204. template<int I, int... Is>
  205. struct Interface
  206. {
  207. static int accumulate()
  208. {
  209. return I + Interface<Is...>::accumulate();
  210. }
  211. };
  212. while a header at ``no_variadics/interface.h`` may contain:
  213. .. code-block:: c++
  214. template<int I1, int I2 = 0, int I3 = 0, int I4 = 0>
  215. struct Interface
  216. {
  217. static int accumulate() { return I1 + I2 + I3 + I4; }
  218. };
  219. It would be possible to write a abstraction ``interface.h`` header
  220. containing something like:
  221. .. code-block:: c++
  222. #include "foo_compiler_detection.h"
  223. #if Foo_COMPILER_CXX_VARIADIC_TEMPLATES
  224. #include "with_variadics/interface.h"
  225. #else
  226. #include "no_variadics/interface.h"
  227. #endif
  228. However this could be unmaintainable if there are many files to
  229. abstract. What is needed is to use alternative include directories
  230. depending on the compiler capabilities.
  231. CMake provides a ``COMPILE_FEATURES``
  232. :manual:`generator expression <cmake-generator-expressions(7)>` to implement
  233. such conditions. This may be used with the build-property commands such as
  234. :command:`target_include_directories` and :command:`target_link_libraries`
  235. to set the appropriate :manual:`buildsystem <cmake-buildsystem(7)>`
  236. properties:
  237. .. code-block:: cmake
  238. add_library(foo INTERFACE)
  239. set(with_variadics ${CMAKE_CURRENT_SOURCE_DIR}/with_variadics)
  240. set(no_variadics ${CMAKE_CURRENT_SOURCE_DIR}/no_variadics)
  241. target_include_directories(foo
  242. INTERFACE
  243. "$<$<COMPILE_FEATURES:cxx_variadic_templates>:${with_variadics}>"
  244. "$<$<NOT:$<COMPILE_FEATURES:cxx_variadic_templates>>:${no_variadics}>"
  245. )
  246. Consuming code then simply links to the ``foo`` target as usual and uses
  247. the feature-appropriate include directory
  248. .. code-block:: cmake
  249. add_executable(consumer_with consumer_with.cpp)
  250. target_link_libraries(consumer_with foo)
  251. set_property(TARGET consumer_with CXX_STANDARD 11)
  252. add_executable(consumer_no consumer_no.cpp)
  253. target_link_libraries(consumer_no foo)
  254. Supported Compilers
  255. ===================
  256. CMake is currently aware of the :prop_tgt:`C++ standards <CXX_STANDARD>`
  257. and :prop_gbl:`compile features <CMAKE_CXX_KNOWN_FEATURES>` available from
  258. the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  259. versions specified for each:
  260. * ``AppleClang``: Apple Clang for Xcode versions 4.4 though 6.2.
  261. * ``Clang``: Clang compiler versions 2.9 through 3.4.
  262. * ``GNU``: GNU compiler versions 4.4 through 5.0.
  263. * ``MSVC``: Microsoft Visual Studio versions 2010 through 2017.
  264. * ``SunPro``: Oracle SolarisStudio versions 12.4 through 12.6.
  265. * ``Intel``: Intel compiler versions 12.1 through 17.0.
  266. CMake is currently aware of the :prop_tgt:`C standards <C_STANDARD>`
  267. and :prop_gbl:`compile features <CMAKE_C_KNOWN_FEATURES>` available from
  268. the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  269. versions specified for each:
  270. * all compilers and versions listed above for C++.
  271. * ``GNU``: GNU compiler versions 3.4 through 5.0.
  272. CMake is currently aware of the :prop_tgt:`C++ standards <CXX_STANDARD>` and
  273. their associated meta-features (e.g. ``cxx_std_11``) available from the
  274. following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  275. versions specified for each:
  276. * ``Cray``: Cray Compiler Environment version 8.1 through 8.5.8.
  277. * ``PGI``: PGI version 12.10 through 17.5.
  278. * ``XL``: IBM XL version 10.1 through 13.1.5.
  279. CMake is currently aware of the :prop_tgt:`C standards <C_STANDARD>` and
  280. their associated meta-features (e.g. ``c_std_99``) available from the
  281. following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  282. versions specified for each:
  283. * all compilers and versions listed above with only meta-features for C++.
  284. CMake is currently aware of the :prop_tgt:`CUDA standards <CUDA_STANDARD>`
  285. from the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  286. versions specified for each:
  287. * ``NVIDIA``: NVIDIA nvcc compiler 7.5 though 8.0.