cmake-generator-expressions.7.rst 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. They enable conditional linking, conditional definitions used when compiling,
  17. conditional include directories, and more. The conditions may be based on
  18. the build configuration, target properties, platform information or any other
  19. queryable information.
  20. Generator expressions have the form ``$<...>``. To avoid confusion, this page
  21. deviates from most of the CMake documentation in that it omits angular brackets
  22. ``<...>`` around placeholders like ``condition``, ``string``, ``target``,
  23. among others.
  24. Generator expressions can be nested, as shown in most of the examples below.
  25. .. _`Boolean Generator Expressions`:
  26. Boolean Generator Expressions
  27. =============================
  28. Boolean expressions evaluate to either ``0`` or ``1``.
  29. They are typically used to construct the condition in a :ref:`conditional
  30. generator expression<Conditional Generator Expressions>`.
  31. Available boolean expressions are:
  32. Logical Operators
  33. -----------------
  34. .. genex:: $<BOOL:string>
  35. Converts ``string`` to ``0`` or ``1``. Evaluates to ``0`` if any of the
  36. following is true:
  37. * ``string`` is empty,
  38. * ``string`` is a case-insensitive equal of
  39. ``0``, ``FALSE``, ``OFF``, ``N``, ``NO``, ``IGNORE``, or ``NOTFOUND``, or
  40. * ``string`` ends in the suffix ``-NOTFOUND`` (case-sensitive).
  41. Otherwise evaluates to ``1``.
  42. .. genex:: $<AND:conditions>
  43. where ``conditions`` is a comma-separated list of boolean expressions.
  44. Evaluates to ``1`` if all conditions are ``1``.
  45. Otherwise evaluates to ``0``.
  46. .. genex:: $<OR:conditions>
  47. where ``conditions`` is a comma-separated list of boolean expressions.
  48. Evaluates to ``1`` if at least one of the conditions is ``1``.
  49. Otherwise evaluates to ``0``.
  50. .. genex:: $<NOT:condition>
  51. ``0`` if ``condition`` is ``1``, else ``1``.
  52. String Comparisons
  53. ------------------
  54. .. genex:: $<STREQUAL:string1,string2>
  55. ``1`` if ``string1`` and ``string2`` are equal, else ``0``.
  56. The comparison is case-sensitive. For a case-insensitive comparison,
  57. combine with a :ref:`string transforming generator expression
  58. <String Transforming Generator Expressions>`,
  59. .. code-block:: cmake
  60. $<STREQUAL:$<UPPER_CASE:${foo}>,"BAR"> # "1" if ${foo} is any of "BAR", "Bar", "bar", ...
  61. .. genex:: $<EQUAL:value1,value2>
  62. ``1`` if ``value1`` and ``value2`` are numerically equal, else ``0``.
  63. .. genex:: $<IN_LIST:string,list>
  64. ``1`` if ``string`` is member of the semicolon-separated ``list``, else ``0``.
  65. Uses case-sensitive comparisons.
  66. .. genex:: $<VERSION_LESS:v1,v2>
  67. ``1`` if ``v1`` is a version less than ``v2``, else ``0``.
  68. .. genex:: $<VERSION_GREATER:v1,v2>
  69. ``1`` if ``v1`` is a version greater than ``v2``, else ``0``.
  70. .. genex:: $<VERSION_EQUAL:v1,v2>
  71. ``1`` if ``v1`` is the same version as ``v2``, else ``0``.
  72. .. genex:: $<VERSION_LESS_EQUAL:v1,v2>
  73. ``1`` if ``v1`` is a version less than or equal to ``v2``, else ``0``.
  74. .. genex:: $<VERSION_GREATER_EQUAL:v1,v2>
  75. ``1`` if ``v1`` is a version greater than or equal to ``v2``, else ``0``.
  76. Variable Queries
  77. ----------------
  78. .. genex:: $<TARGET_EXISTS:target>
  79. ``1`` if ``target`` exists, else ``0``.
  80. .. genex:: $<CONFIG:cfgs>
  81. ``1`` if config is any one of the entries in ``cfgs``, else ``0``. This is a
  82. case-insensitive comparison. The mapping in
  83. :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` is also considered by this
  84. expression when it is evaluated on a property on an :prop_tgt:`IMPORTED`
  85. target.
  86. .. genex:: $<PLATFORM_ID:platform_ids>
  87. where ``platform_ids`` is a comma-separated list.
  88. ``1`` if the CMake's platform id matches any one of the entries in
  89. ``platform_ids``, otherwise ``0``.
  90. See also the :variable:`CMAKE_SYSTEM_NAME` variable.
  91. .. genex:: $<C_COMPILER_ID:compiler_ids>
  92. where ``compiler_ids`` is a comma-separated list.
  93. ``1`` if the CMake's compiler id of the C compiler matches any one
  94. of the entries in ``compiler_ids``, otherwise ``0``.
  95. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  96. .. genex:: $<CXX_COMPILER_ID:compiler_ids>
  97. where ``compiler_ids`` is a comma-separated list.
  98. ``1`` if the CMake's compiler id of the CXX compiler matches any one
  99. of the entries in ``compiler_ids``, otherwise ``0``.
  100. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  101. .. genex:: $<CUDA_COMPILER_ID:compiler_ids>
  102. where ``compiler_ids`` is a comma-separated list.
  103. ``1`` if the CMake's compiler id of the CUDA compiler matches any one
  104. of the entries in ``compiler_ids``, otherwise ``0``.
  105. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  106. .. genex:: $<OBJC_COMPILER_ID:compiler_ids>
  107. where ``compiler_ids`` is a comma-separated list.
  108. ``1`` if the CMake's compiler id of the Objective-C compiler matches any one
  109. of the entries in ``compiler_ids``, otherwise ``0``.
  110. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  111. .. genex:: $<OBJCXX_COMPILER_ID:compiler_ids>
  112. where ``compiler_ids`` is a comma-separated list.
  113. ``1`` if the CMake's compiler id of the Objective-C++ compiler matches any one
  114. of the entries in ``compiler_ids``, otherwise ``0``.
  115. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  116. .. genex:: $<Fortran_COMPILER_ID:compiler_ids>
  117. where ``compiler_ids`` is a comma-separated list.
  118. ``1`` if the CMake's compiler id of the Fortran compiler matches any one
  119. of the entries in ``compiler_ids``, otherwise ``0``.
  120. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  121. .. genex:: $<HIP_COMPILER_ID:compiler_ids>
  122. where ``compiler_ids`` is a comma-separated list.
  123. ``1`` if the CMake's compiler id of the HIP compiler matches any one
  124. of the entries in ``compiler_ids``, otherwise ``0``.
  125. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  126. .. genex:: $<ISPC_COMPILER_ID:compiler_ids>
  127. where ``compiler_ids`` is a comma-separated list.
  128. ``1`` if the CMake's compiler id of the ISPC compiler matches any one
  129. of the entries in ``compiler_ids``, otherwise ``0``.
  130. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  131. .. genex:: $<C_COMPILER_VERSION:version>
  132. ``1`` if the version of the C compiler matches ``version``, otherwise ``0``.
  133. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  134. .. genex:: $<CXX_COMPILER_VERSION:version>
  135. ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``.
  136. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  137. .. genex:: $<CUDA_COMPILER_VERSION:version>
  138. ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``.
  139. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  140. .. genex:: $<OBJC_COMPILER_VERSION:version>
  141. ``1`` if the version of the OBJC compiler matches ``version``, otherwise ``0``.
  142. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  143. .. genex:: $<OBJCXX_COMPILER_VERSION:version>
  144. ``1`` if the version of the OBJCXX compiler matches ``version``, otherwise ``0``.
  145. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  146. .. genex:: $<Fortran_COMPILER_VERSION:version>
  147. ``1`` if the version of the Fortran compiler matches ``version``, otherwise ``0``.
  148. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  149. .. genex:: $<HIP_COMPILER_VERSION:version>
  150. ``1`` if the version of the HIP compiler matches ``version``, otherwise ``0``.
  151. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  152. .. genex:: $<ISPC_COMPILER_VERSION:version>
  153. ``1`` if the version of the ISPC compiler matches ``version``, otherwise ``0``.
  154. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  155. .. genex:: $<TARGET_POLICY:policy>
  156. ``1`` if the ``policy`` was NEW when the 'head' target was created,
  157. else ``0``. If the ``policy`` was not set, the warning message for the policy
  158. will be emitted. This generator expression only works for a subset of
  159. policies.
  160. .. genex:: $<COMPILE_FEATURES:features>
  161. where ``features`` is a comma-spearated list.
  162. Evaluates to ``1`` if all of the ``features`` are available for the 'head'
  163. target, and ``0`` otherwise. If this expression is used while evaluating
  164. the link implementation of a target and if any dependency transitively
  165. increases the required :prop_tgt:`C_STANDARD` or :prop_tgt:`CXX_STANDARD`
  166. for the 'head' target, an error is reported. See the
  167. :manual:`cmake-compile-features(7)` manual for information on
  168. compile features and a list of supported compilers.
  169. .. _`Boolean COMPILE_LANGUAGE Generator Expression`:
  170. .. genex:: $<COMPILE_LANG_AND_ID:language,compiler_ids>
  171. ``1`` when the language used for compilation unit matches ``language`` and
  172. the CMake's compiler id of the language compiler matches any one of the
  173. entries in ``compiler_ids``, otherwise ``0``. This expression is a short form
  174. for the combination of ``$<COMPILE_LANGUAGE:language>`` and
  175. ``$<LANG_COMPILER_ID:compiler_ids>``. This expression may be used to specify
  176. compile options, compile definitions, and include directories for source files of a
  177. particular language and compiler combination in a target. For example:
  178. .. code-block:: cmake
  179. add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
  180. target_compile_definitions(myapp
  181. PRIVATE $<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang>:COMPILING_CXX_WITH_CLANG>
  182. $<$<COMPILE_LANG_AND_ID:CXX,Intel>:COMPILING_CXX_WITH_INTEL>
  183. $<$<COMPILE_LANG_AND_ID:C,Clang>:COMPILING_C_WITH_CLANG>
  184. )
  185. This specifies the use of different compile definitions based on both
  186. the compiler id and compilation language. This example will have a
  187. ``COMPILING_CXX_WITH_CLANG`` compile definition when Clang is the CXX
  188. compiler, and ``COMPILING_CXX_WITH_INTEL`` when Intel is the CXX compiler.
  189. Likewise when the C compiler is Clang it will only see the ``COMPILING_C_WITH_CLANG``
  190. definition.
  191. Without the ``COMPILE_LANG_AND_ID`` generator expression the same logic
  192. would be expressed as:
  193. .. code-block:: cmake
  194. target_compile_definitions(myapp
  195. PRIVATE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:AppleClang,Clang>>:COMPILING_CXX_WITH_CLANG>
  196. $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:Intel>>:COMPILING_CXX_WITH_INTEL>
  197. $<$<AND:$<COMPILE_LANGUAGE:C>,$<C_COMPILER_ID:Clang>>:COMPILING_C_WITH_CLANG>
  198. )
  199. .. genex:: $<COMPILE_LANGUAGE:languages>
  200. ``1`` when the language used for compilation unit matches any of the entries
  201. in ``languages``, otherwise ``0``. This expression may be used to specify
  202. compile options, compile definitions, and include directories for source files of a
  203. particular language in a target. For example:
  204. .. code-block:: cmake
  205. add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
  206. target_compile_options(myapp
  207. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
  208. )
  209. target_compile_definitions(myapp
  210. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
  211. $<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
  212. )
  213. target_include_directories(myapp
  214. PRIVATE $<$<COMPILE_LANGUAGE:CXX,CUDA>:/opt/foo/headers>
  215. )
  216. This specifies the use of the ``-fno-exceptions`` compile option,
  217. ``COMPILING_CXX`` compile definition, and ``cxx_headers`` include
  218. directory for C++ only (compiler id checks elided). It also specifies
  219. a ``COMPILING_CUDA`` compile definition for CUDA.
  220. Note that with :ref:`Visual Studio Generators` and :generator:`Xcode` there
  221. is no way to represent target-wide compile definitions or include directories
  222. separately for ``C`` and ``CXX`` languages.
  223. Also, with :ref:`Visual Studio Generators` there is no way to represent
  224. target-wide flags separately for ``C`` and ``CXX`` languages. Under these
  225. generators, expressions for both C and C++ sources will be evaluated
  226. using ``CXX`` if there are any C++ sources and otherwise using ``C``.
  227. A workaround is to create separate libraries for each source file language
  228. instead:
  229. .. code-block:: cmake
  230. add_library(myapp_c foo.c)
  231. add_library(myapp_cxx bar.cpp)
  232. target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
  233. add_executable(myapp main.cpp)
  234. target_link_libraries(myapp myapp_c myapp_cxx)
  235. .. _`Boolean LINK_LANGUAGE Generator Expression`:
  236. .. genex:: $<LINK_LANG_AND_ID:language,compiler_ids>
  237. ``1`` when the language used for link step matches ``language`` and the
  238. CMake's compiler id of the language linker matches any one of the entries
  239. in ``compiler_ids``, otherwise ``0``. This expression is a short form for the
  240. combination of ``$<LINK_LANGUAGE:language>`` and
  241. ``$<LANG_COMPILER_ID:compiler_ids>``. This expression may be used to specify
  242. link libraries, link options, link directories and link dependencies of a
  243. particular language and linker combination in a target. For example:
  244. .. code-block:: cmake
  245. add_library(libC_Clang ...)
  246. add_library(libCXX_Clang ...)
  247. add_library(libC_Intel ...)
  248. add_library(libCXX_Intel ...)
  249. add_executable(myapp main.c)
  250. if (CXX_CONFIG)
  251. target_sources(myapp PRIVATE file.cxx)
  252. endif()
  253. target_link_libraries(myapp
  254. PRIVATE $<$<LINK_LANG_AND_ID:CXX,Clang,AppleClang>:libCXX_Clang>
  255. $<$<LINK_LANG_AND_ID:C,Clang,AppleClang>:libC_Clang>
  256. $<$<LINK_LANG_AND_ID:CXX,Intel>:libCXX_Intel>
  257. $<$<LINK_LANG_AND_ID:C,Intel>:libC_Intel>)
  258. This specifies the use of different link libraries based on both the
  259. compiler id and link language. This example will have target ``libCXX_Clang``
  260. as link dependency when ``Clang`` or ``AppleClang`` is the ``CXX``
  261. linker, and ``libCXX_Intel`` when ``Intel`` is the ``CXX`` linker.
  262. Likewise when the ``C`` linker is ``Clang`` or ``AppleClang``, target
  263. ``libC_Clang`` will be added as link dependency and ``libC_Intel`` when
  264. ``Intel`` is the ``C`` linker.
  265. See :ref:`the note related to
  266. <Constraints LINK_LANGUAGE Generator Expression>`
  267. ``$<LINK_LANGUAGE:language>`` for constraints about the usage of this
  268. generator expression.
  269. .. genex:: $<LINK_LANGUAGE:languages>
  270. ``1`` when the language used for link step matches any of the entries
  271. in ``languages``, otherwise ``0``. This expression may be used to specify
  272. link libraries, link options, link directories and link dependencies of a
  273. particular language in a target. For example:
  274. .. code-block:: cmake
  275. add_library(api_C ...)
  276. add_library(api_CXX ...)
  277. add_library(api INTERFACE)
  278. target_link_options(api INTERFACE $<$<LINK_LANGUAGE:C>:-opt_c>
  279. $<$<LINK_LANGUAGE:CXX>:-opt_cxx>)
  280. target_link_libraries(api INTERFACE $<$<LINK_LANGUAGE:C>:api_C>
  281. $<$<LINK_LANGUAGE:CXX>:api_CXX>)
  282. add_executable(myapp1 main.c)
  283. target_link_options(myapp1 PRIVATE api)
  284. add_executable(myapp2 main.cpp)
  285. target_link_options(myapp2 PRIVATE api)
  286. This specifies to use the ``api`` target for linking targets ``myapp1`` and
  287. ``myapp2``. In practice, ``myapp1`` will link with target ``api_C`` and
  288. option ``-opt_c`` because it will use ``C`` as link language. And ``myapp2``
  289. will link with ``api_CXX`` and option ``-opt_cxx`` because ``CXX`` will be
  290. the link language.
  291. .. _`Constraints LINK_LANGUAGE Generator Expression`:
  292. .. note::
  293. To determine the link language of a target, it is required to collect,
  294. transitively, all the targets which will be linked to it. So, for link
  295. libraries properties, a double evaluation will be done. During the first
  296. evaluation, ``$<LINK_LANGUAGE:..>`` expressions will always return ``0``.
  297. The link language computed after this first pass will be used to do the
  298. second pass. To avoid inconsistency, it is required that the second pass
  299. do not change the link language. Moreover, to avoid unexpected
  300. side-effects, it is required to specify complete entities as part of the
  301. ``$<LINK_LANGUAGE:..>`` expression. For example:
  302. .. code-block:: cmake
  303. add_library(lib STATIC file.cxx)
  304. add_library(libother STATIC file.c)
  305. # bad usage
  306. add_executable(myapp1 main.c)
  307. target_link_libraries(myapp1 PRIVATE lib$<$<LINK_LANGUAGE:C>:other>)
  308. # correct usage
  309. add_executable(myapp2 main.c)
  310. target_link_libraries(myapp2 PRIVATE $<$<LINK_LANGUAGE:C>:libother>)
  311. In this example, for ``myapp1``, the first pass will, unexpectedly,
  312. determine that the link language is ``CXX`` because the evaluation of the
  313. generator expression will be an empty string so ``myapp1`` will depends on
  314. target ``lib`` which is ``C++``. On the contrary, for ``myapp2``, the first
  315. evaluation will give ``C`` as link language, so the second pass will
  316. correctly add target ``libother`` as link dependency.
  317. .. genex:: $<DEVICE_LINK:list>
  318. Returns the list if it is the device link step, an empty list otherwise.
  319. The device link step is controlled by :prop_tgt:`CUDA_SEPARABLE_COMPILATION`
  320. and :prop_tgt:`CUDA_RESOLVE_DEVICE_SYMBOLS` properties and
  321. policy :policy:`CMP0105`. This expression can only be used to specify link
  322. options.
  323. .. genex:: $<HOST_LINK:list>
  324. Returns the list if it is the normal link step, an empty list otherwise.
  325. This expression is mainly useful when a device link step is also involved
  326. (see ``$<DEVICE_LINK:list>`` generator expression). This expression can only
  327. be used to specify link options.
  328. String-Valued Generator Expressions
  329. ===================================
  330. These expressions expand to some string.
  331. For example,
  332. .. code-block:: cmake
  333. include_directories(/usr/include/$<CXX_COMPILER_ID>/)
  334. expands to ``/usr/include/GNU/`` or ``/usr/include/Clang/`` etc, depending on
  335. the compiler identifier.
  336. String-valued expressions may also be combined with other expressions.
  337. Here an example for a string-valued expression within a boolean expressions
  338. within a conditional expression:
  339. .. code-block:: cmake
  340. $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
  341. expands to ``OLD_COMPILER`` if the
  342. :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
  343. than 4.2.0.
  344. And here two nested string-valued expressions:
  345. .. code-block:: cmake
  346. -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
  347. generates a string of the entries in the :prop_tgt:`INCLUDE_DIRECTORIES` target
  348. property with each entry preceded by ``-I``.
  349. Expanding on the previous example, if one first wants to check if the
  350. ``INCLUDE_DIRECTORIES`` property is non-empty, then it is advisable to
  351. introduce a helper variable to keep the code readable:
  352. .. code-block:: cmake
  353. set(prop "$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>") # helper variable
  354. $<$<BOOL:${prop}>:-I$<JOIN:${prop}, -I>>
  355. The following string-valued generator expressions are available:
  356. Escaped Characters
  357. ------------------
  358. String literals to escape the special meaning a character would otherwise have:
  359. .. genex:: $<ANGLE-R>
  360. A literal ``>``. Used for example to compare strings that contain a ``>``.
  361. .. genex:: $<COMMA>
  362. A literal ``,``. Used for example to compare strings which contain a ``,``.
  363. .. genex:: $<SEMICOLON>
  364. A literal ``;``. Used to prevent list expansion on an argument with ``;``.
  365. .. _`Conditional Generator Expressions`:
  366. Conditional Expressions
  367. -----------------------
  368. Conditional generator expressions depend on a boolean condition
  369. that must be ``0`` or ``1``.
  370. .. genex:: $<condition:true_string>
  371. Evaluates to ``true_string`` if ``condition`` is ``1``.
  372. Otherwise evaluates to the empty string.
  373. .. genex:: $<IF:condition,true_string,false_string>
  374. Evaluates to ``true_string`` if ``condition`` is ``1``.
  375. Otherwise evaluates to ``false_string``.
  376. Typically, the ``condition`` is a :ref:`boolean generator expression
  377. <Boolean Generator Expressions>`. For instance,
  378. .. code-block:: cmake
  379. $<$<CONFIG:Debug>:DEBUG_MODE>
  380. expands to ``DEBUG_MODE`` when the ``Debug`` configuration is used, and
  381. otherwise expands to the empty string.
  382. .. _`String Transforming Generator Expressions`:
  383. String Transformations
  384. ----------------------
  385. .. genex:: $<JOIN:list,string>
  386. Joins the list with the content of ``string``.
  387. .. genex:: $<REMOVE_DUPLICATES:list>
  388. Removes duplicated items in the given ``list``.
  389. .. genex:: $<FILTER:list,INCLUDE|EXCLUDE,regex>
  390. Includes or removes items from ``list`` that match the regular expression ``regex``.
  391. .. genex:: $<LOWER_CASE:string>
  392. Content of ``string`` converted to lower case.
  393. .. genex:: $<UPPER_CASE:string>
  394. Content of ``string`` converted to upper case.
  395. .. genex:: $<GENEX_EVAL:expr>
  396. Content of ``expr`` evaluated as a generator expression in the current
  397. context. This enables consumption of generator expressions whose
  398. evaluation results itself in generator expressions.
  399. .. genex:: $<TARGET_GENEX_EVAL:tgt,expr>
  400. Content of ``expr`` evaluated as a generator expression in the context of
  401. ``tgt`` target. This enables consumption of custom target properties that
  402. themselves contain generator expressions.
  403. Having the capability to evaluate generator expressions is very useful when
  404. you want to manage custom properties supporting generator expressions.
  405. For example:
  406. .. code-block:: cmake
  407. add_library(foo ...)
  408. set_property(TARGET foo PROPERTY
  409. CUSTOM_KEYS $<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>
  410. )
  411. add_custom_target(printFooKeys
  412. COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_PROPERTY:foo,CUSTOM_KEYS>
  413. )
  414. This naive implementation of the ``printFooKeys`` custom command is wrong
  415. because ``CUSTOM_KEYS`` target property is not evaluated and the content
  416. is passed as is (i.e. ``$<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>``).
  417. To have the expected result (i.e. ``FOO_EXTRA_THINGS`` if config is
  418. ``Debug``), it is required to evaluate the output of
  419. ``$<TARGET_PROPERTY:foo,CUSTOM_KEYS>``:
  420. .. code-block:: cmake
  421. add_custom_target(printFooKeys
  422. COMMAND ${CMAKE_COMMAND} -E
  423. echo $<TARGET_GENEX_EVAL:foo,$<TARGET_PROPERTY:foo,CUSTOM_KEYS>>
  424. )
  425. Variable Queries
  426. ----------------
  427. .. genex:: $<CONFIG>
  428. Configuration name.
  429. .. genex:: $<CONFIGURATION>
  430. Configuration name. Deprecated since CMake 3.0. Use ``CONFIG`` instead.
  431. .. genex:: $<PLATFORM_ID>
  432. The current system's CMake platform id.
  433. See also the :variable:`CMAKE_SYSTEM_NAME` variable.
  434. .. genex:: $<C_COMPILER_ID>
  435. The CMake's compiler id of the C compiler used.
  436. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  437. .. genex:: $<CXX_COMPILER_ID>
  438. The CMake's compiler id of the CXX compiler used.
  439. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  440. .. genex:: $<CUDA_COMPILER_ID>
  441. The CMake's compiler id of the CUDA compiler used.
  442. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  443. .. genex:: $<OBJC_COMPILER_ID>
  444. The CMake's compiler id of the OBJC compiler used.
  445. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  446. .. genex:: $<OBJCXX_COMPILER_ID>
  447. The CMake's compiler id of the OBJCXX compiler used.
  448. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  449. .. genex:: $<Fortran_COMPILER_ID>
  450. The CMake's compiler id of the Fortran compiler used.
  451. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  452. .. genex:: $<HIP_COMPILER_ID>
  453. The CMake's compiler id of the HIP compiler used.
  454. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  455. .. genex:: $<ISPC_COMPILER_ID>
  456. The CMake's compiler id of the ISPC compiler used.
  457. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  458. .. genex:: $<C_COMPILER_VERSION>
  459. The version of the C compiler used.
  460. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  461. .. genex:: $<CXX_COMPILER_VERSION>
  462. The version of the CXX compiler used.
  463. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  464. .. genex:: $<CUDA_COMPILER_VERSION>
  465. The version of the CUDA compiler used.
  466. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  467. .. genex:: $<OBJC_COMPILER_VERSION>
  468. The version of the OBJC compiler used.
  469. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  470. .. genex:: $<OBJCXX_COMPILER_VERSION>
  471. The version of the OBJCXX compiler used.
  472. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  473. .. genex:: $<Fortran_COMPILER_VERSION>
  474. The version of the Fortran compiler used.
  475. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  476. .. genex:: $<HIP_COMPILER_VERSION>
  477. The version of the HIP compiler used.
  478. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  479. .. genex:: $<ISPC_COMPILER_VERSION>
  480. The version of the ISPC compiler used.
  481. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  482. .. genex:: $<COMPILE_LANGUAGE>
  483. The compile language of source files when evaluating compile options.
  484. See :ref:`the related boolean expression
  485. <Boolean COMPILE_LANGUAGE Generator Expression>`
  486. ``$<COMPILE_LANGUAGE:language>``
  487. for notes about the portability of this generator expression.
  488. .. genex:: $<LINK_LANGUAGE>
  489. The link language of target when evaluating link options.
  490. See :ref:`the related boolean expression
  491. <Boolean LINK_LANGUAGE Generator Expression>` ``$<LINK_LANGUAGE:language>``
  492. for notes about the portability of this generator expression.
  493. .. note::
  494. This generator expression is not supported by the link libraries
  495. properties to avoid side-effects due to the double evaluation of
  496. these properties.
  497. .. _`Target-Dependent Queries`:
  498. Target-Dependent Queries
  499. ------------------------
  500. These queries refer to a target ``tgt``. This can be any runtime artifact,
  501. namely:
  502. * an executable target created by :command:`add_executable`
  503. * a shared library target (``.so``, ``.dll`` but not their ``.lib`` import library)
  504. created by :command:`add_library`
  505. * a static library target created by :command:`add_library`
  506. In the following, "the ``tgt`` filename" means the name of the ``tgt``
  507. binary file. This has to be distinguished from "the target name",
  508. which is just the string ``tgt``.
  509. .. genex:: $<TARGET_NAME_IF_EXISTS:tgt>
  510. The target name ``tgt`` if the target exists, an empty string otherwise.
  511. Note that ``tgt`` is not added as a dependency of the target this
  512. expression is evaluated on.
  513. .. genex:: $<TARGET_FILE:tgt>
  514. Full path to the ``tgt`` binary file.
  515. .. genex:: $<TARGET_FILE_BASE_NAME:tgt>
  516. Base name of ``tgt``, i.e. ``$<TARGET_FILE_NAME:tgt>`` without prefix and
  517. suffix.
  518. For example, if the ``tgt`` filename is ``libbase.so``, the base name is ``base``.
  519. See also the :prop_tgt:`OUTPUT_NAME`, :prop_tgt:`ARCHIVE_OUTPUT_NAME`,
  520. :prop_tgt:`LIBRARY_OUTPUT_NAME` and :prop_tgt:`RUNTIME_OUTPUT_NAME`
  521. target properties and their configuration specific variants
  522. :prop_tgt:`OUTPUT_NAME_<CONFIG>`, :prop_tgt:`ARCHIVE_OUTPUT_NAME_<CONFIG>`,
  523. :prop_tgt:`LIBRARY_OUTPUT_NAME_<CONFIG>` and
  524. :prop_tgt:`RUNTIME_OUTPUT_NAME_<CONFIG>`.
  525. The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
  526. properties can also be considered.
  527. Note that ``tgt`` is not added as a dependency of the target this
  528. expression is evaluated on.
  529. .. genex:: $<TARGET_FILE_PREFIX:tgt>
  530. Prefix of the ``tgt`` filename (such as ``lib``).
  531. See also the :prop_tgt:`PREFIX` target property.
  532. Note that ``tgt`` is not added as a dependency of the target this
  533. expression is evaluated on.
  534. .. genex:: $<TARGET_FILE_SUFFIX:tgt>
  535. Suffix of the ``tgt`` filename (extension such as ``.so`` or ``.exe``).
  536. See also the :prop_tgt:`SUFFIX` target property.
  537. Note that ``tgt`` is not added as a dependency of the target this
  538. expression is evaluated on.
  539. .. genex:: $<TARGET_FILE_NAME:tgt>
  540. The ``tgt`` filename.
  541. Note that ``tgt`` is not added as a dependency of the target this
  542. expression is evaluated on (see policy :policy:`CMP0112`).
  543. .. genex:: $<TARGET_FILE_DIR:tgt>
  544. Directory of the ``tgt`` binary file.
  545. Note that ``tgt`` is not added as a dependency of the target this
  546. expression is evaluated on (see policy :policy:`CMP0112`).
  547. .. genex:: $<TARGET_LINKER_FILE:tgt>
  548. File used when linking to the ``tgt`` target. This will usually
  549. be the library that ``tgt`` represents (``.a``, ``.lib``, ``.so``),
  550. but for a shared library on DLL platforms, it would be the ``.lib``
  551. import library associated with the DLL.
  552. .. genex:: $<TARGET_LINKER_FILE_BASE_NAME:tgt>
  553. Base name of file used to link the target ``tgt``, i.e.
  554. ``$<TARGET_LINKER_FILE_NAME:tgt>`` without prefix and suffix. For example,
  555. if target file name is ``libbase.a``, the base name is ``base``.
  556. See also the :prop_tgt:`OUTPUT_NAME`, :prop_tgt:`ARCHIVE_OUTPUT_NAME`,
  557. and :prop_tgt:`LIBRARY_OUTPUT_NAME` target properties and their configuration
  558. specific variants :prop_tgt:`OUTPUT_NAME_<CONFIG>`,
  559. :prop_tgt:`ARCHIVE_OUTPUT_NAME_<CONFIG>` and
  560. :prop_tgt:`LIBRARY_OUTPUT_NAME_<CONFIG>`.
  561. The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
  562. properties can also be considered.
  563. Note that ``tgt`` is not added as a dependency of the target this
  564. expression is evaluated on.
  565. .. genex:: $<TARGET_LINKER_FILE_PREFIX:tgt>
  566. Prefix of file used to link target ``tgt``.
  567. See also the :prop_tgt:`PREFIX` and :prop_tgt:`IMPORT_PREFIX` target
  568. properties.
  569. Note that ``tgt`` is not added as a dependency of the target this
  570. expression is evaluated on.
  571. .. genex:: $<TARGET_LINKER_FILE_SUFFIX:tgt>
  572. Suffix of file used to link where ``tgt`` is the name of a target.
  573. The suffix corresponds to the file extension (such as ".so" or ".lib").
  574. See also the :prop_tgt:`SUFFIX` and :prop_tgt:`IMPORT_SUFFIX` target
  575. properties.
  576. Note that ``tgt`` is not added as a dependency of the target this
  577. expression is evaluated on.
  578. .. genex:: $<TARGET_LINKER_FILE_NAME:tgt>
  579. Name of file used to link target ``tgt``.
  580. Note that ``tgt`` is not added as a dependency of the target this
  581. expression is evaluated on (see policy :policy:`CMP0112`).
  582. .. genex:: $<TARGET_LINKER_FILE_DIR:tgt>
  583. Directory of file used to link target ``tgt``.
  584. Note that ``tgt`` is not added as a dependency of the target this
  585. expression is evaluated on (see policy :policy:`CMP0112`).
  586. .. genex:: $<TARGET_SONAME_FILE:tgt>
  587. File with soname (``.so.3``) where ``tgt`` is the name of a target.
  588. .. genex:: $<TARGET_SONAME_FILE_NAME:tgt>
  589. Name of file with soname (``.so.3``).
  590. Note that ``tgt`` is not added as a dependency of the target this
  591. expression is evaluated on (see policy :policy:`CMP0112`).
  592. .. genex:: $<TARGET_SONAME_FILE_DIR:tgt>
  593. Directory of with soname (``.so.3``).
  594. Note that ``tgt`` is not added as a dependency of the target this
  595. expression is evaluated on (see policy :policy:`CMP0112`).
  596. .. genex:: $<TARGET_PDB_FILE:tgt>
  597. Full path to the linker generated program database file (.pdb)
  598. where ``tgt`` is the name of a target.
  599. See also the :prop_tgt:`PDB_NAME` and :prop_tgt:`PDB_OUTPUT_DIRECTORY`
  600. target properties and their configuration specific variants
  601. :prop_tgt:`PDB_NAME_<CONFIG>` and :prop_tgt:`PDB_OUTPUT_DIRECTORY_<CONFIG>`.
  602. .. genex:: $<TARGET_PDB_FILE_BASE_NAME:tgt>
  603. Base name of the linker generated program database file (.pdb)
  604. where ``tgt`` is the name of a target.
  605. The base name corresponds to the target PDB file name (see
  606. ``$<TARGET_PDB_FILE_NAME:tgt>``) without prefix and suffix. For example,
  607. if target file name is ``base.pdb``, the base name is ``base``.
  608. See also the :prop_tgt:`PDB_NAME` target property and its configuration
  609. specific variant :prop_tgt:`PDB_NAME_<CONFIG>`.
  610. The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
  611. properties can also be considered.
  612. Note that ``tgt`` is not added as a dependency of the target this
  613. expression is evaluated on.
  614. .. genex:: $<TARGET_PDB_FILE_NAME:tgt>
  615. Name of the linker generated program database file (.pdb).
  616. Note that ``tgt`` is not added as a dependency of the target this
  617. expression is evaluated on (see policy :policy:`CMP0112`).
  618. .. genex:: $<TARGET_PDB_FILE_DIR:tgt>
  619. Directory of the linker generated program database file (.pdb).
  620. Note that ``tgt`` is not added as a dependency of the target this
  621. expression is evaluated on (see policy :policy:`CMP0112`).
  622. .. genex:: $<TARGET_BUNDLE_DIR:tgt>
  623. Full path to the bundle directory (``my.app``, ``my.framework``, or
  624. ``my.bundle``) where ``tgt`` is the name of a target.
  625. Note that ``tgt`` is not added as a dependency of the target this
  626. expression is evaluated on (see policy :policy:`CMP0112`).
  627. .. genex:: $<TARGET_BUNDLE_CONTENT_DIR:tgt>
  628. Full path to the bundle content directory where ``tgt`` is the name of a
  629. target. For the macOS SDK it leads to ``my.app/Contents``, ``my.framework``,
  630. or ``my.bundle/Contents``. For all other SDKs (e.g. iOS) it leads to
  631. ``my.app``, ``my.framework``, or ``my.bundle`` due to the flat bundle
  632. structure.
  633. Note that ``tgt`` is not added as a dependency of the target this
  634. expression is evaluated on (see policy :policy:`CMP0112`).
  635. .. genex:: $<TARGET_PROPERTY:tgt,prop>
  636. Value of the property ``prop`` on the target ``tgt``.
  637. Note that ``tgt`` is not added as a dependency of the target this
  638. expression is evaluated on.
  639. .. genex:: $<TARGET_PROPERTY:prop>
  640. Value of the property ``prop`` on the target for which the expression
  641. is being evaluated. Note that for generator expressions in
  642. :ref:`Target Usage Requirements` this is the consuming target rather
  643. than the target specifying the requirement.
  644. .. genex:: $<TARGET_RUNTIME_DLLS:tgt>
  645. List of DLLs that the target depends on at runtime. This is determined by
  646. the locations of all the ``SHARED`` and ``MODULE`` targets in the target's
  647. transitive dependencies. Using this generator expression on targets other
  648. than executables, ``SHARED`` libraries, and ``MODULE`` libraries is an error.
  649. On non-DLL platforms, it evaluates to an empty string.
  650. This generator expression can be used to copy all of the DLLs that a target
  651. depends on into its output directory in a ``POST_BUILD`` custom command. For
  652. example:
  653. .. code-block:: cmake
  654. find_package(foo REQUIRED)
  655. add_executable(exe main.c)
  656. target_link_libraries(exe PRIVATE foo::foo foo::bar)
  657. add_custom_command(TARGET exe POST_BUILD
  658. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:exe> $<TARGET_FILE_DIR:exe>
  659. COMMAND_EXPAND_LISTS
  660. )
  661. .. genex:: $<INSTALL_PREFIX>
  662. Content of the install prefix when the target is exported via
  663. :command:`install(EXPORT)`, or when evaluated in
  664. :prop_tgt:`INSTALL_NAME_DIR`, and empty otherwise.
  665. Output-Related Expressions
  666. --------------------------
  667. .. genex:: $<TARGET_NAME:...>
  668. Marks ``...`` as being the name of a target. This is required if exporting
  669. targets to multiple dependent export sets. The ``...`` must be a literal
  670. name of a target- it may not contain generator expressions.
  671. .. genex:: $<LINK_ONLY:...>
  672. Content of ``...`` except when evaluated in a link interface while
  673. propagating :ref:`Target Usage Requirements`, in which case it is the
  674. empty string.
  675. Intended for use only in an :prop_tgt:`INTERFACE_LINK_LIBRARIES` target
  676. property, perhaps via the :command:`target_link_libraries` command,
  677. to specify private link dependencies without other usage requirements.
  678. .. genex:: $<INSTALL_INTERFACE:...>
  679. Content of ``...`` when the property is exported using :command:`install(EXPORT)`,
  680. and empty otherwise.
  681. .. genex:: $<BUILD_INTERFACE:...>
  682. Content of ``...`` when the property is exported using :command:`export`, or
  683. when the target is used by another target in the same buildsystem. Expands to
  684. the empty string otherwise.
  685. .. genex:: $<MAKE_C_IDENTIFIER:...>
  686. Content of ``...`` converted to a C identifier. The conversion follows the
  687. same behavior as :command:`string(MAKE_C_IDENTIFIER)`.
  688. .. genex:: $<TARGET_OBJECTS:objLib>
  689. List of objects resulting from build of ``objLib``.
  690. .. genex:: $<SHELL_PATH:...>
  691. Content of ``...`` converted to shell path style. For example, slashes are
  692. converted to backslashes in Windows shells and drive letters are converted
  693. to posix paths in MSYS shells. The ``...`` must be an absolute path.
  694. The ``...`` may be a :ref:`semicolon-separated list <CMake Language Lists>`
  695. of paths, in which case each path is converted individually and a result
  696. list is generated using the shell path separator (``:`` on POSIX and
  697. ``;`` on Windows). Be sure to enclose the argument containing this genex
  698. in double quotes in CMake source code so that ``;`` does not split arguments.
  699. .. genex:: $<OUTPUT_CONFIG:...>
  700. .. versionadded:: 3.20
  701. Only valid in :command:`add_custom_command` and :command:`add_custom_target`
  702. as the outer-most generator expression in an argument.
  703. With the :generator:`Ninja Multi-Config` generator, generator expressions
  704. in ``...`` are evaluated using the custom command's "output config".
  705. With other generators, the content of ``...`` is evaluated normally.
  706. .. genex:: $<COMMAND_CONFIG:...>
  707. .. versionadded:: 3.20
  708. Only valid in :command:`add_custom_command` and :command:`add_custom_target`
  709. as the outer-most generator expression in an argument.
  710. With the :generator:`Ninja Multi-Config` generator, generator expressions
  711. in ``...`` are evaluated using the custom command's "command config".
  712. With other generators, the content of ``...`` is evaluated normally.
  713. Debugging
  714. =========
  715. Since generator expressions are evaluated during generation of the buildsystem,
  716. and not during processing of ``CMakeLists.txt`` files, it is not possible to
  717. inspect their result with the :command:`message()` command.
  718. One possible way to generate debug messages is to add a custom target,
  719. .. code-block:: cmake
  720. add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")
  721. The shell command ``make genexdebug`` (invoked after execution of ``cmake``)
  722. would then print the result of ``$<...>``.
  723. Another way is to write debug messages to a file:
  724. .. code-block:: cmake
  725. file(GENERATE OUTPUT filename CONTENT "$<...>")