cmake-generator-expressions.7.rst 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. .. cmake-manual-description: CMake Generator Expressions
  2. cmake-generator-expressions(7)
  3. ******************************
  4. .. only:: html or latex
  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. ``$<0:...>``
  30. Empty string (ignores ``...``)
  31. ``$<1:...>``
  32. Content of ``...``
  33. ``$<BOOL:...>``
  34. ``1`` if the ``...`` is true, else ``0``
  35. ``$<AND:?[,?]...>``
  36. ``1`` if all ``?`` are ``1``, else ``0``
  37. The ``?`` must always be either ``0`` or ``1`` in boolean expressions.
  38. ``$<OR:?[,?]...>``
  39. ``0`` if all ``?`` are ``0``, else ``1``
  40. ``$<NOT:?>``
  41. ``0`` if ``?`` is ``1``, else ``1``
  42. ``$<STREQUAL:a,b>``
  43. ``1`` if ``a`` is STREQUAL ``b``, else ``0``
  44. ``$<EQUAL:a,b>``
  45. ``1`` if ``a`` is EQUAL ``b`` in a numeric comparison, else ``0``
  46. ``$<CONFIG:cfg>``
  47. ``1`` if config is ``cfg``, else ``0``. This is a case-insensitive comparison.
  48. The mapping in :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` is also considered by
  49. this expression when it is evaluated on a property on an :prop_tgt:`IMPORTED`
  50. target.
  51. ``$<PLATFORM_ID:comp>``
  52. ``1`` if the CMake-id of the platform matches ``comp``, otherwise ``0``.
  53. ``$<C_COMPILER_ID:comp>``
  54. ``1`` if the CMake-id of the C compiler matches ``comp``, otherwise ``0``.
  55. ``$<CXX_COMPILER_ID:comp>``
  56. ``1`` if the CMake-id of the CXX compiler matches ``comp``, otherwise ``0``.
  57. ``$<VERSION_GREATER:v1,v2>``
  58. ``1`` if ``v1`` is a version greater than ``v2``, else ``0``.
  59. ``$<VERSION_LESS:v1,v2>``
  60. ``1`` if ``v1`` is a version less than ``v2``, else ``0``.
  61. ``$<VERSION_EQUAL:v1,v2>``
  62. ``1`` if ``v1`` is the same version as ``v2``, else ``0``.
  63. ``$<C_COMPILER_VERSION:ver>``
  64. ``1`` if the version of the C compiler matches ``ver``, otherwise ``0``.
  65. ``$<CXX_COMPILER_VERSION:ver>``
  66. ``1`` if the version of the CXX compiler matches ``ver``, otherwise ``0``.
  67. ``$<TARGET_POLICY:pol>``
  68. ``1`` if the policy ``pol`` was NEW when the 'head' target was created,
  69. else ``0``. If the policy was not set, the warning message for the policy
  70. will be emitted. This generator expression only works for a subset of
  71. policies.
  72. ``$<COMPILE_FEATURES:feature[,feature]...>``
  73. ``1`` if all of the ``feature`` features are available for the 'head'
  74. target, and ``0`` otherwise. If this expression is used while evaluating
  75. the link implementation of a target and if any dependency transitively
  76. increases the required :prop_tgt:`C_STANDARD` or :prop_tgt:`CXX_STANDARD`
  77. for the 'head' target, an error is reported. See the
  78. :manual:`cmake-compile-features(7)` manual for information on
  79. compile features.
  80. Informational Expressions
  81. =========================
  82. These expressions expand to some information. The information may be used
  83. directly, eg::
  84. include_directories(/usr/include/$<CXX_COMPILER_ID>/)
  85. expands to ``/usr/include/GNU/`` or ``/usr/include/Clang/`` etc, depending on
  86. the Id of the compiler.
  87. These expressions may also may be combined with logical expressions::
  88. $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
  89. expands to ``OLD_COMPILER`` if the
  90. :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
  91. than 4.2.0.
  92. ``$<CONFIGURATION>``
  93. Configuration name. Deprecated. Use ``CONFIG`` instead.
  94. ``$<CONFIG>``
  95. Configuration name
  96. ``$<PLATFORM_ID>``
  97. The CMake-id of the platform.
  98. See also the :variable:`CMAKE_SYSTEM_NAME` variable.
  99. ``$<C_COMPILER_ID>``
  100. The CMake-id of the C compiler used.
  101. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  102. ``$<CXX_COMPILER_ID>``
  103. The CMake-id of the CXX compiler used.
  104. See also the :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  105. ``$<C_COMPILER_VERSION>``
  106. The version of the C compiler used.
  107. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  108. ``$<CXX_COMPILER_VERSION>``
  109. The version of the CXX compiler used.
  110. See also the :variable:`CMAKE_<LANG>_COMPILER_VERSION` variable.
  111. ``$<TARGET_FILE:tgt>``
  112. Full path to main file (.exe, .so.1.2, .a) where ``tgt`` is the name of a target.
  113. ``$<TARGET_FILE_NAME:tgt>``
  114. Name of main file (.exe, .so.1.2, .a).
  115. ``$<TARGET_FILE_DIR:tgt>``
  116. Directory of main file (.exe, .so.1.2, .a).
  117. ``$<TARGET_LINKER_FILE:tgt>``
  118. File used to link (.a, .lib, .so) where ``tgt`` is the name of a target.
  119. ``$<TARGET_LINKER_FILE_NAME:tgt>``
  120. Name of file used to link (.a, .lib, .so).
  121. ``$<TARGET_LINKER_FILE_DIR:tgt>``
  122. Directory of file used to link (.a, .lib, .so).
  123. ``$<TARGET_SONAME_FILE:tgt>``
  124. File with soname (.so.3) where ``tgt`` is the name of a target.
  125. ``$<TARGET_SONAME_FILE_NAME:tgt>``
  126. Name of file with soname (.so.3).
  127. ``$<TARGET_SONAME_FILE_DIR:tgt>``
  128. Directory of with soname (.so.3).
  129. ``$<TARGET_PDB_FILE:tgt>``
  130. Full path to the linker generated program database file (.pdb)
  131. where ``tgt`` is the name of a target.
  132. See also the :prop_tgt:`PDB_NAME` and :prop_tgt:`PDB_OUTPUT_DIRECTORY`
  133. target properties and their configuration specific variants
  134. :prop_tgt:`PDB_NAME_<CONFIG>` and :prop_tgt:`PDB_OUTPUT_DIRECTORY_<CONFIG>`.
  135. ``$<TARGET_PDB_FILE_NAME:tgt>``
  136. Name of the linker generated program database file (.pdb).
  137. ``$<TARGET_PDB_FILE_DIR:tgt>``
  138. Directory of the linker generated program database file (.pdb).
  139. ``$<TARGET_PROPERTY:tgt,prop>``
  140. Value of the property ``prop`` on the target ``tgt``.
  141. Note that ``tgt`` is not added as a dependency of the target this
  142. expression is evaluated on.
  143. ``$<TARGET_PROPERTY:prop>``
  144. Value of the property ``prop`` on the target on which the generator
  145. expression is evaluated.
  146. ``$<INSTALL_PREFIX>``
  147. Content of the install prefix when the target is exported via
  148. :command:`install(EXPORT)` and empty otherwise.
  149. Output Expressions
  150. ==================
  151. These expressions generate output, in some cases depending on an input. These
  152. expressions may be combined with other expressions for information or logical
  153. comparison::
  154. -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
  155. generates a string of the entries in the :prop_tgt:`INCLUDE_DIRECTORIES` target
  156. property with each entry preceeded by ``-I``. Note that a more-complete use
  157. in this situation would require first checking if the INCLUDE_DIRECTORIES
  158. property is non-empty::
  159. $<$<BOOL:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>>:-I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>>
  160. ``$<JOIN:list,...>``
  161. Joins the list with the content of ``...``
  162. ``$<ANGLE-R>``
  163. A literal ``>``. Used to compare strings which contain a ``>`` for example.
  164. ``$<COMMA>``
  165. A literal ``,``. Used to compare strings which contain a ``,`` for example.
  166. ``$<SEMICOLON>``
  167. A literal ``;``. Used to prevent list expansion on an argument with ``;``.
  168. ``$<TARGET_NAME:...>``
  169. Marks ``...`` as being the name of a target. This is required if exporting
  170. targets to multiple dependent export sets. The ``...`` must be a literal
  171. name of a target- it may not contain generator expressions.
  172. ``$<LINK_ONLY:...>``
  173. Content of ``...`` except when evaluated in a link interface while
  174. propagating :ref:`Target Usage Requirements`, in which case it is the
  175. empty string.
  176. Intended for use only in an :prop_tgt:`INTERFACE_LINK_LIBRARIES` target
  177. property, perhaps via the :command:`target_link_libraries` command,
  178. to specify private link dependencies without other usage requirements.
  179. ``$<INSTALL_INTERFACE:...>``
  180. Content of ``...`` when the property is exported using :command:`install(EXPORT)`,
  181. and empty otherwise.
  182. ``$<BUILD_INTERFACE:...>``
  183. Content of ``...`` when the property is exported using :command:`export`, or
  184. when the target is used by another target in the same buildsystem. Expands to
  185. the empty string otherwise.
  186. ``$<LOWER_CASE:...>``
  187. Content of ``...`` converted to lower case.
  188. ``$<UPPER_CASE:...>``
  189. Content of ``...`` converted to upper case.
  190. ``$<MAKE_C_IDENTIFIER:...>``
  191. Content of ``...`` converted to a C identifier.
  192. ``$<TARGET_OBJECTS:objLib>``
  193. List of objects resulting from build of ``objLib``. ``objLib`` must be an
  194. object of type ``OBJECT_LIBRARY``. This expression may only be used in
  195. the sources of :command:`add_library` and :command:`add_executable`
  196. commands.