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.
16 Generator expressions are allowed in the context of many target properties,
17 such as :prop_tgt:`LINK_LIBRARIES`, :prop_tgt:`INCLUDE_DIRECTORIES`,
18 :prop_tgt:`COMPILE_DEFINITIONS` and others. They may also be used when using
19 commands to populate those properties, such as :command:`target_link_libraries`,
20 :command:`target_include_directories`, :command:`target_compile_definitions`
23 They enable conditional linking, conditional definitions used when compiling,
24 conditional include directories, and more. The conditions may be based on
25 the build configuration, target properties, platform information or any other
26 queryable information.
28 Generator expressions have the form ``$<...>``. To avoid confusion, this page
29 deviates from most of the CMake documentation in that it omits angular brackets
30 ``<...>`` around placeholders like ``condition``, ``string``, ``target``,
33 Generator expressions can be nested, as shown in most of the examples below.
35 .. _`Boolean Generator Expressions`:
37 Boolean Generator Expressions
38 =============================
40 Boolean expressions evaluate to either ``0`` or ``1``.
41 They are typically used to construct the condition in a :ref:`conditional
42 generator expression<Conditional Generator Expressions>`.
44 Available boolean expressions are:
49 .. genex:: $<BOOL:string>
51 Converts ``string`` to ``0`` or ``1``. Evaluates to ``0`` if any of the
54 * ``string`` is empty,
55 * ``string`` is a case-insensitive equal of
56 ``0``, ``FALSE``, ``OFF``, ``N``, ``NO``, ``IGNORE``, or ``NOTFOUND``, or
57 * ``string`` ends in the suffix ``-NOTFOUND`` (case-sensitive).
59 Otherwise evaluates to ``1``.
61 .. genex:: $<AND:conditions>
63 where ``conditions`` is a comma-separated list of boolean expressions.
64 Evaluates to ``1`` if all conditions are ``1``.
65 Otherwise evaluates to ``0``.
67 .. genex:: $<OR:conditions>
69 where ``conditions`` is a comma-separated list of boolean expressions.
70 Evaluates to ``1`` if at least one of the conditions is ``1``.
71 Otherwise evaluates to ``0``.
73 .. genex:: $<NOT:condition>
75 ``0`` if ``condition`` is ``1``, else ``1``.
80 .. genex:: $<STREQUAL:string1,string2>
82 ``1`` if ``string1`` and ``string2`` are equal, else ``0``.
83 The comparison is case-sensitive. For a case-insensitive comparison,
84 combine with a :ref:`string transforming generator expression
85 <String Transforming Generator Expressions>`,
89 $<STREQUAL:$<UPPER_CASE:${foo}>,"BAR"> # "1" if ${foo} is any of "BAR", "Bar", "bar", ...
91 .. genex:: $<EQUAL:value1,value2>
93 ``1`` if ``value1`` and ``value2`` are numerically equal, else ``0``.
95 .. genex:: $<IN_LIST:string,list>
97 .. versionadded:: 3.12
99 ``1`` if ``string`` is member of the semicolon-separated ``list``, else ``0``.
100 Uses case-sensitive comparisons.
102 .. genex:: $<VERSION_LESS:v1,v2>
104 ``1`` if ``v1`` is a version less than ``v2``, else ``0``.
106 .. genex:: $<VERSION_GREATER:v1,v2>
108 ``1`` if ``v1`` is a version greater than ``v2``, else ``0``.
110 .. genex:: $<VERSION_EQUAL:v1,v2>
112 ``1`` if ``v1`` is the same version as ``v2``, else ``0``.
114 .. genex:: $<VERSION_LESS_EQUAL:v1,v2>
116 .. versionadded:: 3.7
118 ``1`` if ``v1`` is a version less than or equal to ``v2``, else ``0``.
120 .. genex:: $<VERSION_GREATER_EQUAL:v1,v2>
122 .. versionadded:: 3.7
124 ``1`` if ``v1`` is a version greater than or equal to ``v2``, else ``0``.
129 .. genex:: $<TARGET_EXISTS:target>
131 .. versionadded:: 3.12
133 ``1`` if ``target`` exists, else ``0``.
135 .. genex:: $<CONFIG:cfgs>
137 ``1`` if config is any one of the entries in comma-separated list
138 ``cfgs``, else ``0``. This is a case-insensitive comparison. The mapping in
139 :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` is also considered by this
140 expression when it is evaluated on a property on an :prop_tgt:`IMPORTED`
143 .. genex:: $<PLATFORM_ID:platform_ids>
145 where ``platform_ids`` is a comma-separated list.
146 ``1`` if the CMake's platform id matches any one of the entries in
147 ``platform_ids``, otherwise ``0``.
148 See also the :variable:`CMAKE_SYSTEM_NAME` variable.
150 .. genex:: $<C_COMPILER_ID:compiler_ids>
152 where ``compiler_ids`` is a comma-separated list.
153 ``1`` if the CMake's compiler id of the C compiler matches any one
154 of the entries in ``compiler_ids``, otherwise ``0``.
155 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
157 .. genex:: $<CXX_COMPILER_ID:compiler_ids>
159 where ``compiler_ids`` is a comma-separated list.
160 ``1`` if the CMake's compiler id of the CXX compiler matches any one
161 of the entries in ``compiler_ids``, otherwise ``0``.
162 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
164 .. genex:: $<CUDA_COMPILER_ID:compiler_ids>
166 .. versionadded:: 3.15
168 where ``compiler_ids`` is a comma-separated list.
169 ``1`` if the CMake's compiler id of the CUDA compiler matches any one
170 of the entries in ``compiler_ids``, otherwise ``0``.
171 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
173 .. genex:: $<OBJC_COMPILER_ID:compiler_ids>
175 .. versionadded:: 3.16
177 where ``compiler_ids`` is a comma-separated list.
178 ``1`` if the CMake's compiler id of the Objective-C compiler matches any one
179 of the entries in ``compiler_ids``, otherwise ``0``.
180 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
182 .. genex:: $<OBJCXX_COMPILER_ID:compiler_ids>
184 .. versionadded:: 3.16
186 where ``compiler_ids`` is a comma-separated list.
187 ``1`` if the CMake's compiler id of the Objective-C++ compiler matches any one
188 of the entries in ``compiler_ids``, otherwise ``0``.
189 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
191 .. genex:: $<Fortran_COMPILER_ID:compiler_ids>
193 where ``compiler_ids`` is a comma-separated list.
194 ``1`` if the CMake's compiler id of the Fortran compiler matches any one
195 of the entries in ``compiler_ids``, otherwise ``0``.
196 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
198 .. genex:: $<HIP_COMPILER_ID:compiler_ids>
200 .. versionadded:: 3.21
202 where ``compiler_ids`` is a comma-separated list.
203 ``1`` if the CMake's compiler id of the HIP compiler matches any one
204 of the entries in ``compiler_ids``, otherwise ``0``.
205 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
207 .. genex:: $<ISPC_COMPILER_ID:compiler_ids>
209 .. versionadded:: 3.19
211 where ``compiler_ids`` is a comma-separated list.
212 ``1`` if the CMake's compiler id of the ISPC compiler matches any one
213 of the entries in ``compiler_ids``, otherwise ``0``.
214 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
216 .. genex:: $<C_COMPILER_VERSION:version>
218 ``1`` if the version of the C compiler matches ``version``, otherwise ``0``.
219 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
221 .. genex:: $<CXX_COMPILER_VERSION:version>
223 ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``.
224 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
226 .. genex:: $<CUDA_COMPILER_VERSION:version>
228 .. versionadded:: 3.15
230 ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``.
231 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
233 .. genex:: $<OBJC_COMPILER_VERSION:version>
235 .. versionadded:: 3.16
237 ``1`` if the version of the OBJC compiler matches ``version``, otherwise ``0``.
238 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
240 .. genex:: $<OBJCXX_COMPILER_VERSION:version>
242 .. versionadded:: 3.16
244 ``1`` if the version of the OBJCXX compiler matches ``version``, otherwise ``0``.
245 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
247 .. genex:: $<Fortran_COMPILER_VERSION:version>
249 ``1`` if the version of the Fortran compiler matches ``version``, otherwise ``0``.
250 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
252 .. genex:: $<HIP_COMPILER_VERSION:version>
254 .. versionadded:: 3.21
256 ``1`` if the version of the HIP compiler matches ``version``, otherwise ``0``.
257 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
259 .. genex:: $<ISPC_COMPILER_VERSION:version>
261 .. versionadded:: 3.19
263 ``1`` if the version of the ISPC compiler matches ``version``, otherwise ``0``.
264 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
266 .. genex:: $<TARGET_POLICY:policy>
268 ``1`` if the ``policy`` was NEW when the 'head' target was created,
269 else ``0``. If the ``policy`` was not set, the warning message for the policy
270 will be emitted. This generator expression only works for a subset of
273 .. genex:: $<COMPILE_FEATURES:features>
275 .. versionadded:: 3.1
277 where ``features`` is a comma-spearated list.
278 Evaluates to ``1`` if all of the ``features`` are available for the 'head'
279 target, and ``0`` otherwise. If this expression is used while evaluating
280 the link implementation of a target and if any dependency transitively
281 increases the required :prop_tgt:`C_STANDARD` or :prop_tgt:`CXX_STANDARD`
282 for the 'head' target, an error is reported. See the
283 :manual:`cmake-compile-features(7)` manual for information on
284 compile features and a list of supported compilers.
286 .. _`Boolean COMPILE_LANGUAGE Generator Expression`:
288 .. genex:: $<COMPILE_LANG_AND_ID:language,compiler_ids>
290 .. versionadded:: 3.15
292 ``1`` when the language used for compilation unit matches ``language`` and
293 the CMake's compiler id of the language compiler matches any one of the
294 entries in ``compiler_ids``, otherwise ``0``. This expression is a short form
295 for the combination of ``$<COMPILE_LANGUAGE:language>`` and
296 ``$<LANG_COMPILER_ID:compiler_ids>``. This expression may be used to specify
297 compile options, compile definitions, and include directories for source files of a
298 particular language and compiler combination in a target. For example:
300 .. code-block:: cmake
302 add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
303 target_compile_definitions(myapp
304 PRIVATE $<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang>:COMPILING_CXX_WITH_CLANG>
305 $<$<COMPILE_LANG_AND_ID:CXX,Intel>:COMPILING_CXX_WITH_INTEL>
306 $<$<COMPILE_LANG_AND_ID:C,Clang>:COMPILING_C_WITH_CLANG>
309 This specifies the use of different compile definitions based on both
310 the compiler id and compilation language. This example will have a
311 ``COMPILING_CXX_WITH_CLANG`` compile definition when Clang is the CXX
312 compiler, and ``COMPILING_CXX_WITH_INTEL`` when Intel is the CXX compiler.
313 Likewise when the C compiler is Clang it will only see the ``COMPILING_C_WITH_CLANG``
316 Without the ``COMPILE_LANG_AND_ID`` generator expression the same logic
317 would be expressed as:
319 .. code-block:: cmake
321 target_compile_definitions(myapp
322 PRIVATE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:AppleClang,Clang>>:COMPILING_CXX_WITH_CLANG>
323 $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:Intel>>:COMPILING_CXX_WITH_INTEL>
324 $<$<AND:$<COMPILE_LANGUAGE:C>,$<C_COMPILER_ID:Clang>>:COMPILING_C_WITH_CLANG>
327 .. genex:: $<COMPILE_LANGUAGE:languages>
329 .. versionadded:: 3.3
331 ``1`` when the language used for compilation unit matches any of the entries
332 in ``languages``, otherwise ``0``. This expression may be used to specify
333 compile options, compile definitions, and include directories for source files of a
334 particular language in a target. For example:
336 .. code-block:: cmake
338 add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
339 target_compile_options(myapp
340 PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
342 target_compile_definitions(myapp
343 PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
344 $<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
346 target_include_directories(myapp
347 PRIVATE $<$<COMPILE_LANGUAGE:CXX,CUDA>:/opt/foo/headers>
350 This specifies the use of the ``-fno-exceptions`` compile option,
351 ``COMPILING_CXX`` compile definition, and ``cxx_headers`` include
352 directory for C++ only (compiler id checks elided). It also specifies
353 a ``COMPILING_CUDA`` compile definition for CUDA.
355 Note that with :ref:`Visual Studio Generators` and :generator:`Xcode` there
356 is no way to represent target-wide compile definitions or include directories
357 separately for ``C`` and ``CXX`` languages.
358 Also, with :ref:`Visual Studio Generators` there is no way to represent
359 target-wide flags separately for ``C`` and ``CXX`` languages. Under these
360 generators, expressions for both C and C++ sources will be evaluated
361 using ``CXX`` if there are any C++ sources and otherwise using ``C``.
362 A workaround is to create separate libraries for each source file language
365 .. code-block:: cmake
367 add_library(myapp_c foo.c)
368 add_library(myapp_cxx bar.cpp)
369 target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
370 add_executable(myapp main.cpp)
371 target_link_libraries(myapp myapp_c myapp_cxx)
373 .. _`Boolean LINK_LANGUAGE Generator Expression`:
375 .. genex:: $<LINK_LANG_AND_ID:language,compiler_ids>
377 .. versionadded:: 3.18
379 ``1`` when the language used for link step matches ``language`` and the
380 CMake's compiler id of the language linker matches any one of the entries
381 in ``compiler_ids``, otherwise ``0``. This expression is a short form for the
382 combination of ``$<LINK_LANGUAGE:language>`` and
383 ``$<LANG_COMPILER_ID:compiler_ids>``. This expression may be used to specify
384 link libraries, link options, link directories and link dependencies of a
385 particular language and linker combination in a target. For example:
387 .. code-block:: cmake
389 add_library(libC_Clang ...)
390 add_library(libCXX_Clang ...)
391 add_library(libC_Intel ...)
392 add_library(libCXX_Intel ...)
394 add_executable(myapp main.c)
396 target_sources(myapp PRIVATE file.cxx)
398 target_link_libraries(myapp
399 PRIVATE $<$<LINK_LANG_AND_ID:CXX,Clang,AppleClang>:libCXX_Clang>
400 $<$<LINK_LANG_AND_ID:C,Clang,AppleClang>:libC_Clang>
401 $<$<LINK_LANG_AND_ID:CXX,Intel>:libCXX_Intel>
402 $<$<LINK_LANG_AND_ID:C,Intel>:libC_Intel>)
404 This specifies the use of different link libraries based on both the
405 compiler id and link language. This example will have target ``libCXX_Clang``
406 as link dependency when ``Clang`` or ``AppleClang`` is the ``CXX``
407 linker, and ``libCXX_Intel`` when ``Intel`` is the ``CXX`` linker.
408 Likewise when the ``C`` linker is ``Clang`` or ``AppleClang``, target
409 ``libC_Clang`` will be added as link dependency and ``libC_Intel`` when
410 ``Intel`` is the ``C`` linker.
412 See :ref:`the note related to
413 <Constraints LINK_LANGUAGE Generator Expression>`
414 ``$<LINK_LANGUAGE:language>`` for constraints about the usage of this
415 generator expression.
417 .. genex:: $<LINK_LANGUAGE:languages>
419 .. versionadded:: 3.18
421 ``1`` when the language used for link step matches any of the entries
422 in ``languages``, otherwise ``0``. This expression may be used to specify
423 link libraries, link options, link directories and link dependencies of a
424 particular language in a target. For example:
426 .. code-block:: cmake
428 add_library(api_C ...)
429 add_library(api_CXX ...)
430 add_library(api INTERFACE)
431 target_link_options(api INTERFACE $<$<LINK_LANGUAGE:C>:-opt_c>
432 $<$<LINK_LANGUAGE:CXX>:-opt_cxx>)
433 target_link_libraries(api INTERFACE $<$<LINK_LANGUAGE:C>:api_C>
434 $<$<LINK_LANGUAGE:CXX>:api_CXX>)
436 add_executable(myapp1 main.c)
437 target_link_options(myapp1 PRIVATE api)
439 add_executable(myapp2 main.cpp)
440 target_link_options(myapp2 PRIVATE api)
442 This specifies to use the ``api`` target for linking targets ``myapp1`` and
443 ``myapp2``. In practice, ``myapp1`` will link with target ``api_C`` and
444 option ``-opt_c`` because it will use ``C`` as link language. And ``myapp2``
445 will link with ``api_CXX`` and option ``-opt_cxx`` because ``CXX`` will be
448 .. _`Constraints LINK_LANGUAGE Generator Expression`:
452 To determine the link language of a target, it is required to collect,
453 transitively, all the targets which will be linked to it. So, for link
454 libraries properties, a double evaluation will be done. During the first
455 evaluation, ``$<LINK_LANGUAGE:..>`` expressions will always return ``0``.
456 The link language computed after this first pass will be used to do the
457 second pass. To avoid inconsistency, it is required that the second pass
458 do not change the link language. Moreover, to avoid unexpected
459 side-effects, it is required to specify complete entities as part of the
460 ``$<LINK_LANGUAGE:..>`` expression. For example:
462 .. code-block:: cmake
464 add_library(lib STATIC file.cxx)
465 add_library(libother STATIC file.c)
468 add_executable(myapp1 main.c)
469 target_link_libraries(myapp1 PRIVATE lib$<$<LINK_LANGUAGE:C>:other>)
472 add_executable(myapp2 main.c)
473 target_link_libraries(myapp2 PRIVATE $<$<LINK_LANGUAGE:C>:libother>)
475 In this example, for ``myapp1``, the first pass will, unexpectedly,
476 determine that the link language is ``CXX`` because the evaluation of the
477 generator expression will be an empty string so ``myapp1`` will depends on
478 target ``lib`` which is ``C++``. On the contrary, for ``myapp2``, the first
479 evaluation will give ``C`` as link language, so the second pass will
480 correctly add target ``libother`` as link dependency.
482 .. genex:: $<DEVICE_LINK:list>
484 .. versionadded:: 3.18
486 Returns the list if it is the device link step, an empty list otherwise.
487 The device link step is controlled by :prop_tgt:`CUDA_SEPARABLE_COMPILATION`
488 and :prop_tgt:`CUDA_RESOLVE_DEVICE_SYMBOLS` properties and
489 policy :policy:`CMP0105`. This expression can only be used to specify link
492 .. genex:: $<HOST_LINK:list>
494 .. versionadded:: 3.18
496 Returns the list if it is the normal link step, an empty list otherwise.
497 This expression is mainly useful when a device link step is also involved
498 (see ``$<DEVICE_LINK:list>`` generator expression). This expression can only
499 be used to specify link options.
501 String-Valued Generator Expressions
502 ===================================
504 These expressions expand to some string.
507 .. code-block:: cmake
509 include_directories(/usr/include/$<CXX_COMPILER_ID>/)
511 expands to ``/usr/include/GNU/`` or ``/usr/include/Clang/`` etc, depending on
512 the compiler identifier.
514 String-valued expressions may also be combined with other expressions.
515 Here an example for a string-valued expression within a boolean expressions
516 within a conditional expression:
518 .. code-block:: cmake
520 $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
522 expands to ``OLD_COMPILER`` if the
523 :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
526 And here two nested string-valued expressions:
528 .. code-block:: cmake
530 -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
532 generates a string of the entries in the :prop_tgt:`INCLUDE_DIRECTORIES` target
533 property with each entry preceded by ``-I``.
535 Expanding on the previous example, if one first wants to check if the
536 ``INCLUDE_DIRECTORIES`` property is non-empty, then it is advisable to
537 introduce a helper variable to keep the code readable:
539 .. code-block:: cmake
541 set(prop "$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>") # helper variable
542 $<$<BOOL:${prop}>:-I$<JOIN:${prop}, -I>>
544 The following string-valued generator expressions are available:
549 String literals to escape the special meaning a character would otherwise have:
551 .. genex:: $<ANGLE-R>
553 A literal ``>``. Used for example to compare strings that contain a ``>``.
557 A literal ``,``. Used for example to compare strings which contain a ``,``.
559 .. genex:: $<SEMICOLON>
561 A literal ``;``. Used to prevent list expansion on an argument with ``;``.
563 .. _`Conditional Generator Expressions`:
565 Conditional Expressions
566 -----------------------
568 Conditional generator expressions depend on a boolean condition
569 that must be ``0`` or ``1``.
571 .. genex:: $<condition:true_string>
573 Evaluates to ``true_string`` if ``condition`` is ``1``.
574 Otherwise evaluates to the empty string.
576 .. genex:: $<IF:condition,true_string,false_string>
578 .. versionadded:: 3.8
580 Evaluates to ``true_string`` if ``condition`` is ``1``.
581 Otherwise evaluates to ``false_string``.
583 Typically, the ``condition`` is a :ref:`boolean generator expression
584 <Boolean Generator Expressions>`. For instance,
586 .. code-block:: cmake
588 $<$<CONFIG:Debug>:DEBUG_MODE>
590 expands to ``DEBUG_MODE`` when the ``Debug`` configuration is used, and
591 otherwise expands to the empty string.
593 .. _`String Transforming Generator Expressions`:
595 String Transformations
596 ----------------------
598 .. genex:: $<JOIN:list,string>
600 Joins the list with the content of ``string``.
602 .. genex:: $<REMOVE_DUPLICATES:list>
604 .. versionadded:: 3.15
606 Removes duplicated items in the given ``list``.
608 .. genex:: $<FILTER:list,INCLUDE|EXCLUDE,regex>
610 .. versionadded:: 3.15
612 Includes or removes items from ``list`` that match the regular expression ``regex``.
614 .. genex:: $<LOWER_CASE:string>
616 Content of ``string`` converted to lower case.
618 .. genex:: $<UPPER_CASE:string>
620 Content of ``string`` converted to upper case.
622 .. genex:: $<GENEX_EVAL:expr>
624 .. versionadded:: 3.12
626 Content of ``expr`` evaluated as a generator expression in the current
627 context. This enables consumption of generator expressions whose
628 evaluation results itself in generator expressions.
630 .. genex:: $<TARGET_GENEX_EVAL:tgt,expr>
632 .. versionadded:: 3.12
634 Content of ``expr`` evaluated as a generator expression in the context of
635 ``tgt`` target. This enables consumption of custom target properties that
636 themselves contain generator expressions.
638 Having the capability to evaluate generator expressions is very useful when
639 you want to manage custom properties supporting generator expressions.
642 .. code-block:: cmake
646 set_property(TARGET foo PROPERTY
647 CUSTOM_KEYS $<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>
650 add_custom_target(printFooKeys
651 COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_PROPERTY:foo,CUSTOM_KEYS>
654 This naive implementation of the ``printFooKeys`` custom command is wrong
655 because ``CUSTOM_KEYS`` target property is not evaluated and the content
656 is passed as is (i.e. ``$<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>``).
658 To have the expected result (i.e. ``FOO_EXTRA_THINGS`` if config is
659 ``Debug``), it is required to evaluate the output of
660 ``$<TARGET_PROPERTY:foo,CUSTOM_KEYS>``:
662 .. code-block:: cmake
664 add_custom_target(printFooKeys
665 COMMAND ${CMAKE_COMMAND} -E
666 echo $<TARGET_GENEX_EVAL:foo,$<TARGET_PROPERTY:foo,CUSTOM_KEYS>>
676 .. genex:: $<CONFIGURATION>
678 Configuration name. Deprecated since CMake 3.0. Use ``CONFIG`` instead.
680 .. genex:: $<PLATFORM_ID>
682 The current system's CMake platform id.
683 See also the :variable:`CMAKE_SYSTEM_NAME` variable.
685 .. genex:: $<C_COMPILER_ID>
687 The CMake's compiler id of the C compiler used.
688 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
690 .. genex:: $<CXX_COMPILER_ID>
692 The CMake's compiler id of the CXX compiler used.
693 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
695 .. genex:: $<CUDA_COMPILER_ID>
697 The CMake's compiler id of the CUDA compiler used.
698 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
700 .. genex:: $<OBJC_COMPILER_ID>
702 .. versionadded:: 3.16
704 The CMake's compiler id of the OBJC compiler used.
705 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
707 .. genex:: $<OBJCXX_COMPILER_ID>
709 .. versionadded:: 3.16
711 The CMake's compiler id of the OBJCXX compiler used.
712 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
714 .. genex:: $<Fortran_COMPILER_ID>
716 The CMake's compiler id of the Fortran compiler used.
717 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
719 .. genex:: $<HIP_COMPILER_ID>
721 .. versionadded:: 3.21
723 The CMake's compiler id of the HIP compiler used.
724 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
726 .. genex:: $<ISPC_COMPILER_ID>
728 .. versionadded:: 3.19
730 The CMake's compiler id of the ISPC compiler used.
731 See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
733 .. genex:: $<C_COMPILER_VERSION>
735 The version of the C compiler used.
736 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
738 .. genex:: $<CXX_COMPILER_VERSION>
740 The version of the CXX compiler used.
741 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
743 .. genex:: $<CUDA_COMPILER_VERSION>
745 The version of the CUDA compiler used.
746 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
748 .. genex:: $<OBJC_COMPILER_VERSION>
750 .. versionadded:: 3.16
752 The version of the OBJC compiler used.
753 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
755 .. genex:: $<OBJCXX_COMPILER_VERSION>
757 .. versionadded:: 3.16
759 The version of the OBJCXX compiler used.
760 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
762 .. genex:: $<Fortran_COMPILER_VERSION>
764 The version of the Fortran compiler used.
765 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
767 .. genex:: $<HIP_COMPILER_VERSION>
769 .. versionadded:: 3.21
771 The version of the HIP compiler used.
772 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
774 .. genex:: $<ISPC_COMPILER_VERSION>
776 .. versionadded:: 3.19
778 The version of the ISPC compiler used.
779 See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
781 .. genex:: $<COMPILE_LANGUAGE>
783 .. versionadded:: 3.3
785 The compile language of source files when evaluating compile options.
786 See :ref:`the related boolean expression
787 <Boolean COMPILE_LANGUAGE Generator Expression>`
788 ``$<COMPILE_LANGUAGE:language>``
789 for notes about the portability of this generator expression.
791 .. genex:: $<LINK_LANGUAGE>
793 .. versionadded:: 3.18
795 The link language of target when evaluating link options.
796 See :ref:`the related boolean expression
797 <Boolean LINK_LANGUAGE Generator Expression>` ``$<LINK_LANGUAGE:language>``
798 for notes about the portability of this generator expression.
802 This generator expression is not supported by the link libraries
803 properties to avoid side-effects due to the double evaluation of
806 .. _`Target-Dependent Queries`:
808 Target-Dependent Queries
809 ------------------------
811 These queries refer to a target ``tgt``. This can be any runtime artifact,
814 * an executable target created by :command:`add_executable`
815 * a shared library target (``.so``, ``.dll`` but not their ``.lib`` import library)
816 created by :command:`add_library`
817 * a static library target created by :command:`add_library`
819 In the following, "the ``tgt`` filename" means the name of the ``tgt``
820 binary file. This has to be distinguished from "the target name",
821 which is just the string ``tgt``.
823 .. genex:: $<TARGET_NAME_IF_EXISTS:tgt>
825 .. versionadded:: 3.12
827 The target name ``tgt`` if the target exists, an empty string otherwise.
829 Note that ``tgt`` is not added as a dependency of the target this
830 expression is evaluated on.
832 .. genex:: $<TARGET_FILE:tgt>
834 Full path to the ``tgt`` binary file.
836 .. genex:: $<TARGET_FILE_BASE_NAME:tgt>
838 .. versionadded:: 3.15
840 Base name of ``tgt``, i.e. ``$<TARGET_FILE_NAME:tgt>`` without prefix and
842 For example, if the ``tgt`` filename is ``libbase.so``, the base name is ``base``.
844 See also the :prop_tgt:`OUTPUT_NAME`, :prop_tgt:`ARCHIVE_OUTPUT_NAME`,
845 :prop_tgt:`LIBRARY_OUTPUT_NAME` and :prop_tgt:`RUNTIME_OUTPUT_NAME`
846 target properties and their configuration specific variants
847 :prop_tgt:`OUTPUT_NAME_<CONFIG>`, :prop_tgt:`ARCHIVE_OUTPUT_NAME_<CONFIG>`,
848 :prop_tgt:`LIBRARY_OUTPUT_NAME_<CONFIG>` and
849 :prop_tgt:`RUNTIME_OUTPUT_NAME_<CONFIG>`.
851 The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
852 properties can also be considered.
854 Note that ``tgt`` is not added as a dependency of the target this
855 expression is evaluated on.
857 .. genex:: $<TARGET_FILE_PREFIX:tgt>
859 .. versionadded:: 3.15
861 Prefix of the ``tgt`` filename (such as ``lib``).
863 See also the :prop_tgt:`PREFIX` target property.
865 Note that ``tgt`` is not added as a dependency of the target this
866 expression is evaluated on.
868 .. genex:: $<TARGET_FILE_SUFFIX:tgt>
870 .. versionadded:: 3.15
872 Suffix of the ``tgt`` filename (extension such as ``.so`` or ``.exe``).
874 See also the :prop_tgt:`SUFFIX` target property.
876 Note that ``tgt`` is not added as a dependency of the target this
877 expression is evaluated on.
879 .. genex:: $<TARGET_FILE_NAME:tgt>
881 The ``tgt`` filename.
883 Note that ``tgt`` is not added as a dependency of the target this
884 expression is evaluated on (see policy :policy:`CMP0112`).
886 .. genex:: $<TARGET_FILE_DIR:tgt>
888 Directory of the ``tgt`` binary file.
890 Note that ``tgt`` is not added as a dependency of the target this
891 expression is evaluated on (see policy :policy:`CMP0112`).
893 .. genex:: $<TARGET_LINKER_FILE:tgt>
895 File used when linking to the ``tgt`` target. This will usually
896 be the library that ``tgt`` represents (``.a``, ``.lib``, ``.so``),
897 but for a shared library on DLL platforms, it would be the ``.lib``
898 import library associated with the DLL.
900 .. genex:: $<TARGET_LINKER_FILE_BASE_NAME:tgt>
902 .. versionadded:: 3.15
904 Base name of file used to link the target ``tgt``, i.e.
905 ``$<TARGET_LINKER_FILE_NAME:tgt>`` without prefix and suffix. For example,
906 if target file name is ``libbase.a``, the base name is ``base``.
908 See also the :prop_tgt:`OUTPUT_NAME`, :prop_tgt:`ARCHIVE_OUTPUT_NAME`,
909 and :prop_tgt:`LIBRARY_OUTPUT_NAME` target properties and their configuration
910 specific variants :prop_tgt:`OUTPUT_NAME_<CONFIG>`,
911 :prop_tgt:`ARCHIVE_OUTPUT_NAME_<CONFIG>` and
912 :prop_tgt:`LIBRARY_OUTPUT_NAME_<CONFIG>`.
914 The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
915 properties can also be considered.
917 Note that ``tgt`` is not added as a dependency of the target this
918 expression is evaluated on.
920 .. genex:: $<TARGET_LINKER_FILE_PREFIX:tgt>
922 .. versionadded:: 3.15
924 Prefix of file used to link target ``tgt``.
926 See also the :prop_tgt:`PREFIX` and :prop_tgt:`IMPORT_PREFIX` target
929 Note that ``tgt`` is not added as a dependency of the target this
930 expression is evaluated on.
932 .. genex:: $<TARGET_LINKER_FILE_SUFFIX:tgt>
934 .. versionadded:: 3.15
936 Suffix of file used to link where ``tgt`` is the name of a target.
938 The suffix corresponds to the file extension (such as ".so" or ".lib").
940 See also the :prop_tgt:`SUFFIX` and :prop_tgt:`IMPORT_SUFFIX` target
943 Note that ``tgt`` is not added as a dependency of the target this
944 expression is evaluated on.
946 .. genex:: $<TARGET_LINKER_FILE_NAME:tgt>
948 Name of file used to link target ``tgt``.
950 Note that ``tgt`` is not added as a dependency of the target this
951 expression is evaluated on (see policy :policy:`CMP0112`).
953 .. genex:: $<TARGET_LINKER_FILE_DIR:tgt>
955 Directory of file used to link target ``tgt``.
957 Note that ``tgt`` is not added as a dependency of the target this
958 expression is evaluated on (see policy :policy:`CMP0112`).
960 .. genex:: $<TARGET_SONAME_FILE:tgt>
962 File with soname (``.so.3``) where ``tgt`` is the name of a target.
963 .. genex:: $<TARGET_SONAME_FILE_NAME:tgt>
965 Name of file with soname (``.so.3``).
967 Note that ``tgt`` is not added as a dependency of the target this
968 expression is evaluated on (see policy :policy:`CMP0112`).
970 .. genex:: $<TARGET_SONAME_FILE_DIR:tgt>
972 Directory of with soname (``.so.3``).
974 Note that ``tgt`` is not added as a dependency of the target this
975 expression is evaluated on (see policy :policy:`CMP0112`).
977 .. genex:: $<TARGET_PDB_FILE:tgt>
979 .. versionadded:: 3.1
981 Full path to the linker generated program database file (.pdb)
982 where ``tgt`` is the name of a target.
984 See also the :prop_tgt:`PDB_NAME` and :prop_tgt:`PDB_OUTPUT_DIRECTORY`
985 target properties and their configuration specific variants
986 :prop_tgt:`PDB_NAME_<CONFIG>` and :prop_tgt:`PDB_OUTPUT_DIRECTORY_<CONFIG>`.
988 .. genex:: $<TARGET_PDB_FILE_BASE_NAME:tgt>
990 .. versionadded:: 3.15
992 Base name of the linker generated program database file (.pdb)
993 where ``tgt`` is the name of a target.
995 The base name corresponds to the target PDB file name (see
996 ``$<TARGET_PDB_FILE_NAME:tgt>``) without prefix and suffix. For example,
997 if target file name is ``base.pdb``, the base name is ``base``.
999 See also the :prop_tgt:`PDB_NAME` target property and its configuration
1000 specific variant :prop_tgt:`PDB_NAME_<CONFIG>`.
1002 The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
1003 properties can also be considered.
1005 Note that ``tgt`` is not added as a dependency of the target this
1006 expression is evaluated on.
1008 .. genex:: $<TARGET_PDB_FILE_NAME:tgt>
1010 .. versionadded:: 3.1
1012 Name of the linker generated program database file (.pdb).
1014 Note that ``tgt`` is not added as a dependency of the target this
1015 expression is evaluated on (see policy :policy:`CMP0112`).
1017 .. genex:: $<TARGET_PDB_FILE_DIR:tgt>
1019 .. versionadded:: 3.1
1021 Directory of the linker generated program database file (.pdb).
1023 Note that ``tgt`` is not added as a dependency of the target this
1024 expression is evaluated on (see policy :policy:`CMP0112`).
1026 .. genex:: $<TARGET_BUNDLE_DIR:tgt>
1028 .. versionadded:: 3.9
1030 Full path to the bundle directory (``my.app``, ``my.framework``, or
1031 ``my.bundle``) where ``tgt`` is the name of a target.
1033 Note that ``tgt`` is not added as a dependency of the target this
1034 expression is evaluated on (see policy :policy:`CMP0112`).
1036 .. genex:: $<TARGET_BUNDLE_CONTENT_DIR:tgt>
1038 .. versionadded:: 3.9
1040 Full path to the bundle content directory where ``tgt`` is the name of a
1041 target. For the macOS SDK it leads to ``my.app/Contents``, ``my.framework``,
1042 or ``my.bundle/Contents``. For all other SDKs (e.g. iOS) it leads to
1043 ``my.app``, ``my.framework``, or ``my.bundle`` due to the flat bundle
1046 Note that ``tgt`` is not added as a dependency of the target this
1047 expression is evaluated on (see policy :policy:`CMP0112`).
1049 .. genex:: $<TARGET_PROPERTY:tgt,prop>
1051 Value of the property ``prop`` on the target ``tgt``.
1053 Note that ``tgt`` is not added as a dependency of the target this
1054 expression is evaluated on.
1056 .. genex:: $<TARGET_PROPERTY:prop>
1058 Value of the property ``prop`` on the target for which the expression
1059 is being evaluated. Note that for generator expressions in
1060 :ref:`Target Usage Requirements` this is the consuming target rather
1061 than the target specifying the requirement.
1063 .. genex:: $<TARGET_RUNTIME_DLLS:tgt>
1065 .. versionadded:: 3.21
1067 List of DLLs that the target depends on at runtime. This is determined by
1068 the locations of all the ``SHARED`` targets in the target's transitive
1069 dependencies. Using this generator expression on targets other than
1070 executables, ``SHARED`` libraries, and ``MODULE`` libraries is an error. On
1071 non-DLL platforms, it evaluates to an empty string.
1073 This generator expression can be used to copy all of the DLLs that a target
1074 depends on into its output directory in a ``POST_BUILD`` custom command. For
1077 .. code-block:: cmake
1079 find_package(foo CONFIG REQUIRED) # package generated by install(EXPORT)
1081 add_executable(exe main.c)
1082 target_link_libraries(exe PRIVATE foo::foo foo::bar)
1083 add_custom_command(TARGET exe POST_BUILD
1084 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:exe> $<TARGET_FILE_DIR:exe>
1085 COMMAND_EXPAND_LISTS
1090 :ref:`Imported Targets` are supported only if they know the location
1091 of their ``.dll`` files. An imported ``SHARED`` library must have
1092 :prop_tgt:`IMPORTED_LOCATION` set to its ``.dll`` file. See the
1093 :ref:`add_library imported libraries <add_library imported libraries>`
1094 section for details. Many :ref:`Find Modules` produce imported targets
1095 with the ``UNKNOWN`` type and therefore will be ignored.
1097 .. genex:: $<INSTALL_PREFIX>
1099 Content of the install prefix when the target is exported via
1100 :command:`install(EXPORT)`, or when evaluated in the
1101 :prop_tgt:`INSTALL_NAME_DIR` property or the ``INSTALL_NAME_DIR`` argument of
1102 :command:`install(RUNTIME_DEPENDENCY_SET)`, and empty otherwise.
1104 Output-Related Expressions
1105 --------------------------
1107 .. genex:: $<TARGET_NAME:...>
1109 Marks ``...`` as being the name of a target. This is required if exporting
1110 targets to multiple dependent export sets. The ``...`` must be a literal
1111 name of a target- it may not contain generator expressions.
1113 .. genex:: $<LINK_ONLY:...>
1115 .. versionadded:: 3.1
1117 Content of ``...`` except when evaluated in a link interface while
1118 propagating :ref:`Target Usage Requirements`, in which case it is the
1120 Intended for use only in an :prop_tgt:`INTERFACE_LINK_LIBRARIES` target
1121 property, perhaps via the :command:`target_link_libraries` command,
1122 to specify private link dependencies without other usage requirements.
1124 .. genex:: $<INSTALL_INTERFACE:...>
1126 Content of ``...`` when the property is exported using :command:`install(EXPORT)`,
1127 and empty otherwise.
1129 .. genex:: $<BUILD_INTERFACE:...>
1131 Content of ``...`` when the property is exported using :command:`export`, or
1132 when the target is used by another target in the same buildsystem. Expands to
1133 the empty string otherwise.
1135 .. genex:: $<MAKE_C_IDENTIFIER:...>
1137 Content of ``...`` converted to a C identifier. The conversion follows the
1138 same behavior as :command:`string(MAKE_C_IDENTIFIER)`.
1140 .. genex:: $<TARGET_OBJECTS:objLib>
1142 .. versionadded:: 3.1
1144 List of objects resulting from build of ``objLib``.
1146 .. genex:: $<SHELL_PATH:...>
1148 .. versionadded:: 3.4
1150 Content of ``...`` converted to shell path style. For example, slashes are
1151 converted to backslashes in Windows shells and drive letters are converted
1152 to posix paths in MSYS shells. The ``...`` must be an absolute path.
1154 .. versionadded:: 3.14
1155 The ``...`` may be a :ref:`semicolon-separated list <CMake Language Lists>`
1156 of paths, in which case each path is converted individually and a result
1157 list is generated using the shell path separator (``:`` on POSIX and
1158 ``;`` on Windows). Be sure to enclose the argument containing this genex
1159 in double quotes in CMake source code so that ``;`` does not split arguments.
1161 .. genex:: $<OUTPUT_CONFIG:...>
1163 .. versionadded:: 3.20
1165 Only valid in :command:`add_custom_command` and :command:`add_custom_target`
1166 as the outer-most generator expression in an argument.
1167 With the :generator:`Ninja Multi-Config` generator, generator expressions
1168 in ``...`` are evaluated using the custom command's "output config".
1169 With other generators, the content of ``...`` is evaluated normally.
1171 .. genex:: $<COMMAND_CONFIG:...>
1173 .. versionadded:: 3.20
1175 Only valid in :command:`add_custom_command` and :command:`add_custom_target`
1176 as the outer-most generator expression in an argument.
1177 With the :generator:`Ninja Multi-Config` generator, generator expressions
1178 in ``...`` are evaluated using the custom command's "command config".
1179 With other generators, the content of ``...`` is evaluated normally.
1184 Since generator expressions are evaluated during generation of the buildsystem,
1185 and not during processing of ``CMakeLists.txt`` files, it is not possible to
1186 inspect their result with the :command:`message()` command.
1188 One possible way to generate debug messages is to add a custom target,
1190 .. code-block:: cmake
1192 add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")
1194 The shell command ``make genexdebug`` (invoked after execution of ``cmake``)
1195 would then print the result of ``$<...>``.
1197 Another way is to write debug messages to a file:
1199 .. code-block:: cmake
1201 file(GENERATE OUTPUT filename CONTENT "$<...>")