cmake-generator-expressions.7.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. Informational Expressions
  73. =========================
  74. These expressions expand to some information. The information may be used
  75. directly, eg::
  76. include_directories(/usr/include/$<CXX_COMPILER_ID>/)
  77. expands to ``/usr/include/GNU/`` or ``/usr/include/Clang/`` etc, depending on
  78. the Id of the compiler.
  79. These expressions may also may be combined with logical expressions::
  80. $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
  81. expands to ``OLD_COMPILER`` if the
  82. :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
  83. than 4.2.0.
  84. ``$<CONFIGURATION>``
  85. Configuration name
  86. ``$<PLATFORM_ID>``
  87. The CMake-id of the platform
  88. ``$<C_COMPILER_ID>``
  89. The CMake-id of the C compiler used.
  90. ``$<CXX_COMPILER_ID>``
  91. The CMake-id of the CXX compiler used.
  92. ``$<C_COMPILER_VERSION>``
  93. The version of the C compiler used.
  94. ``$<CXX_COMPILER_VERSION>``
  95. The version of the CXX compiler used.
  96. ``$<TARGET_FILE:tgt>``
  97. Full path to main file (.exe, .so.1.2, .a) where ``tgt`` is the name of a target.
  98. ``$<TARGET_FILE_NAME:tgt>``
  99. Name of main file (.exe, .so.1.2, .a).
  100. ``$<TARGET_FILE_DIR:tgt>``
  101. Directory of main file (.exe, .so.1.2, .a).
  102. ``$<TARGET_LINKER_FILE:tgt>``
  103. File used to link (.a, .lib, .so) where ``tgt`` is the name of a target.
  104. ``$<TARGET_LINKER_FILE_NAME:tgt>``
  105. Name of file used to link (.a, .lib, .so).
  106. ``$<TARGET_LINKER_FILE_DIR:tgt>``
  107. Directory of file used to link (.a, .lib, .so).
  108. ``$<TARGET_SONAME_FILE:tgt>``
  109. File with soname (.so.3) where ``tgt`` is the name of a target.
  110. ``$<TARGET_SONAME_FILE_NAME:tgt>``
  111. Name of file with soname (.so.3).
  112. ``$<TARGET_SONAME_FILE_DIR:tgt>``
  113. Directory of with soname (.so.3).
  114. ``$<TARGET_PROPERTY:tgt,prop>``
  115. Value of the property ``prop`` on the target ``tgt``.
  116. Note that ``tgt`` is not added as a dependency of the target this
  117. expression is evaluated on.
  118. ``$<TARGET_PROPERTY:prop>``
  119. Value of the property ``prop`` on the target on which the generator
  120. expression is evaluated.
  121. ``$<INSTALL_PREFIX>``
  122. Content of the install prefix when the target is exported via
  123. :command:`install(EXPORT)` and empty otherwise.
  124. Output Expressions
  125. ==================
  126. These expressions generate output, in some cases depending on an input. These
  127. expressions may be combined with other expressions for information or logical
  128. comparison::
  129. -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
  130. generates a string of the entries in the :prop_tgt:`INCLUDE_DIRECTORIES` target
  131. property with each entry preceeded by ``-I``. Note that a more-complete use
  132. in this situation would require first checking if the INCLUDE_DIRECTORIES
  133. property is non-empty::
  134. $<$<BOOL:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>>:-I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>>
  135. ``$<JOIN:list,...>``
  136. Joins the list with the content of ``...``
  137. ``$<ANGLE-R>``
  138. A literal ``>``. Used to compare strings which contain a ``>`` for example.
  139. ``$<COMMA>``
  140. A literal ``,``. Used to compare strings which contain a ``,`` for example.
  141. ``$<SEMICOLON>``
  142. A literal ``;``. Used to prevent list expansion on an argument with ``;``.
  143. ``$<TARGET_NAME:...>``
  144. Marks ``...`` as being the name of a target. This is required if exporting
  145. targets to multiple dependent export sets. The ``...`` must be a literal
  146. name of a target- it may not contain generator expressions.
  147. ``$<INSTALL_INTERFACE:...>``
  148. Content of ``...`` when the property is exported using :command:`install(EXPORT)`,
  149. and empty otherwise.
  150. ``$<BUILD_INTERFACE:...>``
  151. Content of ``...`` when the property is exported using :command:`export`, or
  152. when the target is used by another target in the same buildsystem. Expands to
  153. the empty string otherwise.
  154. ``$<LOWER_CASE:...>``
  155. Content of ``...`` converted to lower case.
  156. ``$<UPPER_CASE:...>``
  157. Content of ``...`` converted to upper case.
  158. ``$<MAKE_C_IDENTIFIER:...>``
  159. Content of ``...`` converted to a C identifier.