Help: Fix link in cmake-buildsystem(7)
[kiteware-cmake.git] / Help / manual / cmake-buildsystem.7.rst
blob9eca05dee903ff00884d6c0acb3f4fead7933ed0
1 .. cmake-manual-description: CMake Buildsystem Reference
3 cmake-buildsystem(7)
4 ********************
6 .. only:: html
8    .. contents::
10 Introduction
11 ============
13 A CMake-based buildsystem is organized as a set of high-level logical
14 targets.  Each target corresponds to an executable or library, or
15 is a custom target containing custom commands.  Dependencies between the
16 targets are expressed in the buildsystem to determine the build order
17 and the rules for regeneration in response to change.
19 Binary Targets
20 ==============
22 Executables and libraries are defined using the :command:`add_executable`
23 and :command:`add_library` commands.  The resulting binary files have
24 appropriate :prop_tgt:`PREFIX`, :prop_tgt:`SUFFIX` and extensions for the
25 platform targeted. Dependencies between binary targets are expressed using
26 the :command:`target_link_libraries` command:
28 .. code-block:: cmake
30   add_library(archive archive.cpp zip.cpp lzma.cpp)
31   add_executable(zipapp zipapp.cpp)
32   target_link_libraries(zipapp archive)
34 ``archive`` is defined as a ``STATIC`` library -- an archive containing objects
35 compiled from ``archive.cpp``, ``zip.cpp``, and ``lzma.cpp``.  ``zipapp``
36 is defined as an executable formed by compiling and linking ``zipapp.cpp``.
37 When linking the ``zipapp`` executable, the ``archive`` static library is
38 linked in.
40 .. _`Binary Executables`:
42 Binary Executables
43 ------------------
45 The :command:`add_executable` command defines an executable target:
47 .. code-block:: cmake
49   add_executable(mytool mytool.cpp)
51 Commands such as :command:`add_custom_command`, which generates rules to be
52 run at build time can transparently use an :prop_tgt:`EXECUTABLE <TYPE>`
53 target as a ``COMMAND`` executable.  The buildsystem rules will ensure that
54 the executable is built before attempting to run the command.
56 Binary Library Types
57 --------------------
59 .. _`Normal Libraries`:
61 Normal Libraries
62 ^^^^^^^^^^^^^^^^
64 By default, the :command:`add_library` command defines a ``STATIC`` library,
65 unless a type is specified.  A type may be specified when using the command:
67 .. code-block:: cmake
69   add_library(archive SHARED archive.cpp zip.cpp lzma.cpp)
71 .. code-block:: cmake
73   add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)
75 The :variable:`BUILD_SHARED_LIBS` variable may be enabled to change the
76 behavior of :command:`add_library` to build shared libraries by default.
78 In the context of the buildsystem definition as a whole, it is largely
79 irrelevant whether particular libraries are ``SHARED`` or ``STATIC`` --
80 the commands, dependency specifications and other APIs work similarly
81 regardless of the library type.  The ``MODULE`` library type is
82 dissimilar in that it is generally not linked to -- it is not used in
83 the right-hand-side of the :command:`target_link_libraries` command.
84 It is a type which is loaded as a plugin using runtime techniques.
85 If the library does not export any unmanaged symbols (e.g. Windows
86 resource DLL, C++/CLI DLL), it is required that the library not be a
87 ``SHARED`` library because CMake expects ``SHARED`` libraries to export
88 at least one symbol.
90 .. code-block:: cmake
92   add_library(archive MODULE 7z.cpp)
94 .. _`Apple Frameworks`:
96 Apple Frameworks
97 """"""""""""""""
99 A ``SHARED`` library may be marked with the :prop_tgt:`FRAMEWORK`
100 target property to create an macOS or iOS Framework Bundle.
101 A library with the ``FRAMEWORK`` target property should also set the
102 :prop_tgt:`FRAMEWORK_VERSION` target property.  This property is typically
103 set to the value of "A" by macOS conventions.
104 The ``MACOSX_FRAMEWORK_IDENTIFIER`` sets the ``CFBundleIdentifier`` key
105 and it uniquely identifies the bundle.
107 .. code-block:: cmake
109   add_library(MyFramework SHARED MyFramework.cpp)
110   set_target_properties(MyFramework PROPERTIES
111     FRAMEWORK TRUE
112     FRAMEWORK_VERSION A # Version "A" is macOS convention
113     MACOSX_FRAMEWORK_IDENTIFIER org.cmake.MyFramework
114   )
116 .. _`Object Libraries`:
118 Object Libraries
119 ^^^^^^^^^^^^^^^^
121 The ``OBJECT`` library type defines a non-archival collection of object files
122 resulting from compiling the given source files.  The object files collection
123 may be used as source inputs to other targets by using the syntax
124 :genex:`$<TARGET_OBJECTS:name>`.  This is a
125 :manual:`generator expression <cmake-generator-expressions(7)>` that can be
126 used to supply the ``OBJECT`` library content to other targets:
128 .. code-block:: cmake
130   add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)
132   add_library(archiveExtras STATIC $<TARGET_OBJECTS:archive> extras.cpp)
134   add_executable(test_exe $<TARGET_OBJECTS:archive> test.cpp)
136 The link (or archiving) step of those other targets will use the object
137 files collection in addition to those from their own sources.
139 Alternatively, object libraries may be linked into other targets:
141 .. code-block:: cmake
143   add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)
145   add_library(archiveExtras STATIC extras.cpp)
146   target_link_libraries(archiveExtras PUBLIC archive)
148   add_executable(test_exe test.cpp)
149   target_link_libraries(test_exe archive)
151 The link (or archiving) step of those other targets will use the object
152 files from ``OBJECT`` libraries that are *directly* linked.  Additionally,
153 usage requirements of the ``OBJECT`` libraries will be honored when compiling
154 sources in those other targets.  Furthermore, those usage requirements
155 will propagate transitively to dependents of those other targets.
157 Object libraries may not be used as the ``TARGET`` in a use of the
158 :command:`add_custom_command(TARGET)` command signature.  However,
159 the list of objects can be used by :command:`add_custom_command(OUTPUT)`
160 or :command:`file(GENERATE)` by using ``$<TARGET_OBJECTS:objlib>``.
162 Build Specification and Usage Requirements
163 ==========================================
165 Targets build according to their own
166 `build specification <Target Build Specification_>`_ in combination with
167 `usage requirements <Target Usage Requirements_>`_ propagated from their
168 link dependencies.  Both may be specified using target-specific
169 `commands <Target Commands_>`_.
171 For example:
173 .. code-block:: cmake
175   add_library(archive SHARED archive.cpp zip.cpp)
177   if (LZMA_FOUND)
178     # Add a source implementing support for lzma.
179     target_sources(archive PRIVATE lzma.cpp)
181     # Compile the 'archive' library sources with '-DBUILDING_WITH_LZMA'.
182     target_compile_definitions(archive PRIVATE BUILDING_WITH_LZMA)
183   endif()
185   target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)
187   add_executable(consumer consumer.cpp)
189   # Link 'consumer' to 'archive'.  This also consumes its usage requirements,
190   # so 'consumer.cpp' is compiled with '-DUSING_ARCHIVE_LIB'.
191   target_link_libraries(consumer archive)
194 Target Commands
195 ---------------
197 Target-specific commands populate the
198 `build specification <Target Build Specification_>`_ of `Binary Targets`_ and
199 `usage requirements <Target Usage Requirements_>`_ of `Binary Targets`_,
200 `Interface Libraries`_, and `Imported Targets`_.
202 .. _`Target Command Scope`:
204 Invocations must specify scope keywords, each affecting the visibility
205 of arguments following it.  The scopes are:
207 ``PUBLIC``
208   Populates both properties for `building <Target Build Specification_>`_
209   and properties for `using <Target Usage Requirements_>`_ a target.
211 ``PRIVATE``
212   Populates only properties for `building <Target Build Specification_>`_
213   a target.
215 ``INTERFACE``
216   Populates only properties for `using <Target Usage Requirements_>`_
217   a target.
219 The commands are:
221 :command:`target_compile_definitions`
222   Populates the :prop_tgt:`COMPILE_DEFINITIONS` build specification and
223   :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS` usage requirement properties.
225   For example, the call
227   .. code-block:: cmake
229     target_compile_definitions(archive
230       PRIVATE   BUILDING_WITH_LZMA
231       INTERFACE USING_ARCHIVE_LIB
232     )
234   appends ``BUILDING_WITH_LZMA`` to the target's ``COMPILE_DEFINITIONS``
235   property and appends ``USING_ARCHIVE_LIB`` to the target's
236   ``INTERFACE_COMPILE_DEFINITIONS`` property.
238 :command:`target_compile_options`
239   Populates the :prop_tgt:`COMPILE_OPTIONS` build specification and
240   :prop_tgt:`INTERFACE_COMPILE_OPTIONS` usage requirement properties.
242 :command:`target_compile_features`
243   .. versionadded:: 3.1
245   Populates the :prop_tgt:`COMPILE_FEATURES` build specification and
246   :prop_tgt:`INTERFACE_COMPILE_FEATURES` usage requirement properties.
248 :command:`target_include_directories`
249   Populates the :prop_tgt:`INCLUDE_DIRECTORIES` build specification
250   and :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` usage requirement
251   properties.  With the ``SYSTEM`` option, it also populates the
252   :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES` usage requirement.
254   For convenience, the :variable:`CMAKE_INCLUDE_CURRENT_DIR` variable
255   may be enabled to add the source directory and corresponding build
256   directory as ``INCLUDE_DIRECTORIES`` on all targets.  Similarly,
257   the :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE` variable may
258   be enabled to add them as ``INTERFACE_INCLUDE_DIRECTORIES`` on all
259   targets.
261 :command:`target_sources`
262   .. versionadded:: 3.1
264   Populates the :prop_tgt:`SOURCES` build specification and
265   :prop_tgt:`INTERFACE_SOURCES` usage requirement properties.
267   It also supports specifying :ref:`File Sets`, which can add C++ module
268   sources and headers not listed in the ``SOURCES`` and ``INTERFACE_SOURCES``
269   properties.  File sets may also populate the :prop_tgt:`INCLUDE_DIRECTORIES`
270   build specification and :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` usage
271   requirement properties with the include directories containing the headers.
273 :command:`target_precompile_headers`
274   .. versionadded:: 3.16
276   Populates the :prop_tgt:`PRECOMPILE_HEADERS` build specification and
277   :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` usage requirement properties.
279 :command:`target_link_libraries`
280   Populates the :prop_tgt:`LINK_LIBRARIES` build specification
281   and :prop_tgt:`INTERFACE_LINK_LIBRARIES` usage requirement properties.
283   This is the primary mechanism by which link dependencies and their
284   `usage requirements <Target Usage Requirements_>`_ are transitively
285   propagated to affect compilation and linking of a target.
287 :command:`target_link_directories`
288   .. versionadded:: 3.13
290   Populates the :prop_tgt:`LINK_DIRECTORIES` build specification and
291   :prop_tgt:`INTERFACE_LINK_DIRECTORIES` usage requirement properties.
293 :command:`target_link_options`
294   .. versionadded:: 3.13
296   Populates the :prop_tgt:`LINK_OPTIONS` build specification and
297   :prop_tgt:`INTERFACE_LINK_OPTIONS` usage requirement properties.
299 .. _`Target Build Specification`:
301 Target Build Specification
302 --------------------------
304 The build specification of `Binary Targets`_ is represented by target
305 properties.  For each of the following `build <Target Build Properties_>`_
306 and `link <Target Link Properties_>`_ properties, compilation and linking
307 of the target is affected both by its own value and by the corresponding
308 `usage requirement <Target Usage Requirements_>`_ property, named with
309 an ``INTERFACE_`` prefix, collected from the transitive closure of link
310 dependencies.
312 .. _`Target Build Properties`:
314 Target Build Properties
315 ^^^^^^^^^^^^^^^^^^^^^^^
317 :prop_tgt:`COMPILE_DEFINITIONS`
318   List of compile definitions for compiling sources in the target.
319   These are passed to the compiler with ``-D`` flags, or equivalent,
320   in an unspecified order.
322   The :prop_tgt:`DEFINE_SYMBOL` target property is also used
323   as a compile definition as a special convenience case for
324   ``SHARED`` and ``MODULE`` library targets.
326 :prop_tgt:`COMPILE_OPTIONS`
327   List of compile options for compiling sources in the target.
328   These are passed to the compiler as flags, in the order of appearance.
330   Compile options are automatically escaped for the shell.
332   Some compile options are best specified via dedicated settings,
333   such as the :prop_tgt:`POSITION_INDEPENDENT_CODE` target property.
335 :prop_tgt:`COMPILE_FEATURES`
336   .. versionadded:: 3.1
338   List of :manual:`compile features <cmake-compile-features(7)>` needed
339   for compiling sources in the target.  Typically these ensure the
340   target's sources are compiled using a sufficient language standard level.
342 :prop_tgt:`INCLUDE_DIRECTORIES`
343   List of include directories for compiling sources in the target.
344   These are passed to the compiler with ``-I`` or ``-isystem`` flags,
345   or equivalent, in the order of appearance.
347   For convenience, the :variable:`CMAKE_INCLUDE_CURRENT_DIR` variable
348   may be enabled to add the source directory and corresponding build
349   directory as ``INCLUDE_DIRECTORIES`` on all targets.
351 :prop_tgt:`SOURCES`
352   List of source files associated with the target.  This includes sources
353   specified when the target was created by the :command:`add_executable`,
354   :command:`add_library`, or :command:`add_custom_target` command.
355   It also includes sources added by the :command:`target_sources` command,
356   but does not include :ref:`File Sets`.
358 :prop_tgt:`PRECOMPILE_HEADERS`
359   .. versionadded:: 3.16
361   List of header files to precompile and include when compiling
362   sources in the target.
364 :prop_tgt:`AUTOMOC_MACRO_NAMES`
365   .. versionadded:: 3.10
367   List of macro names used by :prop_tgt:`AUTOMOC` to determine if a
368   C++ source in the target needs to be processed by ``moc``.
370 :prop_tgt:`AUTOUIC_OPTIONS`
371   .. versionadded:: 3.0
373   List of options used by :prop_tgt:`AUTOUIC` when invoking ``uic``
374   for the target.
376 .. _`Target Link Properties`:
378 Target Link Properties
379 ^^^^^^^^^^^^^^^^^^^^^^
381 :prop_tgt:`LINK_LIBRARIES`
382   List of link libraries for linking the target, if it is an executable,
383   shared library, or module library.  Entries for `Normal Libraries`_ are
384   passed to the linker either via paths to their link artifacts, or
385   with ``-l`` flags or equivalent.  Entries for `Object Libraries`_ are
386   passed to the linker via paths to their object files.
388   Additionally, for compiling and linking the target itself,
389   `usage requirements <Target Usage Requirements_>`_ are propagated from
390   ``LINK_LIBRARIES`` entries naming `Normal Libraries`_,
391   `Interface Libraries`_, `Object Libraries`_, and `Imported Targets`_,
392   collected over the transitive closure of their
393   :prop_tgt:`INTERFACE_LINK_LIBRARIES` properties.
395 :prop_tgt:`LINK_DIRECTORIES`
396   .. versionadded:: 3.13
398   List of link directories for linking the target, if it is an executable,
399   shared library, or module library.  The directories are passed to the
400   linker with ``-L`` flags, or equivalent.
402 :prop_tgt:`LINK_OPTIONS`
403   .. versionadded:: 3.13
405   List of link options for linking the target, if it is an executable,
406   shared library, or module library.  The options are passed to the
407   linker as flags, in the order of appearance.
409   Link options are automatically escaped for the shell.
411 :prop_tgt:`LINK_DEPENDS`
412   List of files on which linking the target depends, if it is an executable,
413   shared library, or module library.  For example, linker scripts specified
414   via :prop_tgt:`LINK_OPTIONS` may be listed here such that changing them
415   causes binaries to be linked again.
417 .. _`Target Usage Requirements`:
419 Target Usage Requirements
420 -------------------------
422 The *usage requirements* of a target are settings that propagate to consumers,
423 which link to the target via :command:`target_link_libraries`, in order to
424 correctly compile and link with it.  They are represented by transitive
425 `build <Transitive Build Properties_>`_ and
426 `link <Transitive Link Properties_>`_ properties.
428 Note that usage requirements are not designed as a way to make downstreams
429 use particular :prop_tgt:`COMPILE_OPTIONS`, :prop_tgt:`COMPILE_DEFINITIONS`,
430 etc. for convenience only.  The contents of the properties must be
431 **requirements**, not merely recommendations.
433 See the :ref:`Creating Relocatable Packages` section of the
434 :manual:`cmake-packages(7)` manual for discussion of additional care
435 that must be taken when specifying usage requirements while creating
436 packages for redistribution.
438 The usage requirements of a target can transitively propagate to the dependents.
439 The :command:`target_link_libraries` command has ``PRIVATE``,
440 ``INTERFACE`` and ``PUBLIC`` keywords to control the propagation.
442 .. code-block:: cmake
444   add_library(archive archive.cpp)
445   target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)
447   add_library(serialization serialization.cpp)
448   target_compile_definitions(serialization INTERFACE USING_SERIALIZATION_LIB)
450   add_library(archiveExtras extras.cpp)
451   target_link_libraries(archiveExtras PUBLIC archive)
452   target_link_libraries(archiveExtras PRIVATE serialization)
453   # archiveExtras is compiled with -DUSING_ARCHIVE_LIB
454   # and -DUSING_SERIALIZATION_LIB
456   add_executable(consumer consumer.cpp)
457   # consumer is compiled with -DUSING_ARCHIVE_LIB
458   target_link_libraries(consumer archiveExtras)
460 Because the ``archive`` is a ``PUBLIC`` dependency of ``archiveExtras``, the
461 usage requirements of it are propagated to ``consumer`` too.
463 Because
464 ``serialization`` is a ``PRIVATE`` dependency of ``archiveExtras``, the usage
465 requirements of it are not propagated to ``consumer``.
467 Generally, a dependency should be specified in a use of
468 :command:`target_link_libraries` with the ``PRIVATE`` keyword if it is used by
469 only the implementation of a library, and not in the header files.  If a
470 dependency is additionally used in the header files of a library (e.g. for
471 class inheritance), then it should be specified as a ``PUBLIC`` dependency.
472 A dependency which is not used by the implementation of a library, but only by
473 its headers should be specified as an ``INTERFACE`` dependency.  The
474 :command:`target_link_libraries` command may be invoked with multiple uses of
475 each keyword:
477 .. code-block:: cmake
479   target_link_libraries(archiveExtras
480     PUBLIC archive
481     PRIVATE serialization
482   )
484 Usage requirements are propagated by reading the ``INTERFACE_`` variants
485 of target properties from dependencies and appending the values to the
486 non-``INTERFACE_`` variants of the operand.  For example, the
487 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of dependencies is read and
488 appended to the :prop_tgt:`INCLUDE_DIRECTORIES` of the operand.  In cases
489 where order is relevant and maintained, and the order resulting from the
490 :command:`target_link_libraries` calls does not allow correct compilation,
491 use of an appropriate command to set the property directly may update the
492 order.
494 For example, if the linked libraries for a target must be specified
495 in the order ``lib1`` ``lib2`` ``lib3`` , but the include directories must
496 be specified in the order ``lib3`` ``lib1`` ``lib2``:
498 .. code-block:: cmake
500   target_link_libraries(myExe lib1 lib2 lib3)
501   target_include_directories(myExe
502     PRIVATE $<TARGET_PROPERTY:lib3,INTERFACE_INCLUDE_DIRECTORIES>)
504 Note that care must be taken when specifying usage requirements for targets
505 which will be exported for installation using the :command:`install(EXPORT)`
506 command.  See :ref:`Creating Packages` for more.
508 .. _`Transitive Build Properties`:
510 Transitive Build Properties
511 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
513 :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`
514   List of compile definitions for compiling sources in the target's consumers.
515   Typically these are used by the target's header files.
517 :prop_tgt:`INTERFACE_COMPILE_OPTIONS`
518   List of compile options for compiling sources in the target's consumers.
520 :prop_tgt:`INTERFACE_COMPILE_FEATURES`
521   .. versionadded:: 3.1
523   List of :manual:`compile features <cmake-compile-features(7)>` needed
524   for compiling sources in the target's consumers.  Typically these
525   ensure the target's header files are processed when compiling consumers
526   using a sufficient language standard level.
528 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
529   List of include directories for compiling sources in the target's consumers.
530   Typically these are the locations of the target's header files.
532 :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES`
533   List of directories that, when specified as include directories, e.g., by
534   :prop_tgt:`INCLUDE_DIRECTORIES` or :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
535   should be treated as "system" include directories when compiling sources
536   in the target's consumers.
538 :prop_tgt:`INTERFACE_SOURCES`
539   List of source files to associate with the target's consumers.
541 :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS`
542   .. versionadded:: 3.16
544   List of header files to precompile and include when compiling
545   sources in the target's consumers.
547 :prop_tgt:`INTERFACE_AUTOMOC_MACRO_NAMES`
548   .. versionadded:: 3.27
550   List of macro names used by :prop_tgt:`AUTOMOC` to determine if a
551   C++ source in the target's consumers needs to be processed by ``moc``.
553 :prop_tgt:`INTERFACE_AUTOUIC_OPTIONS`
554   .. versionadded:: 3.0
556   List of options used by :prop_tgt:`AUTOUIC` when invoking ``uic``
557   for the target's consumers.
559 .. _`Transitive Link Properties`:
561 Transitive Link Properties
562 ^^^^^^^^^^^^^^^^^^^^^^^^^^
564 :prop_tgt:`INTERFACE_LINK_LIBRARIES`
565   List of link libraries for linking the target's consumers, for
566   those that are executables, shared libraries, or module libraries.
567   These are the transitive dependencies of the target.
569   Additionally, for compiling and linking the target's consumers,
570   `usage requirements <Target Usage Requirements_>`_ are collected from
571   the transitive closure of ``INTERFACE_LINK_LIBRARIES`` entries naming
572   `Normal Libraries`_, `Interface Libraries`_, `Object Libraries`_,
573   and `Imported Targets`_,
575 :prop_tgt:`INTERFACE_LINK_DIRECTORIES`
576   .. versionadded:: 3.13
578   List of link directories for linking the target's consumers, for
579   those that are executables, shared libraries, or module libraries.
581 :prop_tgt:`INTERFACE_LINK_OPTIONS`
582   .. versionadded:: 3.13
584   List of link options for linking the target's consumers, for
585   those that are executables, shared libraries, or module libraries.
587 :prop_tgt:`INTERFACE_LINK_DEPENDS`
588   .. versionadded:: 3.13
590   List of files on which linking the target's consumers depends, for
591   those that are executables, shared libraries, or module libraries.
593 .. _`Compatible Interface Properties`:
595 Compatible Interface Properties
596 -------------------------------
598 Some target properties are required to be compatible between a target and
599 the interface of each dependency.  For example, the
600 :prop_tgt:`POSITION_INDEPENDENT_CODE` target property may specify a
601 boolean value of whether a target should be compiled as
602 position-independent-code, which has platform-specific consequences.
603 A target may also specify the usage requirement
604 :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE` to communicate that
605 consumers must be compiled as position-independent-code.
607 .. code-block:: cmake
609   add_executable(exe1 exe1.cpp)
610   set_property(TARGET exe1 PROPERTY POSITION_INDEPENDENT_CODE ON)
612   add_library(lib1 SHARED lib1.cpp)
613   set_property(TARGET lib1 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
615   add_executable(exe2 exe2.cpp)
616   target_link_libraries(exe2 lib1)
618 Here, both ``exe1`` and ``exe2`` will be compiled as position-independent-code.
619 ``lib1`` will also be compiled as position-independent-code because that is the
620 default setting for ``SHARED`` libraries.  If dependencies have conflicting,
621 non-compatible requirements :manual:`cmake(1)` issues a diagnostic:
623 .. code-block:: cmake
625   add_library(lib1 SHARED lib1.cpp)
626   set_property(TARGET lib1 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
628   add_library(lib2 SHARED lib2.cpp)
629   set_property(TARGET lib2 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF)
631   add_executable(exe1 exe1.cpp)
632   target_link_libraries(exe1 lib1)
633   set_property(TARGET exe1 PROPERTY POSITION_INDEPENDENT_CODE OFF)
635   add_executable(exe2 exe2.cpp)
636   target_link_libraries(exe2 lib1 lib2)
638 The ``lib1`` requirement ``INTERFACE_POSITION_INDEPENDENT_CODE`` is not
639 "compatible" with the :prop_tgt:`POSITION_INDEPENDENT_CODE` property of
640 the ``exe1`` target.  The library requires that consumers are built as
641 position-independent-code, while the executable specifies to not built as
642 position-independent-code, so a diagnostic is issued.
644 The ``lib1`` and ``lib2`` requirements are not "compatible".  One of them
645 requires that consumers are built as position-independent-code, while
646 the other requires that consumers are not built as position-independent-code.
647 Because ``exe2`` links to both and they are in conflict, a CMake error message
648 is issued::
650   CMake Error: The INTERFACE_POSITION_INDEPENDENT_CODE property of "lib2" does
651   not agree with the value of POSITION_INDEPENDENT_CODE already determined
652   for "exe2".
654 To be "compatible", the :prop_tgt:`POSITION_INDEPENDENT_CODE` property,
655 if set must be either the same, in a boolean sense, as the
656 :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE` property of all transitively
657 specified dependencies on which that property is set.
659 This property of "compatible interface requirement" may be extended to other
660 properties by specifying the property in the content of the
661 :prop_tgt:`COMPATIBLE_INTERFACE_BOOL` target property.  Each specified property
662 must be compatible between the consuming target and the corresponding property
663 with an ``INTERFACE_`` prefix from each dependency:
665 .. code-block:: cmake
667   add_library(lib1Version2 SHARED lib1_v2.cpp)
668   set_property(TARGET lib1Version2 PROPERTY INTERFACE_CUSTOM_PROP ON)
669   set_property(TARGET lib1Version2 APPEND PROPERTY
670     COMPATIBLE_INTERFACE_BOOL CUSTOM_PROP
671   )
673   add_library(lib1Version3 SHARED lib1_v3.cpp)
674   set_property(TARGET lib1Version3 PROPERTY INTERFACE_CUSTOM_PROP OFF)
676   add_executable(exe1 exe1.cpp)
677   target_link_libraries(exe1 lib1Version2) # CUSTOM_PROP will be ON
679   add_executable(exe2 exe2.cpp)
680   target_link_libraries(exe2 lib1Version2 lib1Version3) # Diagnostic
682 Non-boolean properties may also participate in "compatible interface"
683 computations.  Properties specified in the
684 :prop_tgt:`COMPATIBLE_INTERFACE_STRING`
685 property must be either unspecified or compare to the same string among
686 all transitively specified dependencies. This can be useful to ensure
687 that multiple incompatible versions of a library are not linked together
688 through transitive requirements of a target:
690 .. code-block:: cmake
692   add_library(lib1Version2 SHARED lib1_v2.cpp)
693   set_property(TARGET lib1Version2 PROPERTY INTERFACE_LIB_VERSION 2)
694   set_property(TARGET lib1Version2 APPEND PROPERTY
695     COMPATIBLE_INTERFACE_STRING LIB_VERSION
696   )
698   add_library(lib1Version3 SHARED lib1_v3.cpp)
699   set_property(TARGET lib1Version3 PROPERTY INTERFACE_LIB_VERSION 3)
701   add_executable(exe1 exe1.cpp)
702   target_link_libraries(exe1 lib1Version2) # LIB_VERSION will be "2"
704   add_executable(exe2 exe2.cpp)
705   target_link_libraries(exe2 lib1Version2 lib1Version3) # Diagnostic
707 The :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MAX` target property specifies
708 that content will be evaluated numerically and the maximum number among all
709 specified will be calculated:
711 .. code-block:: cmake
713   add_library(lib1Version2 SHARED lib1_v2.cpp)
714   set_property(TARGET lib1Version2 PROPERTY INTERFACE_CONTAINER_SIZE_REQUIRED 200)
715   set_property(TARGET lib1Version2 APPEND PROPERTY
716     COMPATIBLE_INTERFACE_NUMBER_MAX CONTAINER_SIZE_REQUIRED
717   )
719   add_library(lib1Version3 SHARED lib1_v3.cpp)
720   set_property(TARGET lib1Version3 PROPERTY INTERFACE_CONTAINER_SIZE_REQUIRED 1000)
722   add_executable(exe1 exe1.cpp)
723   # CONTAINER_SIZE_REQUIRED will be "200"
724   target_link_libraries(exe1 lib1Version2)
726   add_executable(exe2 exe2.cpp)
727   # CONTAINER_SIZE_REQUIRED will be "1000"
728   target_link_libraries(exe2 lib1Version2 lib1Version3)
730 Similarly, the :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MIN` may be used to
731 calculate the numeric minimum value for a property from dependencies.
733 Each calculated "compatible" property value may be read in the consumer at
734 generate-time using generator expressions.
736 Note that for each dependee, the set of properties specified in each
737 compatible interface property must not intersect with the set specified in
738 any of the other properties.
740 Property Origin Debugging
741 -------------------------
743 Because build specifications can be determined by dependencies, the lack of
744 locality of code which creates a target and code which is responsible for
745 setting build specifications may make the code more difficult to reason about.
746 :manual:`cmake(1)` provides a debugging facility to print the origin of the
747 contents of properties which may be determined by dependencies.  The properties
748 which can be debugged are listed in the
749 :variable:`CMAKE_DEBUG_TARGET_PROPERTIES` variable documentation:
751 .. code-block:: cmake
753   set(CMAKE_DEBUG_TARGET_PROPERTIES
754     INCLUDE_DIRECTORIES
755     COMPILE_DEFINITIONS
756     POSITION_INDEPENDENT_CODE
757     CONTAINER_SIZE_REQUIRED
758     LIB_VERSION
759   )
760   add_executable(exe1 exe1.cpp)
762 In the case of properties listed in :prop_tgt:`COMPATIBLE_INTERFACE_BOOL` or
763 :prop_tgt:`COMPATIBLE_INTERFACE_STRING`, the debug output shows which target
764 was responsible for setting the property, and which other dependencies also
765 defined the property.  In the case of
766 :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MAX` and
767 :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MIN`, the debug output shows the
768 value of the property from each dependency, and whether the value determines
769 the new extreme.
771 Build Specification with Generator Expressions
772 ----------------------------------------------
774 Build specifications may use
775 :manual:`generator expressions <cmake-generator-expressions(7)>` containing
776 content which may be conditional or known only at generate-time.  For example,
777 the calculated "compatible" value of a property may be read with the
778 ``TARGET_PROPERTY`` expression:
780 .. code-block:: cmake
782   add_library(lib1Version2 SHARED lib1_v2.cpp)
783   set_property(TARGET lib1Version2 PROPERTY
784     INTERFACE_CONTAINER_SIZE_REQUIRED 200)
785   set_property(TARGET lib1Version2 APPEND PROPERTY
786     COMPATIBLE_INTERFACE_NUMBER_MAX CONTAINER_SIZE_REQUIRED
787   )
789   add_executable(exe1 exe1.cpp)
790   target_link_libraries(exe1 lib1Version2)
791   target_compile_definitions(exe1 PRIVATE
792       CONTAINER_SIZE=$<TARGET_PROPERTY:CONTAINER_SIZE_REQUIRED>
793   )
795 In this case, the ``exe1`` source files will be compiled with
796 ``-DCONTAINER_SIZE=200``.
798 The unary ``TARGET_PROPERTY`` generator expression and the ``TARGET_POLICY``
799 generator expression are evaluated with the consuming target context.  This
800 means that a usage requirement specification may be evaluated differently based
801 on the consumer:
803 .. code-block:: cmake
805   add_library(lib1 lib1.cpp)
806   target_compile_definitions(lib1 INTERFACE
807     $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:LIB1_WITH_EXE>
808     $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:LIB1_WITH_SHARED_LIB>
809     $<$<TARGET_POLICY:CMP0041>:CONSUMER_CMP0041_NEW>
810   )
812   add_executable(exe1 exe1.cpp)
813   target_link_libraries(exe1 lib1)
815   cmake_policy(SET CMP0041 NEW)
817   add_library(shared_lib shared_lib.cpp)
818   target_link_libraries(shared_lib lib1)
820 The ``exe1`` executable will be compiled with ``-DLIB1_WITH_EXE``, while the
821 ``shared_lib`` shared library will be compiled with ``-DLIB1_WITH_SHARED_LIB``
822 and ``-DCONSUMER_CMP0041_NEW``, because policy :policy:`CMP0041` is
823 ``NEW`` at the point where the ``shared_lib`` target is created.
825 The ``BUILD_INTERFACE`` expression wraps requirements which are only used when
826 consumed from a target in the same buildsystem, or when consumed from a target
827 exported to the build directory using the :command:`export` command.  The
828 ``INSTALL_INTERFACE`` expression wraps requirements which are only used when
829 consumed from a target which has been installed and exported with the
830 :command:`install(EXPORT)` command:
832 .. code-block:: cmake
834   add_library(ClimbingStats climbingstats.cpp)
835   target_compile_definitions(ClimbingStats INTERFACE
836     $<BUILD_INTERFACE:ClimbingStats_FROM_BUILD_LOCATION>
837     $<INSTALL_INTERFACE:ClimbingStats_FROM_INSTALLED_LOCATION>
838   )
839   install(TARGETS ClimbingStats EXPORT libExport ${InstallArgs})
840   install(EXPORT libExport NAMESPACE Upstream::
841           DESTINATION lib/cmake/ClimbingStats)
842   export(EXPORT libExport NAMESPACE Upstream::)
844   add_executable(exe1 exe1.cpp)
845   target_link_libraries(exe1 ClimbingStats)
847 In this case, the ``exe1`` executable will be compiled with
848 ``-DClimbingStats_FROM_BUILD_LOCATION``.  The exporting commands generate
849 :prop_tgt:`IMPORTED` targets with either the ``INSTALL_INTERFACE`` or the
850 ``BUILD_INTERFACE`` omitted, and the ``*_INTERFACE`` marker stripped away.
851 A separate project consuming the ``ClimbingStats`` package would contain:
853 .. code-block:: cmake
855   find_package(ClimbingStats REQUIRED)
857   add_executable(Downstream main.cpp)
858   target_link_libraries(Downstream Upstream::ClimbingStats)
860 Depending on whether the ``ClimbingStats`` package was used from the build
861 location or the install location, the ``Downstream`` target would be compiled
862 with either ``-DClimbingStats_FROM_BUILD_LOCATION`` or
863 ``-DClimbingStats_FROM_INSTALL_LOCATION``.  For more about packages and
864 exporting see the :manual:`cmake-packages(7)` manual.
866 .. _`Include Directories and Usage Requirements`:
868 Include Directories and Usage Requirements
869 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
871 Include directories require some special consideration when specified as usage
872 requirements and when used with generator expressions.  The
873 :command:`target_include_directories` command accepts both relative and
874 absolute include directories:
876 .. code-block:: cmake
878   add_library(lib1 lib1.cpp)
879   target_include_directories(lib1 PRIVATE
880     /absolute/path
881     relative/path
882   )
884 Relative paths are interpreted relative to the source directory where the
885 command appears.  Relative paths are not allowed in the
886 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of :prop_tgt:`IMPORTED` targets.
888 In cases where a non-trivial generator expression is used, the
889 ``INSTALL_PREFIX`` expression may be used within the argument of an
890 ``INSTALL_INTERFACE`` expression.  It is a replacement marker which
891 expands to the installation prefix when imported by a consuming project.
893 Include directories usage requirements commonly differ between the build-tree
894 and the install-tree.  The ``BUILD_INTERFACE`` and ``INSTALL_INTERFACE``
895 generator expressions can be used to describe separate usage requirements
896 based on the usage location.  Relative paths are allowed within the
897 ``INSTALL_INTERFACE`` expression and are interpreted relative to the
898 installation prefix.  For example:
900 .. code-block:: cmake
902   add_library(ClimbingStats climbingstats.cpp)
903   target_include_directories(ClimbingStats INTERFACE
904     $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated>
905     $<INSTALL_INTERFACE:/absolute/path>
906     $<INSTALL_INTERFACE:relative/path>
907     $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/$<CONFIG>/generated>
908   )
910 Two convenience APIs are provided relating to include directories usage
911 requirements.  The :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE` variable
912 may be enabled, with an equivalent effect to:
914 .. code-block:: cmake
916   set_property(TARGET tgt APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
917     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_BINARY_DIR}>
918   )
920 for each target affected.  The convenience for installed targets is
921 an ``INCLUDES DESTINATION`` component with the :command:`install(TARGETS)`
922 command:
924 .. code-block:: cmake
926   install(TARGETS foo bar bat EXPORT tgts ${dest_args}
927     INCLUDES DESTINATION include
928   )
929   install(EXPORT tgts ${other_args})
930   install(FILES ${headers} DESTINATION include)
932 This is equivalent to appending ``${CMAKE_INSTALL_PREFIX}/include`` to the
933 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of each of the installed
934 :prop_tgt:`IMPORTED` targets when generated by :command:`install(EXPORT)`.
936 When the :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of an
937 :ref:`imported target <Imported targets>` is consumed, the entries in the
938 property may be treated as system include directories.  The effects of that
939 are toolchain-dependent, but one common effect is to omit compiler warnings
940 for headers found in those directories.  The :prop_tgt:`SYSTEM` property of
941 the installed target determines this behavior (see the
942 :prop_tgt:`EXPORT_NO_SYSTEM` property for how to modify the installed value
943 for a target).  It is also possible to change how consumers interpret the
944 system behavior of consumed imported targets by setting the
945 :prop_tgt:`NO_SYSTEM_FROM_IMPORTED` target property on the *consumer*.
947 If a binary target is linked transitively to a macOS :prop_tgt:`FRAMEWORK`, the
948 ``Headers`` directory of the framework is also treated as a usage requirement.
949 This has the same effect as passing the framework directory as an include
950 directory.
952 Link Libraries and Generator Expressions
953 ----------------------------------------
955 Like build specifications, :prop_tgt:`link libraries <LINK_LIBRARIES>` may be
956 specified with generator expression conditions.  However, as consumption of
957 usage requirements is based on collection from linked dependencies, there is
958 an additional limitation that the link dependencies must form a "directed
959 acyclic graph".  That is, if linking to a target is dependent on the value of
960 a target property, that target property may not be dependent on the linked
961 dependencies:
963 .. code-block:: cmake
965   add_library(lib1 lib1.cpp)
966   add_library(lib2 lib2.cpp)
967   target_link_libraries(lib1 PUBLIC
968     $<$<TARGET_PROPERTY:POSITION_INDEPENDENT_CODE>:lib2>
969   )
970   add_library(lib3 lib3.cpp)
971   set_property(TARGET lib3 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
973   add_executable(exe1 exe1.cpp)
974   target_link_libraries(exe1 lib1 lib3)
976 As the value of the :prop_tgt:`POSITION_INDEPENDENT_CODE` property of
977 the ``exe1`` target is dependent on the linked libraries (``lib3``), and the
978 edge of linking ``exe1`` is determined by the same
979 :prop_tgt:`POSITION_INDEPENDENT_CODE` property, the dependency graph above
980 contains a cycle.  :manual:`cmake(1)` issues an error message.
982 .. _`Output Artifacts`:
984 Output Artifacts
985 ----------------
987 The buildsystem targets created by the :command:`add_library` and
988 :command:`add_executable` commands create rules to create binary outputs.
989 The exact output location of the binaries can only be determined at
990 generate-time because it can depend on the build-configuration and the
991 link-language of linked dependencies etc.  ``TARGET_FILE``,
992 ``TARGET_LINKER_FILE`` and related expressions can be used to access the
993 name and location of generated binaries.  These expressions do not work
994 for ``OBJECT`` libraries however, as there is no single file generated
995 by such libraries which is relevant to the expressions.
997 There are three kinds of output artifacts that may be build by targets
998 as detailed in the following sections.  Their classification differs
999 between DLL platforms and non-DLL platforms.  All Windows-based
1000 systems including Cygwin are DLL platforms.
1002 .. _`Runtime Output Artifacts`:
1004 Runtime Output Artifacts
1005 ^^^^^^^^^^^^^^^^^^^^^^^^
1007 A *runtime* output artifact of a buildsystem target may be:
1009 * The executable file (e.g. ``.exe``) of an executable target
1010   created by the :command:`add_executable` command.
1012 * On DLL platforms: the executable file (e.g. ``.dll``) of a shared
1013   library target created by the :command:`add_library` command
1014   with the ``SHARED`` option.
1016 The :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` and :prop_tgt:`RUNTIME_OUTPUT_NAME`
1017 target properties may be used to control runtime output artifact locations
1018 and names in the build tree.
1020 .. _`Library Output Artifacts`:
1022 Library Output Artifacts
1023 ^^^^^^^^^^^^^^^^^^^^^^^^
1025 A *library* output artifact of a buildsystem target may be:
1027 * The loadable module file (e.g. ``.dll`` or ``.so``) of a module
1028   library target created by the :command:`add_library` command
1029   with the ``MODULE`` option.
1031 * On non-DLL platforms: the shared library file (e.g. ``.so`` or ``.dylib``)
1032   of a shared library target created by the :command:`add_library`
1033   command with the ``SHARED`` option.
1035 The :prop_tgt:`LIBRARY_OUTPUT_DIRECTORY` and :prop_tgt:`LIBRARY_OUTPUT_NAME`
1036 target properties may be used to control library output artifact locations
1037 and names in the build tree.
1039 .. _`Archive Output Artifacts`:
1041 Archive Output Artifacts
1042 ^^^^^^^^^^^^^^^^^^^^^^^^
1044 An *archive* output artifact of a buildsystem target may be:
1046 * The static library file (e.g. ``.lib`` or ``.a``) of a static
1047   library target created by the :command:`add_library` command
1048   with the ``STATIC`` option.
1050 * On DLL platforms: the import library file (e.g. ``.lib``) of a shared
1051   library target created by the :command:`add_library` command
1052   with the ``SHARED`` option.  This file is only guaranteed to exist if
1053   the library exports at least one unmanaged symbol.
1055 * On DLL platforms: the import library file (e.g. ``.lib``) of an
1056   executable target created by the :command:`add_executable` command
1057   when its :prop_tgt:`ENABLE_EXPORTS` target property is set.
1059 * On AIX: the linker import file (e.g. ``.imp``) of an executable target
1060   created by the :command:`add_executable` command when its
1061   :prop_tgt:`ENABLE_EXPORTS` target property is set.
1063 * On macOS: the linker import file (e.g. ``.tbd``) of a shared library target
1064   created by the :command:`add_library` command with the ``SHARED`` option and
1065   when its :prop_tgt:`ENABLE_EXPORTS` target property is set.
1067 The :prop_tgt:`ARCHIVE_OUTPUT_DIRECTORY` and :prop_tgt:`ARCHIVE_OUTPUT_NAME`
1068 target properties may be used to control archive output artifact locations
1069 and names in the build tree.
1071 Directory-Scoped Commands
1072 -------------------------
1074 The :command:`target_include_directories`,
1075 :command:`target_compile_definitions` and
1076 :command:`target_compile_options` commands have an effect on only one
1077 target at a time.  The commands :command:`add_compile_definitions`,
1078 :command:`add_compile_options` and :command:`include_directories` have
1079 a similar function, but operate at directory scope instead of target
1080 scope for convenience.
1082 .. _`Build Configurations`:
1084 Build Configurations
1085 ====================
1087 Configurations determine specifications for a certain type of build, such
1088 as ``Release`` or ``Debug``.  The way this is specified depends on the type
1089 of :manual:`generator <cmake-generators(7)>` being used.  For single
1090 configuration generators like  :ref:`Makefile Generators` and
1091 :generator:`Ninja`, the configuration is specified at configure time by the
1092 :variable:`CMAKE_BUILD_TYPE` variable. For multi-configuration generators
1093 like :ref:`Visual Studio <Visual Studio Generators>`, :generator:`Xcode`, and
1094 :generator:`Ninja Multi-Config`, the configuration is chosen by the user at
1095 build time and :variable:`CMAKE_BUILD_TYPE` is ignored.  In the
1096 multi-configuration case, the set of *available* configurations is specified
1097 at configure time by the :variable:`CMAKE_CONFIGURATION_TYPES` variable,
1098 but the actual configuration used cannot be known until the build stage.
1099 This difference is often misunderstood, leading to problematic code like the
1100 following:
1102 .. code-block:: cmake
1104   # WARNING: This is wrong for multi-config generators because they don't use
1105   #          and typically don't even set CMAKE_BUILD_TYPE
1106   string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
1107   if (build_type STREQUAL debug)
1108     target_compile_definitions(exe1 PRIVATE DEBUG_BUILD)
1109   endif()
1111 :manual:`Generator expressions <cmake-generator-expressions(7)>` should be
1112 used instead to handle configuration-specific logic correctly, regardless of
1113 the generator used.  For example:
1115 .. code-block:: cmake
1117   # Works correctly for both single and multi-config generators
1118   target_compile_definitions(exe1 PRIVATE
1119     $<$<CONFIG:Debug>:DEBUG_BUILD>
1120   )
1122 In the presence of :prop_tgt:`IMPORTED` targets, the content of
1123 :prop_tgt:`MAP_IMPORTED_CONFIG_DEBUG <MAP_IMPORTED_CONFIG_<CONFIG>>` is also
1124 accounted for by the above :genex:`$<CONFIG:Debug>` expression.
1127 Case Sensitivity
1128 ----------------
1130 :variable:`CMAKE_BUILD_TYPE` and :variable:`CMAKE_CONFIGURATION_TYPES` are
1131 just like other variables in that any string comparisons made with their
1132 values will be case-sensitive.  The :genex:`$<CONFIG>` generator expression also
1133 preserves the casing of the configuration as set by the user or CMake defaults.
1134 For example:
1136 .. code-block:: cmake
1138   # NOTE: Don't use these patterns, they are for illustration purposes only.
1140   set(CMAKE_BUILD_TYPE Debug)
1141   if(CMAKE_BUILD_TYPE STREQUAL DEBUG)
1142     # ... will never get here, "Debug" != "DEBUG"
1143   endif()
1144   add_custom_target(print_config ALL
1145     # Prints "Config is Debug" in this single-config case
1146     COMMAND ${CMAKE_COMMAND} -E echo "Config is $<CONFIG>"
1147     VERBATIM
1148   )
1150   set(CMAKE_CONFIGURATION_TYPES Debug Release)
1151   if(DEBUG IN_LIST CMAKE_CONFIGURATION_TYPES)
1152     # ... will never get here, "Debug" != "DEBUG"
1153   endif()
1155 In contrast, CMake treats the configuration type case-insensitively when
1156 using it internally in places that modify behavior based on the configuration.
1157 For example, the :genex:`$<CONFIG:Debug>` generator expression will evaluate to 1
1158 for a configuration of not only ``Debug``, but also ``DEBUG``, ``debug`` or
1159 even ``DeBuG``.  Therefore, you can specify configuration types in
1160 :variable:`CMAKE_BUILD_TYPE` and :variable:`CMAKE_CONFIGURATION_TYPES` with
1161 any mixture of upper and lowercase, although there are strong conventions
1162 (see the next section).  If you must test the value in string comparisons,
1163 always convert the value to upper or lowercase first and adjust the test
1164 accordingly.
1166 Default And Custom Configurations
1167 ---------------------------------
1169 By default, CMake defines a number of standard configurations:
1171 * ``Debug``
1172 * ``Release``
1173 * ``RelWithDebInfo``
1174 * ``MinSizeRel``
1176 In multi-config generators, the :variable:`CMAKE_CONFIGURATION_TYPES` variable
1177 will be populated with (potentially a subset of) the above list by default,
1178 unless overridden by the project or user.  The actual configuration used is
1179 selected by the user at build time.
1181 For single-config generators, the configuration is specified with the
1182 :variable:`CMAKE_BUILD_TYPE` variable at configure time and cannot be changed
1183 at build time.  The default value will often be none of the above standard
1184 configurations and will instead be an empty string.  A common misunderstanding
1185 is that this is the same as ``Debug``, but that is not the case.  Users should
1186 always explicitly specify the build type instead to avoid this common problem.
1188 The above standard configuration types provide reasonable behavior on most
1189 platforms, but they can be extended to provide other types.  Each configuration
1190 defines a set of compiler and linker flag variables for the language in use.
1191 These variables follow the convention :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>`,
1192 where ``<CONFIG>`` is always the uppercase configuration name.  When defining
1193 a custom configuration type, make sure these variables are set appropriately,
1194 typically as cache variables.
1197 Pseudo Targets
1198 ==============
1200 Some target types do not represent outputs of the buildsystem, but only inputs
1201 such as external dependencies, aliases or other non-build artifacts.  Pseudo
1202 targets are not represented in the generated buildsystem.
1204 .. _`Imported Targets`:
1206 Imported Targets
1207 ----------------
1209 An :prop_tgt:`IMPORTED` target represents a pre-existing dependency.  Usually
1210 such targets are defined by an upstream package and should be treated as
1211 immutable. After declaring an :prop_tgt:`IMPORTED` target one can adjust its
1212 target properties by using the customary commands such as
1213 :command:`target_compile_definitions`, :command:`target_include_directories`,
1214 :command:`target_compile_options` or :command:`target_link_libraries` just like
1215 with any other regular target.
1217 :prop_tgt:`IMPORTED` targets may have the same usage requirement properties
1218 populated as binary targets, such as
1219 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
1220 :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`,
1221 :prop_tgt:`INTERFACE_COMPILE_OPTIONS`,
1222 :prop_tgt:`INTERFACE_LINK_LIBRARIES`, and
1223 :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE`.
1225 The :prop_tgt:`LOCATION` may also be read from an IMPORTED target, though there
1226 is rarely reason to do so.  Commands such as :command:`add_custom_command` can
1227 transparently use an :prop_tgt:`IMPORTED` :prop_tgt:`EXECUTABLE <TYPE>` target
1228 as a ``COMMAND`` executable.
1230 The scope of the definition of an :prop_tgt:`IMPORTED` target is the directory
1231 where it was defined.  It may be accessed and used from subdirectories, but
1232 not from parent directories or sibling directories.  The scope is similar to
1233 the scope of a cmake variable.
1235 It is also possible to define a ``GLOBAL`` :prop_tgt:`IMPORTED` target which is
1236 accessible globally in the buildsystem.
1238 See the :manual:`cmake-packages(7)` manual for more on creating packages
1239 with :prop_tgt:`IMPORTED` targets.
1241 .. _`Alias Targets`:
1243 Alias Targets
1244 -------------
1246 An ``ALIAS`` target is a name which may be used interchangeably with
1247 a binary target name in read-only contexts.  A primary use-case for ``ALIAS``
1248 targets is for example or unit test executables accompanying a library, which
1249 may be part of the same buildsystem or built separately based on user
1250 configuration.
1252 .. code-block:: cmake
1254   add_library(lib1 lib1.cpp)
1255   install(TARGETS lib1 EXPORT lib1Export ${dest_args})
1256   install(EXPORT lib1Export NAMESPACE Upstream:: ${other_args})
1258   add_library(Upstream::lib1 ALIAS lib1)
1260 In another directory, we can link unconditionally to the ``Upstream::lib1``
1261 target, which may be an :prop_tgt:`IMPORTED` target from a package, or an
1262 ``ALIAS`` target if built as part of the same buildsystem.
1264 .. code-block:: cmake
1266   if (NOT TARGET Upstream::lib1)
1267     find_package(lib1 REQUIRED)
1268   endif()
1269   add_executable(exe1 exe1.cpp)
1270   target_link_libraries(exe1 Upstream::lib1)
1272 ``ALIAS`` targets are not mutable, installable or exportable.  They are
1273 entirely local to the buildsystem description.  A name can be tested for
1274 whether it is an ``ALIAS`` name by reading the :prop_tgt:`ALIASED_TARGET`
1275 property from it:
1277 .. code-block:: cmake
1279   get_target_property(_aliased Upstream::lib1 ALIASED_TARGET)
1280   if(_aliased)
1281     message(STATUS "The name Upstream::lib1 is an ALIAS for ${_aliased}.")
1282   endif()
1284 .. _`Interface Libraries`:
1286 Interface Libraries
1287 -------------------
1289 An ``INTERFACE`` library target does not compile sources and does not
1290 produce a library artifact on disk, so it has no :prop_tgt:`LOCATION`.
1292 It may specify usage requirements such as
1293 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
1294 :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`,
1295 :prop_tgt:`INTERFACE_COMPILE_OPTIONS`,
1296 :prop_tgt:`INTERFACE_LINK_LIBRARIES`,
1297 :prop_tgt:`INTERFACE_SOURCES`,
1298 and :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE`.
1299 Only the ``INTERFACE`` modes of the :command:`target_include_directories`,
1300 :command:`target_compile_definitions`, :command:`target_compile_options`,
1301 :command:`target_sources`, and :command:`target_link_libraries` commands
1302 may be used with ``INTERFACE`` libraries.
1304 Since CMake 3.19, an ``INTERFACE`` library target may optionally contain
1305 source files.  An interface library that contains source files will be
1306 included as a build target in the generated buildsystem.  It does not
1307 compile sources, but may contain custom commands to generate other sources.
1308 Additionally, IDEs will show the source files as part of the target for
1309 interactive reading and editing.
1311 A primary use-case for ``INTERFACE`` libraries is header-only libraries.
1312 Since CMake 3.23, header files may be associated with a library by adding
1313 them to a header set using the :command:`target_sources` command:
1315 .. code-block:: cmake
1317   add_library(Eigen INTERFACE)
1319   target_sources(Eigen PUBLIC
1320     FILE_SET HEADERS
1321       BASE_DIRS src
1322       FILES src/eigen.h src/vector.h src/matrix.h
1323   )
1325   add_executable(exe1 exe1.cpp)
1326   target_link_libraries(exe1 Eigen)
1328 When we specify the ``FILE_SET`` here, the ``BASE_DIRS`` we define automatically
1329 become include directories in the usage requirements for the target ``Eigen``.
1330 The usage requirements from the target are consumed and used when compiling, but
1331 have no effect on linking.
1333 Another use-case is to employ an entirely target-focussed design for usage
1334 requirements:
1336 .. code-block:: cmake
1338   add_library(pic_on INTERFACE)
1339   set_property(TARGET pic_on PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
1340   add_library(pic_off INTERFACE)
1341   set_property(TARGET pic_off PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF)
1343   add_library(enable_rtti INTERFACE)
1344   target_compile_options(enable_rtti INTERFACE
1345     $<$<OR:$<COMPILER_ID:GNU>,$<COMPILER_ID:Clang>>:-rtti>
1346   )
1348   add_executable(exe1 exe1.cpp)
1349   target_link_libraries(exe1 pic_on enable_rtti)
1351 This way, the build specification of ``exe1`` is expressed entirely as linked
1352 targets, and the complexity of compiler-specific flags is encapsulated in an
1353 ``INTERFACE`` library target.
1355 ``INTERFACE`` libraries may be installed and exported. We can install the
1356 default header set along with the target:
1358 .. code-block:: cmake
1360   add_library(Eigen INTERFACE)
1362   target_sources(Eigen INTERFACE
1363     FILE_SET HEADERS
1364       BASE_DIRS src
1365       FILES src/eigen.h src/vector.h src/matrix.h
1366   )
1368   install(TARGETS Eigen EXPORT eigenExport
1369     FILE_SET HEADERS DESTINATION include/Eigen)
1370   install(EXPORT eigenExport NAMESPACE Upstream::
1371     DESTINATION lib/cmake/Eigen
1372   )
1374 Here, the headers defined in the header set are installed to ``include/Eigen``.
1375 The install destination automatically becomes an include directory that is a
1376 usage requirement for consumers.