cmake-generator-expressions.7.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. This means that they enable conditional linking, conditional
  17. definitions used when compiling, and conditional include directories and
  18. more. The conditions may be based on the build configuration, target
  19. properties, platform information or any other queryable information.
  20. Logical Expressions
  21. ===================
  22. Logical expressions are used to create conditional output. The basic
  23. expressions are the ``0`` and ``1`` expressions. Because other logical
  24. expressions evaluate to either ``0`` or ``1``, they can be composed to
  25. create conditional output::
  26. $<$<CONFIG:Debug>:DEBUG_MODE>
  27. expands to ``DEBUG_MODE`` when the ``Debug`` configuration is used, and
  28. otherwise expands to nothing.
  29. Available logical expressions are:
  30. ``$<BOOL:...>``
  31. ``1`` if the ``...`` is true, else ``0``
  32. ``$<AND:?[,?]...>``
  33. ``1`` if all ``?`` are ``1``, else ``0``
  34. The ``?`` must always be either ``0`` or ``1`` in boolean expressions.
  35. ``$<OR:?[,?]...>``
  36. ``0`` if all ``?`` are ``0``, else ``1``
  37. ``$<NOT:?>``
  38. ``0`` if ``?`` is ``1``, else ``1``
  39. ``$<IF:?,true-value...,false-value...>```
  40. ``true-value...`` if ``?`` is ``1``, ``false-value...`` if ``?`` is ``0``
  41. ``$<STREQUAL:a,b>``
  42. ``1`` if ``a`` is STREQUAL ``b``, else ``0``
  43. ``$<EQUAL:a,b>``
  44. ``1`` if ``a`` is EQUAL ``b`` in a numeric comparison, else ``0``
  45. ``$<CONFIG:cfg>``
  46. ``1`` if config is ``cfg``, else ``0``. This is a case-insensitive comparison.
  47. The mapping in :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` is also considered by
  48. this expression when it is evaluated on a property on an :prop_tgt:`IMPORTED`
  49. target.
  50. ``$<PLATFORM_ID:comp>``
  51. ``1`` if the CMake-id of the platform matches ``comp``, otherwise ``0``.
  52. ``$<C_COMPILER_ID:comp>``
  53. ``1`` if the CMake-id of the C compiler matches ``comp``, otherwise ``0``.
  54. ``$<CXX_COMPILER_ID:comp>``
  55. ``1`` if the CMake-id of the CXX compiler matches ``comp``, otherwise ``0``.
  56. ``$<VERSION_LESS:v1,v2>``
  57. ``1`` if ``v1`` is a version less than ``v2``, else ``0``.
  58. ``$<VERSION_GREATER:v1,v2>``
  59. ``1`` if ``v1`` is a version greater than ``v2``, else ``0``.
  60. ``$<VERSION_EQUAL:v1,v2>``
  61. ``1`` if ``v1`` is the same version as ``v2``, else ``0``.
  62. ``$<VERSION_LESS_EQUAL:v1,v2>``
  63. ``1`` if ``v1`` is a version less than or equal to ``v2``, else ``0``.
  64. ``$<VERSION_GREATER_EQUAL:v1,v2>``
  65. ``1`` if ``v1`` is a version greater than or equal to ``v2``, else ``0``.
  66. ``$<C_COMPILER_VERSION:ver>``
  67. ``1`` if the version of the C compiler matches ``ver``, otherwise ``0``.
  68. ``$<CXX_COMPILER_VERSION:ver>``
  69. ``1`` if the version of the CXX compiler matches ``ver``, otherwise ``0``.
  70. ``$<TARGET_POLICY:pol>``
  71. ``1`` if the policy ``pol`` was NEW when the 'head' target was created,
  72. else ``0``. If the policy was not set, the warning message for the policy
  73. will be emitted. This generator expression only works for a subset of
  74. policies.
  75. ``$<COMPILE_FEATURES:feature[,feature]...>``
  76. ``1`` if all of the ``feature`` features are available for the 'head'
  77. target, and ``0`` otherwise. If this expression is used while evaluating
  78. the link implementation of a target and if any dependency transitively
  79. increases the required :prop_tgt:`C_STANDARD` or :prop_tgt:`CXX_STANDARD`
  80. for the 'head' target, an error is reported. See the
  81. :manual:`cmake-compile-features(7)` manual for information on
  82. compile features and a list of supported compilers.
  83. ``$<COMPILE_LANGUAGE:lang>``
  84. ``1`` when the language used for compilation unit matches ``lang``,
  85. otherwise ``0``. This expression used to specify compile options for
  86. source files of a particular language in a target. For example, to specify
  87. the use of the ``-fno-exceptions`` compile option (compiler id checks
  88. elided):
  89. .. code-block:: cmake
  90. add_executable(myapp main.cpp foo.c bar.cpp)
  91. target_compile_options(myapp
  92. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
  93. )
  94. This generator expression has limited use because it is not possible to
  95. use it with the Visual Studio generators. Portable buildsystems would
  96. not use this expression, and would create separate libraries for each
  97. source file language instead:
  98. .. code-block:: cmake
  99. add_library(myapp_c foo.c)
  100. add_library(myapp_cxx foo.c)
  101. target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
  102. add_executable(myapp main.cpp)
  103. target_link_libraries(myapp myapp_c myapp_cxx)
  104. The ``Makefile`` and ``Ninja`` based generators can also use this
  105. expression to specify compile-language specific compile definitions
  106. and include directories:
  107. .. code-block:: cmake
  108. add_executable(myapp main.cpp foo.c bar.cpp)
  109. target_compile_definitions(myapp
  110. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
  111. )
  112. target_include_directories(myapp
  113. PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/opt/foo/cxx_headers>
  114. )
  115. Informational Expressions
  116. =========================
  117. These expressions expand to some information. The information may be used
  118. directly, eg::
  119. include_directories(/usr/include/$<CXX_COMPILER_ID>/)
  120. expands to ``/usr/include/GNU/`` or ``/usr/include/Clang/`` etc, depending on
  121. the Id of the compiler.
  122. These expressions may also may be combined with logical expressions::
  123. $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
  124. expands to ``OLD_COMPILER`` if the
  125. :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
  126. than 4.2.0.
  127. Available informational expressions are:
  128. ``$<CONFIGURATION>``
  129. Configuration name. Deprecated. Use ``CONFIG`` instead.
  130. ``$<CONFIG>``
  131. Configuration name
  132. ``$<PLATFORM_ID>``
  133. The CMake-id of the platform.
  134. See also the :variable:`CMAKE_SYSTEM_NAME` variable.
  135. ``$<C_COMPILER_ID>``
  136. The CMake-id of the C compiler used.
  137. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  138. ``$<CXX_COMPILER_ID>``
  139. The CMake-id of the CXX compiler used.
  140. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  141. ``$<C_COMPILER_VERSION>``
  142. The version of the C compiler used.
  143. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  144. ``$<CXX_COMPILER_VERSION>``
  145. The version of the CXX compiler used.
  146. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  147. ``$<TARGET_FILE:tgt>``
  148. Full path to main file (.exe, .so.1.2, .a) where ``tgt`` is the name of a target.
  149. ``$<TARGET_FILE_NAME:tgt>``
  150. Name of main file (.exe, .so.1.2, .a).
  151. ``$<TARGET_FILE_DIR:tgt>``
  152. Directory of main file (.exe, .so.1.2, .a).
  153. ``$<TARGET_LINKER_FILE:tgt>``
  154. File used to link (.a, .lib, .so) where ``tgt`` is the name of a target.
  155. ``$<TARGET_LINKER_FILE_NAME:tgt>``
  156. Name of file used to link (.a, .lib, .so).
  157. ``$<TARGET_LINKER_FILE_DIR:tgt>``
  158. Directory of file used to link (.a, .lib, .so).
  159. ``$<TARGET_SONAME_FILE:tgt>``
  160. File with soname (.so.3) where ``tgt`` is the name of a target.
  161. ``$<TARGET_SONAME_FILE_NAME:tgt>``
  162. Name of file with soname (.so.3).
  163. ``$<TARGET_SONAME_FILE_DIR:tgt>``
  164. Directory of with soname (.so.3).
  165. ``$<TARGET_PDB_FILE:tgt>``
  166. Full path to the linker generated program database file (.pdb)
  167. where ``tgt`` is the name of a target.
  168. See also the :prop_tgt:`PDB_NAME` and :prop_tgt:`PDB_OUTPUT_DIRECTORY`
  169. target properties and their configuration specific variants
  170. :prop_tgt:`PDB_NAME_<CONFIG>` and :prop_tgt:`PDB_OUTPUT_DIRECTORY_<CONFIG>`.
  171. ``$<TARGET_PDB_FILE_NAME:tgt>``
  172. Name of the linker generated program database file (.pdb).
  173. ``$<TARGET_PDB_FILE_DIR:tgt>``
  174. Directory of the linker generated program database file (.pdb).
  175. ``$<TARGET_PROPERTY:tgt,prop>``
  176. Value of the property ``prop`` on the target ``tgt``.
  177. Note that ``tgt`` is not added as a dependency of the target this
  178. expression is evaluated on.
  179. ``$<TARGET_PROPERTY:prop>``
  180. Value of the property ``prop`` on the target on which the generator
  181. expression is evaluated.
  182. ``$<INSTALL_PREFIX>``
  183. Content of the install prefix when the target is exported via
  184. :command:`install(EXPORT)` and empty otherwise.
  185. ``$<COMPILE_LANGUAGE>``
  186. The compile language of source files when evaluating compile options. See
  187. the unary version for notes about portability of this generator
  188. expression.
  189. Output Expressions
  190. ==================
  191. These expressions generate output, in some cases depending on an input. These
  192. expressions may be combined with other expressions for information or logical
  193. comparison::
  194. -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
  195. generates a string of the entries in the :prop_tgt:`INCLUDE_DIRECTORIES` target
  196. property with each entry preceded by ``-I``. Note that a more-complete use
  197. in this situation would require first checking if the INCLUDE_DIRECTORIES
  198. property is non-empty::
  199. $<$<BOOL:${prop}>:-I$<JOIN:${prop}, -I>>
  200. where ``${prop}`` refers to a helper variable::
  201. set(prop "$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>")
  202. Available output expressions are:
  203. ``$<0:...>``
  204. Empty string (ignores ``...``)
  205. ``$<1:...>``
  206. Content of ``...``
  207. ``$<JOIN:list,...>``
  208. Joins the list with the content of ``...``
  209. ``$<ANGLE-R>``
  210. A literal ``>``. Used to compare strings which contain a ``>`` for example.
  211. ``$<COMMA>``
  212. A literal ``,``. Used to compare strings which contain a ``,`` for example.
  213. ``$<SEMICOLON>``
  214. A literal ``;``. Used to prevent list expansion on an argument with ``;``.
  215. ``$<TARGET_NAME:...>``
  216. Marks ``...`` as being the name of a target. This is required if exporting
  217. targets to multiple dependent export sets. The ``...`` must be a literal
  218. name of a target- it may not contain generator expressions.
  219. ``$<LINK_ONLY:...>``
  220. Content of ``...`` except when evaluated in a link interface while
  221. propagating :ref:`Target Usage Requirements`, in which case it is the
  222. empty string.
  223. Intended for use only in an :prop_tgt:`INTERFACE_LINK_LIBRARIES` target
  224. property, perhaps via the :command:`target_link_libraries` command,
  225. to specify private link dependencies without other usage requirements.
  226. ``$<INSTALL_INTERFACE:...>``
  227. Content of ``...`` when the property is exported using :command:`install(EXPORT)`,
  228. and empty otherwise.
  229. ``$<BUILD_INTERFACE:...>``
  230. Content of ``...`` when the property is exported using :command:`export`, or
  231. when the target is used by another target in the same buildsystem. Expands to
  232. the empty string otherwise.
  233. ``$<LOWER_CASE:...>``
  234. Content of ``...`` converted to lower case.
  235. ``$<UPPER_CASE:...>``
  236. Content of ``...`` converted to upper case.
  237. ``$<MAKE_C_IDENTIFIER:...>``
  238. Content of ``...`` converted to a C identifier.
  239. ``$<TARGET_OBJECTS:objLib>``
  240. List of objects resulting from build of ``objLib``. ``objLib`` must be an
  241. object of type ``OBJECT_LIBRARY``. This expression may only be used in
  242. the sources of :command:`add_library` and :command:`add_executable`
  243. commands.
  244. ``$<SHELL_PATH:...>``
  245. Content of ``...`` converted to shell path style. For example, slashes are
  246. converted to backslashes in Windows shells and drive letters are converted
  247. to posix paths in MSYS shells. The ``...`` must be an absolute path.