Major revamp of the ament_cmake documentation.#3812
Conversation
Rearrange the document to more closely follow the order a typical CMakeLists.txt is arranged in. While doing that, update the documentation to match our current best practices, and add in additional advice for building/installing executables as opposed to libraries. Signed-off-by: Chris Lalancette <clalancette@gmail.com>
Ryanf55
left a comment
There was a problem hiding this comment.
Overall, the changes look good. A few touch-ups here and there. Great improvement.
| - Put all headers which should be usable by clients of this library (and therefore must be installed) into a subdirectory of the ``include`` folder named like the package, while all other files (``.c/.cpp`` and header files which should not be exported) are inside the ``src`` folder | ||
|
|
||
| When building a reusable library, some information needs to be exported for downstream packages to easily use it. | ||
| - Only ``.c/.cpp`` files are explicitly referenced in the call to ``add_library`` |
There was a problem hiding this comment.
Perhaps consider target_sources? I actually really like this by adding a CMakeLists in src and include folders to make the top level one more digestable. However, neither splitting the CMake code nor using target_sources is common the ROS core.
There was a problem hiding this comment.
We don't use this in the core currently, so I don't think I'll add this here.
| There are two ways to link your targets against a dependency. | ||
|
|
||
| There are two additional functions which can be used but are superfluous for target based installs: | ||
| The first and recommended way is to use the ament macro ``ament_target_dependencies``. |
There was a problem hiding this comment.
Could be useful to explain why we are still recommending ament_target_dependencies
There was a problem hiding this comment.
It's a hard one to explain, to be honest. The fact is that you can do almost everything you want with target_link_libraries nowadays, and I've been (slowly) converting the core over to using that over ament_target_dependencies. The one big thing that doesn't work is message packages; you can't depend on sensor_msgs::sensor_msgs, you have to instead depend on ${sensor_msgs_TARGETS}. That's the main reason to still recommend ament_target_dependencies, but I'm not convinced that explanation is necessary/useful here.
There was a problem hiding this comment.
The special case for using messages is already covered in other tutorials. I use target_link_libraries for basically every library except message packages. It could be useful to instead acknowledge the fact that both may be used together.
| .. code-block:: cmake | ||
|
|
||
| The following best practice is proposed: | ||
| if(NOT CMAKE_C_STANDARD) |
There was a problem hiding this comment.
Might want to add the DEFINED keyword because it could be defined, but empty, in which case you end up not setting the standard.
https://github.com/cpp-best-practices/cmake_template/blob/d19d8921771ba582824948f0cc57f849a5c6efd8/CMakeLists.txt#L8
There was a problem hiding this comment.
I ended up not doing this, because this isn't what we do in the core.
Actually, this is one place where what ros2 pkg create does and what the core does differ. In the core, we use:
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
But in ros2 pkg create, we generate:
target_compile_features(mylib PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
I'm not sure which is better, and we don't use DEFINED in either place. I think I'm going to leave this one as-is for now, though we should probably rectify this situation one way or the other.
There was a problem hiding this comment.
I'll create another ticket in ros2/ros2.
Signed-off-by: Chris Lalancette <clalancette@gmail.com>
| .. code-block:: cmake | ||
|
|
||
| In principle, using generator expressions here is not necessary if both folders are called ``include`` and top-level with respect to ``${CMAKE_CURRENT_SOURCE_DIR}`` and ``${CMAKE_INSTALL_DIR}``, but it is very common. | ||
| if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
There was a problem hiding this comment.
CMAKE_COMPILER_IS_GNUCXX is deprecated.
| if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") | |
| if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
There was a problem hiding this comment.
In the core, we use CMAKE_COMPILER_IS_GNUCXX everywhere. This could probably also be changed as part of ros2/ros2#1467, but I think here we should document what we currently use.
There was a problem hiding this comment.
Do you have any automatic refactoring and/or PR tools? There's 167 instances of it in ros2 rolling repos, and it looks like a simple sed replacement would work.
Signed-off-by: Chris Lalancette <clalancette@gmail.com>
|
I'm going to go ahead and merge and backport this one. Thanks for all of your feedback. |
* Major revamp of the ament_cmake documentation. Rearrange the document to more closely follow the order a typical CMakeLists.txt is arranged in. While doing that, update the documentation to match our current best practices, and add in additional advice for building/installing executables as opposed to libraries. Signed-off-by: Chris Lalancette <clalancette@gmail.com> Co-authored-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> (cherry picked from commit 38d8e74)
* Major revamp of the ament_cmake documentation. Rearrange the document to more closely follow the order a typical CMakeLists.txt is arranged in. While doing that, update the documentation to match our current best practices, and add in additional advice for building/installing executables as opposed to libraries. Signed-off-by: Chris Lalancette <clalancette@gmail.com> Co-authored-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> (cherry picked from commit 38d8e74)
* Major revamp of the ament_cmake documentation. Rearrange the document to more closely follow the order a typical CMakeLists.txt is arranged in. While doing that, update the documentation to match our current best practices, and add in additional advice for building/installing executables as opposed to libraries. Signed-off-by: Chris Lalancette <clalancette@gmail.com> Co-authored-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> (cherry picked from commit 38d8e74) Co-authored-by: Chris Lalancette <clalancette@gmail.com>
* Major revamp of the ament_cmake documentation. Rearrange the document to more closely follow the order a typical CMakeLists.txt is arranged in. While doing that, update the documentation to match our current best practices, and add in additional advice for building/installing executables as opposed to libraries. Signed-off-by: Chris Lalancette <clalancette@gmail.com> Co-authored-by: Ryan Friedman <ryanfriedman5410+github@gmail.com> (cherry picked from commit 38d8e74) Co-authored-by: Chris Lalancette <clalancette@gmail.com>
Rearrange the document to more closely follow the order a typical CMakeLists.txt is arranged in. While doing that, update the documentation to match our current best practices, and add in additional advice for building/installing executables as opposed to libraries.
@Ryanf55 FYI. I'm very interested to hear your feedback about this revamp.