cmake-generator-expressions.7.rst 18 KB

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