Should CMake be more than just a test runner?
Quincunx271 opened this issue · comments
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.
Thanks a lot @Quincunx271 that is very helpful. I will try to do this when I find the time. Cheers.
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/
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.
Yeah it's made by programmers and is 'relatively' new, but the information it has is good. :-)