0d15ddfc34ac1a89d50d545300e22e564ff7395a
[platform/upstream/cmake.git] / Help / manual / cmake-compile-features.7.rst
1 .. cmake-manual-description: CMake Compile Features Reference
2
3 cmake-compile-features(7)
4 *************************
5
6 .. only:: html
7
8    .. contents::
9
10 Introduction
11 ============
12
13 Project source code may depend on, or be conditional on, the availability
14 of certain features of the compiler.  There are three use-cases which arise:
15 `Compile Feature Requirements`_, `Optional Compile Features`_
16 and `Conditional Compilation Options`_.
17
18 While features are typically specified in programming language standards,
19 CMake provides a primary user interface based on granular handling of
20 the features, not the language standard that introduced the feature.
21
22 The :prop_gbl:`CMAKE_C_KNOWN_FEATURES`, :prop_gbl:`CMAKE_CUDA_KNOWN_FEATURES`,
23 and :prop_gbl:`CMAKE_CXX_KNOWN_FEATURES` global properties contain all the
24 features known to CMake, regardless of compiler support for the feature.
25 The :variable:`CMAKE_C_COMPILE_FEATURES`, :variable:`CMAKE_CUDA_COMPILE_FEATURES`
26 , and :variable:`CMAKE_CXX_COMPILE_FEATURES` variables contain all features
27 CMake knows are known to the compiler, regardless of language standard
28 or compile flags needed to use them.
29
30 Features known to CMake are named mostly following the same convention
31 as the Clang feature test macros.  There are some exceptions, such as
32 CMake using ``cxx_final`` and ``cxx_override`` instead of the single
33 ``cxx_override_control`` used by Clang.
34
35 Note that there are no separate compile features properties or variables for
36 the ``OBJC`` or ``OBJCXX`` languages.  These are based off ``C`` or ``C++``
37 respectively, so the properties and variables for their corresponding base
38 language should be used instead.
39
40 Compile Feature Requirements
41 ============================
42
43 Compile feature requirements may be specified with the
44 :command:`target_compile_features` command.  For example, if a target must
45 be compiled with compiler support for the
46 :prop_gbl:`cxx_constexpr <CMAKE_CXX_KNOWN_FEATURES>` feature:
47
48 .. code-block:: cmake
49
50   add_library(mylib requires_constexpr.cpp)
51   target_compile_features(mylib PRIVATE cxx_constexpr)
52
53 In processing the requirement for the ``cxx_constexpr`` feature,
54 :manual:`cmake(1)` will ensure that the in-use C++ compiler is capable
55 of the feature, and will add any necessary flags such as ``-std=gnu++11``
56 to the compile lines of C++ files in the ``mylib`` target.  A
57 ``FATAL_ERROR`` is issued if the compiler is not capable of the
58 feature.
59
60 The exact compile flags and language standard are deliberately not part
61 of the user interface for this use-case.  CMake will compute the
62 appropriate compile flags to use by considering the features specified
63 for each target.
64
65 Such compile flags are added even if the compiler supports the
66 particular feature without the flag. For example, the GNU compiler
67 supports variadic templates (with a warning) even if ``-std=gnu++98`` is
68 used.  CMake adds the ``-std=gnu++11`` flag if ``cxx_variadic_templates``
69 is specified as a requirement.
70
71 In the above example, ``mylib`` requires ``cxx_constexpr`` when it
72 is built itself, but consumers of ``mylib`` are not required to use a
73 compiler which supports ``cxx_constexpr``.  If the interface of
74 ``mylib`` does require the ``cxx_constexpr`` feature (or any other
75 known feature), that may be specified with the ``PUBLIC`` or
76 ``INTERFACE`` signatures of :command:`target_compile_features`:
77
78 .. code-block:: cmake
79
80   add_library(mylib requires_constexpr.cpp)
81   # cxx_constexpr is a usage-requirement
82   target_compile_features(mylib PUBLIC cxx_constexpr)
83
84   # main.cpp will be compiled with -std=gnu++11 on GNU for cxx_constexpr.
85   add_executable(myexe main.cpp)
86   target_link_libraries(myexe mylib)
87
88 Feature requirements are evaluated transitively by consuming the link
89 implementation.  See :manual:`cmake-buildsystem(7)` for more on
90 transitive behavior of build properties and usage requirements.
91
92 .. _`Requiring Language Standards`:
93
94 Requiring Language Standards
95 ----------------------------
96
97 In projects that use a large number of commonly available features from
98 a particular language standard (e.g. C++ 11) one may specify a
99 meta-feature (e.g. ``cxx_std_11``) that requires use of a compiler mode
100 that is at minimum aware of that standard, but could be greater.
101 This is simpler than specifying all the features individually, but does
102 not guarantee the existence of any particular feature.
103 Diagnosis of use of unsupported features will be delayed until compile time.
104
105 For example, if C++ 11 features are used extensively in a project's
106 header files, then clients must use a compiler mode that is no less
107 than C++ 11.  This can be requested with the code:
108
109 .. code-block:: cmake
110
111   target_compile_features(mylib PUBLIC cxx_std_11)
112
113 In this example, CMake will ensure the compiler is invoked in a mode
114 of at-least C++ 11 (or C++ 14, C++ 17, ...), adding flags such as
115 ``-std=gnu++11`` if necessary.  This applies to sources within ``mylib``
116 as well as any dependents (that may include headers from ``mylib``).
117
118 Availability of Compiler Extensions
119 -----------------------------------
120
121 Because the :prop_tgt:`CXX_EXTENSIONS` target property is ``ON`` by default,
122 CMake uses extended variants of language dialects by default, such as
123 ``-std=gnu++11`` instead of ``-std=c++11``.  That target property may be
124 set to ``OFF`` to use the non-extended variant of the dialect flag.  Note
125 that because most compilers enable extensions by default, this could
126 expose cross-platform bugs in user code or in the headers of third-party
127 dependencies.
128
129 Optional Compile Features
130 =========================
131
132 Compile features may be preferred if available, without creating a hard
133 requirement.  For example, a library may provides alternative
134 implementations depending on whether the ``cxx_variadic_templates``
135 feature is available:
136
137 .. code-block:: c++
138
139   #if Foo_COMPILER_CXX_VARIADIC_TEMPLATES
140   template<int I, int... Is>
141   struct Interface;
142
143   template<int I>
144   struct Interface<I>
145   {
146     static int accumulate()
147     {
148       return I;
149     }
150   };
151
152   template<int I, int... Is>
153   struct Interface
154   {
155     static int accumulate()
156     {
157       return I + Interface<Is...>::accumulate();
158     }
159   };
160   #else
161   template<int I1, int I2 = 0, int I3 = 0, int I4 = 0>
162   struct Interface
163   {
164     static int accumulate() { return I1 + I2 + I3 + I4; }
165   };
166   #endif
167
168 Such an interface depends on using the correct preprocessor defines for the
169 compiler features.  CMake can generate a header file containing such
170 defines using the :module:`WriteCompilerDetectionHeader` module.  The
171 module contains the ``write_compiler_detection_header`` function which
172 accepts parameters to control the content of the generated header file:
173
174 .. code-block:: cmake
175
176   write_compiler_detection_header(
177     FILE "${CMAKE_CURRENT_BINARY_DIR}/foo_compiler_detection.h"
178     PREFIX Foo
179     COMPILERS GNU
180     FEATURES
181       cxx_variadic_templates
182   )
183
184 Such a header file may be used internally in the source code of a project,
185 and it may be installed and used in the interface of library code.
186
187 For each feature listed in ``FEATURES``, a preprocessor definition
188 is created in the header file, and defined to either ``1`` or ``0``.
189
190 Additionally, some features call for additional defines, such as the
191 ``cxx_final`` and ``cxx_override`` features. Rather than being used in
192 ``#ifdef`` code, the ``final`` keyword is abstracted by a symbol
193 which is defined to either ``final``, a compiler-specific equivalent, or
194 to empty.  That way, C++ code can be written to unconditionally use the
195 symbol, and compiler support determines what it is expanded to:
196
197 .. code-block:: c++
198
199   struct Interface {
200     virtual void Execute() = 0;
201   };
202
203   struct Concrete Foo_FINAL {
204     void Execute() Foo_OVERRIDE;
205   };
206
207 In this case, ``Foo_FINAL`` will expand to ``final`` if the
208 compiler supports the keyword, or to empty otherwise.
209
210 In this use-case, the CMake code will wish to enable a particular language
211 standard if available from the compiler. The :prop_tgt:`CXX_STANDARD`
212 target property variable may be set to the desired language standard
213 for a particular target, and the :variable:`CMAKE_CXX_STANDARD` may be
214 set to influence all following targets:
215
216 .. code-block:: cmake
217
218   write_compiler_detection_header(
219     FILE "${CMAKE_CURRENT_BINARY_DIR}/foo_compiler_detection.h"
220     PREFIX Foo
221     COMPILERS GNU
222     FEATURES
223       cxx_final cxx_override
224   )
225
226   # Includes foo_compiler_detection.h and uses the Foo_FINAL symbol
227   # which will expand to 'final' if the compiler supports the requested
228   # CXX_STANDARD.
229   add_library(foo foo.cpp)
230   set_property(TARGET foo PROPERTY CXX_STANDARD 11)
231
232   # Includes foo_compiler_detection.h and uses the Foo_FINAL symbol
233   # which will expand to 'final' if the compiler supports the feature,
234   # even though CXX_STANDARD is not set explicitly.  The requirement of
235   # cxx_constexpr causes CMake to set CXX_STANDARD internally, which
236   # affects the compile flags.
237   add_library(foo_impl foo_impl.cpp)
238   target_compile_features(foo_impl PRIVATE cxx_constexpr)
239
240 The ``write_compiler_detection_header`` function also creates compatibility
241 code for other features which have standard equivalents.  For example, the
242 ``cxx_static_assert`` feature is emulated with a template and abstracted
243 via the ``<PREFIX>_STATIC_ASSERT`` and ``<PREFIX>_STATIC_ASSERT_MSG``
244 function-macros.
245
246 Conditional Compilation Options
247 ===============================
248
249 Libraries may provide entirely different header files depending on
250 requested compiler features.
251
252 For example, a header at ``with_variadics/interface.h`` may contain:
253
254 .. code-block:: c++
255
256   template<int I, int... Is>
257   struct Interface;
258
259   template<int I>
260   struct Interface<I>
261   {
262     static int accumulate()
263     {
264       return I;
265     }
266   };
267
268   template<int I, int... Is>
269   struct Interface
270   {
271     static int accumulate()
272     {
273       return I + Interface<Is...>::accumulate();
274     }
275   };
276
277 while a header at ``no_variadics/interface.h`` may contain:
278
279 .. code-block:: c++
280
281   template<int I1, int I2 = 0, int I3 = 0, int I4 = 0>
282   struct Interface
283   {
284     static int accumulate() { return I1 + I2 + I3 + I4; }
285   };
286
287 It would be possible to write a abstraction ``interface.h`` header
288 containing something like:
289
290 .. code-block:: c++
291
292   #include "foo_compiler_detection.h"
293   #if Foo_COMPILER_CXX_VARIADIC_TEMPLATES
294   #include "with_variadics/interface.h"
295   #else
296   #include "no_variadics/interface.h"
297   #endif
298
299 However this could be unmaintainable if there are many files to
300 abstract. What is needed is to use alternative include directories
301 depending on the compiler capabilities.
302
303 CMake provides a ``COMPILE_FEATURES``
304 :manual:`generator expression <cmake-generator-expressions(7)>` to implement
305 such conditions.  This may be used with the build-property commands such as
306 :command:`target_include_directories` and :command:`target_link_libraries`
307 to set the appropriate :manual:`buildsystem <cmake-buildsystem(7)>`
308 properties:
309
310 .. code-block:: cmake
311
312   add_library(foo INTERFACE)
313   set(with_variadics ${CMAKE_CURRENT_SOURCE_DIR}/with_variadics)
314   set(no_variadics ${CMAKE_CURRENT_SOURCE_DIR}/no_variadics)
315   target_include_directories(foo
316     INTERFACE
317       "$<$<COMPILE_FEATURES:cxx_variadic_templates>:${with_variadics}>"
318       "$<$<NOT:$<COMPILE_FEATURES:cxx_variadic_templates>>:${no_variadics}>"
319     )
320
321 Consuming code then simply links to the ``foo`` target as usual and uses
322 the feature-appropriate include directory
323
324 .. code-block:: cmake
325
326   add_executable(consumer_with consumer_with.cpp)
327   target_link_libraries(consumer_with foo)
328   set_property(TARGET consumer_with CXX_STANDARD 11)
329
330   add_executable(consumer_no consumer_no.cpp)
331   target_link_libraries(consumer_no foo)
332
333 Supported Compilers
334 ===================
335
336 CMake is currently aware of the :prop_tgt:`C++ standards <CXX_STANDARD>`
337 and :prop_gbl:`compile features <CMAKE_CXX_KNOWN_FEATURES>` available from
338 the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
339 versions specified for each:
340
341 * ``AppleClang``: Apple Clang for Xcode versions 4.4+.
342 * ``Clang``: Clang compiler versions 2.9+.
343 * ``GNU``: GNU compiler versions 4.4+.
344 * ``MSVC``: Microsoft Visual Studio versions 2010+.
345 * ``SunPro``: Oracle SolarisStudio versions 12.4+.
346 * ``Intel``: Intel compiler versions 12.1+.
347
348 CMake is currently aware of the :prop_tgt:`C standards <C_STANDARD>`
349 and :prop_gbl:`compile features <CMAKE_C_KNOWN_FEATURES>` available from
350 the following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
351 versions specified for each:
352
353 * all compilers and versions listed above for C++.
354 * ``GNU``: GNU compiler versions 3.4+
355
356 CMake is currently aware of the :prop_tgt:`C++ standards <CXX_STANDARD>` and
357 their associated meta-features (e.g. ``cxx_std_11``) available from the
358 following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
359 versions specified for each:
360
361 * ``Cray``: Cray Compiler Environment version 8.1+.
362 * ``PGI``: PGI version 12.10+.
363 * ``NVHPC``: NVIDIA HPC compilers version 11.0+.
364 * ``TI``: Texas Instruments compiler.
365 * ``XL``: IBM XL version 10.1+.
366
367 CMake is currently aware of the :prop_tgt:`C standards <C_STANDARD>` and
368 their associated meta-features (e.g. ``c_std_99``) available from the
369 following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
370 versions specified for each:
371
372 * all compilers and versions listed above with only meta-features for C++.
373
374 CMake is currently aware of the :prop_tgt:`CUDA standards <CUDA_STANDARD>` and
375 their associated meta-features (e.g. ``cuda_std_11``) available from the
376 following :variable:`compiler ids <CMAKE_<LANG>_COMPILER_ID>` as of the
377 versions specified for each:
378
379 * ``Clang``: Clang compiler 5.0+.
380 * ``NVIDIA``: NVIDIA nvcc compiler 7.5+.