Merge topic 'target-name-genex-export-doc'

6184ff08b9 Help: Document TARGET_NAME genex for namespacing on export

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !12426
This commit is contained in:
Brad King
2026-08-25 10:38:47 -04:00
committed by Kitware Robot
+36 -7
View File
@@ -2109,7 +2109,10 @@ Link Features
:prop_tgt:`INTERFACE_LINK_LIBRARIES` property of a target, it will be
included in the imported target generated by a :command:`install(EXPORT)`
command. It is the responsibility of the environment consuming this
import to define the link feature used by this expression.
import to define the link feature used by this expression. Any CMake
target named in the ``library-list`` should be wrapped in
:genex:`TARGET_NAME` so that its name is namespaced on export; otherwise it
is exported verbatim and will not resolve as an imported target.
* Each target or library involved in the link step must have at most only
one kind of library feature. The absence of a feature is also incompatible
@@ -2285,7 +2288,10 @@ Link Features
:prop_tgt:`INTERFACE_LINK_LIBRARIES` property of a target, it will be
included in the imported target generated by a :command:`install(EXPORT)`
command. It is the responsibility of the environment consuming this
import to define the link feature used by this expression.
import to define the link feature used by this expression. Any CMake
target named in the ``library-list`` should be wrapped in
:genex:`TARGET_NAME` so that its name is namespaced on export; otherwise it
is exported verbatim and will not resolve as an imported target.
Link Context
^^^^^^^^^^^^
@@ -2685,11 +2691,34 @@ These expressions look up information about a target.
.. genex:: $<TARGET_NAME:tgt>
The target name ``tgt`` as written. This marks ``tgt`` as being the name
of a target inside a larger expression, which is required if exporting
targets to multiple dependent export sets. The ``tgt`` text must be a
literal name of a target; it may not contain generator expressions.
The target does not have to exist.
The target name ``tgt`` as written. This marks ``tgt`` as being the name of
a target wherever it appears inside a larger expression. When the property
holding that expression is exported with a namespace by
:command:`install(EXPORT)` or :command:`export`, a marked target name is
rewritten to its namespaced imported target name (e.g. ``mylib::mylib``), the
same way a bare target name in a link-library list would be. Marking is
needed when a target name is nested inside another generator expression that
the export machinery does not otherwise descend into, such as
:genex:`LINK_LIBRARY` or :genex:`LINK_GROUP`, and also when exporting targets
to multiple dependent export sets.
For example, a target name used inside :genex:`LINK_LIBRARY` is exported
verbatim unless it is marked:
.. code-block:: cmake
# Exported as bare "mylib"; a consumer would see an unresolved -lmylib.
target_link_libraries(myotherlib PUBLIC
$<LINK_LIBRARY:WHOLE_ARCHIVE,mylib>)
# Marked, so export rewrites it to the namespaced "mylib::mylib".
target_link_libraries(myotherlib PUBLIC
$<LINK_LIBRARY:WHOLE_ARCHIVE,$<TARGET_NAME:mylib>>)
The ``tgt`` text must be a literal name of a target; it may not contain
generator expressions. During ordinary evaluation the target does not have
to exist, but when the expression is exported the marked name must resolve to
a target reachable from the export.
.. genex:: $<TARGET_POLICY:policy>