cmake-generator-expressions.7.rst 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. .. versionadded:: 3.12
  65. ``1`` if ``string`` is member of the semicolon-separated ``list``, else ``0``.
  66. Uses case-sensitive comparisons.
  67. .. genex:: $<VERSION_LESS:v1,v2>
  68. ``1`` if ``v1`` is a version less than ``v2``, else ``0``.
  69. .. genex:: $<VERSION_GREATER:v1,v2>
  70. ``1`` if ``v1`` is a version greater than ``v2``, else ``0``.
  71. .. genex:: $<VERSION_EQUAL:v1,v2>
  72. ``1`` if ``v1`` is the same version as ``v2``, else ``0``.
  73. .. genex:: $<VERSION_LESS_EQUAL:v1,v2>
  74. .. versionadded:: 3.7
  75. ``1`` if ``v1`` is a version less than or equal to ``v2``, else ``0``.
  76. .. genex:: $<VERSION_GREATER_EQUAL:v1,v2>
  77. .. versionadded:: 3.7
  78. ``1`` if ``v1`` is a version greater than or equal to ``v2``, else ``0``.
  79. Variable Queries
  80. ----------------
  81. .. genex:: $<TARGET_EXISTS:target>
  82. .. versionadded:: 3.12
  83. ``1`` if ``target`` exists, else ``0``.
  84. .. genex:: $<CONFIG:cfgs>
  85. ``1`` if config is any one of the entries in comma-separated list
  86. ``cfgs``, else ``0``. This is a case-insensitive comparison. The mapping in
  87. :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` is also considered by this
  88. expression when it is evaluated on a property on an :prop_tgt:`IMPORTED`
  89. target.
  90. .. genex:: $<PLATFORM_ID:platform_ids>
  91. where ``platform_ids`` is a comma-separated list.
  92. ``1`` if the CMake's platform id matches any one of the entries in
  93. ``platform_ids``, otherwise ``0``.
  94. See also the :variable:`CMAKE_SYSTEM_NAME` variable.
  95. .. genex:: $<C_COMPILER_ID:compiler_ids>
  96. where ``compiler_ids`` is a comma-separated list.
  97. ``1`` if the CMake's compiler id of the C compiler matches any one
  98. of the entries in ``compiler_ids``, otherwise ``0``.
  99. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  100. .. genex:: $<CXX_COMPILER_ID:compiler_ids>
  101. where ``compiler_ids`` is a comma-separated list.
  102. ``1`` if the CMake's compiler id of the CXX compiler matches any one
  103. of the entries in ``compiler_ids``, otherwise ``0``.
  104. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  105. .. genex:: $<CUDA_COMPILER_ID:compiler_ids>
  106. .. versionadded:: 3.15
  107. where ``compiler_ids`` is a comma-separated list.
  108. ``1`` if the CMake's compiler id of the CUDA 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:: $<OBJC_COMPILER_ID:compiler_ids>
  112. .. versionadded:: 3.16
  113. where ``compiler_ids`` is a comma-separated list.
  114. ``1`` if the CMake's compiler id of the Objective-C compiler matches any one
  115. of the entries in ``compiler_ids``, otherwise ``0``.
  116. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  117. .. genex:: $<OBJCXX_COMPILER_ID:compiler_ids>
  118. .. versionadded:: 3.16
  119. where ``compiler_ids`` is a comma-separated list.
  120. ``1`` if the CMake's compiler id of the Objective-C++ compiler matches any one
  121. of the entries in ``compiler_ids``, otherwise ``0``.
  122. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  123. .. genex:: $<Fortran_COMPILER_ID:compiler_ids>
  124. where ``compiler_ids`` is a comma-separated list.
  125. ``1`` if the CMake's compiler id of the Fortran compiler matches any one
  126. of the entries in ``compiler_ids``, otherwise ``0``.
  127. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  128. .. genex:: $<HIP_COMPILER_ID:compiler_ids>
  129. where ``compiler_ids`` is a comma-separated list.
  130. ``1`` if the CMake's compiler id of the HIP compiler matches any one
  131. of the entries in ``compiler_ids``, otherwise ``0``.
  132. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  133. .. genex:: $<ISPC_COMPILER_ID:compiler_ids>
  134. .. versionadded:: 3.19
  135. where ``compiler_ids`` is a comma-separated list.
  136. ``1`` if the CMake's compiler id of the ISPC compiler matches any one
  137. of the entries in ``compiler_ids``, otherwise ``0``.
  138. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  139. .. genex:: $<C_COMPILER_VERSION:version>
  140. ``1`` if the version of the C compiler matches ``version``, otherwise ``0``.
  141. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  142. .. genex:: $<CXX_COMPILER_VERSION:version>
  143. ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``.
  144. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  145. .. genex:: $<CUDA_COMPILER_VERSION:version>
  146. .. versionadded:: 3.15
  147. ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``.
  148. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  149. .. genex:: $<OBJC_COMPILER_VERSION:version>
  150. .. versionadded:: 3.16
  151. ``1`` if the version of the OBJC compiler matches ``version``, otherwise ``0``.
  152. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  153. .. genex:: $<OBJCXX_COMPILER_VERSION:version>
  154. .. versionadded:: 3.16
  155. ``1`` if the version of the OBJCXX compiler matches ``version``, otherwise ``0``.
  156. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  157. .. genex:: $<Fortran_COMPILER_VERSION:version>
  158. ``1`` if the version of the Fortran compiler matches ``version``, otherwise ``0``.
  159. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  160. .. genex:: $<HIP_COMPILER_VERSION:version>
  161. ``1`` if the version of the HIP compiler matches ``version``, otherwise ``0``.
  162. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  163. .. genex:: $<ISPC_COMPILER_VERSION:version>
  164. .. versionadded:: 3.19
  165. ``1`` if the version of the ISPC compiler matches ``version``, otherwise ``0``.
  166. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  167. .. genex:: $<TARGET_POLICY:policy>
  168. ``1`` if the ``policy`` was NEW when the 'head' target was created,
  169. else ``0``. If the ``policy`` was not set, the warning message for the policy
  170. will be emitted. This generator expression only works for a subset of
  171. policies.
  172. .. genex:: $<COMPILE_FEATURES:features>
  173. .. versionadded:: 3.1
  174. where ``features`` is a comma-spearated list.
  175. Evaluates to ``1`` if all of the ``features`` are available for the 'head'
  176. target, and ``0`` otherwise. If this expression is used while evaluating
  177. the link implementation of a target and if any dependency transitively
  178. increases the required :prop_tgt:`C_STANDARD` or :prop_tgt:`CXX_STANDARD`
  179. for the 'head' target, an error is reported. See the
  180. :manual:`cmake-compile-features(7)` manual for information on
  181. compile features and a list of supported compilers.
  182. .. _`Boolean COMPILE_LANGUAGE Generator Expression`:
  183. .. genex:: $<COMPILE_LANG_AND_ID:language,compiler_ids>
  184. .. versionadded:: 3.15
  185. ``1`` when the language used for compilation unit matches ``language`` and
  186. the CMake's compiler id of the language compiler matches any one of the
  187. entries in ``compiler_ids``, otherwise ``0``. This expression is a short form
  188. for the combination of ``$<COMPILE_LANGUAGE:language>`` and
  189. ``$<LANG_COMPILER_ID:compiler_ids>``. This expression may be used to specify
  190. compile options, compile definitions, and include directories for source files of a
  191. particular language and compiler combination in a target. For example:
  192. .. code-block:: cmake
  193. add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
  194. target_compile_definitions(myapp
  195. PRIVATE $<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang>:COMPILING_CXX_WITH_CLANG>
  196. $<$<COMPILE_LANG_AND_ID:CXX,Intel>:COMPILING_CXX_WITH_INTEL>
  197. $<$<COMPILE_LANG_AND_ID:C,Clang>:COMPILING_C_WITH_CLANG>
  198. )
  199. This specifies the use of different compile definitions based on both
  200. the compiler id and compilation language. This example will have a
  201. ``COMPILING_CXX_WITH_CLANG`` compile definition when Clang is the CXX
  202. compiler, and ``COMPILING_CXX_WITH_INTEL`` when Intel is the CXX compiler.
  203. Likewise when the C compiler is Clang it will only see the ``COMPILING_C_WITH_CLANG``
  204. definition.
  205. Without the ``COMPILE_LANG_AND_ID`` generator expression the same logic
  206. would be expressed as:
  207. .. code-block:: cmake
  208. target_compile_definitions(myapp
  209. PRIVATE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:AppleClang,Clang>>:COMPILING_CXX_WITH_CLANG>
  210. $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:Intel>>:COMPILING_CXX_WITH_INTEL>
  211. $<$<AND:$<COMPILE_LANGUAGE:C>,$<C_COMPILER_ID:Clang>>:COMPILING_C_WITH_CLANG>
  212. )
  213. .. genex:: $<COMPILE_LANGUAGE:languages>
  214. .. versionadded:: 3.3
  215. ``1`` when the language used for compilation unit matches any of the entries
  216. in ``languages``, otherwise ``0``. This expression may be used to specify
  217. compile options, compile definitions, and include directories for source files of a
  218. particular language in a target. For example:
  219. .. code-block:: cmake
  220. add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
  221. target_compile_options(myapp
  222. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
  223. )
  224. target_compile_definitions(myapp
  225. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
  226. $<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
  227. )
  228. target_include_directories(myapp
  229. PRIVATE $<$<COMPILE_LANGUAGE:CXX,CUDA>:/opt/foo/headers>
  230. )
  231. This specifies the use of the ``-fno-exceptions`` compile option,
  232. ``COMPILING_CXX`` compile definition, and ``cxx_headers`` include
  233. directory for C++ only (compiler id checks elided). It also specifies
  234. a ``COMPILING_CUDA`` compile definition for CUDA.
  235. Note that with :ref:`Visual Studio Generators` and :generator:`Xcode` there
  236. is no way to represent target-wide compile definitions or include directories
  237. separately for ``C`` and ``CXX`` languages.
  238. Also, with :ref:`Visual Studio Generators` there is no way to represent
  239. target-wide flags separately for ``C`` and ``CXX`` languages. Under these
  240. generators, expressions for both C and C++ sources will be evaluated
  241. using ``CXX`` if there are any C++ sources and otherwise using ``C``.
  242. A workaround is to create separate libraries for each source file language
  243. instead:
  244. .. code-block:: cmake
  245. add_library(myapp_c foo.c)
  246. add_library(myapp_cxx bar.cpp)
  247. target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
  248. add_executable(myapp main.cpp)
  249. target_link_libraries(myapp myapp_c myapp_cxx)
  250. .. _`Boolean LINK_LANGUAGE Generator Expression`:
  251. .. genex:: $<LINK_LANG_AND_ID:language,compiler_ids>
  252. .. versionadded:: 3.18
  253. ``1`` when the language used for link step matches ``language`` and the
  254. CMake's compiler id of the language linker matches any one of the entries
  255. in ``compiler_ids``, otherwise ``0``. This expression is a short form for the
  256. combination of ``$<LINK_LANGUAGE:language>`` and
  257. ``$<LANG_COMPILER_ID:compiler_ids>``. This expression may be used to specify
  258. link libraries, link options, link directories and link dependencies of a
  259. particular language and linker combination in a target. For example:
  260. .. code-block:: cmake
  261. add_library(libC_Clang ...)
  262. add_library(libCXX_Clang ...)
  263. add_library(libC_Intel ...)
  264. add_library(libCXX_Intel ...)
  265. add_executable(myapp main.c)
  266. if (CXX_CONFIG)
  267. target_sources(myapp PRIVATE file.cxx)
  268. endif()
  269. target_link_libraries(myapp
  270. PRIVATE $<$<LINK_LANG_AND_ID:CXX,Clang,AppleClang>:libCXX_Clang>
  271. $<$<LINK_LANG_AND_ID:C,Clang,AppleClang>:libC_Clang>
  272. $<$<LINK_LANG_AND_ID:CXX,Intel>:libCXX_Intel>
  273. $<$<LINK_LANG_AND_ID:C,Intel>:libC_Intel>)
  274. This specifies the use of different link libraries based on both the
  275. compiler id and link language. This example will have target ``libCXX_Clang``
  276. as link dependency when ``Clang`` or ``AppleClang`` is the ``CXX``
  277. linker, and ``libCXX_Intel`` when ``Intel`` is the ``CXX`` linker.
  278. Likewise when the ``C`` linker is ``Clang`` or ``AppleClang``, target
  279. ``libC_Clang`` will be added as link dependency and ``libC_Intel`` when
  280. ``Intel`` is the ``C`` linker.
  281. See :ref:`the note related to
  282. <Constraints LINK_LANGUAGE Generator Expression>`
  283. ``$<LINK_LANGUAGE:language>`` for constraints about the usage of this
  284. generator expression.
  285. .. genex:: $<LINK_LANGUAGE:languages>
  286. .. versionadded:: 3.18
  287. ``1`` when the language used for link step matches any of the entries
  288. in ``languages``, otherwise ``0``. This expression may be used to specify
  289. link libraries, link options, link directories and link dependencies of a
  290. particular language in a target. For example:
  291. .. code-block:: cmake
  292. add_library(api_C ...)
  293. add_library(api_CXX ...)
  294. add_library(api INTERFACE)
  295. target_link_options(api INTERFACE $<$<LINK_LANGUAGE:C>:-opt_c>
  296. $<$<LINK_LANGUAGE:CXX>:-opt_cxx>)
  297. target_link_libraries(api INTERFACE $<$<LINK_LANGUAGE:C>:api_C>
  298. $<$<LINK_LANGUAGE:CXX>:api_CXX>)
  299. add_executable(myapp1 main.c)
  300. target_link_options(myapp1 PRIVATE api)
  301. add_executable(myapp2 main.cpp)
  302. target_link_options(myapp2 PRIVATE api)
  303. This specifies to use the ``api`` target for linking targets ``myapp1`` and
  304. ``myapp2``. In practice, ``myapp1`` will link with target ``api_C`` and
  305. option ``-opt_c`` because it will use ``C`` as link language. And ``myapp2``
  306. will link with ``api_CXX`` and option ``-opt_cxx`` because ``CXX`` will be
  307. the link language.
  308. .. _`Constraints LINK_LANGUAGE Generator Expression`:
  309. .. note::
  310. To determine the link language of a target, it is required to collect,
  311. transitively, all the targets which will be linked to it. So, for link
  312. libraries properties, a double evaluation will be done. During the first
  313. evaluation, ``$<LINK_LANGUAGE:..>`` expressions will always return ``0``.
  314. The link language computed after this first pass will be used to do the
  315. second pass. To avoid inconsistency, it is required that the second pass
  316. do not change the link language. Moreover, to avoid unexpected
  317. side-effects, it is required to specify complete entities as part of the
  318. ``$<LINK_LANGUAGE:..>`` expression. For example:
  319. .. code-block:: cmake
  320. add_library(lib STATIC file.cxx)
  321. add_library(libother STATIC file.c)
  322. # bad usage
  323. add_executable(myapp1 main.c)
  324. target_link_libraries(myapp1 PRIVATE lib$<$<LINK_LANGUAGE:C>:other>)
  325. # correct usage
  326. add_executable(myapp2 main.c)
  327. target_link_libraries(myapp2 PRIVATE $<$<LINK_LANGUAGE:C>:libother>)
  328. In this example, for ``myapp1``, the first pass will, unexpectedly,
  329. determine that the link language is ``CXX`` because the evaluation of the
  330. generator expression will be an empty string so ``myapp1`` will depends on
  331. target ``lib`` which is ``C++``. On the contrary, for ``myapp2``, the first
  332. evaluation will give ``C`` as link language, so the second pass will
  333. correctly add target ``libother`` as link dependency.
  334. .. genex:: $<DEVICE_LINK:list>
  335. .. versionadded:: 3.18
  336. Returns the list if it is the device link step, an empty list otherwise.
  337. The device link step is controlled by :prop_tgt:`CUDA_SEPARABLE_COMPILATION`
  338. and :prop_tgt:`CUDA_RESOLVE_DEVICE_SYMBOLS` properties and
  339. policy :policy:`CMP0105`. This expression can only be used to specify link
  340. options.
  341. .. genex:: $<HOST_LINK:list>
  342. .. versionadded:: 3.18
  343. Returns the list if it is the normal link step, an empty list otherwise.
  344. This expression is mainly useful when a device link step is also involved
  345. (see ``$<DEVICE_LINK:list>`` generator expression). This expression can only
  346. be used to specify link options.
  347. String-Valued Generator Expressions
  348. ===================================
  349. These expressions expand to some string.
  350. For example,
  351. .. code-block:: cmake
  352. include_directories(/usr/include/$<CXX_COMPILER_ID>/)
  353. expands to ``/usr/include/GNU/`` or ``/usr/include/Clang/`` etc, depending on
  354. the compiler identifier.
  355. String-valued expressions may also be combined with other expressions.
  356. Here an example for a string-valued expression within a boolean expressions
  357. within a conditional expression:
  358. .. code-block:: cmake
  359. $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
  360. expands to ``OLD_COMPILER`` if the
  361. :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
  362. than 4.2.0.
  363. And here two nested string-valued expressions:
  364. .. code-block:: cmake
  365. -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
  366. generates a string of the entries in the :prop_tgt:`INCLUDE_DIRECTORIES` target
  367. property with each entry preceded by ``-I``.
  368. Expanding on the previous example, if one first wants to check if the
  369. ``INCLUDE_DIRECTORIES`` property is non-empty, then it is advisable to
  370. introduce a helper variable to keep the code readable:
  371. .. code-block:: cmake
  372. set(prop "$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>") # helper variable
  373. $<$<BOOL:${prop}>:-I$<JOIN:${prop}, -I>>
  374. The following string-valued generator expressions are available:
  375. Escaped Characters
  376. ------------------
  377. String literals to escape the special meaning a character would otherwise have:
  378. .. genex:: $<ANGLE-R>
  379. A literal ``>``. Used for example to compare strings that contain a ``>``.
  380. .. genex:: $<COMMA>
  381. A literal ``,``. Used for example to compare strings which contain a ``,``.
  382. .. genex:: $<SEMICOLON>
  383. A literal ``;``. Used to prevent list expansion on an argument with ``;``.
  384. .. _`Conditional Generator Expressions`:
  385. Conditional Expressions
  386. -----------------------
  387. Conditional generator expressions depend on a boolean condition
  388. that must be ``0`` or ``1``.
  389. .. genex:: $<condition:true_string>
  390. Evaluates to ``true_string`` if ``condition`` is ``1``.
  391. Otherwise evaluates to the empty string.
  392. .. genex:: $<IF:condition,true_string,false_string>
  393. .. versionadded:: 3.8
  394. Evaluates to ``true_string`` if ``condition`` is ``1``.
  395. Otherwise evaluates to ``false_string``.
  396. Typically, the ``condition`` is a :ref:`boolean generator expression
  397. <Boolean Generator Expressions>`. For instance,
  398. .. code-block:: cmake
  399. $<$<CONFIG:Debug>:DEBUG_MODE>
  400. expands to ``DEBUG_MODE`` when the ``Debug`` configuration is used, and
  401. otherwise expands to the empty string.
  402. .. _`String Transforming Generator Expressions`:
  403. String Transformations
  404. ----------------------
  405. .. genex:: $<JOIN:list,string>
  406. Joins the list with the content of ``string``.
  407. .. genex:: $<REMOVE_DUPLICATES:list>
  408. .. versionadded:: 3.15
  409. Removes duplicated items in the given ``list``. The relative order of items
  410. is preserved, but if duplicates are encountered, only the first instance is
  411. preserved.
  412. .. genex:: $<FILTER:list,INCLUDE|EXCLUDE,regex>
  413. .. versionadded:: 3.15
  414. Includes or removes items from ``list`` that match the regular expression ``regex``.
  415. .. genex:: $<LOWER_CASE:string>
  416. Content of ``string`` converted to lower case.
  417. .. genex:: $<UPPER_CASE:string>
  418. Content of ``string`` converted to upper case.
  419. .. genex:: $<GENEX_EVAL:expr>
  420. .. versionadded:: 3.12
  421. Content of ``expr`` evaluated as a generator expression in the current
  422. context. This enables consumption of generator expressions whose
  423. evaluation results itself in generator expressions.
  424. .. genex:: $<TARGET_GENEX_EVAL:tgt,expr>
  425. .. versionadded:: 3.12
  426. Content of ``expr`` evaluated as a generator expression in the context of
  427. ``tgt`` target. This enables consumption of custom target properties that
  428. themselves contain generator expressions.
  429. Having the capability to evaluate generator expressions is very useful when
  430. you want to manage custom properties supporting generator expressions.
  431. For example:
  432. .. code-block:: cmake
  433. add_library(foo ...)
  434. set_property(TARGET foo PROPERTY
  435. CUSTOM_KEYS $<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>
  436. )
  437. add_custom_target(printFooKeys
  438. COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_PROPERTY:foo,CUSTOM_KEYS>
  439. )
  440. This naive implementation of the ``printFooKeys`` custom command is wrong
  441. because ``CUSTOM_KEYS`` target property is not evaluated and the content
  442. is passed as is (i.e. ``$<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>``).
  443. To have the expected result (i.e. ``FOO_EXTRA_THINGS`` if config is
  444. ``Debug``), it is required to evaluate the output of
  445. ``$<TARGET_PROPERTY:foo,CUSTOM_KEYS>``:
  446. .. code-block:: cmake
  447. add_custom_target(printFooKeys
  448. COMMAND ${CMAKE_COMMAND} -E
  449. echo $<TARGET_GENEX_EVAL:foo,$<TARGET_PROPERTY:foo,CUSTOM_KEYS>>
  450. )
  451. Variable Queries
  452. ----------------
  453. .. genex:: $<CONFIG>
  454. Configuration name.
  455. .. genex:: $<CONFIGURATION>
  456. Configuration name. Deprecated since CMake 3.0. Use ``CONFIG`` instead.
  457. .. genex:: $<PLATFORM_ID>
  458. The current system's CMake platform id.
  459. See also the :variable:`CMAKE_SYSTEM_NAME` variable.
  460. .. genex:: $<C_COMPILER_ID>
  461. The CMake's compiler id of the C compiler used.
  462. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  463. .. genex:: $<CXX_COMPILER_ID>
  464. The CMake's compiler id of the CXX compiler used.
  465. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  466. .. genex:: $<CUDA_COMPILER_ID>
  467. The CMake's compiler id of the CUDA compiler used.
  468. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  469. .. genex:: $<OBJC_COMPILER_ID>
  470. .. versionadded:: 3.16
  471. The CMake's compiler id of the OBJC compiler used.
  472. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  473. .. genex:: $<OBJCXX_COMPILER_ID>
  474. .. versionadded:: 3.16
  475. The CMake's compiler id of the OBJCXX compiler used.
  476. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  477. .. genex:: $<Fortran_COMPILER_ID>
  478. The CMake's compiler id of the Fortran compiler used.
  479. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  480. .. genex:: $<HIP_COMPILER_ID>
  481. The CMake's compiler id of the HIP compiler used.
  482. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  483. .. genex:: $<ISPC_COMPILER_ID>
  484. .. versionadded:: 3.19
  485. The CMake's compiler id of the ISPC compiler used.
  486. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  487. .. genex:: $<C_COMPILER_VERSION>
  488. The version of the C compiler used.
  489. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  490. .. genex:: $<CXX_COMPILER_VERSION>
  491. The version of the CXX compiler used.
  492. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  493. .. genex:: $<CUDA_COMPILER_VERSION>
  494. The version of the CUDA compiler used.
  495. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  496. .. genex:: $<OBJC_COMPILER_VERSION>
  497. .. versionadded:: 3.16
  498. The version of the OBJC compiler used.
  499. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  500. .. genex:: $<OBJCXX_COMPILER_VERSION>
  501. .. versionadded:: 3.16
  502. The version of the OBJCXX compiler used.
  503. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  504. .. genex:: $<Fortran_COMPILER_VERSION>
  505. The version of the Fortran compiler used.
  506. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  507. .. genex:: $<HIP_COMPILER_VERSION>
  508. The version of the HIP compiler used.
  509. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  510. .. genex:: $<ISPC_COMPILER_VERSION>
  511. .. versionadded:: 3.19
  512. The version of the ISPC compiler used.
  513. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  514. .. genex:: $<COMPILE_LANGUAGE>
  515. .. versionadded:: 3.3
  516. The compile language of source files when evaluating compile options.
  517. See :ref:`the related boolean expression
  518. <Boolean COMPILE_LANGUAGE Generator Expression>`
  519. ``$<COMPILE_LANGUAGE:language>``
  520. for notes about the portability of this generator expression.
  521. .. genex:: $<LINK_LANGUAGE>
  522. .. versionadded:: 3.18
  523. The link language of target when evaluating link options.
  524. See :ref:`the related boolean expression
  525. <Boolean LINK_LANGUAGE Generator Expression>` ``$<LINK_LANGUAGE:language>``
  526. for notes about the portability of this generator expression.
  527. .. note::
  528. This generator expression is not supported by the link libraries
  529. properties to avoid side-effects due to the double evaluation of
  530. these properties.
  531. .. _`Target-Dependent Queries`:
  532. Target-Dependent Queries
  533. ------------------------
  534. These queries refer to a target ``tgt``. This can be any runtime artifact,
  535. namely:
  536. * an executable target created by :command:`add_executable`
  537. * a shared library target (``.so``, ``.dll`` but not their ``.lib`` import library)
  538. created by :command:`add_library`
  539. * a static library target created by :command:`add_library`
  540. In the following, "the ``tgt`` filename" means the name of the ``tgt``
  541. binary file. This has to be distinguished from "the target name",
  542. which is just the string ``tgt``.
  543. .. genex:: $<TARGET_NAME_IF_EXISTS:tgt>
  544. .. versionadded:: 3.12
  545. The target name ``tgt`` if the target exists, an empty string otherwise.
  546. Note that ``tgt`` is not added as a dependency of the target this
  547. expression is evaluated on.
  548. .. genex:: $<TARGET_FILE:tgt>
  549. Full path to the ``tgt`` binary file.
  550. .. genex:: $<TARGET_FILE_BASE_NAME:tgt>
  551. .. versionadded:: 3.15
  552. Base name of ``tgt``, i.e. ``$<TARGET_FILE_NAME:tgt>`` without prefix and
  553. suffix.
  554. For example, if the ``tgt`` filename is ``libbase.so``, the base name is ``base``.
  555. See also the :prop_tgt:`OUTPUT_NAME`, :prop_tgt:`ARCHIVE_OUTPUT_NAME`,
  556. :prop_tgt:`LIBRARY_OUTPUT_NAME` and :prop_tgt:`RUNTIME_OUTPUT_NAME`
  557. target properties and their configuration specific variants
  558. :prop_tgt:`OUTPUT_NAME_<CONFIG>`, :prop_tgt:`ARCHIVE_OUTPUT_NAME_<CONFIG>`,
  559. :prop_tgt:`LIBRARY_OUTPUT_NAME_<CONFIG>` and
  560. :prop_tgt:`RUNTIME_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_FILE_PREFIX:tgt>
  566. .. versionadded:: 3.15
  567. Prefix of the ``tgt`` filename (such as ``lib``).
  568. See also the :prop_tgt:`PREFIX` target property.
  569. Note that ``tgt`` is not added as a dependency of the target this
  570. expression is evaluated on.
  571. .. genex:: $<TARGET_FILE_SUFFIX:tgt>
  572. .. versionadded:: 3.15
  573. Suffix of the ``tgt`` filename (extension such as ``.so`` or ``.exe``).
  574. See also the :prop_tgt:`SUFFIX` target property.
  575. Note that ``tgt`` is not added as a dependency of the target this
  576. expression is evaluated on.
  577. .. genex:: $<TARGET_FILE_NAME:tgt>
  578. The ``tgt`` filename.
  579. Note that ``tgt`` is not added as a dependency of the target this
  580. expression is evaluated on (see policy :policy:`CMP0112`).
  581. .. genex:: $<TARGET_FILE_DIR:tgt>
  582. Directory of the ``tgt`` binary file.
  583. Note that ``tgt`` is not added as a dependency of the target this
  584. expression is evaluated on (see policy :policy:`CMP0112`).
  585. .. genex:: $<TARGET_LINKER_FILE:tgt>
  586. File used when linking to the ``tgt`` target. This will usually
  587. be the library that ``tgt`` represents (``.a``, ``.lib``, ``.so``),
  588. but for a shared library on DLL platforms, it would be the ``.lib``
  589. import library associated with the DLL.
  590. .. genex:: $<TARGET_LINKER_FILE_BASE_NAME:tgt>
  591. .. versionadded:: 3.15
  592. Base name of file used to link the target ``tgt``, i.e.
  593. ``$<TARGET_LINKER_FILE_NAME:tgt>`` without prefix and suffix. For example,
  594. if target file name is ``libbase.a``, the base name is ``base``.
  595. See also the :prop_tgt:`OUTPUT_NAME`, :prop_tgt:`ARCHIVE_OUTPUT_NAME`,
  596. and :prop_tgt:`LIBRARY_OUTPUT_NAME` target properties and their configuration
  597. specific variants :prop_tgt:`OUTPUT_NAME_<CONFIG>`,
  598. :prop_tgt:`ARCHIVE_OUTPUT_NAME_<CONFIG>` and
  599. :prop_tgt:`LIBRARY_OUTPUT_NAME_<CONFIG>`.
  600. The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
  601. properties can also be considered.
  602. Note that ``tgt`` is not added as a dependency of the target this
  603. expression is evaluated on.
  604. .. genex:: $<TARGET_LINKER_FILE_PREFIX:tgt>
  605. .. versionadded:: 3.15
  606. Prefix of file used to link target ``tgt``.
  607. See also the :prop_tgt:`PREFIX` and :prop_tgt:`IMPORT_PREFIX` target
  608. properties.
  609. Note that ``tgt`` is not added as a dependency of the target this
  610. expression is evaluated on.
  611. .. genex:: $<TARGET_LINKER_FILE_SUFFIX:tgt>
  612. .. versionadded:: 3.15
  613. Suffix of file used to link where ``tgt`` is the name of a target.
  614. The suffix corresponds to the file extension (such as ".so" or ".lib").
  615. See also the :prop_tgt:`SUFFIX` and :prop_tgt:`IMPORT_SUFFIX` target
  616. properties.
  617. Note that ``tgt`` is not added as a dependency of the target this
  618. expression is evaluated on.
  619. .. genex:: $<TARGET_LINKER_FILE_NAME:tgt>
  620. Name of file used to link target ``tgt``.
  621. Note that ``tgt`` is not added as a dependency of the target this
  622. expression is evaluated on (see policy :policy:`CMP0112`).
  623. .. genex:: $<TARGET_LINKER_FILE_DIR:tgt>
  624. Directory of file used to link target ``tgt``.
  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_SONAME_FILE:tgt>
  628. File with soname (``.so.3``) where ``tgt`` is the name of a target.
  629. .. genex:: $<TARGET_SONAME_FILE_NAME:tgt>
  630. Name of file with soname (``.so.3``).
  631. Note that ``tgt`` is not added as a dependency of the target this
  632. expression is evaluated on (see policy :policy:`CMP0112`).
  633. .. genex:: $<TARGET_SONAME_FILE_DIR:tgt>
  634. Directory of with soname (``.so.3``).
  635. Note that ``tgt`` is not added as a dependency of the target this
  636. expression is evaluated on (see policy :policy:`CMP0112`).
  637. .. genex:: $<TARGET_PDB_FILE:tgt>
  638. .. versionadded:: 3.1
  639. Full path to the linker generated program database file (.pdb)
  640. where ``tgt`` is the name of a target.
  641. See also the :prop_tgt:`PDB_NAME` and :prop_tgt:`PDB_OUTPUT_DIRECTORY`
  642. target properties and their configuration specific variants
  643. :prop_tgt:`PDB_NAME_<CONFIG>` and :prop_tgt:`PDB_OUTPUT_DIRECTORY_<CONFIG>`.
  644. .. genex:: $<TARGET_PDB_FILE_BASE_NAME:tgt>
  645. .. versionadded:: 3.15
  646. Base name of the linker generated program database file (.pdb)
  647. where ``tgt`` is the name of a target.
  648. The base name corresponds to the target PDB file name (see
  649. ``$<TARGET_PDB_FILE_NAME:tgt>``) without prefix and suffix. For example,
  650. if target file name is ``base.pdb``, the base name is ``base``.
  651. See also the :prop_tgt:`PDB_NAME` target property and its configuration
  652. specific variant :prop_tgt:`PDB_NAME_<CONFIG>`.
  653. The :prop_tgt:`<CONFIG>_POSTFIX` and :prop_tgt:`DEBUG_POSTFIX` target
  654. properties can also be considered.
  655. Note that ``tgt`` is not added as a dependency of the target this
  656. expression is evaluated on.
  657. .. genex:: $<TARGET_PDB_FILE_NAME:tgt>
  658. .. versionadded:: 3.1
  659. Name of the linker generated program database file (.pdb).
  660. Note that ``tgt`` is not added as a dependency of the target this
  661. expression is evaluated on (see policy :policy:`CMP0112`).
  662. .. genex:: $<TARGET_PDB_FILE_DIR:tgt>
  663. .. versionadded:: 3.1
  664. Directory of the linker generated program database file (.pdb).
  665. Note that ``tgt`` is not added as a dependency of the target this
  666. expression is evaluated on (see policy :policy:`CMP0112`).
  667. .. genex:: $<TARGET_BUNDLE_DIR:tgt>
  668. .. versionadded:: 3.9
  669. Full path to the bundle directory (``/path/to/my.app``,
  670. ``/path/to/my.framework``, or ``/path/to/my.bundle``),
  671. where ``tgt`` is the name of a target.
  672. Note that ``tgt`` is not added as a dependency of the target this
  673. expression is evaluated on (see policy :policy:`CMP0112`).
  674. .. genex:: $<TARGET_BUNDLE_DIR_NAME:tgt>
  675. .. versionadded:: 3.24
  676. Name of the bundle directory (``my.app``, ``my.framework``, or
  677. ``my.bundle``), where ``tgt`` is the name of a target.
  678. Note that ``tgt`` is not added as a dependency of the target this
  679. expression is evaluated on (see policy :policy:`CMP0112`).
  680. .. genex:: $<TARGET_BUNDLE_CONTENT_DIR:tgt>
  681. .. versionadded:: 3.9
  682. Full path to the bundle content directory where ``tgt`` is the name of a
  683. target. For the macOS SDK it leads to ``/path/to/my.app/Contents``,
  684. ``/path/to/my.framework``, or ``/path/to/my.bundle/Contents``.
  685. For all other SDKs (e.g. iOS) it leads to ``/path/to/my.app``,
  686. ``/path/to/my.framework``, or ``/path/to/my.bundle`` due to the flat
  687. bundle structure.
  688. Note that ``tgt`` is not added as a dependency of the target this
  689. expression is evaluated on (see policy :policy:`CMP0112`).
  690. .. genex:: $<TARGET_PROPERTY:tgt,prop>
  691. Value of the property ``prop`` on the target ``tgt``.
  692. Note that ``tgt`` is not added as a dependency of the target this
  693. expression is evaluated on.
  694. .. genex:: $<TARGET_PROPERTY:prop>
  695. Value of the property ``prop`` on the target for which the expression
  696. is being evaluated. Note that for generator expressions in
  697. :ref:`Target Usage Requirements` this is the consuming target rather
  698. than the target specifying the requirement.
  699. .. genex:: $<TARGET_RUNTIME_DLLS:tgt>
  700. .. versionadded:: 3.21
  701. List of DLLs that the target depends on at runtime. This is determined by
  702. the locations of all the ``SHARED`` targets in the target's transitive
  703. dependencies. Using this generator expression on targets other than
  704. executables, ``SHARED`` libraries, and ``MODULE`` libraries is an error. On
  705. non-DLL platforms, it evaluates to an empty string.
  706. This generator expression can be used to copy all of the DLLs that a target
  707. depends on into its output directory in a ``POST_BUILD`` custom command. For
  708. example:
  709. .. code-block:: cmake
  710. find_package(foo CONFIG REQUIRED) # package generated by install(EXPORT)
  711. add_executable(exe main.c)
  712. target_link_libraries(exe PRIVATE foo::foo foo::bar)
  713. add_custom_command(TARGET exe POST_BUILD
  714. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:exe> $<TARGET_FILE_DIR:exe>
  715. COMMAND_EXPAND_LISTS
  716. )
  717. .. note::
  718. :ref:`Imported Targets` are supported only if they know the location
  719. of their ``.dll`` files. An imported ``SHARED`` library must have
  720. :prop_tgt:`IMPORTED_LOCATION` set to its ``.dll`` file. See the
  721. :ref:`add_library imported libraries <add_library imported libraries>`
  722. section for details. Many :ref:`Find Modules` produce imported targets
  723. with the ``UNKNOWN`` type and therefore will be ignored.
  724. .. genex:: $<INSTALL_PREFIX>
  725. Content of the install prefix when the target is exported via
  726. :command:`install(EXPORT)`, or when evaluated in the
  727. :prop_tgt:`INSTALL_NAME_DIR` property or the ``INSTALL_NAME_DIR`` argument of
  728. :command:`install(RUNTIME_DEPENDENCY_SET)`, and empty otherwise.
  729. Output-Related Expressions
  730. --------------------------
  731. .. genex:: $<TARGET_NAME:...>
  732. Marks ``...`` as being the name of a target. This is required if exporting
  733. targets to multiple dependent export sets. The ``...`` must be a literal
  734. name of a target- it may not contain generator expressions.
  735. .. genex:: $<LINK_ONLY:...>
  736. .. versionadded:: 3.1
  737. Content of ``...``, except while collecting :ref:`Target Usage Requirements`,
  738. in which case it is the empty string. This is intended for use in an
  739. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property, typically populated
  740. via the :command:`target_link_libraries` command, to specify private link
  741. dependencies without other usage requirements.
  742. .. versionadded:: 3.24
  743. ``LINK_ONLY`` may also be used in a :prop_tgt:`LINK_LIBRARIES` target
  744. property. See policy :policy:`CMP0131`.
  745. .. genex:: $<LINK_LIBRARY:feature,library-list>
  746. .. versionadded:: 3.24
  747. Manage how libraries are specified during the link step.
  748. This expression may be used to specify how to link libraries in a target.
  749. For example:
  750. .. code-block:: cmake
  751. add_library(lib1 STATIC ...)
  752. add_library(lib2 ...)
  753. target_link_libraries(lib2 PRIVATE "$<LINK_LIBRARY:load_archive,lib1>")
  754. This specify to use the ``lib1`` target with feature ``load_archive`` for
  755. linking target ``lib2``. The feature must have be defined by
  756. :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>` variable or, if
  757. :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED` is false,
  758. by :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>` variable.
  759. .. note::
  760. The evaluation of this generator expression will use, for the following
  761. variables, the values defined at the level of the creation of the target:
  762. * :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED`
  763. * :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>`
  764. * :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED`
  765. * :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>`
  766. This expression can only be used to specify link libraries (i.e. part of
  767. :command:`link_libraries` or :command:`target_link_libraries` commands and
  768. :prop_tgt:`LINK_LIBRARIES` or :prop_tgt:`INTERFACE_LINK_LIBRARIES` target
  769. properties).
  770. .. note::
  771. If this expression appears in the :prop_tgt:`INTERFACE_LINK_LIBRARIES`
  772. property of a target, it will be included in the imported target generated
  773. by :command:`install(EXPORT)` command. It is the responsibility of the
  774. environment consuming this import to define the link feature used by this
  775. expression.
  776. The ``library-list`` argument can hold CMake targets or external libraries.
  777. Any CMake target of type :ref:`OBJECT <Object Libraries>` or
  778. :ref:`INTERFACE <Interface Libraries>` will be ignored by this expression and
  779. will be handled in the standard way.
  780. Each target or external library involved in the link step must have only one
  781. kind of feature (the absence of feature is also incompatible with any
  782. feature). For example:
  783. .. code-block:: cmake
  784. add_library(lib1 ...)
  785. add_library(lib2 ...)
  786. target_link_libraries(lib2 PUBLIC "$<LINK_LIBRARY:feature1,lib1>")
  787. add_library(lib3 ...)
  788. target_link_libraries(lib3 PRIVATE lib1 lib2)
  789. # an error will be raised here because lib1 has two different features
  790. To resolve such incompatibilities, the :prop_tgt:`LINK_LIBRARY_OVERRIDE`
  791. and :prop_tgt:`LINK_LIBRARY_OVERRIDE_<LIBRARY>` target properties can be
  792. used.
  793. .. note::
  794. This expression does not guarantee that the list of specified libraries
  795. will be kept grouped. So, to manage constructs like ``start-group`` and
  796. ``end-group``, as supported by ``GNU ld``, the :genex:`LINK_GROUP`
  797. generator expression can be used.
  798. CMake pre-defines some features of general interest:
  799. .. include:: ../variable/LINK_LIBRARY_PREDEFINED_FEATURES.txt
  800. .. genex:: $<LINK_GROUP:feature,library-list>
  801. .. versionadded:: 3.24
  802. Manage the grouping of libraries during the link step.
  803. This expression may be used to specify how to kept groups of libraries during
  804. the link of a target.
  805. For example:
  806. .. code-block:: cmake
  807. add_library(lib1 STATIC ...)
  808. add_library(lib2 ...)
  809. target_link_libraries(lib2 PRIVATE "$<LINK_GROUP:cross_refs,lib1,external>")
  810. This specify to use the ``lib1`` target and ``external`` library with the
  811. group feature ``cross_refs`` for linking target ``lib2``. The feature must
  812. have be defined by :variable:`CMAKE_<LANG>_LINK_GROUP_USING_<FEATURE>`
  813. variable or, if :variable:`CMAKE_<LANG>_LINK_GROUP_USING_<FEATURE>_SUPPORTED`
  814. is false, by :variable:`CMAKE_LINK_GROUP_USING_<FEATURE>` variable.
  815. .. note::
  816. The evaluation of this generator expression will use, for the following
  817. variables, the values defined at the level of the creation of the target:
  818. * :variable:`CMAKE_<LANG>_LINK_GROUP_USING_<FEATURE>_SUPPORTED`
  819. * :variable:`CMAKE_<LANG>_LINK_GROUP_USING_<FEATURE>`
  820. * :variable:`CMAKE_LINK_GROUP_USING_<FEATURE>_SUPPORTED`
  821. * :variable:`CMAKE_LINK_GROUP_USING_<FEATURE>`
  822. This expression can only be used to specify link libraries (i.e. part of
  823. :command:`link_libraries` or :command:`target_link_libraries` commands and
  824. :prop_tgt:`LINK_LIBRARIES` or :prop_tgt:`INTERFACE_LINK_LIBRARIES` target
  825. properties).
  826. .. note::
  827. If this expression appears in the :prop_tgt:`INTERFACE_LINK_LIBRARIES`
  828. property of a target, it will be included in the imported target generated
  829. by :command:`install(EXPORT)` command. It is the responsibility of the
  830. environment consuming this import to define the link feature used by this
  831. expression.
  832. The ``library-list`` argument can hold CMake targets or external libraries.
  833. Any CMake target of type :ref:`OBJECT <Object Libraries>` or
  834. :ref:`INTERFACE <Interface Libraries>` will be ignored by this expression and
  835. will be handled in the standard way.
  836. .. note::
  837. This expression is compatible with the :genex:`LINK_LIBRARY` generator
  838. expression. The libraries involved in a group can be specified using the
  839. :genex:`LINK_LIBRARY` generator expression.
  840. Each target or external library involved in the link step can be part of
  841. different groups as far as these groups use the same feature, so mixing
  842. different group features for the same target or library is forbidden. The
  843. different groups will be part of the link step.
  844. .. code-block:: cmake
  845. add_library(lib1 ...)
  846. add_library(lib2 ...)
  847. add_library(lib3 ...)
  848. target_link_libraries(lib3 PUBLIC "$<LINK_GROUP:feature1,lib1,lib2>")
  849. add_library(lib4 ...)
  850. target_link_libraries(lib4 PRIVATE "$<LINK_GROUP:feature1,lib1,lib3>")
  851. # lib4 will be linked with the groups {lib1,lib2} and {lib1,lib3}
  852. add_library(lib5 ...)
  853. target_link_libraries(lib5 PRIVATE "$<LINK_GROUP:feature2,lib1,lib3>")
  854. # an error will be raised here because lib1 is part of two groups with
  855. # different features
  856. When a target or an external library is involved in the link step as part of
  857. a group and also as standalone, any occurrence of the standalone link item
  858. will be replaced by the group or groups it belong to.
  859. .. code-block:: cmake
  860. add_library(lib1 ...)
  861. add_library(lib2 ...)
  862. add_library(lib3 ...)
  863. target_link_libraries(lib3 PUBLIC lib1)
  864. add_library(lib4 ...)
  865. target_link_libraries(lib4 PRIVATE lib3 "$<LINK_GROUP:feature1,lib1,lib2>")
  866. # lib4 will only be linked with lib3 and the group {lib1,lib2}
  867. This example will be "re-written" by CMake in the following form:
  868. .. code-block:: cmake
  869. add_library(lib1 ...)
  870. add_library(lib2 ...)
  871. add_library(lib3 ...)
  872. target_link_libraries(lib3 PUBLIC "$<LINK_GROUP:feature1,lib1,lib2>")
  873. add_library(lib4 ...)
  874. target_link_libraries(lib4 PRIVATE lib3 "$<LINK_GROUP:feature1,lib1,lib2>")
  875. # lib4 will only be linked with lib3 and the group {lib1,lib2}
  876. Be aware that the precedence of the group over the standalone link item can
  877. result in some circular dependency between groups, which will raise an
  878. error because circular dependencies are not allowed for groups.
  879. .. code-block:: cmake
  880. add_library(lib1A ...)
  881. add_library(lib1B ...)
  882. add_library(lib2A ...)
  883. add_library(lib2B ...)
  884. target_link_libraries(lib1A PUBLIC lib2A)
  885. target_link_libraries(lib2B PUBLIC lib1B)
  886. add_library(lib ...)
  887. target_link_libraries(lib3 PRIVATE "$<LINK_GROUP:feat,lib1A,lib1B>"
  888. "$<LINK_GROUP:feat,lib2A,lib2B>")
  889. This example will be "re-written" by CMake in the following form:
  890. .. code-block:: cmake
  891. add_library(lib1A ...)
  892. add_library(lib1B ...)
  893. add_library(lib2A ...)
  894. add_library(lib2B ...)
  895. target_link_libraries(lib1A PUBLIC "$<LINK_GROUP:feat,lib2A,lib2B>")
  896. target_link_libraries(lib2B PUBLIC "$<LINK_GROUP:feat,lib1A,lib1B>")
  897. add_library(lib ...)
  898. target_link_libraries(lib3 PRIVATE "$<LINK_GROUP:feat,lib1A,lib1B>"
  899. "$<LINK_GROUP:feat,lib2A,lib2B>")
  900. So, we have a circular dependency between groups ``{lib1A,lib1B}`` and
  901. ``{lib2A,lib2B}``.
  902. CMake pre-defines some features of general interest:
  903. .. include:: ../variable/LINK_GROUP_PREDEFINED_FEATURES.txt
  904. .. genex:: $<INSTALL_INTERFACE:...>
  905. Content of ``...`` when the property is exported using :command:`install(EXPORT)`,
  906. and empty otherwise.
  907. .. genex:: $<BUILD_INTERFACE:...>
  908. Content of ``...`` when the property is exported using :command:`export`, or
  909. when the target is used by another target in the same buildsystem. Expands to
  910. the empty string otherwise.
  911. .. genex:: $<MAKE_C_IDENTIFIER:...>
  912. Content of ``...`` converted to a C identifier. The conversion follows the
  913. same behavior as :command:`string(MAKE_C_IDENTIFIER)`.
  914. .. genex:: $<TARGET_OBJECTS:objLib>
  915. .. versionadded:: 3.1
  916. List of objects resulting from build of ``objLib``.
  917. .. genex:: $<SHELL_PATH:...>
  918. .. versionadded:: 3.4
  919. Content of ``...`` converted to shell path style. For example, slashes are
  920. converted to backslashes in Windows shells and drive letters are converted
  921. to posix paths in MSYS shells. The ``...`` must be an absolute path.
  922. .. versionadded:: 3.14
  923. The ``...`` may be a :ref:`semicolon-separated list <CMake Language Lists>`
  924. of paths, in which case each path is converted individually and a result
  925. list is generated using the shell path separator (``:`` on POSIX and
  926. ``;`` on Windows). Be sure to enclose the argument containing this genex
  927. in double quotes in CMake source code so that ``;`` does not split arguments.
  928. .. genex:: $<OUTPUT_CONFIG:...>
  929. .. versionadded:: 3.20
  930. Only valid in :command:`add_custom_command` and :command:`add_custom_target`
  931. as the outer-most generator expression in an argument.
  932. With the :generator:`Ninja Multi-Config` generator, generator expressions
  933. in ``...`` are evaluated using the custom command's "output config".
  934. With other generators, the content of ``...`` is evaluated normally.
  935. .. genex:: $<COMMAND_CONFIG:...>
  936. .. versionadded:: 3.20
  937. Only valid in :command:`add_custom_command` and :command:`add_custom_target`
  938. as the outer-most generator expression in an argument.
  939. With the :generator:`Ninja Multi-Config` generator, generator expressions
  940. in ``...`` are evaluated using the custom command's "command config".
  941. With other generators, the content of ``...`` is evaluated normally.
  942. Debugging
  943. =========
  944. Since generator expressions are evaluated during generation of the buildsystem,
  945. and not during processing of ``CMakeLists.txt`` files, it is not possible to
  946. inspect their result with the :command:`message()` command.
  947. One possible way to generate debug messages is to add a custom target,
  948. .. code-block:: cmake
  949. add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")
  950. The shell command ``make genexdebug`` (invoked after execution of ``cmake``)
  951. would then print the result of ``$<...>``.
  952. Another way is to write debug messages to a file:
  953. .. code-block:: cmake
  954. file(GENERATE OUTPUT filename CONTENT "$<...>")