Contents
Generator expressions are evaluated during build system generation to produce information specific to each build configuration.
Generator expressions are allowed in the context of many target properties, such as LINK_LIBRARIES, INCLUDE_DIRECTORIES, COMPILE_DEFINITIONS and others. They may also be used when using commands to populate those properties, such as target_link_libraries(), target_include_directories(), target_compile_definitions() and others.
They enable conditional linking, conditional definitions used when compiling, conditional include directories, and more. The conditions may be based on the build configuration, target properties, platform information or any other queryable information.
Generator expressions have the form $<...>. To avoid confusion, this page deviates from most of the CMake documentation in that it omits angular brackets <...> around placeholders like condition, string, target, among others.
Generator expressions can be nested, as shown in most of the examples below.
Boolean expressions evaluate to either 0 or 1. They are typically used to construct the condition in a conditional generator expression.
Available boolean expressions are:
Converts string to 0 or 1. Evaluates to 0 if any of the following is true:
Otherwise evaluates to 1.
1 if string1 and string2 are equal, else 0. The comparison is case-sensitive. For a case-insensitive comparison, combine with a string transforming generator expression,
$<STREQUAL:$<UPPER_CASE:${foo}>,"BAR"> # "1" if ${foo} is any of "BAR", "Bar", "bar", ...
1 when the language used for compilation unit matches language and the CMake’s compiler id of the language compiler matches any one of the entries in compiler_ids, otherwise 0. This expression is a short form for the combination of $<COMPILE_LANGUAGE:language> and $<LANG_COMPILER_ID:compiler_ids>. This expression may be used to specify compile options, compile definitions, and include directories for source files of a particular language and compiler combination in a target. For example:
add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
target_compile_definitions(myapp
PRIVATE $<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang>:COMPILING_CXX_WITH_CLANG>
$<$<COMPILE_LANG_AND_ID:CXX,Intel>:COMPILING_CXX_WITH_INTEL>
$<$<COMPILE_LANG_AND_ID:C,Clang>:COMPILING_C_WITH_CLANG>
)
This specifies the use of different compile definitions based on both the compiler id and compilation language. This example will have a COMPILING_CXX_WITH_CLANG compile definition when Clang is the CXX compiler, and COMPILING_CXX_WITH_INTEL when Intel is the CXX compiler. Likewise when the C compiler is Clang it will only see the COMPILING_C_WITH_CLANG definition.
Without the COMPILE_LANG_AND_ID generator expression the same logic would be expressed as:
target_compile_definitions(myapp
PRIVATE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:AppleClang,Clang>>:COMPILING_CXX_WITH_CLANG>
$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:Intel>>:COMPILING_CXX_WITH_INTEL>
$<$<AND:$<COMPILE_LANGUAGE:C>,$<C_COMPILER_ID:Clang>>:COMPILING_C_WITH_CLANG>
)
1 when the language used for compilation unit matches any of the entries in languages, otherwise 0. This expression may be used to specify compile options, compile definitions, and include directories for source files of a particular language in a target. For example:
add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
target_compile_options(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
)
target_compile_definitions(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
$<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
)
target_include_directories(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX,CUDA>:/opt/foo/headers>
)
This specifies the use of the -fno-exceptions compile option, COMPILING_CXX compile definition, and cxx_headers include directory for C++ only (compiler id checks elided). It also specifies a COMPILING_CUDA compile definition for CUDA.
Note that with Visual Studio Generators and Xcode there is no way to represent target-wide compile definitions or include directories separately for C and CXX languages. Also, with Visual Studio Generators there is no way to represent target-wide flags separately for C and CXX languages. Under these generators, expressions for both C and C++ sources will be evaluated using CXX if there are any C++ sources and otherwise using C. A workaround is to create separate libraries for each source file language instead:
add_library(myapp_c foo.c)
add_library(myapp_cxx bar.cpp)
target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
add_executable(myapp main.cpp)
target_link_libraries(myapp myapp_c myapp_cxx)
1 when the language used for link step matches language and the CMake’s compiler id of the language linker matches any one of the entries in compiler_ids, otherwise 0. This expression is a short form for the combination of $<LINK_LANGUAGE:language> and $<LANG_COMPILER_ID:compiler_ids>. This expression may be used to specify link libraries, link options, link directories and link dependencies of a particular language and linker combination in a target. For example:
add_library(libC_Clang ...)
add_library(libCXX_Clang ...)
add_library(libC_Intel ...)
add_library(libCXX_Intel ...)
add_executable(myapp main.c)
if (CXX_CONFIG)
target_sources(myapp PRIVATE file.cxx)
endif()
target_link_libraries(myapp
PRIVATE $<$<LINK_LANG_AND_ID:CXX,Clang,AppleClang>:libCXX_Clang>
$<$<LINK_LANG_AND_ID:C,Clang,AppleClang>:libC_Clang>
$<$<LINK_LANG_AND_ID:CXX,Intel>:libCXX_Intel>
$<$<LINK_LANG_AND_ID:C,Intel>:libC_Intel>)
This specifies the use of different link libraries based on both the compiler id and link language. This example will have target libCXX_Clang as link dependency when Clang or AppleClang is the CXX linker, and libCXX_Intel when Intel is the CXX linker. Likewise when the C linker is Clang or AppleClang, target libC_Clang will be added as link dependency and libC_Intel when Intel is the C linker.
See the note related to $<LINK_LANGUAGE:language> for constraints about the usage of this generator expression.
1 when the language used for link step matches any of the entries in languages, otherwise 0. This expression may be used to specify link libraries, link options, link directories and link dependencies of a particular language in a target. For example:
add_library(api_C ...)
add_library(api_CXX ...)
add_library(api INTERFACE)
target_link_options(api INTERFACE $<$<LINK_LANGUAGE:C>:-opt_c>
$<$<LINK_LANGUAGE:CXX>:-opt_cxx>)
target_link_libraries(api INTERFACE $<$<LINK_LANGUAGE:C>:api_C>
$<$<LINK_LANGUAGE:CXX>:api_CXX>)
add_executable(myapp1 main.c)
target_link_options(myapp1 PRIVATE api)
add_executable(myapp2 main.cpp)
target_link_options(myapp2 PRIVATE api)
This specifies to use the api target for linking targets myapp1 and myapp2. In practice, myapp1 will link with target api_C and option -opt_c because it will use C as link language. And myapp2 will link with api_CXX and option -opt_cxx because CXX will be the link language.
Note
To determine the link language of a target, it is required to collect, transitively, all the targets which will be linked to it. So, for link libraries properties, a double evaluation will be done. During the first evaluation, $<LINK_LANGUAGE:..> expressions will always return 0. The link language computed after this first pass will be used to do the second pass. To avoid inconsistency, it is required that the second pass do not change the link language. Moreover, to avoid unexpected side-effects, it is required to specify complete entities as part of the $<LINK_LANGUAGE:..> expression. For example:
add_library(lib STATIC file.cxx)
add_library(libother STATIC file.c)
# bad usage
add_executable(myapp1 main.c)
target_link_libraries(myapp1 PRIVATE lib$<$<LINK_LANGUAGE:C>:other>)
# correct usage
add_executable(myapp2 main.c)
target_link_libraries(myapp2 PRIVATE $<$<LINK_LANGUAGE:C>:libother>)
In this example, for myapp1, the first pass will, unexpectedly, determine that the link language is CXX because the evaluation of the generator expression will be an empty string so myapp1 will depends on target lib which is C++. On the contrary, for myapp2, the first evaluation will give C as link language, so the second pass will correctly add target libother as link dependency.
These expressions expand to some string. For example,
include_directories(/usr/include/$<CXX_COMPILER_ID>/)
expands to /usr/include/GNU/ or /usr/include/Clang/ etc, depending on the compiler identifier.
String-valued expressions may also be combined with other expressions. Here an example for a string-valued expression within a boolean expressions within a conditional expression:
$<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
expands to OLD_COMPILER if the CMAKE_CXX_COMPILER_VERSION is less than 4.2.0.
And here two nested string-valued expressions:
-I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
generates a string of the entries in the INCLUDE_DIRECTORIES target property with each entry preceded by -I.
Expanding on the previous example, if one first wants to check if the INCLUDE_DIRECTORIES property is non-empty, then it is advisable to introduce a helper variable to keep the code readable:
set(prop "$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>") # helper variable
$<$<BOOL:${prop}>:-I$<JOIN:${prop}, -I>>
The following string-valued generator expressions are available:
String literals to escape the special meaning a character would otherwise have:
Conditional generator expressions depend on a boolean condition that must be 0 or 1.
Typically, the condition is a boolean generator expression. For instance,
$<$<CONFIG:Debug>:DEBUG_MODE>
expands to DEBUG_MODE when the Debug configuration is used, and otherwise expands to the empty string.
Content of expr evaluated as a generator expression in the context of tgt target. This enables consumption of custom target properties that themselves contain generator expressions.
Having the capability to evaluate generator expressions is very useful when you want to manage custom properties supporting generator expressions. For example:
add_library(foo ...)
set_property(TARGET foo PROPERTY
CUSTOM_KEYS $<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>
)
add_custom_target(printFooKeys
COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_PROPERTY:foo,CUSTOM_KEYS>
)
This naive implementation of the printFooKeys custom command is wrong because CUSTOM_KEYS target property is not evaluated and the content is passed as is (i.e. $<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>).
To have the expected result (i.e. FOO_EXTRA_THINGS if config is Debug), it is required to evaluate the output of $<TARGET_PROPERTY:foo,CUSTOM_KEYS>:
add_custom_target(printFooKeys
COMMAND ${CMAKE_COMMAND} -E
echo $<TARGET_GENEX_EVAL:foo,$<TARGET_PROPERTY:foo,CUSTOM_KEYS>>
)
The link language of target when evaluating link options. See the related boolean expression $<LINK_LANGUAGE:language> for notes about the portability of this generator expression.
Note
This generator expression is not supported by the link libraries properties to avoid side-effects due to the double evaluation of these properties.
These queries refer to a target tgt. This can be any runtime artifact, namely:
In the following, “the tgt filename” means the name of the tgt binary file. This has to be distinguished from “the target name”, which is just the string tgt.
Base name of tgt, i.e. $<TARGET_FILE_NAME:tgt> without prefix and suffix. For example, if the tgt filename is libbase.so, the base name is base.
See also the OUTPUT_NAME, ARCHIVE_OUTPUT_NAME, LIBRARY_OUTPUT_NAME and RUNTIME_OUTPUT_NAME target properties and their configuration specific variants OUTPUT_NAME_<CONFIG>, ARCHIVE_OUTPUT_NAME_<CONFIG>, LIBRARY_OUTPUT_NAME_<CONFIG> and RUNTIME_OUTPUT_NAME_<CONFIG>.
The <CONFIG>_POSTFIX and DEBUG_POSTFIX target properties can also be considered.
Note that tgt is not added as a dependency of the target this expression is evaluated on.
Prefix of the tgt filename (such as lib).
See also the PREFIX target property.
Note that tgt is not added as a dependency of the target this expression is evaluated on.
Suffix of the tgt filename (extension such as .so or .exe).
See also the SUFFIX target property.
Note that tgt is not added as a dependency of the target this expression is evaluated on.
Base name of file used to link the target tgt, i.e. $<TARGET_LINKER_FILE_NAME:tgt> without prefix and suffix. For example, if target file name is libbase.a, the base name is base.
See also the OUTPUT_NAME, ARCHIVE_OUTPUT_NAME, and LIBRARY_OUTPUT_NAME target properties and their configuration specific variants OUTPUT_NAME_<CONFIG>, ARCHIVE_OUTPUT_NAME_<CONFIG> and LIBRARY_OUTPUT_NAME_<CONFIG>.
The <CONFIG>_POSTFIX and DEBUG_POSTFIX target properties can also be considered.
Note that tgt is not added as a dependency of the target this expression is evaluated on.
Prefix of file used to link target tgt.
See also the PREFIX and IMPORT_PREFIX target properties.
Note that tgt is not added as a dependency of the target this expression is evaluated on.
Suffix of file used to link where tgt is the name of a target.
The suffix corresponds to the file extension (such as ”.so” or ”.lib”).
See also the SUFFIX and IMPORT_SUFFIX target properties.
Note that tgt is not added as a dependency of the target this expression is evaluated on.
Full path to the linker generated program database file (.pdb) where tgt is the name of a target.
See also the PDB_NAME and PDB_OUTPUT_DIRECTORY target properties and their configuration specific variants PDB_NAME_<CONFIG> and PDB_OUTPUT_DIRECTORY_<CONFIG>.
Base name of the linker generated program database file (.pdb) where tgt is the name of a target.
The base name corresponds to the target PDB file name (see $<TARGET_PDB_FILE_NAME:tgt>) without prefix and suffix. For example, if target file name is base.pdb, the base name is base.
See also the PDB_NAME target property and its configuration specific variant PDB_NAME_<CONFIG>.
The <CONFIG>_POSTFIX and DEBUG_POSTFIX target properties can also be considered.
Note that tgt is not added as a dependency of the target this expression is evaluated on.
Value of the property prop on the target tgt.
Note that tgt is not added as a dependency of the target this expression is evaluated on.
Since generator expressions are evaluated during generation of the buildsystem, and not during processing of CMakeLists.txt files, it is not possible to inspect their result with the message() command.
One possible way to generate debug messages is to add a custom target,
add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")
The shell command make genexdebug (invoked after execution of cmake) would then print the result of $<...>.
Another way is to write debug messages to a file:
file(GENERATE OUTPUT filename CONTENT "$<...>")