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

Create a namespaced CMake target for ApprovalTests #75

Closed
claremacrae opened this issue Jan 2, 2020 · 4 comments
Closed

Create a namespaced CMake target for ApprovalTests #75

claremacrae opened this issue Jan 2, 2020 · 4 comments
Labels
enhancement infrastructure CMake, Builds, CI, and similar

Comments

@claremacrae
Copy link
Collaborator

For details, see the really clear explanation by @athrun22 in: #69 (comment)

@claremacrae
Copy link
Collaborator Author

Here's the explanation, copied from the original issue:

@claremacrae, a namespaced CMake target for ApprovalTests would be good to create before this documentation is done. By adding an aliased CMake target for ApprovalTests that prefixes a namespace, users can switch between using an installed version of the library - if support for find_package is added - and add_subdirectory / FetchContent without having to change the ApprovalTests library name. That's dense when I put it into words 🙄

An example might make this more clear. 🤔
The following example links to a hypothetically installed version of ApprovalTests, though this not supported at the moment.

# Use an installed version of ApprovalTests
find_package(ApprovalTests 7.0.0 REQUIRED)
add_executable(my-test PRIVATE main.cpp)
# According to standard practice, imported targets are prefixed with a namespace
target_link_libraries(my-test PRIVATE ApprovalTests::ApprovalTests)

This links to our current setup of ApprovalTests when using add_subdirectory.

add_subdirectory(ApprovalTests)
add_executable(my-test PRIVATE main.cpp)
# Notice the target name is different. This requires the user to change the library name.
target_link_libraries(my-test PRIVATE ApprovalTests)

This links to our current setup of ApprovalTests when using add_subdirectory after adding an aliased target.

add_subdirectory(ApprovalTests)
add_executable(my-test PRIVATE main.cpp)
# Target name is the same and this requires no change by the user.
target_link_libraries(my-test PRIVATE ApprovalTests::ApprovalTests)

Aliasing the ApprovalTests library in CMake is as simple as adding the following somewhere after defining the ApprovalTests interface library, probably in the same directory where it is defined.

add_library(ApprovalTests::ApprovalTests ALIAS ApprovalTests)

@claremacrae
Copy link
Collaborator Author

claremacrae commented Jan 2, 2020

I'd been wondering whether to add an aliased target - this is a good explanation of one reason to do so, that's new to me...

A reason that I was aware of is that in target_link_libraries(), CMake won't warn if ApprovalTests is not found, but it will warn if an library alias isn't found - so that's kinder to users...

@claremacrae claremacrae added the infrastructure CMake, Builds, CI, and similar label Jan 2, 2020
@jwillikers
Copy link
Contributor

@claremacrae Right you are! I use alias libraries internally for this very reason.

@claremacrae claremacrae added this to the vNext release milestone Jan 5, 2020
@claremacrae
Copy link
Collaborator Author

@jwillikers, does the above change look OK? Any feedback on the documentation change? Many thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement infrastructure CMake, Builds, CI, and similar
Projects
None yet
Development

No branches or pull requests

2 participants