Skip to content

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

@Quincunx271

Description

@Quincunx271

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions