cmake-generator-expressions.7.rst 12 KB

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