Skip to content

Commit 26e8995

Browse files
Ryanf55mergify[bot]
authored andcommitted
Fix numerous inconsistencies in ament-cmake-docs w.r.t installation of linkage, and target installation (#3805)
* Fix numerous inconsistencies in ament-cmake-docs * Nodes need a special RUNTIME directory to be recognized by ROS 2 CLI * The order of creating the export set and using it was not intuitive * target_link_libraries was missing linkage type which is not recommended * It did not show to install nodes and libraries at the same time * The installation of headers was overly complex since the install directory is the same for all headers * HAS_LIBRARY_TARGET wasn't explained that it's only needed if you are installing libraries * Updates from review. Signed-off-by: Ryan Friedman <[email protected]> Signed-off-by: Chris Lalancette <[email protected]> (cherry picked from commit 42ccd72)
1 parent 6cf7197 commit 26e8995

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

source/How-To-Guides/Ament-CMake-Documentation.rst

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The basic outline of the ``CMakeLists.txt`` of an ament package contains:
2929

3030
.. code-block:: cmake
3131
32-
cmake_minimum_required(VERSION 3.5)
32+
cmake_minimum_required(VERSION 3.8)
3333
project(my_project)
3434
3535
ament_package()
@@ -74,14 +74,14 @@ The following best practice is proposed:
7474

7575
- only cpp files are explicitly referenced in the call to ``add_library`` or ``add_executable``
7676

77-
- allow to find headers via
77+
- find headers via
7878

7979
.. code-block:: cmake
8080
8181
target_include_directories(my_target
8282
PUBLIC
83-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
84-
$<INSTALL_INTERFACE:include>)
83+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
84+
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
8585
8686
This adds all files in the folder ``${CMAKE_CURRENT_SOURCE_DIR}/include`` to the public interface during build time and all files in the include folder (relative to ``${CMAKE_INSTALL_DIR}``) when being installed.
8787

@@ -98,7 +98,7 @@ As an example, suppose we want to link ``my_target`` against the linear algebra
9898
.. code-block:: cmake
9999
100100
find_package(Eigen3 REQUIRED)
101-
ament_target_dependencies(my_target Eigen3)
101+
ament_target_dependencies(my_library PUBLIC Eigen3::Eigen)
102102
103103
It includes the necessary headers and libraries and their dependencies to be correctly found by the project.
104104
It will also ensure that the include directories of all dependencies are ordered correctly when using overlay workspaces.
@@ -109,14 +109,13 @@ The recommended way in modern CMake is to only use targets, exporting and linkin
109109
CMake targets are namespaced, similar to C++.
110110
For instance, ``Eigen3`` defines the target ``Eigen3::Eigen``.
111111

112-
At least until ``Crystal Clemmys`` target names are not supported in the ``ament_target_dependencies`` macro.
113112
Sometimes it will be necessary to call the ``target_link_libaries`` CMake function.
114113
In the example of Eigen3, the call should then look like
115114

116115
.. code-block:: cmake
117116
118117
find_package(Eigen3 REQUIRED)
119-
target_link_libraries(my_target Eigen3::Eigen)
118+
target_link_libraries(my_target PUBLIC Eigen3::Eigen)
120119
121120
This will also include necessary headers, libraries and their dependencies, but in contrast to ``ament_target_dependencies`` it might not correctly order the dependencies when using overlay workspaces.
122121

@@ -130,48 +129,50 @@ Building a Library
130129

131130
When building a reusable library, some information needs to be exported for downstream packages to easily use it.
132131

133-
.. code-block:: cmake
132+
First, install the headers files which should be available to clients.
134133

135-
ament_export_targets(my_libraryTargets HAS_LIBRARY_TARGET)
136-
ament_export_dependencies(some_dependency)
134+
.. code-block:: cmake
137135
138136
install(
139137
DIRECTORY include/
140-
DESTINATION include
138+
DESTINATION include/${PROJECT_NAME}
141139
)
142140
141+
Next, install the targets and create the export set ``export_${PROJECT_NAME}``.
142+
The include directory is custom to support overlays in ``colcon``.
143+
144+
Add all the libraries for your project to the ``TARGETS`` argument.
145+
146+
.. code-block:: cmake
147+
143148
install(
144149
TARGETS my_library
145-
EXPORT my_libraryTargets
150+
EXPORT export_${PROJECT_NAME}
146151
LIBRARY DESTINATION lib
147152
ARCHIVE DESTINATION lib
148153
RUNTIME DESTINATION bin
149-
INCLUDES DESTINATION include
150154
)
151155
156+
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
157+
ament_export_dependencies(some_dependency)
152158
153159
Here, we assume that the folder ``include`` contains the headers which need to be exported.
154160
Note that it is not necessary to put all headers into a separate folder, only those that should be included by clients.
155161

156162
Here is what's happening in the snippet above:
157163

158164
- The ``ament_export_targets`` macro exports the targets for CMake.
159-
This is necessary to allow your library's clients to use the ``target_link_libraries(client my_library::my_library)`` syntax.
160-
``ament_export_targets`` can take an arbitrary list of targets named as ``EXPORT`` in an install call and an additional option ``HAS_LIBRARY_TARGET``, which adds potential libraries to environment variables.
165+
This is necessary to allow your library's clients to use the ``target_link_libraries(client PRIVATE my_library::my_library)`` syntax.
166+
If the export set includes a library, add the option ``HAS_LIBRARY_TARGET`` to ``ament_export_targets``, which adds potential libraries to environment variables.
161167

162168
- The ``ament_export_dependencies`` exports dependencies to downstream packages.
163169
This is necessary so that the user of the library does not have to call ``find_package`` for those dependencies, too.
164170

165-
- The first ``install`` commands installs the header files which should be available to clients.
166-
167171
.. warning::
168172

169173
Calling ``ament_export_targets``, ``ament_export_dependencies``, or other ament commands from a CMake subdirectory will not work as expected.
170174
This is because the CMake subdirectory has no way of setting necessary variables in the parent scope where ``ament_package`` is called.
171175

172-
- The last large install command installs the library.
173-
Archive and library files will be exported to the lib folder, runtime binaries will be installed to the bin folder and the path to installed headers is ``include``.
174-
175176
.. note::
176177

177178
Windows DLLs are treated as runtime artifacts and installed into the ``RUNTIME DESTINATION`` folder.

0 commit comments

Comments
 (0)