.. cmake-manual-description: CMake Generator Expressions
cmake-generator-expressions(7)
******************************
.. only:: html
.. contents::
Introduction
============
Generator expressions are evaluated during build system generation to produce
information specific to each build configuration. They have the form
``$<...>``. For example:
.. code-block:: cmake
target_include_directories(tgt PRIVATE /opt/include/$<CXX_COMPILER_ID>)
This would expand to ``/opt/include/GNU``, ``/opt/include/Clang``, etc.
depending on the C++ compiler used.
Generator expressions are allowed in the context of many target properties,
such as :prop_tgt:`LINK_LIBRARIES`, :prop_tgt:`INCLUDE_DIRECTORIES`,
:prop_tgt:`COMPILE_DEFINITIONS` and others. They may also be used when using
commands to populate those properties, such as :command:`target_link_libraries`,
:command:`target_include_directories`, :command:`target_compile_definitions`
and others. They enable conditional linking, conditional definitions used when
compiling, conditional include directories, and more. The conditions may be
based on the build configuration, target properties, platform information,
or any other queryable information.
Generator expressions can be nested:
.. code-block:: cmake
target_compile_definitions(tgt PRIVATE
$<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
)
The above would expand to ``OLD_COMPILER`` if the
:variable:`CMAKE_CXX_COMPILER_VERSION <CMAKE_<LANG>_COMPILER_VERSION>` is less
than 4.2.0.
Whitespace And Quoting
======================
Generator expressions are typically parsed after command arguments.
If a generator expression contains spaces, new lines, semicolons or
other characters that may be interpreted as command argument separators,
the whole expression should be surrounded by quotes when passed to a
command. Failure to do so may result in the expression being split and
it may no longer be recognized as a generator expression.
When using :command:`add_custom_command` or :command:`add_custom_target`,
use the ``VERBATIM`` and ``COMMAND_EXPAND_LISTS`` options to obtain robust
argument splitting and quoting.
.. code-block:: cmake
# WRONG: Embedded space will be treated as an argument separator.
# This ends up not being seen as a generator expression at all.
add_custom_target(run_some_tool
COMMAND some_tool -I$<JOIN:$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>, -I>
VERBATIM
)
.. code-block:: cmake
# Better, but still not robust. Quotes prevent the space from splitting the
# expression. However, the tool will receive the expanded value as a single
# argument.
add_custom_target(run_some_tool
COMMAND some_tool "-I$<JOIN:$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>, -I>"
VERBATIM
)
.. code-block:: cmake
# Nearly correct. Using a semicolon to separate arguments and adding the
# COMMAND_EXPAND_LISTS option means that paths with spaces will be handled
# correctly. Quoting the whole expression ensures it is seen as a generator
# expression. But if the target property is empty, we will get a bare -I
# with nothing after it.
add_custom_target(run_some_tool
COMMAND some_tool "-I$<JOIN:$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>,;-I>"
COMMAND_EXPAND_LISTS
VERBATIM
)
Using variables to build up a more complex generator expression is also a
good way to reduce errors and improve readability. The above example can be
improved further like so:
.. code-block:: cmake
# The $<BOOL:...> check prevents adding anything if the property is empty,
# assuming the property value cannot be one of CMake's false constants.
set(prop "$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>")
add_custom_target(run_some_tool
COMMAND some_tool "$<$<BOOL:${prop}>:-I$<JOIN:${prop},;-I>>"
COMMAND_EXPAND_LISTS
VERBATIM
)
Finally, the above example can be expressed in a more simple and robust way
using an alternate generator expression:
.. code-block:: cmake
add_custom_target(run_some_tool
COMMAND some_tool "$<LIST:TRANSFORM,$<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>,PREPEND,-I>"
COMMAND_EXPAND_LISTS
VERBATIM
)
A common mistake is to try to split a generator expression across multiple
lines with indenting:
.. code-block:: cmake
# WRONG: New lines and spaces all treated as argument separators, so the
# generator expression is split and not recognized correctly.
target_compile_definitions(tgt PRIVATE
$<$<AND:
$<CXX_COMPILER_ID:GNU>,
$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,5>
>:HAVE_5_OR_LATER>
)
Again, use helper variables with well-chosen names to build up a readable
expression instead:
.. code-block:: cmake
set(is_gnu "$<CXX_COMPILER_ID:GNU>")
set(v5_or_later "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,5>")
set(meet_requirements "$<AND:${is_gnu},${v5_or_later}>")
target_compile_definitions(tgt PRIVATE
"$<${meet_requirements}:HAVE_5_OR_LATER>"
)
Debugging
=========
Since generator expressions are evaluated during generation of the buildsystem,
and not during processing of ``CMakeLists.txt`` files, it is not possible to
inspect their result with the :command:`message()` command. One possible way
to generate debug messages is to add a custom target:
.. code-block:: cmake
add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")
After running :program:`cmake`, you can then build the ``genexdebug`` target to print
the result of the ``$<...>`` expression (i.e. run the command
:option:`cmake --build ... --target genexdebug <cmake--build --target>`).
Another way is to write debug messages to a file with :command:`file(GENERATE)`:
.. code-block:: cmake
file(GENERATE OUTPUT filename CONTENT "$<...>")
Generator Expression Reference
==============================
.. note::
This reference deviates from most of the CMake documentation in that it
omits angular brackets ``<...>`` around placeholders like ``condition``,
``string``, ``target``, etc. This is to prevent an opportunity for those
placeholders to be misinterpreted as generator expressions.
.. _`Conditional Generator Expressions`:
Conditional Expressions
-----------------------
A fundamental category of generator expressions relates to conditional logic.
Two forms of conditional generator expressions are supported:
.. genex:: $<condition:true_string>
Evaluates to ``true_string`` if ``condition`` is ``1``, or an empty string
if ``condition`` evaluates to ``0``. Any other value for ``condition``
results in an error.
.. genex:: $<IF:condition,true_string,false_string>
.. versionadded:: 3.8
Evaluates to ``true_string`` if ``condition`` is ``1``, or ``false_string``
if ``condition`` is ``0``. Any other value for ``condition`` results in an
error.
.. versionadded:: 3.28
This generator expression short-circuits such that generator expressions in
``false_string`` will not evaluate when ``condition`` is ``1``, and generator
expressions in ``true_string`` will not evaluate when condition is ``0``.
Typically, the ``condition`` is itself a generator expression. For instance,
the following expression expands to ``DEBUG_MODE`` when the ``Debug``
configuration is used, and the empty string for all other configurations:
.. code-block:: cmake
$<$<CONFIG:Debug>:DEBUG_MODE>
Boolean-like ``condition`` values other than ``1`` or ``0`` can be handled
by wrapping them with the ``$<BOOL:...>`` generator expression:
.. genex:: $<BOOL:string>
Converts ``string`` to ``0`` or ``1``. Evaluates to ``0`` if any of the
following is true:
* ``string`` is empty,
* ``string`` is a case-insensitive equal of
``0``, ``FALSE``, ``OFF``, ``N``, ``NO``, ``IGNORE``, or ``NOTFOUND``, or
* ``string`` ends in the suffix ``-NOTFOUND`` (case-sensitive).
Otherwise evaluates to ``1``.
The ``$<BOOL:...>`` generator expression is often used when a ``condition``
is provided by a CMake variable:
.. code-block:: cmake
没有合适的资源?快使用搜索试试~ 我知道了~
cmake-3.30.0-rc4-windows-x86_64.zip
共2000个文件
txt:1147个
html:853个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 43 浏览量
2024-08-02
07:48:13
上传
评论
收藏 43.4MB ZIP 举报
温馨提示
cmake-3.30.0-rc4-windows-x86_64.zip
资源推荐
资源详情
资源评论
收起资源包目录
cmake-3.30.0-rc4-windows-x86_64.zip (2000个子文件)
genindex.html 2.99MB
cmake-generator-expressions.7.html 322KB
ctest.1.html 242KB
cmake-buildsystem.7.html 193KB
cmake-file-api.7.html 180KB
cmake.1.html 178KB
cmake-presets.7.html 126KB
cmake-variables.7.html 115KB
rpm.html 113KB
index.html 107KB
cmake-toolchains.7.html 94KB
cmake-properties.7.html 83KB
cmake-packages.7.html 82KB
index.html 79KB
deb.html 76KB
cmake-language.7.html 67KB
cmake-developer.7.html 59KB
cmake-policies.7.html 55KB
nuget.html 54KB
index.html 54KB
ifw.html 52KB
cmake-qt.7.html 51KB
innosetup.html 45KB
cmake-compile-features.7.html 45KB
A Basic Starting Point.html 45KB
ccmake.1.html 45KB
wix.html 43KB
Adding a Library.html 42KB
cpack.1.html 40KB
cmake-modules.7.html 37KB
CMAKE_LINK_LIBRARY_USING_FEATURE.html 37KB
CMAKE_LANG_LINK_LIBRARY_USING_FEATURE.html 37KB
external.html 36KB
cmake-configure-log.7.html 34KB
Installing and Testing.html 34KB
Adding Usage Requirements for a Library.html 32KB
productbuild.html 32KB
cmake-gui.1.html 29KB
nsis.html 27KB
Adding System Introspection.html 25KB
Adding Export Configuration.html 23KB
Adding Generator Expressions.html 22KB
dmg.html 22KB
cmake-commands.7.html 22KB
Adding a Custom Command and Generated File.html 22KB
freebsd.html 21KB
index.html 21KB
CMAKE_SYSTEM_NAME.html 20KB
CMAKE_LINK_GROUP_USING_FEATURE.html 20KB
CMAKE_LANG_LINK_GROUP_USING_FEATURE.html 19KB
FIXTURES_REQUIRED.html 18KB
CMAKE_LINK_LIBRARY_FEATURE_ATTRIBUTES.html 17KB
cmake-env-variables.7.html 17KB
cmake-generators.7.html 16KB
CMAKE_GENERATOR_TOOLSET.html 16KB
cmake-cxxmodules.7.html 16KB
archive.html 16KB
Adding Support for a Testing Dashboard.html 15KB
CMAKE_LINKER_TYPE.html 15KB
Packaging Debug and Release.html 14KB
CMAKE_POLICY_WARNING_CMPNNNN.html 14KB
Selecting Static or Shared Libraries.html 14KB
CMAKE_LANG_COMPILER_ID.html 13KB
CMAKE_SYSTEM_PREFIX_PATH.html 13KB
Packaging an Installer.html 13KB
CMAKE_GENERATOR_PLATFORM.html 12KB
CMAKE_MAKE_PROGRAM.html 12KB
CMAKE_SYSTEM_IGNORE_PATH.html 12KB
CMAKE_INSTALL_PREFIX.html 12KB
CMAKE_GENERATOR_INSTANCE.html 12KB
bundle.html 12KB
CMAKE_LANG_FLAGS.html 11KB
CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY.html 11KB
CMAKE_IGNORE_PATH.html 11KB
CMAKE_FIND_USE_PACKAGE_REGISTRY.html 11KB
CMAKE_FIND_USE_INSTALL_PREFIX.html 11KB
RESOURCE_GROUPS.html 11KB
CTEST_COVERAGE_COMMAND.html 11KB
CMAKE_LANG_STANDARD_LATEST.html 11KB
PASS_REGULAR_EXPRESSION.html 11KB
SKIP_REGULAR_EXPRESSION.html 11KB
CMAKE_LANG_HOST_COMPILER.html 11KB
CMAKE_MSVC_RUNTIME_LIBRARY.html 11KB
CMAKE_PROJECT_TOP_LEVEL_INCLUDES.html 10KB
BUILD_SHARED_LIBS.html 10KB
CMAKE_MSVC_DEBUG_INFORMATION_FORMAT.html 10KB
CMAKE_MAXIMUM_RECURSION_DEPTH.html 10KB
CPACK_CUSTOM_INSTALL_VARIABLES.html 10KB
CMAKE_WATCOM_RUNTIME_LIBRARY.html 10KB
CMAKE_LANG_IMPLICIT_LINK_DIRECTORIES.html 10KB
CMAKE_FIND_USE_PACKAGE_ROOT_PATH.html 10KB
CMAKE_VERSION.html 10KB
CMAKE_MESSAGE_CONTEXT.html 10KB
index.html 10KB
ENVIRONMENT_MODIFICATION.html 10KB
CMAKE_SYSTEM_IGNORE_PREFIX_PATH.html 10KB
INCLUDE_DIRECTORIES.html 10KB
CMAKE_VS_USE_DEBUG_LIBRARIES.html 10KB
CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH.html 10KB
SKIP_RETURN_CODE.html 10KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
weixin58692541
- 粉丝: 4109
- 资源: 7717
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于机器学习的泊位调度优化与船舶到达时间预测提升港口服务质量和效率的研究
- 基于数据驱动进化算法的风电场布局优化研究与应用
- 电气工程中无铁芯永磁线性电机的设计与磁悬浮应用研究
- 雷达信号处理中的基于流形分离的最大似然联合DOA与极化估计方法
- 无人驾驶 carsim+simulink联合仿真 跟踪双移线轨迹
- 精选毕设项目-爱跑腿外卖.zip
- 精选毕设项目-爱拼宝宝商城.zip
- 精选毕设项目-百度小说.zip
- 精选毕设项目-百度小说搜索.zip
- 精选毕设项目-备忘录.zip
- 精选毕设项目-辩论倒计时.zip
- 精选毕设项目-步步高字典.zip
- 精选毕设项目-侧滑布局.zip
- 精选毕设项目-查拼音.zip
- 精选毕设项目-茶叶商城.zip
- 精选毕设项目-查看电影文章.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功