cmake-compile-features.7.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. Availability of Compiler Extensions
  90. -----------------------------------
  91. Because the :prop_tgt:`CXX_EXTENSIONS` target property is ``ON`` by default,
  92. CMake uses extended variants of language dialects by default, such as
  93. ``-std=gnu++11`` instead of ``-std=c++11``. That target property may be
  94. set to ``OFF`` to use the non-extended variant of the dialect flag. Note
  95. that because most compilers enable extensions by default, this could
  96. expose cross-platform bugs in user code or in the headers of third-party
  97. dependencies.
  98. Optional Compile Features
  99. =========================
  100. Compile features may be preferred if available, without creating a hard
  101. requirement. For example, a library may provides alternative
  102. implementations depending on whether the ``cxx_variadic_templates``
  103. feature is available:
  104. .. code-block:: c++
  105. #if Foo_COMPILER_CXX_VARIADIC_TEMPLATES
  106. template<int I, int... Is>
  107. struct Interface;
  108. template<int I>
  109. struct Interface<I>
  110. {
  111. static int accumulate()
  112. {
  113. return I;
  114. }
  115. };
  116. template<int I, int... Is>
  117. struct Interface
  118. {
  119. static int accumulate()
  120. {
  121. return I + Interface<Is...>::accumulate();
  122. }
  123. };
  124. #else
  125. template<int I1, int I2 = 0, int I3 = 0, int I4 = 0>
  126. struct Interface
  127. {
  128. static int accumulate() { return I1 + I2 + I3 + I4; }
  129. };
  130. #endif
  131. Such an interface depends on using the correct preprocessor defines for the
  132. compiler features. CMake can generate a header file containing such
  133. defines using the :module:`WriteCompilerDetectionHeader` module. The
  134. module contains the ``write_compiler_detection_header`` function which
  135. accepts parameters to control the content of the generated header file:
  136. .. code-block:: cmake
  137. write_compiler_detection_header(
  138. FILE "${CMAKE_CURRENT_BINARY_DIR}/foo_compiler_detection.h"
  139. PREFIX Foo
  140. COMPILERS GNU
  141. FEATURES
  142. cxx_variadic_templates
  143. )
  144. Such a header file may be used internally in the source code of a project,
  145. and it may be installed and used in the interface of library code.
  146. For each feature listed in ``FEATURES``, a preprocessor definition
  147. is created in the header file, and defined to either ``1`` or ``0``.
  148. Additionally, some features call for additional defines, such as the
  149. ``cxx_final`` and ``cxx_override`` features. Rather than being used in
  150. ``#ifdef`` code, the ``final`` keyword is abstracted by a symbol
  151. which is defined to either ``final``, a compiler-specific equivalent, or
  152. to empty. That way, C++ code can be written to unconditionally use the
  153. symbol, and compiler support determines what it is expanded to:
  154. .. code-block:: c++
  155. struct Interface {
  156. virtual void Execute() = 0;
  157. };
  158. struct Concrete Foo_FINAL {
  159. void Execute() Foo_OVERRIDE;
  160. };
  161. In this case, ``Foo_FINAL`` will expand to ``final`` if the
  162. compiler supports the keyword, or to empty otherwise.
  163. In this use-case, the CMake code will wish to enable a particular language
  164. standard if available from the compiler. The :prop_tgt:`CXX_STANDARD`
  165. target property variable may be set to the desired language standard
  166. for a particular target, and the :variable:`CMAKE_CXX_STANDARD` may be
  167. set to influence all following targets:
  168. .. code-block:: cmake
  169. write_compiler_detection_header(
  170. FILE "${CMAKE_CURRENT_BINARY_DIR}/foo_compiler_detection.h"
  171. PREFIX Foo
  172. COMPILERS GNU
  173. FEATURES
  174. cxx_final cxx_override
  175. )
  176. # Includes foo_compiler_detection.h and uses the Foo_FINAL symbol
  177. # which will expand to 'final' if the compiler supports the requested
  178. # CXX_STANDARD.
  179. add_library(foo foo.cpp)
  180. set_property(TARGET foo PROPERTY CXX_STANDARD 11)
  181. # Includes foo_compiler_detection.h and uses the Foo_FINAL symbol
  182. # which will expand to 'final' if the compiler supports the feature,
  183. # even though CXX_STANDARD is not set explicitly. The requirement of
  184. # cxx_constexpr causes CMake to set CXX_STANDARD internally, which
  185. # affects the compile flags.
  186. add_library(foo_impl foo_impl.cpp)
  187. target_compile_features(foo_impl PRIVATE cxx_constexpr)
  188. The ``write_compiler_detection_header`` function also creates compatibility
  189. code for other features which have standard equivalents. For example, the
  190. ``cxx_static_assert`` feature is emulated with a template and abstracted
  191. via the ``<PREFIX>_STATIC_ASSERT`` and ``<PREFIX>_STATIC_ASSERT_MSG``
  192. function-macros.
  193. Conditional Compilation Options
  194. ===============================
  195. Libraries may provide entirely different header files depending on
  196. requested compiler features.
  197. For example, a header at ``with_variadics/interface.h`` may contain:
  198. .. code-block:: c++
  199. template<int I, int... Is>
  200. struct Interface;
  201. template<int I>
  202. struct Interface<I>
  203. {
  204. static int accumulate()
  205. {
  206. return I;
  207. }
  208. };
  209. template<int I, int... Is>
  210. struct Interface
  211. {
  212. static int accumulate()
  213. {
  214. return I + Interface<Is...>::accumulate();
  215. }
  216. };
  217. while a header at ``no_variadics/interface.h`` may contain:
  218. .. code-block:: c++
  219. template<int I1, int I2 = 0, int I3 = 0, int I4 = 0>
  220. struct Interface
  221. {
  222. static int accumulate() { return I1 + I2 + I3 + I4; }
  223. };
  224. It would be possible to write a abstraction ``interface.h`` header
  225. containing something like:
  226. .. code-block:: c++
  227. #include "foo_compiler_detection.h"
  228. #if Foo_COMPILER_CXX_VARIADIC_TEMPLATES
  229. #include "with_variadics/interface.h"
  230. #else
  231. #include "no_variadics/interface.h"
  232. #endif
  233. However this could be unmaintainable if there are many files to
  234. abstract. What is needed is to use alternative include directories
  235. depending on the compiler capabilities.
  236. CMake provides a ``COMPILE_FEATURES``
  237. :manual:`generator expression <cmake-generator-expressions(7)>` to implement
  238. such conditions. This may be used with the build-property commands such as
  239. :command:`target_include_directories` and :command:`target_link_libraries`
  240. to set the appropriate :manual:`buildsystem <cmake-buildsystem(7)>`
  241. properties:
  242. .. code-block:: cmake
  243. add_library(foo INTERFACE)
  244. set(with_variadics ${CMAKE_CURRENT_SOURCE_DIR}/with_variadics)
  245. set(no_variadics ${CMAKE_CURRENT_SOURCE_DIR}/no_variadics)
  246. target_include_directories(foo
  247. INTERFACE
  248. "$<$<COMPILE_FEATURES:cxx_variadic_templates>:${with_variadics}>"
  249. "$<$<NOT:$<COMPILE_FEATURES:cxx_variadic_templates>>:${no_variadics}>"
  250. )
  251. Consuming code then simply links to the ``foo`` target as usual and uses
  252. the feature-appropriate include directory
  253. .. code-block:: cmake
  254. add_executable(consumer_with consumer_with.cpp)
  255. target_link_libraries(consumer_with foo)
  256. set_property(TARGET consumer_with CXX_STANDARD 11)
  257. add_executable(consumer_no consumer_no.cpp)
  258. target_link_libraries(consumer_no foo)
  259. Supported Compilers
  260. ===================
  261. CMake is currently aware of the :prop_tgt:`C++ standards <CXX_STANDARD>`
  262. and :prop_gbl:`compile features <CMAKE_CXX_KNOWN_FEATURES>` available from
  263. the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  264. versions specified for each:
  265. * ``AppleClang``: Apple Clang for Xcode versions 4.4+.
  266. * ``Clang``: Clang compiler versions 2.9+.
  267. * ``GNU``: GNU compiler versions 4.4+.
  268. * ``MSVC``: Microsoft Visual Studio versions 2010+.
  269. * ``SunPro``: Oracle SolarisStudio versions 12.4+.
  270. * ``Intel``: Intel compiler versions 12.1+.
  271. CMake is currently aware of the :prop_tgt:`C standards <C_STANDARD>`
  272. and :prop_gbl:`compile features <CMAKE_C_KNOWN_FEATURES>` available from
  273. the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  274. versions specified for each:
  275. * all compilers and versions listed above for C++.
  276. * ``GNU``: GNU compiler versions 3.4+
  277. CMake is currently aware of the :prop_tgt:`C++ standards <CXX_STANDARD>` and
  278. their associated meta-features (e.g. ``cxx_std_11``) available from the
  279. following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  280. versions specified for each:
  281. * ``Cray``: Cray Compiler Environment version 8.1+.
  282. * ``PGI``: PGI version 12.10+.
  283. * ``NVHPC``: NVIDIA HPC compilers version 11.0+.
  284. * ``TI``: Texas Instruments compiler.
  285. * ``XL``: IBM XL version 10.1+.
  286. CMake is currently aware of the :prop_tgt:`C standards <C_STANDARD>` and
  287. their associated meta-features (e.g. ``c_std_99``) available from the
  288. following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  289. versions specified for each:
  290. * all compilers and versions listed above with only meta-features for C++.
  291. CMake is currently aware of the :prop_tgt:`CUDA standards <CUDA_STANDARD>` and
  292. their associated meta-features (e.g. ``cuda_std_11``) available from the
  293. following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
  294. versions specified for each:
  295. * ``Clang``: Clang compiler 5.0+.
  296. * ``NVIDIA``: NVIDIA nvcc compiler 7.5+.