cmake-generator-expressions.7.rst 16 KB

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