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

Allow usage as submodule #29

Open
Flamefire opened this issue Jan 20, 2020 · 2 comments · May be fixed by #39
Open

Allow usage as submodule #29

Flamefire opened this issue Jan 20, 2020 · 2 comments · May be fixed by #39

Comments

@Flamefire
Copy link

Would be great if this was usable as a git-submodule or via CMakes ExternalProject or FetchContent.

This requires

  • a proper target name, e.g. tcspan
  • a namespaced alias for that target, e.g. tc::span
  • being able to exclude tests

For the latter I'd suggest to remove the enable_testing and use:

if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  include(CTest)
endif()
if(BUILD_TESTING)
  add_subdirectory(test)
endif()

This include adds an option BUILD_TESTING with default ON and enable_testing call if the current project is not the super project (aka cmake was called for another folder and this folder was added via add_subdirectory)

PS: CMAKE_CURRENT_SOURCE_DIR as used is not required and can be omitted.

@tcbrindle
Copy link
Owner

This sounds like a good idea. I'm not a CMake expert though -- I know how to make an "interface library", but how do I do the namespacing bit?

@Flamefire
Copy link
Author

You use an ALIAS library: add_library(tcb::span ALIAS tcb_span) where `tcb_span is your regular INTERFACE library. And while you are at it: You can also add installation support:

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
  target_include_directories(tcb_span INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

  install(TARGETS tcb_span
    EXPORT ${PROJECT_NAME}Targets
  )
  install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  set(configFile ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake)
  set(versionFile ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake)
  set(configInstallDestination lib/cmake/${configFolder})

  configure_package_config_file(
    ${PROJECT_SOURCE_DIR}/Config.cmake.in
    ${configFile} 
    INSTALL_DESTINATION ${configInstallDestination}
  )
  write_basic_package_version_file(
    ${versionFile}
    COMPATIBILITY SameMajorVersion
  )

  install(FILES ${configFile} ${versionFile} DESTINATION ${configInstallDestination})

  install(
    EXPORT ${PROJECT_NAME}Targets
    NAMESPACE "tcb::"
    DESTINATION ${configInstallDestination}
  )

This is mostly C&P from another project so double-check. You'll need to use a suitable project name (with the above users will be able to do find_package(<project-name>)) and use the VERSION x.y.z argument for project. The referenced Config.cmake.in is simple:

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")

And yes it is a shame that so much boilerplate is required in CMake...

If you got any questions, feel free to ask. I can also review a PR

@ClausKlein ClausKlein linked a pull request Oct 31, 2020 that will close this issue
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 a pull request may close this issue.

2 participants