cmake-generator-expressions.7.rst 31 KB

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