1 .. cmake-manual-description: CMake Generator Expressions
3 cmake-generator-expressions(7)
4 ******************************
13 Generator expressions are evaluated during build system generation to produce
14 information specific to each build configuration. They have the form
15 ``$<...>``. For example:
19 target_include_directories(tgt PRIVATE /opt/include/$<CXX_COMPILER_ID>)
21 This would expand to ``/opt/include/GNU``, ``/opt/include/Clang``, etc.
22 depending on the C++ compiler used.
24 Generator expressions are allowed in the context of many target properties,
25 such as :prop_tgt:`LINK_LIBRARIES`, :prop_tgt:`INCLUDE_DIRECTORIES`,
26 :prop_tgt:`COMPILE_DEFINITIONS` and others. They may also be used when using
27 commands to populate those properties, such as :command:`target_link_libraries`,
28 :command:`target_include_directories`, :command:`target_compile_definitions`
29 and others. They enable conditional linking, conditional definitions used when
30 compiling, conditional include directories, and more. The conditions may be
31 based on the build configuration, target properties, platform information,
32 or any other queryable information.
34 Generator expressions can be nested:
38 target_compile_definitions(tgt PRIVATE
39 $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
42 The above would expand to ``OLD_COMPILER`` if the
43 :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
46 Whitespace And Quoting
47 ======================
49 Generator expressions are typically parsed after command arguments.
50 If a generator expression contains spaces, new lines, semicolons or
51 other characters that may be interpreted as command argument separators,
52 the whole expression should be surrounded by quotes when passed to a
53 command. Failure to do so may result in the expression being split and
54 it may no longer be recognized as a generator expression.
56 When using :command:`add_custom_command` or :command:`add_custom_target`,
57 use the ``VERBATIM`` and ``COMMAND_EXPAND_LISTS`` options to obtain robust
58 argument splitting and quoting.
62 # WRONG: Embedded space will be treated as an argument separator.
63 # This ends up not being seen as a generator expression at all.
64 add_custom_target(run_some_tool
65 COMMAND some_tool -I$<JOIN:$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>, -I>
71 # Better, but still not robust. Quotes prevent the space from splitting the
72 # expression. However, the tool will receive the expanded value as a single
74 add_custom_target(run_some_tool
75 COMMAND some_tool "-I$<JOIN:$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>, -I>"
81 # Nearly correct. Using a semicolon to separate arguments and adding the
82 # COMMAND_EXPAND_LISTS option means that paths with spaces will be handled
83 # correctly. Quoting the whole expression ensures it is seen as a generator
84 # expression. But if the target property is empty, we will get a bare -I
85 # with nothing after it.
86 add_custom_target(run_some_tool
87 COMMAND some_tool "-I$<JOIN:$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>,;-I>"
92 Using variables to build up a more complex generator expression is also a
93 good way to reduce errors and improve readability. The above example can be
94 improved further like so:
98 # The $<BOOL:...> check prevents adding anything if the property is empty,
99 # assuming the property value cannot be one of CMake's false constants.
100 set(prop "$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>")
101 add_custom_target(run_some_tool
102 COMMAND some_tool "$<$<BOOL:${prop}>:-I$<JOIN:${prop},;-I>>"
107 A common mistake is to try to split a generator expression across multiple
108 lines with indenting:
110 .. code-block:: cmake
112 # WRONG: New lines and spaces all treated as argument separators, so the
113 # generator expression is split and not recognized correctly.
114 target_compile_definitions(tgt PRIVATE
116 $<CXX_COMPILER_ID:GNU>,
117 $<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,5>
121 Again, use helper variables with well-chosen names to build up a readable
124 .. code-block:: cmake
126 set(is_gnu "$<CXX_COMPILER_ID:GNU>")
127 set(v5_or_later "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,5>")
128 set(meet_requirements "$<AND:${is_gnu},${v5_or_later}>")
129 target_compile_definitions(tgt PRIVATE
130 "$<${meet_requirements}:HAVE_5_OR_LATER>"
136 Since generator expressions are evaluated during generation of the buildsystem,
137 and not during processing of ``CMakeLists.txt`` files, it is not possible to
138 inspect their result with the :command:`message()` command. One possible way
139 to generate debug messages is to add a custom target:
141 .. code-block:: cmake
143 add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")
145 After running ``cmake``, you can then build the ``genexdebug`` target to print
146 the result of the ``$<...>`` expression (i.e. run the command
147 ``cmake --build ... --target genexdebug``).
149 Another way is to write debug messages to a file with :command:`file(GENERATE)`:
151 .. code-block:: cmake
153 file(GENERATE OUTPUT filename CONTENT "$<...>")
155 Generator Expression Reference
156 ==============================
160 This reference deviates from most of the CMake documentation in that it
161 omits angular brackets ``<...>`` around placeholders like ``condition``,
162 ``string``, ``target``, etc. This is to prevent an opportunity for those
163 placeholders to be misinterpreted as generator expressions.
165 .. _`Conditional Generator Expressions`:
167 Conditional Expressions
168 -----------------------
170 A fundamental category of generator expressions relates to conditional logic.
171 Two forms of conditional generator expressions are supported:
173 .. genex:: $<condition:true_string>
175 Evaluates to ``true_string`` if ``condition`` is ``1``, or an empty string
176 if ``condition`` evaluates to ``0``. Any other value for ``condition``
179 .. genex:: $<IF:condition,true_string,false_string>
181 .. versionadded:: 3.8
183 Evaluates to ``true_string`` if ``condition`` is ``1``, or ``false_string``
184 if ``condition`` is ``0``. Any other value for ``condition`` results in an
187 Typically, the ``condition`` is itself a generator expression. For instance,
188 the following expression expands to ``DEBUG_MODE`` when the ``Debug``
189 configuration is used, and the empty string for all other configurations:
191 .. code-block:: cmake
193 $<$<CONFIG:Debug>:DEBUG_MODE>
195 Boolean-like ``condition`` values other than ``1`` or ``0`` can be handled
196 by wrapping them with the ``$<BOOL:...>`` generator expression:
198 .. genex:: $<BOOL:string>
200 Converts ``string`` to ``0`` or ``1``. Evaluates to ``0`` if any of the
203 * ``string`` is empty,
204 * ``string`` is a case-insensitive equal of
205 ``0``, ``FALSE``, ``OFF``, ``N``, ``NO``, ``IGNORE``, or ``NOTFOUND``, or
206 * ``string`` ends in the suffix ``-NOTFOUND`` (case-sensitive).
208 Otherwise evaluates to ``1``.
210 The ``$<BOOL:...>`` generator expression is often used when a ``condition``
211 is provided by a CMake variable:
213 .. code-block:: cmake
215 $<$<BOOL:${HAVE_SOME_FEATURE}>:-DENABLE_SOME_FEATURE>
218 .. _`Boolean Generator Expressions`:
223 The common boolean logic operators are supported:
225 .. genex:: $<AND:conditions>
227 where ``conditions`` is a comma-separated list of boolean expressions,
228 all of which must evaluate to either ``1`` or ``0``. The whole expression
229 evaluates to ``1`` if all conditions are ``1``. If any condition is ``0``,
230 the whole expression evaluates to ``0``.
232 .. genex:: $<OR:conditions>
234 where ``conditions`` is a comma-separated list of boolean expressions.
235 all of which must evaluate to either ``1`` or ``0``. The whole expression
236 evaluates to ``1`` if at least one of the ``conditions`` is ``1``. If all
237 ``conditions`` evaluate to ``0``, the whole expression evaluates to ``0``.
239 .. genex:: $<NOT:condition>
241 ``condition`` must be ``0`` or ``1``. The result of the expression is
242 ``0`` if ``condition`` is ``1``, else ``1``.
244 .. _`Comparison Expressions`:
246 Primary Comparison Expressions
247 ------------------------------
249 CMake supports a variety of generator expressions that compare things.
250 This section covers the primary and most widely used comparison types.
251 Other more specific comparison types are documented in their own separate
252 sections further below.
257 .. genex:: $<STREQUAL:string1,string2>
259 ``1`` if ``string1`` and ``string2`` are equal, else ``0``.
260 The comparison is case-sensitive. For a case-insensitive comparison,
261 combine with a :ref:`string transforming generator expression
262 <String Transforming Generator Expressions>`. For example, the following
263 evaluates to ``1`` if ``${foo}`` is any of ``BAR``, ``Bar``, ``bar``, etc.
265 .. code-block:: cmake
267 $<STREQUAL:$<UPPER_CASE:${foo}>,BAR>
269 .. genex:: $<EQUAL:value1,value2>
271 ``1`` if ``value1`` and ``value2`` are numerically equal, else ``0``.
276 .. genex:: $<VERSION_LESS:v1,v2>
278 ``1`` if ``v1`` is a version less than ``v2``, else ``0``.
280 .. genex:: $<VERSION_GREATER:v1,v2>
282 ``1`` if ``v1`` is a version greater than ``v2``, else ``0``.
284 .. genex:: $<VERSION_EQUAL:v1,v2>
286 ``1`` if ``v1`` is the same version as ``v2``, else ``0``.
288 .. genex:: $<VERSION_LESS_EQUAL:v1,v2>
290 .. versionadded:: 3.7
292 ``1`` if ``v1`` is a version less than or equal to ``v2``, else ``0``.
294 .. genex:: $<VERSION_GREATER_EQUAL:v1,v2>
296 .. versionadded:: 3.7
298 ``1`` if ``v1`` is a version greater than or equal to ``v2``, else ``0``.
300 .. _`String Transforming Generator Expressions`:
302 String Transformations
303 ----------------------
305 .. genex:: $<LOWER_CASE:string>
307 Content of ``string`` converted to lower case.
309 .. genex:: $<UPPER_CASE:string>
311 Content of ``string`` converted to upper case.
313 .. genex:: $<MAKE_C_IDENTIFIER:...>
315 Content of ``...`` converted to a C identifier. The conversion follows the
316 same behavior as :command:`string(MAKE_C_IDENTIFIER)`.
321 .. genex:: $<IN_LIST:string,list>
323 .. versionadded:: 3.12
325 ``1`` if ``string`` is an item in the semicolon-separated ``list``, else ``0``.
326 It uses case-sensitive comparisons.
328 .. genex:: $<JOIN:list,string>
330 Joins the list with the content of ``string`` inserted between each item.
332 .. genex:: $<REMOVE_DUPLICATES:list>
334 .. versionadded:: 3.15
336 Removes duplicated items in the given ``list``. The relative order of items
337 is preserved, but if duplicates are encountered, only the first instance is
340 .. genex:: $<FILTER:list,INCLUDE|EXCLUDE,regex>
342 .. versionadded:: 3.15
344 Includes or removes items from ``list`` that match the regular expression
350 Most of the expressions in this section are closely associated with the
351 :command:`cmake_path` command, providing the same capabilities, but in
352 the form of a generator expression.
354 For all generator expressions in this section, paths are expected to be in
355 cmake-style format. The :ref:`$\<PATH:CMAKE_PATH\> <GenEx PATH-CMAKE_PATH>`
356 generator expression can be used to convert a native path to a cmake-style
359 .. _GenEx Path Comparisons:
364 .. genex:: $<PATH_EQUAL:path1,path2>
366 .. versionadded:: 3.24
368 Compares the lexical representations of two paths. No normalization is
369 performed on either path. Returns ``1`` if the paths are equal, ``0``
372 See :ref:`cmake_path(COMPARE) <Path COMPARE>` for more details.
374 .. _GenEx Path Queries:
379 These expressions provide the generation-time capabilities equivalent to the
380 :ref:`Query <Path Query>` options of the :command:`cmake_path` command.
381 All paths are expected to be in cmake-style format.
383 .. genex:: $<PATH:HAS_*,path>
385 .. versionadded:: 3.24
387 The following operations return ``1`` if the particular path component is
388 present, ``0`` otherwise. See :ref:`Path Structure And Terminology` for the
389 meaning of each path component.
393 $<PATH:HAS_ROOT_NAME,path>
394 $<PATH:HAS_ROOT_DIRECTORY,path>
395 $<PATH:HAS_ROOT_PATH,path>
396 $<PATH:HAS_FILENAME,path>
397 $<PATH:HAS_EXTENSION,path>
398 $<PATH:HAS_STEM,path>
399 $<PATH:HAS_RELATIVE_PART,path>
400 $<PATH:HAS_PARENT_PATH,path>
402 Note the following special cases:
404 * For ``HAS_ROOT_PATH``, a true result will only be returned if at least one
405 of ``root-name`` or ``root-directory`` is non-empty.
407 * For ``HAS_PARENT_PATH``, the root directory is also considered to have a
408 parent, which will be itself. The result is true except if the path
409 consists of just a :ref:`filename <FILENAME_DEF>`.
411 .. genex:: $<PATH:IS_ABSOLUTE,path>
413 .. versionadded:: 3.24
415 Returns ``1`` if the path is :ref:`absolute <IS_ABSOLUTE>`, ``0`` otherwise.
417 .. genex:: $<PATH:IS_RELATIVE,path>
419 .. versionadded:: 3.24
421 This will return the opposite of ``IS_ABSOLUTE``.
423 .. genex:: $<PATH:IS_PREFIX[,NORMALIZE],path,input>
425 .. versionadded:: 3.24
427 Returns ``1`` if ``path`` is the prefix of ``input``, ``0`` otherwise.
429 When the ``NORMALIZE`` option is specified, ``path`` and ``input`` are
430 :ref:`normalized <Normalization>` before the check.
432 .. _GenEx Path Decomposition:
437 These expressions provide the generation-time capabilities equivalent to the
438 :ref:`Decomposition <Path Decomposition>` options of the :command:`cmake_path`
439 command. All paths are expected to be in cmake-style format.
441 .. genex:: $<PATH:GET_*,...>
443 .. versionadded:: 3.24
445 The following operations retrieve a different component or group of
446 components from a path. See :ref:`Path Structure And Terminology` for the
447 meaning of each path component.
451 $<PATH:GET_ROOT_NAME,path>
452 $<PATH:GET_ROOT_DIRECTORY,path>
453 $<PATH:GET_ROOT_PATH,path>
454 $<PATH:GET_FILENAME,path>
455 $<PATH:GET_EXTENSION[,LAST_ONLY],path>
456 $<PATH:GET_STEM[,LAST_ONLY],path>
457 $<PATH:GET_RELATIVE_PART,path>
458 $<PATH:GET_PARENT_PATH,path>
460 If a requested component is not present in the path, an empty string is
463 .. _GenEx Path Transformations:
468 These expressions provide the generation-time capabilities equivalent to the
469 :ref:`Modification <Path Modification>` and :ref:`Generation <Path Generation>`
470 options of the :command:`cmake_path` command. All paths are expected to be
471 in cmake-style format.
473 .. _GenEx PATH-CMAKE_PATH:
475 .. genex:: $<PATH:CMAKE_PATH[,NORMALIZE],path>
477 .. versionadded:: 3.24
479 Returns ``path``. If ``path`` is a native path, it is converted into a
480 cmake-style path with forward-slashes (``/``). On Windows, the long filename
481 marker is taken into account.
483 When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
484 <Normalization>` after the conversion.
486 .. genex:: $<PATH:APPEND,path,input,...>
488 .. versionadded:: 3.24
490 Returns all the ``input`` arguments appended to ``path`` using ``/`` as the
491 ``directory-separator``. Depending on the ``input``, the value of ``path``
494 See :ref:`cmake_path(APPEND) <APPEND>` for more details.
496 .. genex:: $<PATH:REMOVE_FILENAME,path>
498 .. versionadded:: 3.24
500 Returns ``path`` with filename component (as returned by
501 ``$<PATH:GET_FILENAME>``) removed. After removal, any trailing
502 ``directory-separator`` is left alone, if present.
504 See :ref:`cmake_path(REMOVE_FILENAME) <REMOVE_FILENAME>` for more details.
506 .. genex:: $<PATH:REPLACE_FILENAME,path,input>
508 .. versionadded:: 3.24
510 Returns ``path`` with the filename component replaced by ``input``. If
511 ``path`` has no filename component (i.e. ``$<PATH:HAS_FILENAME>`` returns
512 ``0``), ``path`` is unchanged.
514 See :ref:`cmake_path(REPLACE_FILENAME) <REPLACE_FILENAME>` for more details.
516 .. genex:: $<PATH:REMOVE_EXTENSION[,LAST_ONLY],path>
518 .. versionadded:: 3.24
520 Returns ``path`` with the :ref:`extension <EXTENSION_DEF>` removed, if any.
522 See :ref:`cmake_path(REMOVE_EXTENSION) <REMOVE_EXTENSION>` for more details.
524 .. genex:: $<PATH:REPLACE_EXTENSION[,LAST_ONLY],path,input>
526 .. versionadded:: 3.24
528 Returns ``path`` with the :ref:`extension <EXTENSION_DEF>` replaced by
531 See :ref:`cmake_path(REPLACE_EXTENSION) <REPLACE_EXTENSION>` for more details.
533 .. genex:: $<PATH:NORMAL_PATH,path>
535 .. versionadded:: 3.24
537 Returns ``path`` normalized according to the steps described in
538 :ref:`Normalization`.
540 .. genex:: $<PATH:RELATIVE_PATH,path,base_directory>
542 .. versionadded:: 3.24
544 Returns ``path``, modified to make it relative to the ``base_directory``
547 See :ref:`cmake_path(RELATIVE_PATH) <cmake_path-RELATIVE_PATH>` for more
550 .. genex:: $<PATH:ABSOLUTE_PATH[,NORMALIZE],path,base_directory>
552 .. versionadded:: 3.24
554 Returns ``path`` as absolute. If ``path`` is a relative path
555 (``$<PATH:IS_RELATIVE>`` returns ``1``), it is evaluated relative to the
556 given base directory specified by ``base_directory`` argument.
558 When the ``NORMALIZE`` option is specified, the path is
559 :ref:`normalized <Normalization>` after the path computation.
561 See :ref:`cmake_path(ABSOLUTE_PATH) <ABSOLUTE_PATH>` for more details.
566 .. genex:: $<SHELL_PATH:...>
568 .. versionadded:: 3.4
570 Content of ``...`` converted to shell path style. For example, slashes are
571 converted to backslashes in Windows shells and drive letters are converted
572 to posix paths in MSYS shells. The ``...`` must be an absolute path.
574 .. versionadded:: 3.14
575 The ``...`` may be a :ref:`semicolon-separated list <CMake Language Lists>`
576 of paths, in which case each path is converted individually and a result
577 list is generated using the shell path separator (``:`` on POSIX and
578 ``;`` on Windows). Be sure to enclose the argument containing this genex
579 in double quotes in CMake source code so that ``;`` does not split arguments.
581 Configuration Expressions
582 -------------------------
586 Configuration name. Use this instead of the deprecated :genex:`CONFIGURATION`
587 generator expression.
589 .. genex:: $<CONFIG:cfgs>
591 ``1`` if config is any one of the entries in comma-separated list
592 ``cfgs``, else ``0``. This is a case-insensitive comparison. The mapping in
593 :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` is also considered by this
594 expression when it is evaluated on a property of an :prop_tgt:`IMPORTED`
597 .. versionchanged:: 3.19
598 Multiple configurations can be specified for ``cfgs``.
599 CMake 3.18 and earlier only accepted a single configuration.
601 .. genex:: $<OUTPUT_CONFIG:...>
603 .. versionadded:: 3.20
605 Only valid in :command:`add_custom_command` and :command:`add_custom_target`
606 as the outer-most generator expression in an argument.
607 With the :generator:`Ninja Multi-Config` generator, generator expressions
608 in ``...`` are evaluated using the custom command's "output config".
609 With other generators, the content of ``...`` is evaluated normally.
611 .. genex:: $<COMMAND_CONFIG:...>
613 .. versionadded:: 3.20
615 Only valid in :command:`add_custom_command` and :command:`add_custom_target`
616 as the outer-most generator expression in an argument.
617 With the :generator:`Ninja Multi-Config` generator, generator expressions
618 in ``...`` are evaluated using the custom command's "command config".
619 With other generators, the content of ``...`` is evaluated normally.
621 Toolchain And Language Expressions
622 ----------------------------------
627 .. genex:: $<PLATFORM_ID>
629 The current system's CMake platform id.
630 See also the :variable:`CMAKE_SYSTEM_NAME` variable.
632 .. genex:: $<PLATFORM_ID:platform_ids>
634 ``1`` if CMake's platform id matches any one of the entries in
635 comma-separated list ``platform_ids``, otherwise ``0``.
636 See also the :variable:`CMAKE_SYSTEM_NAME` variable.
641 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable, which is
642 closely related to the expressions in this sub-section.
644 .. genex:: $<C_COMPILER_VERSION>
646 The version of the C compiler used.
648 .. genex:: $<C_COMPILER_VERSION:version>
650 ``1`` if the version of the C compiler matches ``version``, otherwise ``0``.
652 .. genex:: $<CXX_COMPILER_VERSION>
654 The version of the CXX compiler used.
656 .. genex:: $<CXX_COMPILER_VERSION:version>
658 ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``.
660 .. genex:: $<CUDA_COMPILER_VERSION>
662 .. versionadded:: 3.15
664 The version of the CUDA compiler used.
666 .. genex:: $<CUDA_COMPILER_VERSION:version>
668 .. versionadded:: 3.15
670 ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``.
672 .. genex:: $<OBJC_COMPILER_VERSION>
674 .. versionadded:: 3.16
676 The version of the OBJC compiler used.
678 .. genex:: $<OBJC_COMPILER_VERSION:version>
680 .. versionadded:: 3.16
682 ``1`` if the version of the OBJC compiler matches ``version``, otherwise ``0``.
684 .. genex:: $<OBJCXX_COMPILER_VERSION>
686 .. versionadded:: 3.16
688 The version of the OBJCXX compiler used.
690 .. genex:: $<OBJCXX_COMPILER_VERSION:version>
692 .. versionadded:: 3.16
694 ``1`` if the version of the OBJCXX compiler matches ``version``, otherwise ``0``.
696 .. genex:: $<Fortran_COMPILER_VERSION>
698 The version of the Fortran compiler used.
700 .. genex:: $<Fortran_COMPILER_VERSION:version>
702 ``1`` if the version of the Fortran compiler matches ``version``, otherwise ``0``.
704 .. genex:: $<HIP_COMPILER_VERSION>
706 .. versionadded:: 3.21
708 The version of the HIP compiler used.
710 .. genex:: $<HIP_COMPILER_VERSION:version>
712 .. versionadded:: 3.21
714 ``1`` if the version of the HIP compiler matches ``version``, otherwise ``0``.
716 .. genex:: $<ISPC_COMPILER_VERSION>
718 .. versionadded:: 3.19
720 The version of the ISPC compiler used.
722 .. genex:: $<ISPC_COMPILER_VERSION:version>
724 .. versionadded:: 3.19
726 ``1`` if the version of the ISPC compiler matches ``version``, otherwise ``0``.
728 Compiler Language And ID
729 ^^^^^^^^^^^^^^^^^^^^^^^^
731 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable, which is closely
732 related to most of the expressions in this sub-section.
734 .. genex:: $<C_COMPILER_ID>
736 CMake's compiler id of the C compiler used.
738 .. genex:: $<C_COMPILER_ID:compiler_ids>
740 where ``compiler_ids`` is a comma-separated list.
741 ``1`` if CMake's compiler id of the C compiler matches any one
742 of the entries in ``compiler_ids``, otherwise ``0``.
744 .. genex:: $<CXX_COMPILER_ID>
746 CMake's compiler id of the CXX compiler used.
748 .. genex:: $<CXX_COMPILER_ID:compiler_ids>
750 where ``compiler_ids`` is a comma-separated list.
751 ``1`` if CMake's compiler id of the CXX compiler matches any one
752 of the entries in ``compiler_ids``, otherwise ``0``.
754 .. genex:: $<CUDA_COMPILER_ID>
756 .. versionadded:: 3.15
758 CMake's compiler id of the CUDA compiler used.
760 .. genex:: $<CUDA_COMPILER_ID:compiler_ids>
762 .. versionadded:: 3.15
764 where ``compiler_ids`` is a comma-separated list.
765 ``1`` if CMake's compiler id of the CUDA compiler matches any one
766 of the entries in ``compiler_ids``, otherwise ``0``.
768 .. genex:: $<OBJC_COMPILER_ID>
770 .. versionadded:: 3.16
772 CMake's compiler id of the OBJC compiler used.
774 .. genex:: $<OBJC_COMPILER_ID:compiler_ids>
776 .. versionadded:: 3.16
778 where ``compiler_ids`` is a comma-separated list.
779 ``1`` if CMake's compiler id of the Objective-C compiler matches any one
780 of the entries in ``compiler_ids``, otherwise ``0``.
782 .. genex:: $<OBJCXX_COMPILER_ID>
784 .. versionadded:: 3.16
786 CMake's compiler id of the OBJCXX compiler used.
788 .. genex:: $<OBJCXX_COMPILER_ID:compiler_ids>
790 .. versionadded:: 3.16
792 where ``compiler_ids`` is a comma-separated list.
793 ``1`` if CMake's compiler id of the Objective-C++ compiler matches any one
794 of the entries in ``compiler_ids``, otherwise ``0``.
796 .. genex:: $<Fortran_COMPILER_ID>
798 CMake's compiler id of the Fortran compiler used.
800 .. genex:: $<Fortran_COMPILER_ID:compiler_ids>
802 where ``compiler_ids`` is a comma-separated list.
803 ``1`` if CMake's compiler id of the Fortran compiler matches any one
804 of the entries in ``compiler_ids``, otherwise ``0``.
806 .. genex:: $<HIP_COMPILER_ID>
808 .. versionadded:: 3.21
810 CMake's compiler id of the HIP compiler used.
812 .. genex:: $<HIP_COMPILER_ID:compiler_ids>
814 .. versionadded:: 3.21
816 where ``compiler_ids`` is a comma-separated list.
817 ``1`` if CMake's compiler id of the HIP compiler matches any one
818 of the entries in ``compiler_ids``, otherwise ``0``.
820 .. genex:: $<ISPC_COMPILER_ID>
822 .. versionadded:: 3.19
824 CMake's compiler id of the ISPC compiler used.
826 .. genex:: $<ISPC_COMPILER_ID:compiler_ids>
828 .. versionadded:: 3.19
830 where ``compiler_ids`` is a comma-separated list.
831 ``1`` if CMake's compiler id of the ISPC compiler matches any one
832 of the entries in ``compiler_ids``, otherwise ``0``.
834 .. genex:: $<COMPILE_LANGUAGE>
836 .. versionadded:: 3.3
838 The compile language of source files when evaluating compile options.
839 See :ref:`the related boolean expression
840 <Boolean COMPILE_LANGUAGE Generator Expression>`
841 ``$<COMPILE_LANGUAGE:language>``
842 for notes about the portability of this generator expression.
844 .. _`Boolean COMPILE_LANGUAGE Generator Expression`:
846 .. genex:: $<COMPILE_LANGUAGE:languages>
848 .. versionadded:: 3.3
850 .. versionchanged:: 3.15
851 Multiple languages can be specified for ``languages``.
852 CMake 3.14 and earlier only accepted a single language.
854 ``1`` when the language used for compilation unit matches any of the
855 comma-separated entries in ``languages``, otherwise ``0``. This expression
856 may be used to specify compile options, compile definitions, and include
857 directories for source files of a particular language in a target. For
860 .. code-block:: cmake
862 add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
863 target_compile_options(myapp
864 PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
866 target_compile_definitions(myapp
867 PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
868 $<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
870 target_include_directories(myapp
871 PRIVATE $<$<COMPILE_LANGUAGE:CXX,CUDA>:/opt/foo/headers>
874 This specifies the use of the ``-fno-exceptions`` compile option,
875 ``COMPILING_CXX`` compile definition, and ``cxx_headers`` include
876 directory for C++ only (compiler id checks elided). It also specifies
877 a ``COMPILING_CUDA`` compile definition for CUDA.
879 Note that with :ref:`Visual Studio Generators` and :generator:`Xcode` there
880 is no way to represent target-wide compile definitions or include directories
881 separately for ``C`` and ``CXX`` languages.
882 Also, with :ref:`Visual Studio Generators` there is no way to represent
883 target-wide flags separately for ``C`` and ``CXX`` languages. Under these
884 generators, expressions for both C and C++ sources will be evaluated
885 using ``CXX`` if there are any C++ sources and otherwise using ``C``.
886 A workaround is to create separate libraries for each source file language
889 .. code-block:: cmake
891 add_library(myapp_c foo.c)
892 add_library(myapp_cxx bar.cpp)
893 target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
894 add_executable(myapp main.cpp)
895 target_link_libraries(myapp myapp_c myapp_cxx)
897 .. genex:: $<COMPILE_LANG_AND_ID:language,compiler_ids>
899 .. versionadded:: 3.15
901 ``1`` when the language used for compilation unit matches ``language`` and
902 CMake's compiler id of the ``language`` compiler matches any one of the
903 comma-separated entries in ``compiler_ids``, otherwise ``0``. This expression
904 is a short form for the combination of ``$<COMPILE_LANGUAGE:language>`` and
905 ``$<LANG_COMPILER_ID:compiler_ids>``. This expression may be used to specify
906 compile options, compile definitions, and include directories for source
907 files of a particular language and compiler combination in a target.
910 .. code-block:: cmake
912 add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
913 target_compile_definitions(myapp
914 PRIVATE $<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang>:COMPILING_CXX_WITH_CLANG>
915 $<$<COMPILE_LANG_AND_ID:CXX,Intel>:COMPILING_CXX_WITH_INTEL>
916 $<$<COMPILE_LANG_AND_ID:C,Clang>:COMPILING_C_WITH_CLANG>
919 This specifies the use of different compile definitions based on both
920 the compiler id and compilation language. This example will have a
921 ``COMPILING_CXX_WITH_CLANG`` compile definition when Clang is the CXX
922 compiler, and ``COMPILING_CXX_WITH_INTEL`` when Intel is the CXX compiler.
923 Likewise, when the C compiler is Clang, it will only see the
924 ``COMPILING_C_WITH_CLANG`` definition.
926 Without the ``COMPILE_LANG_AND_ID`` generator expression, the same logic
927 would be expressed as:
929 .. code-block:: cmake
931 target_compile_definitions(myapp
932 PRIVATE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:AppleClang,Clang>>:COMPILING_CXX_WITH_CLANG>
933 $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:Intel>>:COMPILING_CXX_WITH_INTEL>
934 $<$<AND:$<COMPILE_LANGUAGE:C>,$<C_COMPILER_ID:Clang>>:COMPILING_C_WITH_CLANG>
940 .. genex:: $<COMPILE_FEATURES:features>
942 .. versionadded:: 3.1
944 where ``features`` is a comma-separated list.
945 Evaluates to ``1`` if all of the ``features`` are available for the 'head'
946 target, and ``0`` otherwise. If this expression is used while evaluating
947 the link implementation of a target and if any dependency transitively
948 increases the required :prop_tgt:`C_STANDARD` or :prop_tgt:`CXX_STANDARD`
949 for the 'head' target, an error is reported. See the
950 :manual:`cmake-compile-features(7)` manual for information on
951 compile features and a list of supported compilers.
953 Linker Language And ID
954 ^^^^^^^^^^^^^^^^^^^^^^
956 .. genex:: $<LINK_LANGUAGE>
958 .. versionadded:: 3.18
960 The link language of the target when evaluating link options.
961 See :ref:`the related boolean expression
962 <Boolean LINK_LANGUAGE Generator Expression>` ``$<LINK_LANGUAGE:languages>``
963 for notes about the portability of this generator expression.
967 This generator expression is not supported by the link libraries
968 properties to avoid side-effects due to the double evaluation of
972 .. _`Boolean LINK_LANGUAGE Generator Expression`:
974 .. genex:: $<LINK_LANGUAGE:languages>
976 .. versionadded:: 3.18
978 ``1`` when the language used for link step matches any of the comma-separated
979 entries in ``languages``, otherwise ``0``. This expression may be used to
980 specify link libraries, link options, link directories and link dependencies
981 of a particular language in a target. For example:
983 .. code-block:: cmake
985 add_library(api_C ...)
986 add_library(api_CXX ...)
987 add_library(api INTERFACE)
988 target_link_options(api INTERFACE $<$<LINK_LANGUAGE:C>:-opt_c>
989 $<$<LINK_LANGUAGE:CXX>:-opt_cxx>)
990 target_link_libraries(api INTERFACE $<$<LINK_LANGUAGE:C>:api_C>
991 $<$<LINK_LANGUAGE:CXX>:api_CXX>)
993 add_executable(myapp1 main.c)
994 target_link_options(myapp1 PRIVATE api)
996 add_executable(myapp2 main.cpp)
997 target_link_options(myapp2 PRIVATE api)
999 This specifies to use the ``api`` target for linking targets ``myapp1`` and
1000 ``myapp2``. In practice, ``myapp1`` will link with target ``api_C`` and
1001 option ``-opt_c`` because it will use ``C`` as link language. And ``myapp2``
1002 will link with ``api_CXX`` and option ``-opt_cxx`` because ``CXX`` will be
1005 .. _`Constraints LINK_LANGUAGE Generator Expression`:
1009 To determine the link language of a target, it is required to collect,
1010 transitively, all the targets which will be linked to it. So, for link
1011 libraries properties, a double evaluation will be done. During the first
1012 evaluation, ``$<LINK_LANGUAGE:..>`` expressions will always return ``0``.
1013 The link language computed after this first pass will be used to do the
1014 second pass. To avoid inconsistency, it is required that the second pass
1015 do not change the link language. Moreover, to avoid unexpected
1016 side-effects, it is required to specify complete entities as part of the
1017 ``$<LINK_LANGUAGE:..>`` expression. For example:
1019 .. code-block:: cmake
1021 add_library(lib STATIC file.cxx)
1022 add_library(libother STATIC file.c)
1025 add_executable(myapp1 main.c)
1026 target_link_libraries(myapp1 PRIVATE lib$<$<LINK_LANGUAGE:C>:other>)
1029 add_executable(myapp2 main.c)
1030 target_link_libraries(myapp2 PRIVATE $<$<LINK_LANGUAGE:C>:libother>)
1032 In this example, for ``myapp1``, the first pass will, unexpectedly,
1033 determine that the link language is ``CXX`` because the evaluation of the
1034 generator expression will be an empty string so ``myapp1`` will depends on
1035 target ``lib`` which is ``C++``. On the contrary, for ``myapp2``, the first
1036 evaluation will give ``C`` as link language, so the second pass will
1037 correctly add target ``libother`` as link dependency.
1039 .. genex:: $<LINK_LANG_AND_ID:language,compiler_ids>
1041 .. versionadded:: 3.18
1043 ``1`` when the language used for link step matches ``language`` and the
1044 CMake's compiler id of the language linker matches any one of the comma-separated
1045 entries in ``compiler_ids``, otherwise ``0``. This expression is a short form
1046 for the combination of ``$<LINK_LANGUAGE:language>`` and
1047 ``$<LANG_COMPILER_ID:compiler_ids>``. This expression may be used to specify
1048 link libraries, link options, link directories and link dependencies of a
1049 particular language and linker combination in a target. For example:
1051 .. code-block:: cmake
1053 add_library(libC_Clang ...)
1054 add_library(libCXX_Clang ...)
1055 add_library(libC_Intel ...)
1056 add_library(libCXX_Intel ...)
1058 add_executable(myapp main.c)
1060 target_sources(myapp PRIVATE file.cxx)
1062 target_link_libraries(myapp
1063 PRIVATE $<$<LINK_LANG_AND_ID:CXX,Clang,AppleClang>:libCXX_Clang>
1064 $<$<LINK_LANG_AND_ID:C,Clang,AppleClang>:libC_Clang>
1065 $<$<LINK_LANG_AND_ID:CXX,Intel>:libCXX_Intel>
1066 $<$<LINK_LANG_AND_ID:C,Intel>:libC_Intel>)
1068 This specifies the use of different link libraries based on both the
1069 compiler id and link language. This example will have target ``libCXX_Clang``
1070 as link dependency when ``Clang`` or ``AppleClang`` is the ``CXX``
1071 linker, and ``libCXX_Intel`` when ``Intel`` is the ``CXX`` linker.
1072 Likewise when the ``C`` linker is ``Clang`` or ``AppleClang``, target
1073 ``libC_Clang`` will be added as link dependency and ``libC_Intel`` when
1074 ``Intel`` is the ``C`` linker.
1076 See :ref:`the note related to
1077 <Constraints LINK_LANGUAGE Generator Expression>`
1078 ``$<LINK_LANGUAGE:language>`` for constraints about the usage of this
1079 generator expression.
1084 .. genex:: $<LINK_LIBRARY:feature,library-list>
1086 .. versionadded:: 3.24
1088 Specify a set of libraries to link to a target, along with a ``feature``
1089 which provides details about *how* they should be linked. For example:
1091 .. code-block:: cmake
1093 add_library(lib1 STATIC ...)
1094 add_library(lib2 ...)
1095 target_link_libraries(lib2 PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,lib1>")
1097 This specifies that ``lib2`` should link to ``lib1`` and use the
1098 ``WHOLE_ARCHIVE`` feature when doing so.
1100 Feature names are case-sensitive and may only contain letters, numbers and
1101 underscores. Feature names defined in all uppercase are reserved for CMake's
1102 own built-in features. The pre-defined built-in library features are:
1104 .. include:: ../variable/LINK_LIBRARY_PREDEFINED_FEATURES.txt
1106 Built-in and custom library features are defined in terms of the following
1109 * :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED`
1110 * :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>`
1111 * :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED`
1112 * :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>`
1114 The value used for each of these variables is the value as set at the end of
1115 the directory scope in which the target was created. The usage is as follows:
1117 1. If the language-specific
1118 :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED` variable
1119 is true, the ``feature`` must be defined by the corresponding
1120 :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>` variable.
1121 2. If no language-specific ``feature`` is supported, then the
1122 :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED` variable must be
1123 true and the ``feature`` must be defined by the corresponding
1124 :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>` variable.
1126 The following limitations should be noted:
1128 * The ``library-list`` can specify CMake targets or libraries.
1129 Any CMake target of type :ref:`OBJECT <Object Libraries>`
1130 or :ref:`INTERFACE <Interface Libraries>` will ignore the feature aspect
1131 of the expression and instead be linked in the standard way.
1133 * The ``$<LINK_LIBRARY:...>`` generator expression can only be used to
1134 specify link libraries. In practice, this means it can appear in the
1135 :prop_tgt:`LINK_LIBRARIES`, :prop_tgt:`INTERFACE_LINK_LIBRARIES`, and
1136 :prop_tgt:`INTERFACE_LINK_LIBRARIES_DIRECT` target properties, and be
1137 specified in :command:`target_link_libraries` and :command:`link_libraries`
1140 * If a ``$<LINK_LIBRARY:...>`` generator expression appears in the
1141 :prop_tgt:`INTERFACE_LINK_LIBRARIES` property of a target, it will be
1142 included in the imported target generated by a :command:`install(EXPORT)`
1143 command. It is the responsibility of the environment consuming this
1144 import to define the link feature used by this expression.
1146 * Each target or library involved in the link step must have at most only
1147 one kind of library feature. The absence of a feature is also incompatible
1148 with all other features. For example:
1150 .. code-block:: cmake
1152 add_library(lib1 ...)
1153 add_library(lib2 ...)
1154 add_library(lib3 ...)
1156 # lib1 will be associated with feature1
1157 target_link_libraries(lib2 PUBLIC "$<LINK_LIBRARY:feature1,lib1>")
1159 # lib1 is being linked with no feature here. This conflicts with the
1160 # use of feature1 in the line above and would result in an error.
1161 target_link_libraries(lib3 PRIVATE lib1 lib2)
1163 Where it isn't possible to use the same feature throughout a build for a
1164 given target or library, the :prop_tgt:`LINK_LIBRARY_OVERRIDE` and
1165 :prop_tgt:`LINK_LIBRARY_OVERRIDE_<LIBRARY>` target properties can be
1166 used to resolve such incompatibilities.
1168 * The ``$<LINK_LIBRARY:...>`` generator expression does not guarantee
1169 that the list of specified targets and libraries will be kept grouped
1170 together. To manage constructs like ``--start-group`` and ``--end-group``,
1171 as supported by the GNU ``ld`` linker, use the :genex:`LINK_GROUP`
1172 generator expression instead.
1174 .. genex:: $<LINK_GROUP:feature,library-list>
1176 .. versionadded:: 3.24
1178 Specify a group of libraries to link to a target, along with a ``feature``
1179 which defines how that group should be linked. For example:
1181 .. code-block:: cmake
1183 add_library(lib1 STATIC ...)
1184 add_library(lib2 ...)
1185 target_link_libraries(lib2 PRIVATE "$<LINK_GROUP:RESCAN,lib1,external>")
1187 This specifies that ``lib2`` should link to ``lib1`` and ``external``, and
1188 that both of those two libraries should be included on the linker command
1189 line according to the definition of the ``RESCAN`` feature.
1191 Feature names are case-sensitive and may only contain letters, numbers and
1192 underscores. Feature names defined in all uppercase are reserved for CMake's
1193 own built-in features. Currently, there is only one pre-defined built-in
1196 .. include:: ../variable/LINK_GROUP_PREDEFINED_FEATURES.txt
1198 Built-in and custom group features are defined in terms of the following
1201 * :variable:`CMAKE_<LANG>_LINK_GROUP_USING_<FEATURE>_SUPPORTED`
1202 * :variable:`CMAKE_<LANG>_LINK_GROUP_USING_<FEATURE>`
1203 * :variable:`CMAKE_LINK_GROUP_USING_<FEATURE>_SUPPORTED`
1204 * :variable:`CMAKE_LINK_GROUP_USING_<FEATURE>`
1206 The value used for each of these variables is the value as set at the end of
1207 the directory scope in which the target was created. The usage is as follows:
1209 1. If the language-specific
1210 :variable:`CMAKE_<LANG>_LINK_GROUP_USING_<FEATURE>_SUPPORTED` variable
1211 is true, the ``feature`` must be defined by the corresponding
1212 :variable:`CMAKE_<LANG>_LINK_GROUP_USING_<FEATURE>` variable.
1213 2. If no language-specific ``feature`` is supported, then the
1214 :variable:`CMAKE_LINK_GROUP_USING_<FEATURE>_SUPPORTED` variable must be
1215 true and the ``feature`` must be defined by the corresponding
1216 :variable:`CMAKE_LINK_GROUP_USING_<FEATURE>` variable.
1218 The ``LINK_GROUP`` generator expression is compatible with the
1219 :genex:`LINK_LIBRARY` generator expression. The libraries involved in a
1220 group can be specified using the :genex:`LINK_LIBRARY` generator expression.
1222 Each target or external library involved in the link step is allowed to be
1223 part of multiple groups, but only if all the groups involved specify the
1224 same ``feature``. Such groups will not be merged on the linker command line,
1225 the individual groups will still be preserved. Mixing different group
1226 features for the same target or library is forbidden.
1228 .. code-block:: cmake
1230 add_library(lib1 ...)
1231 add_library(lib2 ...)
1232 add_library(lib3 ...)
1233 add_library(lib4 ...)
1234 add_library(lib5 ...)
1236 target_link_libraries(lib3 PUBLIC "$<LINK_GROUP:feature1,lib1,lib2>")
1237 target_link_libraries(lib4 PRIVATE "$<LINK_GROUP:feature1,lib1,lib3>")
1238 # lib4 will be linked with the groups {lib1,lib2} and {lib1,lib3}.
1239 # Both groups specify the same feature, so this is fine.
1241 target_link_libraries(lib5 PRIVATE "$<LINK_GROUP:feature2,lib1,lib3>")
1242 # An error will be raised here because both lib1 and lib3 are part of two
1243 # groups with different features.
1245 When a target or an external library is involved in the link step as part of
1246 a group and also as not part of any group, any occurrence of the non-group
1247 link item will be replaced by the groups it belongs to.
1249 .. code-block:: cmake
1251 add_library(lib1 ...)
1252 add_library(lib2 ...)
1253 add_library(lib3 ...)
1254 add_library(lib4 ...)
1256 target_link_libraries(lib3 PUBLIC lib1)
1258 target_link_libraries(lib4 PRIVATE lib3 "$<LINK_GROUP:feature1,lib1,lib2>")
1259 # lib4 will only be linked with lib3 and the group {lib1,lib2}
1261 Because ``lib1`` is part of the group defined for ``lib4``, that group then
1262 gets applied back to the use of ``lib1`` for ``lib3``. The end result will
1263 be as though the linking relationship for ``lib3`` had been specified as:
1265 .. code-block:: cmake
1267 target_link_libraries(lib3 PUBLIC "$<LINK_GROUP:feature1,lib1,lib2>")
1269 Be aware that the precedence of the group over the non-group link item can
1270 result in circular dependencies between groups. If this occurs, a fatal
1271 error is raised because circular dependencies are not allowed for groups.
1273 .. code-block:: cmake
1275 add_library(lib1A ...)
1276 add_library(lib1B ...)
1277 add_library(lib2A ...)
1278 add_library(lib2B ...)
1279 add_library(lib3 ...)
1281 # Non-group linking relationships, these are non-circular so far
1282 target_link_libraries(lib1A PUBLIC lib2A)
1283 target_link_libraries(lib2B PUBLIC lib1B)
1285 # The addition of these groups creates circular dependencies
1286 target_link_libraries(lib3 PRIVATE
1287 "$<LINK_GROUP:feat,lib1A,lib1B>"
1288 "$<LINK_GROUP:feat,lib2A,lib2B>"
1291 Because of the groups defined for ``lib3``, the linking relationships for
1292 ``lib1A`` and ``lib2B`` effectively get expanded to the equivalent of:
1294 .. code-block:: cmake
1296 target_link_libraries(lib1A PUBLIC "$<LINK_GROUP:feat,lib2A,lib2B>")
1297 target_link_libraries(lib2B PUBLIC "$<LINK_GROUP:feat,lib1A,lib1B>")
1299 This creates a circular dependency between groups:
1300 ``lib1A --> lib2B --> lib1A``.
1302 The following limitations should also be noted:
1304 * The ``library-list`` can specify CMake targets or libraries.
1305 Any CMake target of type :ref:`OBJECT <Object Libraries>`
1306 or :ref:`INTERFACE <Interface Libraries>` will ignore the feature aspect
1307 of the expression and instead be linked in the standard way.
1309 * The ``$<LINK_GROUP:...>`` generator expression can only be used to
1310 specify link libraries. In practice, this means it can appear in the
1311 :prop_tgt:`LINK_LIBRARIES`, :prop_tgt:`INTERFACE_LINK_LIBRARIES`,and
1312 :prop_tgt:`INTERFACE_LINK_LIBRARIES_DIRECT` target properties, and be
1313 specified in :command:`target_link_libraries` and :command:`link_libraries`
1316 * If a ``$<LINK_GROUP:...>`` generator expression appears in the
1317 :prop_tgt:`INTERFACE_LINK_LIBRARIES` property of a target, it will be
1318 included in the imported target generated by a :command:`install(EXPORT)`
1319 command. It is the responsibility of the environment consuming this
1320 import to define the link feature used by this expression.
1325 .. genex:: $<LINK_ONLY:...>
1327 .. versionadded:: 3.1
1329 Content of ``...``, except while collecting :ref:`Target Usage Requirements`,
1330 in which case it is the empty string. This is intended for use in an
1331 :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property, typically populated
1332 via the :command:`target_link_libraries` command, to specify private link
1333 dependencies without other usage requirements.
1335 .. versionadded:: 3.24
1336 ``LINK_ONLY`` may also be used in a :prop_tgt:`LINK_LIBRARIES` target
1337 property. See policy :policy:`CMP0131`.
1339 .. genex:: $<DEVICE_LINK:list>
1341 .. versionadded:: 3.18
1343 Returns the list if it is the device link step, an empty list otherwise.
1344 The device link step is controlled by :prop_tgt:`CUDA_SEPARABLE_COMPILATION`
1345 and :prop_tgt:`CUDA_RESOLVE_DEVICE_SYMBOLS` properties and
1346 policy :policy:`CMP0105`. This expression can only be used to specify link
1349 .. genex:: $<HOST_LINK:list>
1351 .. versionadded:: 3.18
1353 Returns the list if it is the normal link step, an empty list otherwise.
1354 This expression is mainly useful when a device link step is also involved
1355 (see :genex:`$<DEVICE_LINK:list>` generator expression). This expression can
1356 only be used to specify link options.
1359 .. _`Target-Dependent Queries`:
1361 Target-Dependent Expressions
1362 ----------------------------
1364 These queries refer to a target ``tgt``. Unless otherwise stated, this can
1365 be any runtime artifact, namely:
1367 * An executable target created by :command:`add_executable`.
1368 * A shared library target (``.so``, ``.dll`` but not their ``.lib`` import
1369 library) created by :command:`add_library`.
1370 * A static library target created by :command:`add_library`.
1372 In the following, the phrase "the ``tgt`` filename" means the name of the
1373 ``tgt`` binary file. This has to be distinguished from the phrase
1374 "the target name", which is just the string ``tgt``.
1376 .. genex:: $<TARGET_EXISTS:tgt>
1378 .. versionadded:: 3.12
1380 ``1`` if ``tgt`` exists as a CMake target, else ``0``.
1382 .. genex:: $<TARGET_NAME_IF_EXISTS:tgt>
1384 .. versionadded:: 3.12
1386 The target name ``tgt`` if the target exists, an empty string otherwise.
1388 Note that ``tgt`` is not added as a dependency of the target this
1389 expression is evaluated on.
1391 .. genex:: $<TARGET_NAME:...>
1393 Marks ``...`` as being the name of a target. This is required if exporting
1394 targets to multiple dependent export sets. The ``...`` must be a literal
1395 name of a target, it may not contain generator expressions.
1397 .. genex:: $<TARGET_PROPERTY:tgt,prop>
1399 Value of the property ``prop`` on the target ``tgt``.
1401 Note that ``tgt`` is not added as a dependency of the target this
1402 expression is evaluated on.
1404 .. genex:: $<TARGET_PROPERTY:prop>
1406 Value of the property ``prop`` on the target for which the expression
1407 is being evaluated. Note that for generator expressions in
1408 :ref:`Target Usage Requirements` this is the consuming target rather
1409 than the target specifying the requirement.
1411 .. genex:: $<TARGET_OBJECTS:tgt>
1413 .. versionadded:: 3.1
1415 List of objects resulting from building ``tgt``. This would typically be
1416 used on :ref:`object library <Object Libraries>` targets.
1418 .. genex:: $<TARGET_POLICY:policy>
1420 ``1`` if the ``policy`` was ``NEW`` when the 'head' target was created,
1421 else ``0``. If the ``policy`` was not set, the warning message for the policy
1422 will be emitted. This generator expression only works for a subset of
1425 .. genex:: $<TARGET_FILE:tgt>
1427 Full path to the ``tgt`` binary file.
1429 Note that ``tgt`` is not added as a dependency of the target this
1430 expression is evaluated on, unless the expression is being used in
1431 :command:`add_custom_command` or :command:`add_custom_target`.
1433 .. genex:: $<TARGET_FILE_BASE_NAME:tgt>
1435 .. versionadded:: 3.15
1437 Base name of ``tgt``, i.e. ``$<TARGET_FILE_NAME:tgt>`` without prefix and
1439 For example, if the ``tgt`` filename is ``libbase.so``, the base name is ``base``.
1441 See also the :prop_tgt:`OUTPUT_NAME`, :prop_tgt:`ARCHIVE_OUTPUT_NAME`,
1442 :prop_tgt:`LIBRARY_OUTPUT_NAME` and :prop_tgt:`RUNTIME_OUTPUT_NAME`
1443 target properties and their configuration specific variants
1444 :prop_tgt:`OUTPUT_NAME_<CONFIG>`, :prop_tgt:`ARCHIVE_OUTPUT_NAME_<CONFIG>`,
1445 :prop_tgt:`LIBRARY_OUTPUT_NAME_<CONFIG>` and
1446 :prop_tgt:`RUNTIME_OUTPUT_NAME_<CONFIG>`.
1448 The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
1449 properties can also be considered.
1451 Note that ``tgt`` is not added as a dependency of the target this
1452 expression is evaluated on.
1454 .. genex:: $<TARGET_FILE_PREFIX:tgt>
1456 .. versionadded:: 3.15
1458 Prefix of the ``tgt`` filename (such as ``lib``).
1460 See also the :prop_tgt:`PREFIX` target property.
1462 Note that ``tgt`` is not added as a dependency of the target this
1463 expression is evaluated on.
1465 .. genex:: $<TARGET_FILE_SUFFIX:tgt>
1467 .. versionadded:: 3.15
1469 Suffix of the ``tgt`` filename (extension such as ``.so`` or ``.exe``).
1471 See also the :prop_tgt:`SUFFIX` target property.
1473 Note that ``tgt`` is not added as a dependency of the target this
1474 expression is evaluated on.
1476 .. genex:: $<TARGET_FILE_NAME:tgt>
1478 The ``tgt`` filename.
1480 Note that ``tgt`` is not added as a dependency of the target this
1481 expression is evaluated on (see policy :policy:`CMP0112`).
1483 .. genex:: $<TARGET_FILE_DIR:tgt>
1485 Directory of the ``tgt`` binary file.
1487 Note that ``tgt`` is not added as a dependency of the target this
1488 expression is evaluated on (see policy :policy:`CMP0112`).
1490 .. genex:: $<TARGET_LINKER_FILE:tgt>
1492 File used when linking to the ``tgt`` target. This will usually
1493 be the library that ``tgt`` represents (``.a``, ``.lib``, ``.so``),
1494 but for a shared library on DLL platforms, it would be the ``.lib``
1495 import library associated with the DLL.
1497 .. genex:: $<TARGET_LINKER_FILE_BASE_NAME:tgt>
1499 .. versionadded:: 3.15
1501 Base name of file used to link the target ``tgt``, i.e.
1502 ``$<TARGET_LINKER_FILE_NAME:tgt>`` without prefix and suffix. For example,
1503 if target file name is ``libbase.a``, the base name is ``base``.
1505 See also the :prop_tgt:`OUTPUT_NAME`, :prop_tgt:`ARCHIVE_OUTPUT_NAME`,
1506 and :prop_tgt:`LIBRARY_OUTPUT_NAME` target properties and their configuration
1507 specific variants :prop_tgt:`OUTPUT_NAME_<CONFIG>`,
1508 :prop_tgt:`ARCHIVE_OUTPUT_NAME_<CONFIG>` and
1509 :prop_tgt:`LIBRARY_OUTPUT_NAME_<CONFIG>`.
1511 The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
1512 properties can also be considered.
1514 Note that ``tgt`` is not added as a dependency of the target this
1515 expression is evaluated on.
1517 .. genex:: $<TARGET_LINKER_FILE_PREFIX:tgt>
1519 .. versionadded:: 3.15
1521 Prefix of file used to link target ``tgt``.
1523 See also the :prop_tgt:`PREFIX` and :prop_tgt:`IMPORT_PREFIX` target
1526 Note that ``tgt`` is not added as a dependency of the target this
1527 expression is evaluated on.
1529 .. genex:: $<TARGET_LINKER_FILE_SUFFIX:tgt>
1531 .. versionadded:: 3.15
1533 Suffix of file used to link where ``tgt`` is the name of a target.
1535 The suffix corresponds to the file extension (such as ".so" or ".lib").
1537 See also the :prop_tgt:`SUFFIX` and :prop_tgt:`IMPORT_SUFFIX` target
1540 Note that ``tgt`` is not added as a dependency of the target this
1541 expression is evaluated on.
1543 .. genex:: $<TARGET_LINKER_FILE_NAME:tgt>
1545 Name of file used to link target ``tgt``.
1547 Note that ``tgt`` is not added as a dependency of the target this
1548 expression is evaluated on (see policy :policy:`CMP0112`).
1550 .. genex:: $<TARGET_LINKER_FILE_DIR:tgt>
1552 Directory of file used to link target ``tgt``.
1554 Note that ``tgt`` is not added as a dependency of the target this
1555 expression is evaluated on (see policy :policy:`CMP0112`).
1557 .. genex:: $<TARGET_SONAME_FILE:tgt>
1559 File with soname (``.so.3``) where ``tgt`` is the name of a target.
1560 .. genex:: $<TARGET_SONAME_FILE_NAME:tgt>
1562 Name of file with soname (``.so.3``).
1564 Note that ``tgt`` is not added as a dependency of the target this
1565 expression is evaluated on (see policy :policy:`CMP0112`).
1567 .. genex:: $<TARGET_SONAME_FILE_DIR:tgt>
1569 Directory of with soname (``.so.3``).
1571 Note that ``tgt`` is not added as a dependency of the target this
1572 expression is evaluated on (see policy :policy:`CMP0112`).
1574 .. genex:: $<TARGET_PDB_FILE:tgt>
1576 .. versionadded:: 3.1
1578 Full path to the linker generated program database file (.pdb)
1579 where ``tgt`` is the name of a target.
1581 See also the :prop_tgt:`PDB_NAME` and :prop_tgt:`PDB_OUTPUT_DIRECTORY`
1582 target properties and their configuration specific variants
1583 :prop_tgt:`PDB_NAME_<CONFIG>` and :prop_tgt:`PDB_OUTPUT_DIRECTORY_<CONFIG>`.
1585 .. genex:: $<TARGET_PDB_FILE_BASE_NAME:tgt>
1587 .. versionadded:: 3.15
1589 Base name of the linker generated program database file (.pdb)
1590 where ``tgt`` is the name of a target.
1592 The base name corresponds to the target PDB file name (see
1593 ``$<TARGET_PDB_FILE_NAME:tgt>``) without prefix and suffix. For example,
1594 if target file name is ``base.pdb``, the base name is ``base``.
1596 See also the :prop_tgt:`PDB_NAME` target property and its configuration
1597 specific variant :prop_tgt:`PDB_NAME_<CONFIG>`.
1599 The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
1600 properties can also be considered.
1602 Note that ``tgt`` is not added as a dependency of the target this
1603 expression is evaluated on.
1605 .. genex:: $<TARGET_PDB_FILE_NAME:tgt>
1607 .. versionadded:: 3.1
1609 Name of the linker generated program database file (.pdb).
1611 Note that ``tgt`` is not added as a dependency of the target this
1612 expression is evaluated on (see policy :policy:`CMP0112`).
1614 .. genex:: $<TARGET_PDB_FILE_DIR:tgt>
1616 .. versionadded:: 3.1
1618 Directory of the linker generated program database file (.pdb).
1620 Note that ``tgt`` is not added as a dependency of the target this
1621 expression is evaluated on (see policy :policy:`CMP0112`).
1623 .. genex:: $<TARGET_BUNDLE_DIR:tgt>
1625 .. versionadded:: 3.9
1627 Full path to the bundle directory (``/path/to/my.app``,
1628 ``/path/to/my.framework``, or ``/path/to/my.bundle``),
1629 where ``tgt`` is the name of a target.
1631 Note that ``tgt`` is not added as a dependency of the target this
1632 expression is evaluated on (see policy :policy:`CMP0112`).
1634 .. genex:: $<TARGET_BUNDLE_DIR_NAME:tgt>
1636 .. versionadded:: 3.24
1638 Name of the bundle directory (``my.app``, ``my.framework``, or
1639 ``my.bundle``), where ``tgt`` is the name of a target.
1641 Note that ``tgt`` is not added as a dependency of the target this
1642 expression is evaluated on (see policy :policy:`CMP0112`).
1644 .. genex:: $<TARGET_BUNDLE_CONTENT_DIR:tgt>
1646 .. versionadded:: 3.9
1648 Full path to the bundle content directory where ``tgt`` is the name of a
1649 target. For the macOS SDK it leads to ``/path/to/my.app/Contents``,
1650 ``/path/to/my.framework``, or ``/path/to/my.bundle/Contents``.
1651 For all other SDKs (e.g. iOS) it leads to ``/path/to/my.app``,
1652 ``/path/to/my.framework``, or ``/path/to/my.bundle`` due to the flat
1655 Note that ``tgt`` is not added as a dependency of the target this
1656 expression is evaluated on (see policy :policy:`CMP0112`).
1658 .. genex:: $<TARGET_RUNTIME_DLLS:tgt>
1660 .. versionadded:: 3.21
1662 List of DLLs that the target depends on at runtime. This is determined by
1663 the locations of all the ``SHARED`` targets in the target's transitive
1664 dependencies. Using this generator expression on targets other than
1665 executables, ``SHARED`` libraries, and ``MODULE`` libraries is an error.
1666 **On non-DLL platforms, this expression always evaluates to an empty string**.
1668 This generator expression can be used to copy all of the DLLs that a target
1669 depends on into its output directory in a ``POST_BUILD`` custom command. For
1672 .. code-block:: cmake
1674 find_package(foo CONFIG REQUIRED) # package generated by install(EXPORT)
1676 add_executable(exe main.c)
1677 target_link_libraries(exe PRIVATE foo::foo foo::bar)
1678 add_custom_command(TARGET exe POST_BUILD
1679 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:exe> $<TARGET_FILE_DIR:exe>
1680 COMMAND_EXPAND_LISTS
1685 :ref:`Imported Targets` are supported only if they know the location
1686 of their ``.dll`` files. An imported ``SHARED`` library must have
1687 :prop_tgt:`IMPORTED_LOCATION` set to its ``.dll`` file. See the
1688 :ref:`add_library imported libraries <add_library imported libraries>`
1689 section for details. Many :ref:`Find Modules` produce imported targets
1690 with the ``UNKNOWN`` type and therefore will be ignored.
1693 Export And Install Expressions
1694 ------------------------------
1696 .. genex:: $<INSTALL_INTERFACE:...>
1698 Content of ``...`` when the property is exported using
1699 :command:`install(EXPORT)`, and empty otherwise.
1701 .. genex:: $<BUILD_INTERFACE:...>
1703 Content of ``...`` when the property is exported using :command:`export`, or
1704 when the target is used by another target in the same buildsystem. Expands to
1705 the empty string otherwise.
1707 .. genex:: $<INSTALL_PREFIX>
1709 Content of the install prefix when the target is exported via
1710 :command:`install(EXPORT)`, or when evaluated in the
1711 :prop_tgt:`INSTALL_NAME_DIR` property or the ``INSTALL_NAME_DIR`` argument of
1712 :command:`install(RUNTIME_DEPENDENCY_SET)`, and empty otherwise.
1714 Multi-level Expression Evaluation
1715 ---------------------------------
1717 .. genex:: $<GENEX_EVAL:expr>
1719 .. versionadded:: 3.12
1721 Content of ``expr`` evaluated as a generator expression in the current
1722 context. This enables consumption of generator expressions whose
1723 evaluation results itself in generator expressions.
1725 .. genex:: $<TARGET_GENEX_EVAL:tgt,expr>
1727 .. versionadded:: 3.12
1729 Content of ``expr`` evaluated as a generator expression in the context of
1730 ``tgt`` target. This enables consumption of custom target properties that
1731 themselves contain generator expressions.
1733 Having the capability to evaluate generator expressions is very useful when
1734 you want to manage custom properties supporting generator expressions.
1737 .. code-block:: cmake
1739 add_library(foo ...)
1741 set_property(TARGET foo PROPERTY
1742 CUSTOM_KEYS $<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>
1745 add_custom_target(printFooKeys
1746 COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_PROPERTY:foo,CUSTOM_KEYS>
1749 This naive implementation of the ``printFooKeys`` custom command is wrong
1750 because ``CUSTOM_KEYS`` target property is not evaluated and the content
1751 is passed as is (i.e. ``$<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>``).
1753 To have the expected result (i.e. ``FOO_EXTRA_THINGS`` if config is
1754 ``Debug``), it is required to evaluate the output of
1755 ``$<TARGET_PROPERTY:foo,CUSTOM_KEYS>``:
1757 .. code-block:: cmake
1759 add_custom_target(printFooKeys
1760 COMMAND ${CMAKE_COMMAND} -E
1761 echo $<TARGET_GENEX_EVAL:foo,$<TARGET_PROPERTY:foo,CUSTOM_KEYS>>
1767 These expressions evaluate to specific string literals. Use them in place of
1768 the actual string literal where you need to prevent them from having their
1771 .. genex:: $<ANGLE-R>
1773 A literal ``>``. Used for example to compare strings that contain a ``>``.
1777 A literal ``,``. Used for example to compare strings which contain a ``,``.
1779 .. genex:: $<SEMICOLON>
1781 A literal ``;``. Used to prevent list expansion on an argument with ``;``.
1783 Deprecated Expressions
1784 ----------------------
1786 .. genex:: $<CONFIGURATION>
1788 Configuration name. Deprecated since CMake 3.0. Use :genex:`CONFIG` instead.