cmake-generator-expressions.7.rst 55 KB

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