Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major revamp of the ament_cmake documentation. #3812

Merged
merged 3 commits into from
Aug 4, 2023

Conversation

clalancette
Copy link
Contributor

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.

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 <[email protected]>
@clalancette clalancette requested a review from audrow as a code owner July 31, 2023 18:57
Copy link
Contributor

@Ryanf55 Ryanf55 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, the changes look good. A few touch-ups here and there. Great improvement.

source/How-To-Guides/Ament-CMake-Documentation.rst Outdated Show resolved Hide resolved

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``
Copy link
Contributor

@Ryanf55 Ryanf55 Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use this in the core currently, so I don't think I'll add this here.

source/How-To-Guides/Ament-CMake-Documentation.rst Outdated Show resolved Hide resolved

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``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be useful to explain why we are still recommending ament_target_dependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

source/How-To-Guides/Ament-CMake-Documentation.rst Outdated Show resolved Hide resolved
source/How-To-Guides/Ament-CMake-Documentation.rst Outdated Show resolved Hide resolved

The following best practice is proposed:
if(NOT CMAKE_C_STANDARD)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll create another ticket in ros2/ros2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted to ros2/ros2#1467

Signed-off-by: Chris Lalancette <[email protected]>

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMAKE_COMPILER_IS_GNUCXX is deprecated.

Suggested change
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")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 <[email protected]>
@clalancette clalancette added the backport-all backport at reviewers discretion; from rolling to all versions label Aug 4, 2023
@clalancette
Copy link
Contributor Author

I'm going to go ahead and merge and backport this one. Thanks for all of your feedback.

@clalancette clalancette merged commit 38d8e74 into rolling Aug 4, 2023
5 checks passed
@delete-merged-branch delete-merged-branch bot deleted the clalancette/improve-ament-cmake-doc branch August 4, 2023 13:49
mergify bot pushed a commit that referenced this pull request Aug 4, 2023
* 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 <[email protected]>
Co-authored-by: Ryan Friedman <[email protected]>
(cherry picked from commit 38d8e74)
mergify bot pushed a commit that referenced this pull request Aug 4, 2023
* 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 <[email protected]>
Co-authored-by: Ryan Friedman <[email protected]>
(cherry picked from commit 38d8e74)
clalancette added a commit that referenced this pull request Aug 4, 2023
* 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 <[email protected]>
Co-authored-by: Ryan Friedman <[email protected]>
(cherry picked from commit 38d8e74)

Co-authored-by: Chris Lalancette <[email protected]>
clalancette added a commit that referenced this pull request Aug 4, 2023
* 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 <[email protected]>
Co-authored-by: Ryan Friedman <[email protected]>
(cherry picked from commit 38d8e74)

Co-authored-by: Chris Lalancette <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-all backport at reviewers discretion; from rolling to all versions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants