cmake-generator-expressions.7.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. ``$<CONFIG:cfg>``
  45. ``1`` if config is ``cfg``, else ``0``. This is a case-insensitive comparison.
  46. The mapping in :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>` is also considered by
  47. this expression when it is evaluated on a property on an :prop_tgt:`IMPORTED`
  48. target.
  49. ``$<PLATFORM_ID:comp>``
  50. ``1`` if the CMake-id of the platform matches ``comp``, otherwise ``0``.
  51. ``$<C_COMPILER_ID:comp>``
  52. ``1`` if the CMake-id of the C compiler matches ``comp``, otherwise ``0``.
  53. ``$<CXX_COMPILER_ID:comp>``
  54. ``1`` if the CMake-id of the CXX compiler matches ``comp``, otherwise ``0``.
  55. ``$<VERSION_GREATER:v1,v2>``
  56. ``1`` if ``v1`` is a version greater than ``v2``, else ``0``.
  57. ``$<VERSION_LESS:v1,v2>``
  58. ``1`` if ``v1`` is a version less than ``v2``, else ``0``.
  59. ``$<VERSION_EQUAL:v1,v2>``
  60. ``1`` if ``v1`` is the same version as ``v2``, else ``0``.
  61. ``$<C_COMPILER_VERSION:ver>``
  62. ``1`` if the version of the C compiler matches ``ver``, otherwise ``0``.
  63. ``$<CXX_COMPILER_VERSION:ver>``
  64. ``1`` if the version of the CXX compiler matches ``ver``, otherwise ``0``.
  65. ``$<TARGET_POLICY:pol>``
  66. ``1`` if the policy ``pol`` was NEW when the 'head' target was created,
  67. else ``0``. If the policy was not set, the warning message for the policy
  68. will be emitted. This generator expression only works for a subset of
  69. policies.
  70. Informational Expressions
  71. =========================
  72. These expressions expand to some information. The information may be used
  73. directly, eg::
  74. include_directories(/usr/include/$<CXX_COMPILER_ID>/)
  75. expands to ``/usr/include/GNU/`` or ``/usr/include/Clang/`` etc, depending on
  76. the Id of the compiler.
  77. These expressions may also may be combined with logical expressions::
  78. $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
  79. expands to ``OLD_COMPILER`` if the
  80. :variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
  81. than 4.2.0.
  82. ``$<CONFIGURATION>``
  83. Configuration name
  84. ``$<PLATFORM_ID>``
  85. The CMake-id of the platform
  86. ``$<C_COMPILER_ID>``
  87. The CMake-id of the C compiler used.
  88. ``$<CXX_COMPILER_ID>``
  89. The CMake-id of the CXX compiler used.
  90. ``$<C_COMPILER_VERSION>``
  91. The version of the C compiler used.
  92. ``$<CXX_COMPILER_VERSION>``
  93. The version of the CXX compiler used.
  94. ``$<TARGET_FILE:tgt>``
  95. Full path to main file (.exe, .so.1.2, .a) where ``tgt`` is the name of a target.
  96. ``$<TARGET_FILE_NAME:tgt>``
  97. Name of main file (.exe, .so.1.2, .a).
  98. ``$<TARGET_FILE_DIR:tgt>``
  99. Directory of main file (.exe, .so.1.2, .a).
  100. ``$<TARGET_LINKER_FILE:tgt>``
  101. File used to link (.a, .lib, .so) where ``tgt`` is the name of a target.
  102. ``$<TARGET_LINKER_FILE_NAME:tgt>``
  103. Name of file used to link (.a, .lib, .so).
  104. ``$<TARGET_LINKER_FILE_DIR:tgt>``
  105. Directory of file used to link (.a, .lib, .so).
  106. ``$<TARGET_SONAME_FILE:tgt>``
  107. File with soname (.so.3) where ``tgt`` is the name of a target.
  108. ``$<TARGET_SONAME_FILE_NAME:tgt>``
  109. Name of file with soname (.so.3).
  110. ``$<TARGET_SONAME_FILE_DIR:tgt>``
  111. Directory of with soname (.so.3).
  112. ``$<TARGET_PROPERTY:tgt,prop>``
  113. Value of the property ``prop`` on the target ``tgt``.
  114. Note that ``tgt`` is not added as a dependency of the target this
  115. expression is evaluated on.
  116. ``$<TARGET_PROPERTY:prop>``
  117. Value of the property ``prop`` on the target on which the generator
  118. expression is evaluated.
  119. ``$<INSTALL_PREFIX>``
  120. Content of the install prefix when the target is exported via
  121. :command:`install(EXPORT)` and empty otherwise.
  122. Output Expressions
  123. ==================
  124. These expressions generate output, in some cases depending on an input. These
  125. expressions may be combined with other expressions for information or logical
  126. comparison::
  127. -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
  128. generates a string of the entries in the :prop_tgt:`INCLUDE_DIRECTORIES` target
  129. property with each entry preceeded by ``-I``. Note that a more-complete use
  130. in this situation would require first checking if the INCLUDE_DIRECTORIES
  131. property is non-empty::
  132. $<$<BOOL:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>>:-I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>>
  133. ``$<JOIN:list,...>``
  134. Joins the list with the content of ``...``
  135. ``$<ANGLE-R>``
  136. A literal ``>``. Used to compare strings which contain a ``>`` for example.
  137. ``$<COMMA>``
  138. A literal ``,``. Used to compare strings which contain a ``,`` for example.
  139. ``$<SEMICOLON>``
  140. A literal ``;``. Used to prevent list expansion on an argument with ``;``.
  141. ``$<TARGET_NAME:...>``
  142. Marks ``...`` as being the name of a target. This is required if exporting
  143. targets to multiple dependent export sets. The ``...`` must be a literal
  144. name of a target- it may not contain generator expressions.
  145. ``$<INSTALL_INTERFACE:...>``
  146. Content of ``...`` when the property is exported using :command:`install(EXPORT)`,
  147. and empty otherwise.
  148. ``$<BUILD_INTERFACE:...>``
  149. Content of ``...`` when the property is exported using :command:`export`, or
  150. when the target is used by another target in the same buildsystem. Expands to
  151. the empty string otherwise.