cmake-generator-expressions.7.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. .. cmake-manual-description: CMake Generator Expressions
  2. cmake-generator-expressions(7)
  3. ******************************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. Generator expressions are evaluated during build system generation to produce
  9. information specific to each build configuration.
  10. Generator expressions are allowed in the context of many target properties,
  11. such as :prop_tgt:`LINK_LIBRARIES`, :prop_tgt:`INCLUDE_DIRECTORIES`,
  12. :prop_tgt:`COMPILE_DEFINITIONS` and others. They may also be used when using
  13. commands to populate those properties, such as :command:`target_link_libraries`,
  14. :command:`target_include_directories`, :command:`target_compile_definitions`
  15. and others.
  16. This means that they enable conditional linking, conditional
  17. definitions used when compiling, and conditional include directories and
  18. more. The conditions may be based on the build configuration, target
  19. properties, platform information or any other queryable information.
  20. Logical Expressions
  21. ===================
  22. Logical expressions are used to create conditional output. The basic
  23. expressions are the ``0`` and ``1`` expressions. Because other logical
  24. expressions evaluate to either ``0`` or ``1``, they can be composed to
  25. create conditional output:
  26. .. code-block:: cmake
  27. $<$<CONFIG:Debug>:DEBUG_MODE>
  28. expands to ``DEBUG_MODE`` when the ``Debug`` configuration is used, and
  29. otherwise expands to nothing.
  30. Available logical expressions are:
  31. ``$<BOOL:...>``
  32. ``1`` if the ``...`` is true, else ``0``
  33. ``$<AND:?[,?]...>``
  34. ``1`` if all ``?`` are ``1``, else ``0``
  35. The ``?`` must always be either ``0`` or ``1`` in boolean expressions.
  36. ``$<OR:?[,?]...>``
  37. ``0`` if all ``?`` are ``0``, else ``1``
  38. ``$<NOT:?>``
  39. ``0`` if ``?`` is ``1``, else ``1``
  40. ``$<IF:?,true-value...,false-value...>``
  41. ``true-value...`` if ``?`` is ``1``, ``false-value...`` if ``?`` is ``0``
  42. ``$<STREQUAL:a,b>``
  43. ``1`` if ``a`` is STREQUAL ``b``, else ``0``
  44. ``$<EQUAL:a,b>``
  45. ``1`` if ``a`` is EQUAL ``b`` in a numeric comparison, else ``0``
  46. ``$<IN_LIST:a,b>``
  47. ``1`` if ``a`` is IN_LIST ``b``, else ``0``
  48. ``$<TARGET_EXISTS:tgt>``
  49. ``1`` if ``tgt`` is an existed target name, else ``0``.
  50. ``$<CONFIG:cfg>``
  51. ``1`` if config is ``cfg``, else ``0``. This is a case-insensitive comparison.
  52. The mapping in :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` is also considered by
  53. this expression when it is evaluated on a property on an :prop_tgt:`IMPORTED`
  54. target.
  55. ``$<PLATFORM_ID:comp>``
  56. ``1`` if the CMake-id of the platform matches ``comp``, otherwise ``0``.
  57. See also the :variable:`CMAKE_SYSTEM_NAME` variable.
  58. ``$<C_COMPILER_ID:comp>``
  59. ``1`` if the CMake-id of the C compiler matches ``comp``, otherwise ``0``.
  60. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  61. ``$<CXX_COMPILER_ID:comp>``
  62. ``1`` if the CMake-id of the CXX compiler matches ``comp``, otherwise ``0``.
  63. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  64. ``$<VERSION_LESS:v1,v2>``
  65. ``1`` if ``v1`` is a version less than ``v2``, else ``0``.
  66. ``$<VERSION_GREATER:v1,v2>``
  67. ``1`` if ``v1`` is a version greater than ``v2``, else ``0``.
  68. ``$<VERSION_EQUAL:v1,v2>``
  69. ``1`` if ``v1`` is the same version as ``v2``, else ``0``.
  70. ``$<VERSION_LESS_EQUAL:v1,v2>``
  71. ``1`` if ``v1`` is a version less than or equal to ``v2``, else ``0``.
  72. ``$<VERSION_GREATER_EQUAL:v1,v2>``
  73. ``1`` if ``v1`` is a version greater than or equal to ``v2``, else ``0``.
  74. ``$<C_COMPILER_VERSION:ver>``
  75. ``1`` if the version of the C compiler matches ``ver``, otherwise ``0``.
  76. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  77. ``$<CXX_COMPILER_VERSION:ver>``
  78. ``1`` if the version of the CXX compiler matches ``ver``, otherwise ``0``.
  79. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  80. ``$<TARGET_POLICY:pol>``
  81. ``1`` if the policy ``pol`` was NEW when the 'head' target was created,
  82. else ``0``. If the policy was not set, the warning message for the policy
  83. will be emitted. This generator expression only works for a subset of
  84. policies.
  85. ``$<COMPILE_FEATURES:feature[,feature]...>``
  86. ``1`` if all of the ``feature`` features are available for the 'head'
  87. target, and ``0`` otherwise. If this expression is used while evaluating
  88. the link implementation of a target and if any dependency transitively
  89. increases the required :prop_tgt:`C_STANDARD` or :prop_tgt:`CXX_STANDARD`
  90. for the 'head' target, an error is reported. See the
  91. :manual:`cmake-compile-features(7)` manual for information on
  92. compile features and a list of supported compilers.
  93. ``$<COMPILE_LANGUAGE:lang>``
  94. ``1`` when the language used for compilation unit matches ``lang``,
  95. otherwise ``0``. This expression may be used to specify compile options,
  96. compile definitions, and include directories for source files of a
  97. particular language in a target. For example:
  98. .. code-block:: cmake
  99. add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
  100. target_compile_options(myapp
  101. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
  102. )
  103. target_compile_definitions(myapp
  104. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
  105. $<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
  106. )
  107. target_include_directories(myapp
  108. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/opt/foo/cxx_headers>
  109. )
  110. This specifies the use of the ``-fno-exceptions`` compile option,
  111. ``COMPILING_CXX`` compile definition, and ``cxx_headers`` include
  112. directory for C++ only (compiler id checks elided). It also specifies
  113. a ``COMPILING_CUDA`` compile definition for CUDA.
  114. Note that with :ref:`Visual Studio Generators` and :generator:`Xcode` there
  115. is no way to represent target-wide compile definitions or include directories
  116. separately for ``C`` and ``CXX`` languages.
  117. Also, with :ref:`Visual Studio Generators` there is no way to represent
  118. target-wide flags separately for ``C`` and ``CXX`` languages. Under these
  119. generators, expressions for both C and C++ sources will be evaluated
  120. using ``CXX`` if there are any C++ sources and otherwise using ``C``.
  121. A workaround is to create separate libraries for each source file language
  122. instead:
  123. .. code-block:: cmake
  124. add_library(myapp_c foo.c)
  125. add_library(myapp_cxx bar.cpp)
  126. target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
  127. add_executable(myapp main.cpp)
  128. target_link_libraries(myapp myapp_c myapp_cxx)
  129. Informational Expressions
  130. =========================
  131. These expressions expand to some information. The information may be used
  132. directly, eg:
  133. .. code-block:: cmake
  134. include_directories(/usr/include/$<CXX_COMPILER_ID>/)
  135. expands to ``/usr/include/GNU/`` or ``/usr/include/Clang/`` etc, depending on
  136. the Id of the compiler.
  137. These expressions may also may be combined with logical expressions:
  138. .. code-block:: cmake
  139. $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
  140. expands to ``OLD_COMPILER`` if the
  141. :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
  142. than 4.2.0.
  143. Available informational expressions are:
  144. ``$<CONFIGURATION>``
  145. Configuration name. Deprecated. Use ``CONFIG`` instead.
  146. ``$<CONFIG>``
  147. Configuration name
  148. ``$<PLATFORM_ID>``
  149. The CMake-id of the platform.
  150. See also the :variable:`CMAKE_SYSTEM_NAME` variable.
  151. ``$<C_COMPILER_ID>``
  152. The CMake-id of the C compiler used.
  153. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  154. ``$<CXX_COMPILER_ID>``
  155. The CMake-id of the CXX compiler used.
  156. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  157. ``$<C_COMPILER_VERSION>``
  158. The version of the C compiler used.
  159. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  160. ``$<CXX_COMPILER_VERSION>``
  161. The version of the CXX compiler used.
  162. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  163. ``$<TARGET_FILE:tgt>``
  164. Full path to main file (.exe, .so.1.2, .a) where ``tgt`` is the name of a target.
  165. ``$<TARGET_FILE_NAME:tgt>``
  166. Name of main file (.exe, .so.1.2, .a).
  167. ``$<TARGET_FILE_DIR:tgt>``
  168. Directory of main file (.exe, .so.1.2, .a).
  169. ``$<TARGET_LINKER_FILE:tgt>``
  170. File used to link (.a, .lib, .so) where ``tgt`` is the name of a target.
  171. ``$<TARGET_LINKER_FILE_NAME:tgt>``
  172. Name of file used to link (.a, .lib, .so).
  173. ``$<TARGET_LINKER_FILE_DIR:tgt>``
  174. Directory of file used to link (.a, .lib, .so).
  175. ``$<TARGET_SONAME_FILE:tgt>``
  176. File with soname (.so.3) where ``tgt`` is the name of a target.
  177. ``$<TARGET_SONAME_FILE_NAME:tgt>``
  178. Name of file with soname (.so.3).
  179. ``$<TARGET_SONAME_FILE_DIR:tgt>``
  180. Directory of with soname (.so.3).
  181. ``$<TARGET_PDB_FILE:tgt>``
  182. Full path to the linker generated program database file (.pdb)
  183. where ``tgt`` is the name of a target.
  184. See also the :prop_tgt:`PDB_NAME` and :prop_tgt:`PDB_OUTPUT_DIRECTORY`
  185. target properties and their configuration specific variants
  186. :prop_tgt:`PDB_NAME_<CONFIG>` and :prop_tgt:`PDB_OUTPUT_DIRECTORY_<CONFIG>`.
  187. ``$<TARGET_PDB_FILE_NAME:tgt>``
  188. Name of the linker generated program database file (.pdb).
  189. ``$<TARGET_PDB_FILE_DIR:tgt>``
  190. Directory of the linker generated program database file (.pdb).
  191. ``$<TARGET_BUNDLE_DIR:tgt>``
  192. Full path to the bundle directory (``my.app``, ``my.framework``, or
  193. ``my.bundle``) where ``tgt`` is the name of a target.
  194. ``$<TARGET_BUNDLE_CONTENT_DIR:tgt>``
  195. Full path to the bundle content directory where ``tgt`` is the name of a
  196. target. For the macOS SDK it leads to ``my.app/Contents``, ``my.framework``,
  197. or ``my.bundle/Contents``. For all other SDKs (e.g. iOS) it leads to
  198. ``my.app``, ``my.framework``, or ``my.bundle`` due to the flat bundle
  199. structure.
  200. ``$<TARGET_PROPERTY:tgt,prop>``
  201. Value of the property ``prop`` on the target ``tgt``.
  202. Note that ``tgt`` is not added as a dependency of the target this
  203. expression is evaluated on.
  204. ``$<TARGET_PROPERTY:prop>``
  205. Value of the property ``prop`` on the target on which the generator
  206. expression is evaluated.
  207. ``$<INSTALL_PREFIX>``
  208. Content of the install prefix when the target is exported via
  209. :command:`install(EXPORT)` and empty otherwise.
  210. ``$<COMPILE_LANGUAGE>``
  211. The compile language of source files when evaluating compile options. See
  212. the unary version for notes about portability of this generator
  213. expression.
  214. Output Expressions
  215. ==================
  216. These expressions generate output, in some cases depending on an input. These
  217. expressions may be combined with other expressions for information or logical
  218. comparison:
  219. .. code-block:: cmake
  220. -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
  221. generates a string of the entries in the :prop_tgt:`INCLUDE_DIRECTORIES` target
  222. property with each entry preceded by ``-I``. Note that a more-complete use
  223. in this situation would require first checking if the INCLUDE_DIRECTORIES
  224. property is non-empty:
  225. .. code-block:: cmake
  226. $<$<BOOL:${prop}>:-I$<JOIN:${prop}, -I>>
  227. where ``${prop}`` refers to a helper variable:
  228. .. code-block:: cmake
  229. set(prop "$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>")
  230. Available output expressions are:
  231. ``$<0:...>``
  232. Empty string (ignores ``...``)
  233. ``$<1:...>``
  234. Content of ``...``
  235. ``$<JOIN:list,...>``
  236. Joins the list with the content of ``...``
  237. ``$<ANGLE-R>``
  238. A literal ``>``. Used to compare strings which contain a ``>`` for example.
  239. ``$<COMMA>``
  240. A literal ``,``. Used to compare strings which contain a ``,`` for example.
  241. ``$<SEMICOLON>``
  242. A literal ``;``. Used to prevent list expansion on an argument with ``;``.
  243. ``$<TARGET_NAME:...>``
  244. Marks ``...`` as being the name of a target. This is required if exporting
  245. targets to multiple dependent export sets. The ``...`` must be a literal
  246. name of a target- it may not contain generator expressions.
  247. ``$<TARGET_NAME_IF_EXISTS:...>``
  248. Expands to the ``...`` if the given target exists, an empty string
  249. otherwise.
  250. ``$<LINK_ONLY:...>``
  251. Content of ``...`` except when evaluated in a link interface while
  252. propagating :ref:`Target Usage Requirements`, in which case it is the
  253. empty string.
  254. Intended for use only in an :prop_tgt:`INTERFACE_LINK_LIBRARIES` target
  255. property, perhaps via the :command:`target_link_libraries` command,
  256. to specify private link dependencies without other usage requirements.
  257. ``$<INSTALL_INTERFACE:...>``
  258. Content of ``...`` when the property is exported using :command:`install(EXPORT)`,
  259. and empty otherwise.
  260. ``$<BUILD_INTERFACE:...>``
  261. Content of ``...`` when the property is exported using :command:`export`, or
  262. when the target is used by another target in the same buildsystem. Expands to
  263. the empty string otherwise.
  264. ``$<LOWER_CASE:...>``
  265. Content of ``...`` converted to lower case.
  266. ``$<UPPER_CASE:...>``
  267. Content of ``...`` converted to upper case.
  268. ``$<MAKE_C_IDENTIFIER:...>``
  269. Content of ``...`` converted to a C identifier. The conversion follows the
  270. same behavior as :command:`string(MAKE_C_IDENTIFIER)`.
  271. ``$<TARGET_OBJECTS:objLib>``
  272. List of objects resulting from build of ``objLib``. ``objLib`` must be an
  273. object of type ``OBJECT_LIBRARY``.
  274. ``$<SHELL_PATH:...>``
  275. Content of ``...`` converted to shell path style. For example, slashes are
  276. converted to backslashes in Windows shells and drive letters are converted
  277. to posix paths in MSYS shells. The ``...`` must be an absolute path.
  278. ``$<GENEX_EVAL:...>``
  279. Content of ``...`` evaluated as a generator expression in the current
  280. context. This enables consumption of generator expressions
  281. whose evaluation results itself in generator expressions.
  282. ``$<TARGET_GENEX_EVAL:tgt,...>``
  283. Content of ``...`` evaluated as a generator expression in the context of
  284. ``tgt`` target. This enables consumption of custom target properties that
  285. themselves contain generator expressions.
  286. Having the capability to evaluate generator expressions is very useful when
  287. you want to manage custom properties supporting generator expressions.
  288. For example:
  289. .. code-block:: cmake
  290. add_library(foo ...)
  291. set_property(TARGET foo PROPERTY
  292. CUSTOM_KEYS $<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>
  293. )
  294. add_custom_target(printFooKeys
  295. COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_PROPERTY:foo,CUSTOM_KEYS>
  296. )
  297. This naive implementation of the ``printFooKeys`` custom command is wrong
  298. because ``CUSTOM_KEYS`` target property is not evaluated and the content
  299. is passed as is (i.e. ``$<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>``).
  300. To have the expected result (i.e. ``FOO_EXTRA_THINGS`` if config is
  301. ``Debug``), it is required to evaluate the output of
  302. ``$<TARGET_PROPERTY:foo,CUSTOM_KEYS>``:
  303. .. code-block:: cmake
  304. add_custom_target(printFooKeys
  305. COMMAND ${CMAKE_COMMAND} -E
  306. echo $<TARGET_GENEX_EVAL:foo,$<TARGET_PROPERTY:foo,CUSTOM_KEYS>>
  307. )