Skip to content

Allow modern target_link_libraries with ament_cmake_gtest - #629

Open
nwn wants to merge 1 commit into
ament:rollingfrom
nwn:nwn-scoped-gtest
Open

Allow modern target_link_libraries with ament_cmake_gtest#629
nwn wants to merge 1 commit into
ament:rollingfrom
nwn:nwn-scoped-gtest

Conversation

@nwn

@nwn nwn commented Jun 24, 2026

Copy link
Copy Markdown

As described in #405, the ament_add_gtest() and ament_add_gtest_executable() macros don't support subsequent calls to target_link_libraries() on the produced target with scope keywords (PRIVATE/PUBLIC/INTERFACE). Modern standards prefer to use scope keywords, so we should allow this.

CMake requires that either all or no calls to target_link_libraries() for a given target use scope keywords (CMP0023), so for backwards compatibility to avoid breaking downstream packages, the old behaviour is kept as a default for now, with the ability to opt in to the new behaviour by passing in LINKING_MODE SCOPED to ament_add_gtest() and friends. Use of the old method will now emit a deprecation warning, like:

CMake Deprecation Warning at /root/ws/install/ament_cmake_gtest/share/ament_cmake_gtest/cmake/ament_add_gtest_executable.cmake:64 (message):
  The unscoped signature for ament_add_gtest_executable() is deprecated.
  Instead, pass 'LINKING_MODE SCOPED' to ament_add_gtest_executable() and
  update any related target_link_libraries() calls to use scope keywords
  (PRIVATE/PUBLIC/INTERFACE).  This will eventually become the default.
Call Stack (most recent call first):
  /root/ws/install/ament_cmake_gtest/share/ament_cmake_gtest/cmake/ament_add_gtest_executable.cmake:40 (_ament_add_gtest_executable)
  /root/ws/install/ament_cmake_gtest/share/ament_cmake_gtest/cmake/ament_add_gtest.cmake:74 (ament_add_gtest_executable)
  CMakeLists.txt:146 (ament_add_gtest)

Eventually, after giving downstream users time to migrate, we can switch the default behaviour to SCOPED.

See also #423.

As described in ament#405, the `ament_add_gtest()` and
`ament_add_gtest_executable()` macros don't support subsequent calls to
`target_link_libraries()` on the produced target with scope keywords
(PRIVATE/PUBLIC/INTERFACE). Modern standards prefer to use scope
keywords, so we should allow this.

CMake requires that either _all_ or _no_ calls to
`target_link_libraries()` for a given target use scope keywords
([CMP0023](https://cmake.org/cmake/help/latest/policy/CMP0023.html)),
so for backwards compatibility to avoid breaking downstream packages,
the old behaviour is kept as a default for now, with the ability to opt
in to the new behaviour by passing in `LINKING_MODE SCOPED` to
`ament_add_gtest()` and friends. Use of the old method will now emit a
deprecation warning. After giving downstream users time to migrate, we
can switch the default behaviour to `SCOPED` and eventually remove
support for the unscoped behaviour.

Signed-off-by: Nathan Wiebe Neufeldt <wn.nathan@gmail.com>
@nwn
nwn force-pushed the nwn-scoped-gtest branch from 0cc5c95 to 6f64485 Compare June 24, 2026 17:04
@nwn
nwn marked this pull request as ready for review June 24, 2026 19:30
@mergify

mergify Bot commented Jun 24, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@skyegalaxy

Copy link
Copy Markdown

@cottsay or @asymingt - can you take a look?

@Zyrin

Zyrin commented Jul 2, 2026

Copy link
Copy Markdown

If you want to make the scoped mode the default, eventually (as stated in the warning), why not add something like SCOPED_LINKING_MODE as an option, instead of the one_value_keyword LINKING_MODE (with its keywords SCOPED and UNSCOPED)?
For the UNSCOPED mode ament_add_gtest would remain as is (except for a warning that the behavior will change in the future).
Only if explicit SCOPED mode is required one would add SCOPED_LINKING_MODE (or to silence the warning).
After the deprecation period is over only SCOPED_LINKING_MODE would need to be removed.

@nwn

nwn commented Jul 5, 2026

Copy link
Copy Markdown
Author

[...] why not add something like SCOPED_LINKING_MODE as an option, instead of the one_value_keyword LINKING_MODE (with its keywords SCOPED and UNSCOPED)?

@Zyrin I think the two approaches (single-value keyword or option) would be equivalent; either the default remains as LINKING_MODE UNSCOPED and you would use LINKING_MODE SCOPED to override it to the new way, or the default remains as UNSCOPED_LINKING_MODE and you use SCOPED_LINKING_MODE to override it.

I used a single-value keyword to indicate that only one of the two modes can be specified, but we could also just emit an error if the user specifies both the UNSCOPED_LINKING_MODE and SCOPED_LINKING_MODE options in the call. I'm fine with either approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants