samedi 23 mars 2019

CMake Tests for a library intended as a git submodule

Situation: You develop a library which uses CMake as its (meta-)buildsystem. The library is header-only (actually it does need to, the question still applies), has tests and examples (like benchmarks). The library can be used by installing it and using the resulting package files or (which is often easier to avoid mismatches in ABIs) by adding it as a git submodule and simply add_subdirectory it.

Question: What is the best way to handle tests in that library?

Usually the top-level CMake will look like this:

[...setup code, project(...)`]
add_library(foo src/foo.cpp)
target_include_libraries(foo PUBLIC include)

include(CTest)
if(BUILD_TESTING)
  add_subdirectory(tests)
endif()
if(FOO_BUILD_EXAMPLES)
  add_subdirectory(examples)
endif()
install(TARGETS foo ...)
install(...)

The issue I have with this is that when this is used as a submodule and the super project also uses include(CTest) and BUILD_TESTING then enabling the super projects tests enables also the submodule tests.

Is this what should be done and expected?

Similar for the install commands: Installing the super project installs also the submodule (I think) which may be wrong as the CMAKE_INSTALL_PREFIX given was intended for the super project and might not be appropriate for the submodule.

This would lead to using prefixes and options like FOO_BUILD_TESTING and FOO_INSTALL but I don't see them used often.

Am I worrying to much or missing anything? What is the way to go with building (and running) tests and installs for git submodules?

Aucun commentaire:

Enregistrer un commentaire