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

Should CMake be more than just a test runner? #3

Open
Quincunx271 opened this issue May 8, 2018 · 4 comments
Open

Should CMake be more than just a test runner? #3

Quincunx271 opened this issue May 8, 2018 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Quincunx271
Copy link

Quincunx271 commented May 8, 2018

From r/cpp_review.

I think that we should use cmake as more than just a test runner. I think it should be possible to consume this library through cmake.

The expected way in modern cmake is to have scope_guard be a library, something like:

add_library(ScopeGuard INTERFACE)
add_library(ScopeGuard::ScopeGuard ALIAS ScopeGuard) # for consumption via add_subdirectory()

target_include_directories(ScopeGuard
  INTERFACE
    include # assuming we move the header file to an `include` directory
)

The tests would be behind a BUILD_TESTING check, something like this:

include(CTest)

# ...

if (BUILD_TESTING)
  find_package(Catch2 REQUIRED)
  # Test targets defined here
endif()

And we'd install the target so that it can be found via find_package(ScopeGuard) (or some other variation on the name), something like this:

include(GNUInstallDirs)

install(DIRECTORY include/
  DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(TARGETS ScopeGuard
  EXPORT ScopeGuardConfig
  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)

install(EXPORT ScopeGuardConfig
  NAMESPACE
    ScopeGuard::
  DESTINATION
    "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeGuard}"
)

# For versioning (e.g. find_package(ScopeGuard 1.0 REQUIRED))
include(CMakePackageConfigHelpers)

write_basic_package_version_file(
  "${CMAKE_CURRENT_BINARY_DIR}/ScopeGuardConfigVersion.cmake"
  VERSION ${PROJECT_VERSION} # requires setting the project version, of course
  COMPATIBILITY SameMajorVersion # I believe this is fully customizable
)

install(FILES
    "${CMAKE_CURRENT_BINARY_DIR}/ScopeGuardConfigVersion.cmake"
  DESTINATION
    "${CMAKE_INSTALL_LIBDIR}/cmake/ScopeGuard}"
)

See this article on cmake, connected to this example github repo.


In doing this, it might be easier to move the header file into an include directory.

@ricab
Copy link
Owner

ricab commented May 8, 2018

Thanks a lot @Quincunx271 that is very helpful. I will try to do this when I find the time. Cheers.

@OvermindDL1
Copy link

In essence, if you want to do things the right CMake way, follow this online 'book' by one of the big CMake people: https://cgold.readthedocs.io/en/latest/

@ricab
Copy link
Owner

ricab commented May 8, 2018

Thanks for the pointer @OvermindDL1. I had a browse and it will perhaps be a good resource in the future. ATM I find it still a bit embryonic (many empty sections, not very intelligible in places...). But I'll be sure to check it later on.

@OvermindDL1
Copy link

Yeah it's made by programmers and is 'relatively' new, but the information it has is good. :-)

@ricab ricab added the help wanted Extra attention is needed label Jan 10, 2019
captain-yoshi added a commit to captain-yoshi/moveit_task_constructor that referenced this issue Jul 16, 2024
A custom CMakeLists.txt is used to create the scope_guard libray. The
original cmake libray file is only used to setup tests.
See ricab/scope_guard#3.
captain-yoshi added a commit to captain-yoshi/moveit_task_constructor that referenced this issue Jul 17, 2024
A custom CMakeLists.txt is used to create the scope_guard libray. The
original cmake libray file is only used to setup tests.
See ricab/scope_guard#3.
captain-yoshi added a commit to captain-yoshi/moveit_task_constructor that referenced this issue Jul 17, 2024
A custom CMakeLists.txt is used to create the scope_guard libray. The
original cmake libray file is only used to setup tests.
See ricab/scope_guard#3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants